Fossil SCM

Update the built-in SQLite to version 3.17.0 beta1

drh 2017-02-07 13:54 trunk
Commit 8fe0fefb24e30b6f51836b4b6cf55e0ad441f368
2 files changed +1332 -689 +55 -27
+1332 -689
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
11
/******************************************************************************
22
** This file is an amalgamation of many separate C source files from SQLite
3
-** version 3.16.2. By combining all the individual C code files into this
3
+** version 3.17.0. By combining all the individual C code files into this
44
** single large file, the entire code can be compiled as a single translation
55
** unit. This allows many compilers to do optimizations that would not be
66
** possible if the files were compiled separately. Performance improvements
77
** of 5% or more are commonly seen when SQLite is compiled as a single
88
** translation unit.
@@ -208,10 +208,18 @@
208208
#ifdef __GNUC__
209209
# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
210210
#else
211211
# define GCC_VERSION 0
212212
#endif
213
+
214
+/* What version of CLANG is being used. 0 means CLANG is not being used */
215
+#if defined(__clang__) && !defined(_WIN32)
216
+# define CLANG_VERSION \
217
+ (__clang_major__*1000000+__clang_minor__*1000+__clang_patchlevel__)
218
+#else
219
+# define CLANG_VERSION 0
220
+#endif
213221
214222
/* Needed for various definitions... */
215223
#if defined(__GNUC__) && !defined(_GNU_SOURCE)
216224
# define _GNU_SOURCE
217225
#endif
@@ -379,13 +387,13 @@
379387
**
380388
** See also: [sqlite3_libversion()],
381389
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
382390
** [sqlite_version()] and [sqlite_source_id()].
383391
*/
384
-#define SQLITE_VERSION "3.16.2"
385
-#define SQLITE_VERSION_NUMBER 3016002
386
-#define SQLITE_SOURCE_ID "2017-01-06 16:32:41 a65a62893ca8319e89e48b8a38cf8a59c69a8209"
392
+#define SQLITE_VERSION "3.17.0"
393
+#define SQLITE_VERSION_NUMBER 3017000
394
+#define SQLITE_SOURCE_ID "2017-02-07 13:51:48 a136609c98ed3cc673c5a3c2578d49db3f2518d1"
387395
388396
/*
389397
** CAPI3REF: Run-Time Library Version Numbers
390398
** KEYWORDS: sqlite3_version sqlite3_sourceid
391399
**
@@ -517,11 +525,15 @@
517525
** sqlite3_uint64 and sqlite_uint64 types can store integer values
518526
** between 0 and +18446744073709551615 inclusive.
519527
*/
520528
#ifdef SQLITE_INT64_TYPE
521529
typedef SQLITE_INT64_TYPE sqlite_int64;
522
- typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
530
+# ifdef SQLITE_UINT64_TYPE
531
+ typedef SQLITE_UINT64_TYPE sqlite_uint64;
532
+# else
533
+ typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
534
+# endif
523535
#elif defined(_MSC_VER) || defined(__BORLANDC__)
524536
typedef __int64 sqlite_int64;
525537
typedef unsigned __int64 sqlite_uint64;
526538
#else
527539
typedef long long int sqlite_int64;
@@ -830,11 +842,11 @@
830842
** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
831843
** after reboot following a crash or power loss, the only bytes in a
832844
** file that were written at the application level might have changed
833845
** and that adjacent bytes, even bytes within the same sector are
834846
** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
835
-** flag indicate that a file cannot be deleted when open. The
847
+** flag indicates that a file cannot be deleted when open. The
836848
** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
837849
** read-only media and cannot be changed even by processes with
838850
** elevated privileges.
839851
*/
840852
#define SQLITE_IOCAP_ATOMIC 0x00000001
@@ -980,10 +992,13 @@
980992
** <li> [SQLITE_IOCAP_ATOMIC16K]
981993
** <li> [SQLITE_IOCAP_ATOMIC32K]
982994
** <li> [SQLITE_IOCAP_ATOMIC64K]
983995
** <li> [SQLITE_IOCAP_SAFE_APPEND]
984996
** <li> [SQLITE_IOCAP_SEQUENTIAL]
997
+** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
998
+** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
999
+** <li> [SQLITE_IOCAP_IMMUTABLE]
9851000
** </ul>
9861001
**
9871002
** The SQLITE_IOCAP_ATOMIC property means that all writes of
9881003
** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
9891004
** mean that writes of blocks that are nnn bytes in size and
@@ -5668,11 +5683,11 @@
56685683
** ^(The update hook is not invoked when internal system tables are
56695684
** modified (i.e. sqlite_master and sqlite_sequence).)^
56705685
** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
56715686
**
56725687
** ^In the current implementation, the update hook
5673
-** is not invoked when duplication rows are deleted because of an
5688
+** is not invoked when conflicting rows are deleted because of an
56745689
** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook
56755690
** invoked when rows are deleted using the [truncate optimization].
56765691
** The exceptions defined in this paragraph might change in a future
56775692
** release of SQLite.
56785693
**
@@ -6450,10 +6465,16 @@
64506465
**
64516466
** ^Unless it returns SQLITE_MISUSE, this function sets the
64526467
** [database connection] error code and message accessible via
64536468
** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
64546469
**
6470
+** A BLOB referenced by sqlite3_blob_open() may be read using the
6471
+** [sqlite3_blob_read()] interface and modified by using
6472
+** [sqlite3_blob_write()]. The [BLOB handle] can be moved to a
6473
+** different row of the same table using the [sqlite3_blob_reopen()]
6474
+** interface. However, the column, table, or database of a [BLOB handle]
6475
+** cannot be changed after the [BLOB handle] is opened.
64556476
**
64566477
** ^(If the row that a BLOB handle points to is modified by an
64576478
** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
64586479
** then the BLOB handle is marked as "expired".
64596480
** This is true if any column of the row is changed, even a column
@@ -6473,10 +6494,14 @@
64736494
** and the built-in [zeroblob] SQL function may be used to create a
64746495
** zero-filled blob to read or write using the incremental-blob interface.
64756496
**
64766497
** To avoid a resource leak, every open [BLOB handle] should eventually
64776498
** be released by a call to [sqlite3_blob_close()].
6499
+**
6500
+** See also: [sqlite3_blob_close()],
6501
+** [sqlite3_blob_reopen()], [sqlite3_blob_read()],
6502
+** [sqlite3_blob_bytes()], [sqlite3_blob_write()].
64786503
*/
64796504
SQLITE_API int sqlite3_blob_open(
64806505
sqlite3*,
64816506
const char *zDb,
64826507
const char *zTable,
@@ -6488,15 +6513,15 @@
64886513
64896514
/*
64906515
** CAPI3REF: Move a BLOB Handle to a New Row
64916516
** METHOD: sqlite3_blob
64926517
**
6493
-** ^This function is used to move an existing blob handle so that it points
6518
+** ^This function is used to move an existing [BLOB handle] so that it points
64946519
** to a different row of the same database table. ^The new row is identified
64956520
** by the rowid value passed as the second argument. Only the row can be
64966521
** changed. ^The database, table and column on which the blob handle is open
6497
-** remain the same. Moving an existing blob handle to a new row can be
6522
+** remain the same. Moving an existing [BLOB handle] to a new row is
64986523
** faster than closing the existing handle and opening a new one.
64996524
**
65006525
** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
65016526
** it must exist and there must be either a blob or text value stored in
65026527
** the nominated column.)^ ^If the new row is not present in the table, or if
@@ -8421,22 +8446,22 @@
84218446
** ^These interfaces are only available if SQLite is compiled using the
84228447
** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
84238448
**
84248449
** ^The [sqlite3_preupdate_hook()] interface registers a callback function
84258450
** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
8426
-** on a [rowid table].
8451
+** on a database table.
84278452
** ^At most one preupdate hook may be registered at a time on a single
84288453
** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
84298454
** the previous setting.
84308455
** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
84318456
** with a NULL pointer as the second parameter.
84328457
** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
84338458
** the first parameter to callbacks.
84348459
**
8435
-** ^The preupdate hook only fires for changes to [rowid tables]; the preupdate
8436
-** hook is not invoked for changes to [virtual tables] or [WITHOUT ROWID]
8437
-** tables.
8460
+** ^The preupdate hook only fires for changes to real database tables; the
8461
+** preupdate hook is not invoked for changes to [virtual tables] or to
8462
+** system tables like sqlite_master or sqlite_stat1.
84388463
**
84398464
** ^The second parameter to the preupdate callback is a pointer to
84408465
** the [database connection] that registered the preupdate hook.
84418466
** ^The third parameter to the preupdate callback is one of the constants
84428467
** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
@@ -8446,16 +8471,20 @@
84468471
** will be "main" for the main database or "temp" for TEMP tables or
84478472
** the name given after the AS keyword in the [ATTACH] statement for attached
84488473
** databases.)^
84498474
** ^The fifth parameter to the preupdate callback is the name of the
84508475
** table that is being modified.
8451
-** ^The sixth parameter to the preupdate callback is the initial [rowid] of the
8452
-** row being changes for SQLITE_UPDATE and SQLITE_DELETE changes and is
8453
-** undefined for SQLITE_INSERT changes.
8454
-** ^The seventh parameter to the preupdate callback is the final [rowid] of
8455
-** the row being changed for SQLITE_UPDATE and SQLITE_INSERT changes and is
8456
-** undefined for SQLITE_DELETE changes.
8476
+**
8477
+** For an UPDATE or DELETE operation on a [rowid table], the sixth
8478
+** parameter passed to the preupdate callback is the initial [rowid] of the
8479
+** row being modified or deleted. For an INSERT operation on a rowid table,
8480
+** or any operation on a WITHOUT ROWID table, the value of the sixth
8481
+** parameter is undefined. For an INSERT or UPDATE on a rowid table the
8482
+** seventh parameter is the final rowid value of the row being inserted
8483
+** or updated. The value of the seventh parameter passed to the callback
8484
+** function is not defined for operations on WITHOUT ROWID tables, or for
8485
+** INSERT operations on rowid tables.
84578486
**
84588487
** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
84598488
** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
84608489
** provide additional information about a preupdate event. These routines
84618490
** may only be called from within a preupdate callback. Invoking any of
@@ -9155,11 +9184,12 @@
91559184
**
91569185
** <li> For each row (primary key) that exists in the to-table but not in
91579186
** the from-table, a DELETE record is added to the session object.
91589187
**
91599188
** <li> For each row (primary key) that exists in both tables, but features
9160
-** different in each, an UPDATE record is added to the session.
9189
+** different non-PK values in each, an UPDATE record is added to the
9190
+** session.
91619191
** </ul>
91629192
**
91639193
** To clarify, if this function is called and then a changeset constructed
91649194
** using [sqlite3session_changeset()], then after applying that changeset to
91659195
** database zFrom the contents of the two compatible tables would be
@@ -9740,11 +9770,11 @@
97409770
** considered compatible if all of the following are true:
97419771
**
97429772
** <ul>
97439773
** <li> The table has the same name as the name recorded in the
97449774
** changeset, and
9745
-** <li> The table has the same number of columns as recorded in the
9775
+** <li> The table has at least as many columns as recorded in the
97469776
** changeset, and
97479777
** <li> The table has primary key columns in the same position as
97489778
** recorded in the changeset.
97499779
** </ul>
97509780
**
@@ -9785,11 +9815,15 @@
97859815
** the changeset the row is deleted from the target database.
97869816
**
97879817
** If a row with matching primary key values is found, but one or more of
97889818
** the non-primary key fields contains a value different from the original
97899819
** row value stored in the changeset, the conflict-handler function is
9790
-** invoked with [SQLITE_CHANGESET_DATA] as the second argument.
9820
+** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
9821
+** database table has more columns than are recorded in the changeset,
9822
+** only the values of those non-primary key fields are compared against
9823
+** the current database contents - any trailing database table columns
9824
+** are ignored.
97919825
**
97929826
** If no row with matching primary key values is found in the database,
97939827
** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
97949828
** passed as the second argument.
97959829
**
@@ -9800,11 +9834,13 @@
98009834
** operation is attempted because an earlier call to the conflict handler
98019835
** function returned [SQLITE_CHANGESET_REPLACE].
98029836
**
98039837
** <dt>INSERT Changes<dd>
98049838
** For each INSERT change, an attempt is made to insert the new row into
9805
-** the database.
9839
+** the database. If the changeset row contains fewer fields than the
9840
+** database table, the trailing fields are populated with their default
9841
+** values.
98069842
**
98079843
** If the attempt to insert the row fails because the database already
98089844
** contains a row with the same primary key values, the conflict handler
98099845
** function is invoked with the second argument set to
98109846
** [SQLITE_CHANGESET_CONFLICT].
@@ -9818,17 +9854,17 @@
98189854
**
98199855
** <dt>UPDATE Changes<dd>
98209856
** For each UPDATE change, this function checks if the target database
98219857
** contains a row with the same primary key value (or values) as the
98229858
** original row values stored in the changeset. If it does, and the values
9823
-** stored in all non-primary key columns also match the values stored in
9824
-** the changeset the row is updated within the target database.
9859
+** stored in all modified non-primary key columns also match the values
9860
+** stored in the changeset the row is updated within the target database.
98259861
**
98269862
** If a row with matching primary key values is found, but one or more of
9827
-** the non-primary key fields contains a value different from an original
9828
-** row value stored in the changeset, the conflict-handler function is
9829
-** invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
9863
+** the modified non-primary key fields contains a value different from an
9864
+** original row value stored in the changeset, the conflict-handler function
9865
+** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
98309866
** UPDATE changes only contain values for non-primary key fields that are
98319867
** to be modified, only those fields need to match the original values to
98329868
** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
98339869
**
98349870
** If no row with matching primary key values is found in the database,
@@ -11537,10 +11573,22 @@
1153711573
#include <stdlib.h>
1153811574
#include <string.h>
1153911575
#include <assert.h>
1154011576
#include <stddef.h>
1154111577
11578
+/*
11579
+** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY.
11580
+** This allows better measurements of where memcpy() is used when running
11581
+** cachegrind. But this macro version of memcpy() is very slow so it
11582
+** should not be used in production. This is a performance measurement
11583
+** hack only.
11584
+*/
11585
+#ifdef SQLITE_INLINE_MEMCPY
11586
+# define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\
11587
+ int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
11588
+#endif
11589
+
1154211590
/*
1154311591
** If compiling for a processor that lacks floating point support,
1154411592
** substitute integer for floating-point
1154511593
*/
1154611594
#ifdef SQLITE_OMIT_FLOATING_POINT
@@ -11621,13 +11669,16 @@
1162111669
/*
1162211670
** The default initial allocation for the pagecache when using separate
1162311671
** pagecaches for each database connection. A positive number is the
1162411672
** number of pages. A negative number N translations means that a buffer
1162511673
** of -1024*N bytes is allocated and used for as many pages as it will hold.
11674
+**
11675
+** The default value of "20" was choosen to minimize the run-time of the
11676
+** speedtest1 test program with options: --shrink-memory --reprepare
1162611677
*/
1162711678
#ifndef SQLITE_DEFAULT_PCACHE_INITSZ
11628
-# define SQLITE_DEFAULT_PCACHE_INITSZ 100
11679
+# define SQLITE_DEFAULT_PCACHE_INITSZ 20
1162911680
#endif
1163011681
1163111682
/*
1163211683
** GCC does not define the offsetof() macro so we'll have to do it
1163311684
** ourselves.
@@ -12346,13 +12397,14 @@
1234612397
);
1234712398
SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*);
1234812399
SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*);
1234912400
SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, u8 flags);
1235012401
12351
-/* Allowed flags for the 2nd argument to sqlite3BtreeDelete() */
12402
+/* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */
1235212403
#define BTREE_SAVEPOSITION 0x02 /* Leave cursor pointing at NEXT or PREV */
1235312404
#define BTREE_AUXDELETE 0x04 /* not the primary delete operation */
12405
+#define BTREE_APPEND 0x08 /* Insert is likely an append */
1235412406
1235512407
/* An instance of the BtreePayload object describes the content of a single
1235612408
** entry in either an index or table btree.
1235712409
**
1235812410
** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
@@ -12379,11 +12431,11 @@
1237912431
int nData; /* Size of pData. 0 if none. */
1238012432
int nZero; /* Extra zero data appended after pData,nData */
1238112433
};
1238212434
1238312435
SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
12384
- int bias, int seekResult);
12436
+ int flags, int seekResult);
1238512437
SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
1238612438
SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
1238712439
SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
1238812440
SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
1238912441
SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
@@ -12512,12 +12564,11 @@
1251212564
** as an instance of the following structure:
1251312565
*/
1251412566
struct VdbeOp {
1251512567
u8 opcode; /* What operation to perform */
1251612568
signed char p4type; /* One of the P4_xxx constants for p4 */
12517
- u8 notUsed1;
12518
- u8 p5; /* Fifth parameter is an unsigned character */
12569
+ u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
1251912570
int p1; /* First operand */
1252012571
int p2; /* Second parameter (often the jump destination) */
1252112572
int p3; /* The third parameter */
1252212573
union p4union { /* fourth parameter */
1252312574
int i; /* Integer value if p4type==P4_INT32 */
@@ -12874,11 +12925,11 @@
1287412925
SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
1287512926
SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8);
1287612927
SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
1287712928
SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
1287812929
SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
12879
-SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
12930
+SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
1288012931
SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
1288112932
SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
1288212933
SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
1288312934
SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
1288412935
SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
@@ -13176,18 +13227,20 @@
1317613227
SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
1317713228
SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
1317813229
SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
1317913230
SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
1318013231
SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
13181
-SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager);
13232
+# ifdef SQLITE_DIRECT_OVERFLOW_READ
13233
+SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager, Pgno);
13234
+# endif
1318213235
# ifdef SQLITE_ENABLE_SNAPSHOT
1318313236
SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot);
1318413237
SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot);
1318513238
SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager);
1318613239
# endif
1318713240
#else
13188
-# define sqlite3PagerUseWal(x) 0
13241
+# define sqlite3PagerUseWal(x,y) 0
1318913242
#endif
1319013243
1319113244
#ifdef SQLITE_ENABLE_ZIPVFS
1319213245
SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager);
1319313246
#endif
@@ -14007,10 +14060,11 @@
1400714060
signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
1400814061
u8 suppressErr; /* Do not issue error messages if true */
1400914062
u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
1401014063
u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
1401114064
u8 mTrace; /* zero or more SQLITE_TRACE flags */
14065
+ u8 skipBtreeMutex; /* True if no shared-cache backends */
1401214066
int nextPagesize; /* Pagesize after VACUUM if >0 */
1401314067
u32 magic; /* Magic number for detect library misuse */
1401414068
int nChange; /* Value returned by sqlite3_changes() */
1401514069
int nTotalChange; /* Value returned by sqlite3_total_changes() */
1401614070
int aLimit[SQLITE_N_LIMIT]; /* Limits */
@@ -14272,10 +14326,11 @@
1427214326
#define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
1427314327
#define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
1427414328
#define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */
1427514329
#define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a
1427614330
** single query - might change over time */
14331
+#define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
1427714332
1427814333
/*
1427914334
** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
1428014335
** used to create the initializers for the FuncDef structures.
1428114336
**
@@ -15278,11 +15333,11 @@
1527815333
#define WHERE_DISTINCTBY 0x0080 /* pOrderby is really a DISTINCT clause */
1527915334
#define WHERE_WANT_DISTINCT 0x0100 /* All output needs to be distinct */
1528015335
#define WHERE_SORTBYGROUP 0x0200 /* Support sqlite3WhereIsSorted() */
1528115336
#define WHERE_SEEK_TABLE 0x0400 /* Do not defer seeks on main table */
1528215337
#define WHERE_ORDERBY_LIMIT 0x0800 /* ORDERBY+LIMIT on the inner loop */
15283
- /* 0x1000 not currently used */
15338
+#define WHERE_SEEK_UNIQ_TABLE 0x1000 /* Do not defer seeks if unique */
1528415339
/* 0x2000 not currently used */
1528515340
#define WHERE_USE_LIMIT 0x4000 /* Use the LIMIT in cost estimates */
1528615341
/* 0x8000 not currently used */
1528715342
1528815343
/* Allowed return values from sqlite3WhereIsDistinct()
@@ -15739,25 +15794,23 @@
1573915794
** OPFLAG_AUXDELETE == BTREE_AUXDELETE
1574015795
*/
1574115796
#define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
1574215797
/* Also used in P2 (not P5) of OP_Delete */
1574315798
#define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */
15744
-#define OPFLAG_LASTROWID 0x02 /* Set to update db->lastRowid */
15799
+#define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
1574515800
#define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */
1574615801
#define OPFLAG_APPEND 0x08 /* This is likely to be an append */
1574715802
#define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
15748
-#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
1574915803
#define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
15750
-#endif
1575115804
#define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */
1575215805
#define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */
1575315806
#define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */
1575415807
#define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */
1575515808
#define OPFLAG_FORDELETE 0x08 /* OP_Open should use BTREE_FORDELETE */
1575615809
#define OPFLAG_P2ISREG 0x10 /* P2 to OP_Open** is a register number */
1575715810
#define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */
15758
-#define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete: keep cursor position */
15811
+#define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete/Insert: save cursor pos */
1575915812
#define OPFLAG_AUXDELETE 0x04 /* OP_Delete: index in a DELETE op */
1576015813
1576115814
/*
1576215815
* Each trigger present in the database schema is stored as an instance of
1576315816
* struct Trigger.
@@ -16414,11 +16467,11 @@
1641416467
SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
1641516468
SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
1641616469
SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int);
1641716470
SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, Expr*, int);
1641816471
SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
16419
-SQLITE_PRIVATE void sqlite3ExprCodeAtInit(Parse*, Expr*, int, u8);
16472
+SQLITE_PRIVATE int sqlite3ExprCodeAtInit(Parse*, Expr*, int);
1642016473
SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
1642116474
SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
1642216475
SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
1642316476
SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
1642416477
#define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
@@ -16476,10 +16529,15 @@
1647616529
SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
1647716530
SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
1647816531
SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
1647916532
SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
1648016533
u8,u8,int,int*,int*);
16534
+#ifdef SQLITE_ENABLE_NULL_TRIM
16535
+SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe*,Table*);
16536
+#else
16537
+# define sqlite3SetMakeRecordP5(A,B)
16538
+#endif
1648116539
SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
1648216540
SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
1648316541
SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
1648416542
SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
1648516543
SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
@@ -16754,12 +16812,14 @@
1675416812
#endif
1675516813
1675616814
/*
1675716815
** The interface to the LEMON-generated parser
1675816816
*/
16759
-SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64));
16760
-SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
16817
+#ifndef SQLITE_AMALGAMATION
16818
+SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64));
16819
+SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
16820
+#endif
1676116821
SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
1676216822
#ifdef YYTRACKMAXSTACKDEPTH
1676316823
SQLITE_PRIVATE int sqlite3ParserStackPeak(void*);
1676416824
#endif
1676516825
@@ -16865,10 +16925,11 @@
1686516925
#define sqlite3FkActions(a,b,c,d,e,f)
1686616926
#define sqlite3FkCheck(a,b,c,d,e,f)
1686716927
#define sqlite3FkDropTable(a,b,c)
1686816928
#define sqlite3FkOldmask(a,b) 0
1686916929
#define sqlite3FkRequired(a,b,c,d) 0
16930
+ #define sqlite3FkReferences(a) 0
1687016931
#endif
1687116932
#ifndef SQLITE_OMIT_FOREIGN_KEY
1687216933
SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *, Table*);
1687316934
SQLITE_PRIVATE int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
1687416935
#else
@@ -17193,10 +17254,23 @@
1719317254
** setting.)
1719417255
*/
1719517256
#ifndef SQLITE_STMTJRNL_SPILL
1719617257
# define SQLITE_STMTJRNL_SPILL (64*1024)
1719717258
#endif
17259
+
17260
+/*
17261
+** The default lookaside-configuration, the format "SZ,N". SZ is the
17262
+** number of bytes in each lookaside slot (should be a multiple of 8)
17263
+** and N is the number of slots. The lookaside-configuration can be
17264
+** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
17265
+** or at run-time for an individual database connection using
17266
+** sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE);
17267
+*/
17268
+#ifndef SQLITE_DEFAULT_LOOKASIDE
17269
+# define SQLITE_DEFAULT_LOOKASIDE 1200,100
17270
+#endif
17271
+
1719817272
1719917273
/*
1720017274
** The following singleton contains the global configuration for
1720117275
** the SQLite library.
1720217276
*/
@@ -17206,12 +17280,11 @@
1720617280
SQLITE_THREADSAFE==1, /* bFullMutex */
1720717281
SQLITE_USE_URI, /* bOpenUri */
1720817282
SQLITE_ALLOW_COVERING_INDEX_SCAN, /* bUseCis */
1720917283
0x7ffffffe, /* mxStrlen */
1721017284
0, /* neverCorrupt */
17211
- 512, /* szLookaside */
17212
- 125, /* nLookaside */
17285
+ SQLITE_DEFAULT_LOOKASIDE, /* szLookaside, nLookaside */
1721317286
SQLITE_STMTJRNL_SPILL, /* nStmtSpill */
1721417287
{0,0,0,0,0,0,0,0}, /* m */
1721517288
{0,0,0,0,0,0,0,0,0}, /* mutex */
1721617289
{0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
1721717290
(void*)0, /* pHeap */
@@ -18219,10 +18292,11 @@
1821918292
int iNewReg; /* Register for new.* values */
1822018293
i64 iKey1; /* First key value passed to hook */
1822118294
i64 iKey2; /* Second key value passed to hook */
1822218295
Mem *aNew; /* Array of new.* values */
1822318296
Table *pTab; /* Schema object being upated */
18297
+ Index *pPk; /* PK index if pTab is WITHOUT ROWID */
1822418298
};
1822518299
1822618300
/*
1822718301
** Function prototypes
1822818302
*/
@@ -24283,39 +24357,38 @@
2428324357
2428424358
/*
2428524359
** Do a memory allocation with statistics and alarms. Assume the
2428624360
** lock is already held.
2428724361
*/
24288
-static int mallocWithAlarm(int n, void **pp){
24289
- int nFull;
24362
+static void mallocWithAlarm(int n, void **pp){
2429024363
void *p;
24364
+ int nFull = 0;
2429124365
assert( sqlite3_mutex_held(mem0.mutex) );
24292
- nFull = sqlite3GlobalConfig.m.xRoundup(n);
2429324366
sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
2429424367
if( mem0.alarmThreshold>0 ){
2429524368
sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
24369
+ nFull = sqlite3GlobalConfig.m.xRoundup(n);
2429624370
if( nUsed >= mem0.alarmThreshold - nFull ){
2429724371
mem0.nearlyFull = 1;
2429824372
sqlite3MallocAlarm(nFull);
2429924373
}else{
2430024374
mem0.nearlyFull = 0;
2430124375
}
2430224376
}
24303
- p = sqlite3GlobalConfig.m.xMalloc(nFull);
24377
+ p = sqlite3GlobalConfig.m.xMalloc(n);
2430424378
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
2430524379
if( p==0 && mem0.alarmThreshold>0 ){
2430624380
sqlite3MallocAlarm(nFull);
24307
- p = sqlite3GlobalConfig.m.xMalloc(nFull);
24381
+ p = sqlite3GlobalConfig.m.xMalloc(n);
2430824382
}
2430924383
#endif
2431024384
if( p ){
2431124385
nFull = sqlite3MallocSize(p);
2431224386
sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
2431324387
sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
2431424388
}
2431524389
*pp = p;
24316
- return nFull;
2431724390
}
2431824391
2431924392
/*
2432024393
** Allocate memory. This routine is like sqlite3_malloc() except that it
2432124394
** assumes the memory subsystem has already been initialized.
@@ -24951,11 +25024,10 @@
2495125024
2495225025
/*
2495325026
** Allowed values for et_info.flags
2495425027
*/
2495525028
#define FLAG_SIGNED 1 /* True if the value to convert is signed */
24956
-#define FLAG_INTERN 2 /* True if for internal use only */
2495725029
#define FLAG_STRING 4 /* Allow infinity precision */
2495825030
2495925031
2496025032
/*
2496125033
** The following table is searched linearly, so it is good to put the
@@ -24985,15 +25057,14 @@
2498525057
{ 'i', 10, 1, etRADIX, 0, 0 },
2498625058
{ 'n', 0, 0, etSIZE, 0, 0 },
2498725059
{ '%', 0, 0, etPERCENT, 0, 0 },
2498825060
{ 'p', 16, 0, etPOINTER, 0, 1 },
2498925061
24990
-/* All the rest have the FLAG_INTERN bit set and are thus for internal
24991
-** use only */
24992
- { 'T', 0, 2, etTOKEN, 0, 0 },
24993
- { 'S', 0, 2, etSRCLIST, 0, 0 },
24994
- { 'r', 10, 3, etORDINAL, 0, 0 },
25062
+ /* All the rest are undocumented and are for internal use only */
25063
+ { 'T', 0, 0, etTOKEN, 0, 0 },
25064
+ { 'S', 0, 0, etSRCLIST, 0, 0 },
25065
+ { 'r', 10, 1, etORDINAL, 0, 0 },
2499525066
};
2499625067
2499725068
/*
2499825069
** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
2499925070
** conversions will work.
@@ -25083,11 +25154,10 @@
2508325154
etByte flag_long; /* True if "l" flag is present */
2508425155
etByte flag_longlong; /* True if the "ll" flag is present */
2508525156
etByte done; /* Loop termination flag */
2508625157
etByte xtype = etINVALID; /* Conversion paradigm */
2508725158
u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
25088
- u8 useIntern; /* Ok to use internal conversions (ex: %T) */
2508925159
char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
2509025160
sqlite_uint64 longvalue; /* Value for integer types */
2509125161
LONGDOUBLE_TYPE realvalue; /* Value for real types */
2509225162
const et_info *infop; /* Pointer to the appropriate info structure */
2509325163
char *zOut; /* Rendering buffer */
@@ -25102,17 +25172,15 @@
2510225172
#endif
2510325173
PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
2510425174
char buf[etBUFSIZE]; /* Conversion buffer */
2510525175
2510625176
bufpt = 0;
25107
- if( pAccum->printfFlags ){
25108
- if( (bArgList = (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){
25109
- pArgList = va_arg(ap, PrintfArguments*);
25110
- }
25111
- useIntern = pAccum->printfFlags & SQLITE_PRINTF_INTERNAL;
25177
+ if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
25178
+ pArgList = va_arg(ap, PrintfArguments*);
25179
+ bArgList = 1;
2511225180
}else{
25113
- bArgList = useIntern = 0;
25181
+ bArgList = 0;
2511425182
}
2511525183
for(; (c=(*fmt))!=0; ++fmt){
2511625184
if( c!='%' ){
2511725185
bufpt = (char *)fmt;
2511825186
#if HAVE_STRCHRNUL
@@ -25220,15 +25288,11 @@
2522025288
infop = &fmtinfo[0];
2522125289
xtype = etINVALID;
2522225290
for(idx=0; idx<ArraySize(fmtinfo); idx++){
2522325291
if( c==fmtinfo[idx].fmttype ){
2522425292
infop = &fmtinfo[idx];
25225
- if( useIntern || (infop->flags & FLAG_INTERN)==0 ){
25226
- xtype = infop->type;
25227
- }else{
25228
- return;
25229
- }
25293
+ xtype = infop->type;
2523025294
break;
2523125295
}
2523225296
}
2523325297
2523425298
/*
@@ -25593,22 +25657,28 @@
2559325657
** consume, not the length of the output...
2559425658
** if( precision>=0 && precision<length ) length = precision; */
2559525659
break;
2559625660
}
2559725661
case etTOKEN: {
25598
- Token *pToken = va_arg(ap, Token*);
25662
+ Token *pToken;
25663
+ if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
25664
+ pToken = va_arg(ap, Token*);
2559925665
assert( bArgList==0 );
2560025666
if( pToken && pToken->n ){
2560125667
sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
2560225668
}
2560325669
length = width = 0;
2560425670
break;
2560525671
}
2560625672
case etSRCLIST: {
25607
- SrcList *pSrc = va_arg(ap, SrcList*);
25608
- int k = va_arg(ap, int);
25609
- struct SrcList_item *pItem = &pSrc->a[k];
25673
+ SrcList *pSrc;
25674
+ int k;
25675
+ struct SrcList_item *pItem;
25676
+ if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
25677
+ pSrc = va_arg(ap, SrcList*);
25678
+ k = va_arg(ap, int);
25679
+ pItem = &pSrc->a[k];
2561025680
assert( bArgList==0 );
2561125681
assert( k>=0 && k<pSrc->nSrc );
2561225682
if( pItem->zDatabase ){
2561325683
sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
2561425684
sqlite3StrAccumAppend(pAccum, ".", 1);
@@ -25626,13 +25696,17 @@
2562625696
** The text of the conversion is pointed to by "bufpt" and is
2562725697
** "length" characters long. The field width is "width". Do
2562825698
** the output.
2562925699
*/
2563025700
width -= length;
25631
- if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
25632
- sqlite3StrAccumAppend(pAccum, bufpt, length);
25633
- if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
25701
+ if( width>0 ){
25702
+ if( !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
25703
+ sqlite3StrAccumAppend(pAccum, bufpt, length);
25704
+ if( flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
25705
+ }else{
25706
+ sqlite3StrAccumAppend(pAccum, bufpt, length);
25707
+ }
2563425708
2563525709
if( zExtra ){
2563625710
sqlite3DbFree(pAccum->db, zExtra);
2563725711
zExtra = 0;
2563825712
}
@@ -28601,11 +28675,11 @@
2860128675
#if SQLITE_BYTEORDER==4321
2860228676
u32 x;
2860328677
memcpy(&x,p,4);
2860428678
return x;
2860528679
#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
28606
- && defined(__GNUC__) && GCC_VERSION>=4003000
28680
+ && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
2860728681
u32 x;
2860828682
memcpy(&x,p,4);
2860928683
return __builtin_bswap32(x);
2861028684
#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
2861128685
&& defined(_MSC_VER) && _MSC_VER>=1300
@@ -28619,11 +28693,11 @@
2861928693
}
2862028694
SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
2862128695
#if SQLITE_BYTEORDER==4321
2862228696
memcpy(p,&v,4);
2862328697
#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
28624
- && defined(__GNUC__) && GCC_VERSION>=4003000
28698
+ && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
2862528699
u32 x = __builtin_bswap32(v);
2862628700
memcpy(p,&x,4);
2862728701
#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
2862828702
&& defined(_MSC_VER) && _MSC_VER>=1300
2862928703
u32 x = _byteswap_ulong(v);
@@ -28739,10 +28813,14 @@
2873928813
** the other 64-bit signed integer at *pA and store the result in *pA.
2874028814
** Return 0 on success. Or if the operation would have resulted in an
2874128815
** overflow, leave *pA unchanged and return 1.
2874228816
*/
2874328817
SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
28818
+#if !defined(SQLITE_DISABLE_INTRINSIC) \
28819
+ && (GCC_VERSION>=5004000 || CLANG_VERSION>=4000000)
28820
+ return __builtin_add_overflow(*pA, iB, pA);
28821
+#else
2874428822
i64 iA = *pA;
2874528823
testcase( iA==0 ); testcase( iA==1 );
2874628824
testcase( iB==-1 ); testcase( iB==0 );
2874728825
if( iB>=0 ){
2874828826
testcase( iA>0 && LARGEST_INT64 - iA == iB );
@@ -28753,23 +28831,33 @@
2875328831
testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
2875428832
if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
2875528833
}
2875628834
*pA += iB;
2875728835
return 0;
28836
+#endif
2875828837
}
2875928838
SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
28839
+#if !defined(SQLITE_DISABLE_INTRINSIC) \
28840
+ && (GCC_VERSION>=5004000 || CLANG_VERSION>=4000000)
28841
+ return __builtin_sub_overflow(*pA, iB, pA);
28842
+#else
2876028843
testcase( iB==SMALLEST_INT64+1 );
2876128844
if( iB==SMALLEST_INT64 ){
2876228845
testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
2876328846
if( (*pA)>=0 ) return 1;
2876428847
*pA -= iB;
2876528848
return 0;
2876628849
}else{
2876728850
return sqlite3AddInt64(pA, -iB);
2876828851
}
28852
+#endif
2876928853
}
2877028854
SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
28855
+#if !defined(SQLITE_DISABLE_INTRINSIC) \
28856
+ && (GCC_VERSION>=5004000 || CLANG_VERSION>=4000000)
28857
+ return __builtin_mul_overflow(*pA, iB, pA);
28858
+#else
2877128859
i64 iA = *pA;
2877228860
if( iB>0 ){
2877328861
if( iA>LARGEST_INT64/iB ) return 1;
2877428862
if( iA<SMALLEST_INT64/iB ) return 1;
2877528863
}else if( iB<0 ){
@@ -28781,10 +28869,11 @@
2878128869
if( -iA>LARGEST_INT64/-iB ) return 1;
2878228870
}
2878328871
}
2878428872
*pA = iA*iB;
2878528873
return 0;
28874
+#endif
2878628875
}
2878728876
2878828877
/*
2878928878
** Compute the absolute value of a 32-bit signed integer, of possible. Or
2879028879
** if the integer has a value of -2147483648, return +2147483647
@@ -47485,18 +47574,24 @@
4748547574
** if( pPager->jfd->pMethods ){ ...
4748647575
*/
4748747576
#define isOpen(pFd) ((pFd)->pMethods!=0)
4748847577
4748947578
/*
47490
-** Return true if this pager uses a write-ahead log instead of the usual
47491
-** rollback journal. Otherwise false.
47579
+** Return true if this pager uses a write-ahead log to read page pgno.
47580
+** Return false if the pager reads pgno directly from the database.
4749247581
*/
47582
+#if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_DIRECT_OVERFLOW_READ)
47583
+SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager, Pgno pgno){
47584
+ u32 iRead = 0;
47585
+ int rc;
47586
+ if( pPager->pWal==0 ) return 0;
47587
+ rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
47588
+ return rc || iRead;
47589
+}
47590
+#endif
4749347591
#ifndef SQLITE_OMIT_WAL
47494
-SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager){
47495
- return (pPager->pWal!=0);
47496
-}
47497
-# define pagerUseWal(x) sqlite3PagerUseWal(x)
47592
+# define pagerUseWal(x) ((x)->pWal!=0)
4749847593
#else
4749947594
# define pagerUseWal(x) 0
4750047595
# define pagerRollbackWal(x) 0
4750147596
# define pagerWalFrames(v,w,x,y) 0
4750247597
# define pagerOpenWalIfPresent(z) SQLITE_OK
@@ -58624,27 +58719,38 @@
5862458719
** Enter the mutexes in accending order by BtShared pointer address
5862558720
** to avoid the possibility of deadlock when two threads with
5862658721
** two or more btrees in common both try to lock all their btrees
5862758722
** at the same instant.
5862858723
*/
58629
-SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
58724
+static void SQLITE_NOINLINE btreeEnterAll(sqlite3 *db){
5863058725
int i;
58726
+ int skipOk = 1;
5863158727
Btree *p;
5863258728
assert( sqlite3_mutex_held(db->mutex) );
5863358729
for(i=0; i<db->nDb; i++){
5863458730
p = db->aDb[i].pBt;
58635
- if( p ) sqlite3BtreeEnter(p);
58731
+ if( p && p->sharable ){
58732
+ sqlite3BtreeEnter(p);
58733
+ skipOk = 0;
58734
+ }
5863658735
}
58736
+ db->skipBtreeMutex = skipOk;
5863758737
}
58638
-SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
58738
+SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
58739
+ if( db->skipBtreeMutex==0 ) btreeEnterAll(db);
58740
+}
58741
+static void SQLITE_NOINLINE btreeLeaveAll(sqlite3 *db){
5863958742
int i;
5864058743
Btree *p;
5864158744
assert( sqlite3_mutex_held(db->mutex) );
5864258745
for(i=0; i<db->nDb; i++){
5864358746
p = db->aDb[i].pBt;
5864458747
if( p ) sqlite3BtreeLeave(p);
5864558748
}
58749
+}
58750
+SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
58751
+ if( db->skipBtreeMutex==0 ) btreeLeaveAll(db);
5864658752
}
5864758753
5864858754
#ifndef NDEBUG
5864958755
/*
5865058756
** Return true if the current thread holds the database connection
@@ -62097,16 +62203,18 @@
6209762203
for(i=0; i<nCell; i++){
6209862204
u8 *pCell = findCell(pPage, i);
6209962205
if( eType==PTRMAP_OVERFLOW1 ){
6210062206
CellInfo info;
6210162207
pPage->xParseCell(pPage, pCell, &info);
62102
- if( info.nLocal<info.nPayload
62103
- && pCell+info.nSize-1<=pPage->aData+pPage->maskPage
62104
- && iFrom==get4byte(pCell+info.nSize-4)
62105
- ){
62106
- put4byte(pCell+info.nSize-4, iTo);
62107
- break;
62208
+ if( info.nLocal<info.nPayload ){
62209
+ if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
62210
+ return SQLITE_CORRUPT_BKPT;
62211
+ }
62212
+ if( iFrom==get4byte(pCell+info.nSize-4) ){
62213
+ put4byte(pCell+info.nSize-4, iTo);
62214
+ break;
62215
+ }
6210862216
}
6210962217
}else{
6211062218
if( get4byte(pCell)==iFrom ){
6211162219
put4byte(pCell, iTo);
6211262220
break;
@@ -62777,11 +62885,16 @@
6277762885
if( p && p->inTrans==TRANS_WRITE ){
6277862886
BtShared *pBt = p->pBt;
6277962887
assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
6278062888
assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
6278162889
sqlite3BtreeEnter(p);
62782
- rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
62890
+ if( op==SAVEPOINT_ROLLBACK ){
62891
+ rc = saveAllCursors(pBt, 0, 0);
62892
+ }
62893
+ if( rc==SQLITE_OK ){
62894
+ rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
62895
+ }
6278362896
if( rc==SQLITE_OK ){
6278462897
if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
6278562898
pBt->nPage = 0;
6278662899
}
6278762900
rc = newDatabase(pBt);
@@ -63163,25 +63276,24 @@
6316363276
** for the entry that the pCur cursor is pointing to. The eOp
6316463277
** argument is interpreted as follows:
6316563278
**
6316663279
** 0: The operation is a read. Populate the overflow cache.
6316763280
** 1: The operation is a write. Populate the overflow cache.
63168
-** 2: The operation is a read. Do not populate the overflow cache.
6316963281
**
6317063282
** A total of "amt" bytes are read or written beginning at "offset".
6317163283
** Data is read to or from the buffer pBuf.
6317263284
**
6317363285
** The content being read or written might appear on the main page
6317463286
** or be scattered out on multiple overflow pages.
6317563287
**
63176
-** If the current cursor entry uses one or more overflow pages and the
63177
-** eOp argument is not 2, this function may allocate space for and lazily
63178
-** populates the overflow page-list cache array (BtCursor.aOverflow).
63288
+** If the current cursor entry uses one or more overflow pages
63289
+** this function may allocate space for and lazily populate
63290
+** the overflow page-list cache array (BtCursor.aOverflow).
6317963291
** Subsequent calls use this cache to make seeking to the supplied offset
6318063292
** more efficient.
6318163293
**
63182
-** Once an overflow page-list cache has been allocated, it may be
63294
+** Once an overflow page-list cache has been allocated, it must be
6318363295
** invalidated if some other cursor writes to the same table, or if
6318463296
** the cursor is moved to a different row. Additionally, in auto-vacuum
6318563297
** mode, the following events may invalidate an overflow page-list cache.
6318663298
**
6318763299
** * An incremental vacuum,
@@ -63199,25 +63311,21 @@
6319963311
int rc = SQLITE_OK;
6320063312
int iIdx = 0;
6320163313
MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
6320263314
BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
6320363315
#ifdef SQLITE_DIRECT_OVERFLOW_READ
63204
- unsigned char * const pBufStart = pBuf;
63205
- int bEnd; /* True if reading to end of data */
63316
+ unsigned char * const pBufStart = pBuf; /* Start of original out buffer */
6320663317
#endif
6320763318
6320863319
assert( pPage );
63320
+ assert( eOp==0 || eOp==1 );
6320963321
assert( pCur->eState==CURSOR_VALID );
6321063322
assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
6321163323
assert( cursorHoldsMutex(pCur) );
63212
- assert( eOp!=2 || offset==0 ); /* Always start from beginning for eOp==2 */
6321363324
6321463325
getCellInfo(pCur);
6321563326
aPayload = pCur->info.pPayload;
63216
-#ifdef SQLITE_DIRECT_OVERFLOW_READ
63217
- bEnd = offset+amt==pCur->info.nPayload;
63218
-#endif
6321963327
assert( offset+amt <= pCur->info.nPayload );
6322063328
6322163329
assert( aPayload > pPage->aData );
6322263330
if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
6322363331
/* Trying to read or write past the end of the data is an error. The
@@ -63232,11 +63340,11 @@
6323263340
if( offset<pCur->info.nLocal ){
6323363341
int a = amt;
6323463342
if( a+offset>pCur->info.nLocal ){
6323563343
a = pCur->info.nLocal - offset;
6323663344
}
63237
- rc = copyPayload(&aPayload[offset], pBuf, a, (eOp & 0x01), pPage->pDbPage);
63345
+ rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
6323863346
offset = 0;
6323963347
pBuf += a;
6324063348
amt -= a;
6324163349
}else{
6324263350
offset -= pCur->info.nLocal;
@@ -63248,69 +63356,58 @@
6324863356
Pgno nextPage;
6324963357
6325063358
nextPage = get4byte(&aPayload[pCur->info.nLocal]);
6325163359
6325263360
/* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
63253
- ** Except, do not allocate aOverflow[] for eOp==2.
6325463361
**
6325563362
** The aOverflow[] array is sized at one entry for each overflow page
6325663363
** in the overflow chain. The page number of the first overflow page is
6325763364
** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
6325863365
** means "not yet known" (the cache is lazily populated).
6325963366
*/
63260
- if( eOp!=2 && (pCur->curFlags & BTCF_ValidOvfl)==0 ){
63367
+ if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
6326163368
int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
6326263369
if( nOvfl>pCur->nOvflAlloc ){
6326363370
Pgno *aNew = (Pgno*)sqlite3Realloc(
6326463371
pCur->aOverflow, nOvfl*2*sizeof(Pgno)
6326563372
);
6326663373
if( aNew==0 ){
63267
- rc = SQLITE_NOMEM_BKPT;
63374
+ return SQLITE_NOMEM_BKPT;
6326863375
}else{
6326963376
pCur->nOvflAlloc = nOvfl*2;
6327063377
pCur->aOverflow = aNew;
6327163378
}
6327263379
}
63273
- if( rc==SQLITE_OK ){
63274
- memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
63275
- pCur->curFlags |= BTCF_ValidOvfl;
63380
+ memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
63381
+ pCur->curFlags |= BTCF_ValidOvfl;
63382
+ }else{
63383
+ /* If the overflow page-list cache has been allocated and the
63384
+ ** entry for the first required overflow page is valid, skip
63385
+ ** directly to it.
63386
+ */
63387
+ if( pCur->aOverflow[offset/ovflSize] ){
63388
+ iIdx = (offset/ovflSize);
63389
+ nextPage = pCur->aOverflow[iIdx];
63390
+ offset = (offset%ovflSize);
6327663391
}
6327763392
}
6327863393
63279
- /* If the overflow page-list cache has been allocated and the
63280
- ** entry for the first required overflow page is valid, skip
63281
- ** directly to it.
63282
- */
63283
- if( (pCur->curFlags & BTCF_ValidOvfl)!=0
63284
- && pCur->aOverflow[offset/ovflSize]
63285
- ){
63286
- iIdx = (offset/ovflSize);
63287
- nextPage = pCur->aOverflow[iIdx];
63288
- offset = (offset%ovflSize);
63289
- }
63290
-
63291
- for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
63292
-
63394
+ assert( rc==SQLITE_OK && amt>0 );
63395
+ while( nextPage ){
6329363396
/* If required, populate the overflow page-list cache. */
63294
- if( (pCur->curFlags & BTCF_ValidOvfl)!=0 ){
63295
- assert( pCur->aOverflow[iIdx]==0
63296
- || pCur->aOverflow[iIdx]==nextPage
63297
- || CORRUPT_DB );
63298
- pCur->aOverflow[iIdx] = nextPage;
63299
- }
63397
+ assert( pCur->aOverflow[iIdx]==0
63398
+ || pCur->aOverflow[iIdx]==nextPage
63399
+ || CORRUPT_DB );
63400
+ pCur->aOverflow[iIdx] = nextPage;
6330063401
6330163402
if( offset>=ovflSize ){
6330263403
/* The only reason to read this page is to obtain the page
6330363404
** number for the next page in the overflow chain. The page
6330463405
** data is not required. So first try to lookup the overflow
6330563406
** page-list cache, if any, then fall back to the getOverflowPage()
6330663407
** function.
63307
- **
63308
- ** Note that the aOverflow[] array must be allocated because eOp!=2
63309
- ** here. If eOp==2, then offset==0 and this branch is never taken.
6331063408
*/
63311
- assert( eOp!=2 );
6331263409
assert( pCur->curFlags & BTCF_ValidOvfl );
6331363410
assert( pCur->pBtree->db==pBt->db );
6331463411
if( pCur->aOverflow[iIdx+1] ){
6331563412
nextPage = pCur->aOverflow[iIdx+1];
6331663413
}else{
@@ -63320,11 +63417,11 @@
6332063417
}else{
6332163418
/* Need to read this page properly. It contains some of the
6332263419
** range of data that is being read (eOp==0) or written (eOp!=0).
6332363420
*/
6332463421
#ifdef SQLITE_DIRECT_OVERFLOW_READ
63325
- sqlite3_file *fd;
63422
+ sqlite3_file *fd; /* File from which to do direct overflow read */
6332663423
#endif
6332763424
int a = amt;
6332863425
if( a + offset > ovflSize ){
6332963426
a = ovflSize - offset;
6333063427
}
@@ -63332,31 +63429,29 @@
6333263429
#ifdef SQLITE_DIRECT_OVERFLOW_READ
6333363430
/* If all the following are true:
6333463431
**
6333563432
** 1) this is a read operation, and
6333663433
** 2) data is required from the start of this overflow page, and
63337
- ** 3) the database is file-backed, and
63338
- ** 4) there is no open write-transaction, and
63339
- ** 5) the database is not a WAL database,
63340
- ** 6) all data from the page is being read.
63341
- ** 7) at least 4 bytes have already been read into the output buffer
63434
+ ** 3) there is no open write-transaction, and
63435
+ ** 4) the database is file-backed, and
63436
+ ** 5) the page is not in the WAL file
63437
+ ** 6) at least 4 bytes have already been read into the output buffer
6334263438
**
6334363439
** then data can be read directly from the database file into the
6334463440
** output buffer, bypassing the page-cache altogether. This speeds
6334563441
** up loading large records that span many overflow pages.
6334663442
*/
63347
- if( (eOp&0x01)==0 /* (1) */
63443
+ if( eOp==0 /* (1) */
6334863444
&& offset==0 /* (2) */
63349
- && (bEnd || a==ovflSize) /* (6) */
63350
- && pBt->inTransaction==TRANS_READ /* (4) */
63351
- && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (3) */
63352
- && 0==sqlite3PagerUseWal(pBt->pPager) /* (5) */
63353
- && &pBuf[-4]>=pBufStart /* (7) */
63445
+ && pBt->inTransaction==TRANS_READ /* (3) */
63446
+ && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (4) */
63447
+ && 0==sqlite3PagerUseWal(pBt->pPager, nextPage) /* (5) */
63448
+ && &pBuf[-4]>=pBufStart /* (6) */
6335463449
){
6335563450
u8 aSave[4];
6335663451
u8 *aWrite = &pBuf[-4];
63357
- assert( aWrite>=pBufStart ); /* hence (7) */
63452
+ assert( aWrite>=pBufStart ); /* due to (6) */
6335863453
memcpy(aSave, aWrite, 4);
6335963454
rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
6336063455
nextPage = get4byte(aWrite);
6336163456
memcpy(aWrite, aSave, 4);
6336263457
}else
@@ -63363,28 +63458,31 @@
6336363458
#endif
6336463459
6336563460
{
6336663461
DbPage *pDbPage;
6336763462
rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
63368
- ((eOp&0x01)==0 ? PAGER_GET_READONLY : 0)
63463
+ (eOp==0 ? PAGER_GET_READONLY : 0)
6336963464
);
6337063465
if( rc==SQLITE_OK ){
6337163466
aPayload = sqlite3PagerGetData(pDbPage);
6337263467
nextPage = get4byte(aPayload);
63373
- rc = copyPayload(&aPayload[offset+4], pBuf, a, (eOp&0x01), pDbPage);
63468
+ rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
6337463469
sqlite3PagerUnref(pDbPage);
6337563470
offset = 0;
6337663471
}
6337763472
}
6337863473
amt -= a;
63474
+ if( amt==0 ) return rc;
6337963475
pBuf += a;
6338063476
}
63477
+ if( rc ) break;
63478
+ iIdx++;
6338163479
}
6338263480
}
6338363481
6338463482
if( rc==SQLITE_OK && amt>0 ){
63385
- return SQLITE_CORRUPT_BKPT;
63483
+ return SQLITE_CORRUPT_BKPT; /* Overflow chain ends prematurely */
6338663484
}
6338763485
return rc;
6338863486
}
6338963487
6339063488
/*
@@ -63409,25 +63507,38 @@
6340963507
assert( pCur->eState==CURSOR_VALID );
6341063508
assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
6341163509
assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
6341263510
return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
6341363511
}
63512
+
63513
+/*
63514
+** This variant of sqlite3BtreePayload() works even if the cursor has not
63515
+** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read()
63516
+** interface.
63517
+*/
6341463518
#ifndef SQLITE_OMIT_INCRBLOB
63415
-SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
63519
+static SQLITE_NOINLINE int accessPayloadChecked(
63520
+ BtCursor *pCur,
63521
+ u32 offset,
63522
+ u32 amt,
63523
+ void *pBuf
63524
+){
6341663525
int rc;
6341763526
if ( pCur->eState==CURSOR_INVALID ){
6341863527
return SQLITE_ABORT;
6341963528
}
6342063529
assert( cursorOwnsBtShared(pCur) );
63421
- rc = restoreCursorPosition(pCur);
63422
- if( rc==SQLITE_OK ){
63423
- assert( pCur->eState==CURSOR_VALID );
63424
- assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
63425
- assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
63426
- rc = accessPayload(pCur, offset, amt, pBuf, 0);
63427
- }
63428
- return rc;
63530
+ rc = btreeRestoreCursorPosition(pCur);
63531
+ return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
63532
+}
63533
+SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
63534
+ if( pCur->eState==CURSOR_VALID ){
63535
+ assert( cursorOwnsBtShared(pCur) );
63536
+ return accessPayload(pCur, offset, amt, pBuf, 0);
63537
+ }else{
63538
+ return accessPayloadChecked(pCur, offset, amt, pBuf);
63539
+ }
6342963540
}
6343063541
#endif /* SQLITE_OMIT_INCRBLOB */
6343163542
6343263543
/*
6343363544
** Return a pointer to payload information from the entry that the
@@ -63829,13 +63940,30 @@
6382963940
){
6383063941
if( pCur->info.nKey==intKey ){
6383163942
*pRes = 0;
6383263943
return SQLITE_OK;
6383363944
}
63834
- if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKey<intKey ){
63835
- *pRes = -1;
63836
- return SQLITE_OK;
63945
+ if( pCur->info.nKey<intKey ){
63946
+ if( (pCur->curFlags & BTCF_AtLast)!=0 ){
63947
+ *pRes = -1;
63948
+ return SQLITE_OK;
63949
+ }
63950
+ /* If the requested key is one more than the previous key, then
63951
+ ** try to get there using sqlite3BtreeNext() rather than a full
63952
+ ** binary search. This is an optimization only. The correct answer
63953
+ ** is still obtained without this ase, only a little more slowely */
63954
+ if( pCur->info.nKey+1==intKey && !pCur->skipNext ){
63955
+ *pRes = 0;
63956
+ rc = sqlite3BtreeNext(pCur, pRes);
63957
+ if( rc ) return rc;
63958
+ if( *pRes==0 ){
63959
+ getCellInfo(pCur);
63960
+ if( pCur->info.nKey==intKey ){
63961
+ return SQLITE_OK;
63962
+ }
63963
+ }
63964
+ }
6383763965
}
6383863966
}
6383963967
6384063968
if( pIdxKey ){
6384163969
xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
@@ -63967,11 +64095,12 @@
6396764095
if( pCellKey==0 ){
6396864096
rc = SQLITE_NOMEM_BKPT;
6396964097
goto moveto_finish;
6397064098
}
6397164099
pCur->aiIdx[pCur->iPage] = (u16)idx;
63972
- rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 2);
64100
+ rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
64101
+ pCur->curFlags &= ~BTCF_ValidOvfl;
6397364102
if( rc ){
6397464103
sqlite3_free(pCellKey);
6397564104
goto moveto_finish;
6397664105
}
6397764106
c = xRecordCompare(nCell, pCellKey, pIdxKey);
@@ -66010,11 +66139,10 @@
6601066139
*/
6601166140
usableSpace = pBt->usableSize - 12 + leafCorrection;
6601266141
for(i=0; i<nOld; i++){
6601366142
MemPage *p = apOld[i];
6601466143
szNew[i] = usableSpace - p->nFree;
66015
- if( szNew[i]<0 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
6601666144
for(j=0; j<p->nOverflow; j++){
6601766145
szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
6601866146
}
6601966147
cntNew[i] = cntOld[i];
6602066148
}
@@ -66689,11 +66817,11 @@
6668966817
** to decode the key.
6669066818
*/
6669166819
SQLITE_PRIVATE int sqlite3BtreeInsert(
6669266820
BtCursor *pCur, /* Insert data into the table of this cursor */
6669366821
const BtreePayload *pX, /* Content of the row to be inserted */
66694
- int appendBias, /* True if this is likely an append */
66822
+ int flags, /* True if this is likely an append */
6669566823
int seekResult /* Result of prior MovetoUnpacked() call */
6669666824
){
6669766825
int rc;
6669866826
int loc = seekResult; /* -1: before desired location +1: after */
6669966827
int szNew = 0;
@@ -66701,10 +66829,12 @@
6670166829
MemPage *pPage;
6670266830
Btree *p = pCur->pBtree;
6670366831
BtShared *pBt = p->pBt;
6670466832
unsigned char *oldCell;
6670566833
unsigned char *newCell = 0;
66834
+
66835
+ assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND))==flags );
6670666836
6670766837
if( pCur->eState==CURSOR_FAULT ){
6670866838
assert( pCur->skipNext!=SQLITE_OK );
6670966839
return pCur->skipNext;
6671066840
}
@@ -66742,23 +66872,28 @@
6674266872
assert( pX->pKey==0 );
6674366873
/* If this is an insert into a table b-tree, invalidate any incrblob
6674466874
** cursors open on the row being replaced */
6674566875
invalidateIncrblobCursors(p, pX->nKey, 0);
6674666876
66877
+ /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
66878
+ ** to a row with the same key as the new entry being inserted. */
66879
+ assert( (flags & BTREE_SAVEPOSITION)==0 ||
66880
+ ((pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey) );
66881
+
6674766882
/* If the cursor is currently on the last row and we are appending a
6674866883
** new row onto the end, set the "loc" to avoid an unnecessary
6674966884
** btreeMoveto() call */
6675066885
if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
6675166886
loc = 0;
6675266887
}else if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey>0
6675366888
&& pCur->info.nKey==pX->nKey-1 ){
6675466889
loc = -1;
6675566890
}else if( loc==0 ){
66756
- rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, appendBias, &loc);
66891
+ rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
6675766892
if( rc ) return rc;
6675866893
}
66759
- }else if( loc==0 ){
66894
+ }else if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
6676066895
if( pX->nMem ){
6676166896
UnpackedRecord r;
6676266897
r.pKeyInfo = pCur->pKeyInfo;
6676366898
r.aMem = pX->aMem;
6676466899
r.nField = pX->nMem;
@@ -66765,13 +66900,13 @@
6676566900
r.default_rc = 0;
6676666901
r.errCode = 0;
6676766902
r.r1 = 0;
6676866903
r.r2 = 0;
6676966904
r.eqSeen = 0;
66770
- rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, appendBias, &loc);
66905
+ rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc);
6677166906
}else{
66772
- rc = btreeMoveto(pCur, pX->pKey, pX->nKey, appendBias, &loc);
66907
+ rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
6677366908
}
6677466909
if( rc ) return rc;
6677566910
}
6677666911
assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
6677766912
@@ -66855,10 +66990,24 @@
6685566990
** fails. Internal data structure corruption will result otherwise.
6685666991
** Also, set the cursor state to invalid. This stops saveCursorPosition()
6685766992
** from trying to save the current position of the cursor. */
6685866993
pCur->apPage[pCur->iPage]->nOverflow = 0;
6685966994
pCur->eState = CURSOR_INVALID;
66995
+ if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){
66996
+ rc = moveToRoot(pCur);
66997
+ if( pCur->pKeyInfo ){
66998
+ assert( pCur->pKey==0 );
66999
+ pCur->pKey = sqlite3Malloc( pX->nKey );
67000
+ if( pCur->pKey==0 ){
67001
+ rc = SQLITE_NOMEM;
67002
+ }else{
67003
+ memcpy(pCur->pKey, pX->pKey, pX->nKey);
67004
+ }
67005
+ }
67006
+ pCur->eState = CURSOR_REQUIRESEEK;
67007
+ pCur->nKey = pX->nKey;
67008
+ }
6686067009
}
6686167010
assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
6686267011
6686367012
end_insert:
6686467013
return rc;
@@ -71765,11 +71914,11 @@
7176571914
sqlite3VdbeGetOp(p,addr)->p2 = val;
7176671915
}
7176771916
SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
7176871917
sqlite3VdbeGetOp(p,addr)->p3 = val;
7176971918
}
71770
-SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 p5){
71919
+SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
7177171920
assert( p->nOp>0 || p->db->mallocFailed );
7177271921
if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
7177371922
}
7177471923
7177571924
/*
@@ -73479,64 +73628,63 @@
7347973628
** statement transaction is committed.
7348073629
**
7348173630
** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
7348273631
** Otherwise SQLITE_OK.
7348373632
*/
73484
-SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
73633
+static SQLITE_NOINLINE int vdbeCloseStatement(Vdbe *p, int eOp){
7348573634
sqlite3 *const db = p->db;
7348673635
int rc = SQLITE_OK;
73487
-
73488
- /* If p->iStatement is greater than zero, then this Vdbe opened a
73489
- ** statement transaction that should be closed here. The only exception
73490
- ** is that an IO error may have occurred, causing an emergency rollback.
73491
- ** In this case (db->nStatement==0), and there is nothing to do.
73492
- */
73493
- if( db->nStatement && p->iStatement ){
73494
- int i;
73495
- const int iSavepoint = p->iStatement-1;
73496
-
73497
- assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
73498
- assert( db->nStatement>0 );
73499
- assert( p->iStatement==(db->nStatement+db->nSavepoint) );
73500
-
73501
- for(i=0; i<db->nDb; i++){
73502
- int rc2 = SQLITE_OK;
73503
- Btree *pBt = db->aDb[i].pBt;
73504
- if( pBt ){
73505
- if( eOp==SAVEPOINT_ROLLBACK ){
73506
- rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
73507
- }
73508
- if( rc2==SQLITE_OK ){
73509
- rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
73510
- }
73511
- if( rc==SQLITE_OK ){
73512
- rc = rc2;
73513
- }
73514
- }
73515
- }
73516
- db->nStatement--;
73517
- p->iStatement = 0;
73518
-
73519
- if( rc==SQLITE_OK ){
73520
- if( eOp==SAVEPOINT_ROLLBACK ){
73521
- rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
73522
- }
73523
- if( rc==SQLITE_OK ){
73524
- rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
73525
- }
73526
- }
73527
-
73528
- /* If the statement transaction is being rolled back, also restore the
73529
- ** database handles deferred constraint counter to the value it had when
73530
- ** the statement transaction was opened. */
73531
- if( eOp==SAVEPOINT_ROLLBACK ){
73532
- db->nDeferredCons = p->nStmtDefCons;
73533
- db->nDeferredImmCons = p->nStmtDefImmCons;
73534
- }
73535
- }
73536
- return rc;
73537
-}
73636
+ int i;
73637
+ const int iSavepoint = p->iStatement-1;
73638
+
73639
+ assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
73640
+ assert( db->nStatement>0 );
73641
+ assert( p->iStatement==(db->nStatement+db->nSavepoint) );
73642
+
73643
+ for(i=0; i<db->nDb; i++){
73644
+ int rc2 = SQLITE_OK;
73645
+ Btree *pBt = db->aDb[i].pBt;
73646
+ if( pBt ){
73647
+ if( eOp==SAVEPOINT_ROLLBACK ){
73648
+ rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
73649
+ }
73650
+ if( rc2==SQLITE_OK ){
73651
+ rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
73652
+ }
73653
+ if( rc==SQLITE_OK ){
73654
+ rc = rc2;
73655
+ }
73656
+ }
73657
+ }
73658
+ db->nStatement--;
73659
+ p->iStatement = 0;
73660
+
73661
+ if( rc==SQLITE_OK ){
73662
+ if( eOp==SAVEPOINT_ROLLBACK ){
73663
+ rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
73664
+ }
73665
+ if( rc==SQLITE_OK ){
73666
+ rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
73667
+ }
73668
+ }
73669
+
73670
+ /* If the statement transaction is being rolled back, also restore the
73671
+ ** database handles deferred constraint counter to the value it had when
73672
+ ** the statement transaction was opened. */
73673
+ if( eOp==SAVEPOINT_ROLLBACK ){
73674
+ db->nDeferredCons = p->nStmtDefCons;
73675
+ db->nDeferredImmCons = p->nStmtDefImmCons;
73676
+ }
73677
+ return rc;
73678
+}
73679
+SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
73680
+ if( p->db->nStatement && p->iStatement ){
73681
+ return vdbeCloseStatement(p, eOp);
73682
+ }
73683
+ return SQLITE_OK;
73684
+}
73685
+
7353873686
7353973687
/*
7354073688
** This function is called when a transaction opened by the database
7354173689
** handle associated with the VM passed as an argument is about to be
7354273690
** committed. If there are outstanding deferred foreign key constraint
@@ -75567,14 +75715,14 @@
7556775715
** structure itself, using sqlite3DbFree().
7556875716
**
7556975717
** This function is used to free UnpackedRecord structures allocated by
7557075718
** the vdbeUnpackRecord() function found in vdbeapi.c.
7557175719
*/
75572
-static void vdbeFreeUnpacked(sqlite3 *db, UnpackedRecord *p){
75720
+static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
7557375721
if( p ){
7557475722
int i;
75575
- for(i=0; i<p->nField; i++){
75723
+ for(i=0; i<nField; i++){
7557675724
Mem *pMem = &p->aMem[i];
7557775725
if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
7557875726
}
7557975727
sqlite3DbFree(db, p);
7558075728
}
@@ -75603,14 +75751,19 @@
7560375751
const char *zTbl = pTab->zName;
7560475752
static const u8 fakeSortOrder = 0;
7560575753
7560675754
assert( db->pPreUpdate==0 );
7560775755
memset(&preupdate, 0, sizeof(PreUpdate));
75608
- if( op==SQLITE_UPDATE ){
75609
- iKey2 = v->aMem[iReg].u.i;
75756
+ if( HasRowid(pTab)==0 ){
75757
+ iKey1 = iKey2 = 0;
75758
+ preupdate.pPk = sqlite3PrimaryKeyIndex(pTab);
7561075759
}else{
75611
- iKey2 = iKey1;
75760
+ if( op==SQLITE_UPDATE ){
75761
+ iKey2 = v->aMem[iReg].u.i;
75762
+ }else{
75763
+ iKey2 = iKey1;
75764
+ }
7561275765
}
7561375766
7561475767
assert( pCsr->nField==pTab->nCol
7561575768
|| (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
7561675769
);
@@ -75629,12 +75782,12 @@
7562975782
7563075783
db->pPreUpdate = &preupdate;
7563175784
db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
7563275785
db->pPreUpdate = 0;
7563375786
sqlite3DbFree(db, preupdate.aRecord);
75634
- vdbeFreeUnpacked(db, preupdate.pUnpacked);
75635
- vdbeFreeUnpacked(db, preupdate.pNewUnpacked);
75787
+ vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pUnpacked);
75788
+ vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pNewUnpacked);
7563675789
if( preupdate.aNew ){
7563775790
int i;
7563875791
for(i=0; i<pCsr->nField; i++){
7563975792
sqlite3VdbeMemRelease(&preupdate.aNew[i]);
7564075793
}
@@ -77305,18 +77458,22 @@
7730577458
** This function is called from within a pre-update callback to retrieve
7730677459
** a field of the row currently being updated or deleted.
7730777460
*/
7730877461
SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
7730977462
PreUpdate *p = db->pPreUpdate;
77463
+ Mem *pMem;
7731077464
int rc = SQLITE_OK;
7731177465
7731277466
/* Test that this call is being made from within an SQLITE_DELETE or
7731377467
** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
7731477468
if( !p || p->op==SQLITE_INSERT ){
7731577469
rc = SQLITE_MISUSE_BKPT;
7731677470
goto preupdate_old_out;
7731777471
}
77472
+ if( p->pPk ){
77473
+ iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
77474
+ }
7731877475
if( iIdx>=p->pCsr->nField || iIdx<0 ){
7731977476
rc = SQLITE_RANGE;
7732077477
goto preupdate_old_out;
7732177478
}
7732277479
@@ -77338,21 +77495,18 @@
7733877495
goto preupdate_old_out;
7733977496
}
7734077497
p->aRecord = aRec;
7734177498
}
7734277499
77343
- if( iIdx>=p->pUnpacked->nField ){
77500
+ pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
77501
+ if( iIdx==p->pTab->iPKey ){
77502
+ sqlite3VdbeMemSetInt64(pMem, p->iKey1);
77503
+ }else if( iIdx>=p->pUnpacked->nField ){
7734477504
*ppValue = (sqlite3_value *)columnNullValue();
77345
- }else{
77346
- Mem *pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
77347
- *ppValue = &p->pUnpacked->aMem[iIdx];
77348
- if( iIdx==p->pTab->iPKey ){
77349
- sqlite3VdbeMemSetInt64(pMem, p->iKey1);
77350
- }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
77351
- if( pMem->flags & MEM_Int ){
77352
- sqlite3VdbeMemRealify(pMem);
77353
- }
77505
+ }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
77506
+ if( pMem->flags & MEM_Int ){
77507
+ sqlite3VdbeMemRealify(pMem);
7735477508
}
7735577509
}
7735677510
7735777511
preupdate_old_out:
7735877512
sqlite3Error(db, rc);
@@ -77401,10 +77555,13 @@
7740177555
7740277556
if( !p || p->op==SQLITE_DELETE ){
7740377557
rc = SQLITE_MISUSE_BKPT;
7740477558
goto preupdate_new_out;
7740577559
}
77560
+ if( p->pPk && p->op!=SQLITE_UPDATE ){
77561
+ iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
77562
+ }
7740677563
if( iIdx>=p->pCsr->nField || iIdx<0 ){
7740777564
rc = SQLITE_RANGE;
7740877565
goto preupdate_new_out;
7740977566
}
7741077567
@@ -77421,17 +77578,15 @@
7742177578
rc = SQLITE_NOMEM;
7742277579
goto preupdate_new_out;
7742377580
}
7742477581
p->pNewUnpacked = pUnpack;
7742577582
}
77426
- if( iIdx>=pUnpack->nField ){
77583
+ pMem = &pUnpack->aMem[iIdx];
77584
+ if( iIdx==p->pTab->iPKey ){
77585
+ sqlite3VdbeMemSetInt64(pMem, p->iKey2);
77586
+ }else if( iIdx>=pUnpack->nField ){
7742777587
pMem = (sqlite3_value *)columnNullValue();
77428
- }else{
77429
- pMem = &pUnpack->aMem[iIdx];
77430
- if( iIdx==p->pTab->iPKey ){
77431
- sqlite3VdbeMemSetInt64(pMem, p->iKey2);
77432
- }
7743377588
}
7743477589
}else{
7743577590
/* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
7743677591
** value. Make a copy of the cell contents and return a pointer to it.
7743777592
** It is not safe to return a pointer to the memory cell itself as the
@@ -78404,12 +78559,10 @@
7840478559
Mem *aMem = p->aMem; /* Copy of p->aMem */
7840578560
Mem *pIn1 = 0; /* 1st input operand */
7840678561
Mem *pIn2 = 0; /* 2nd input operand */
7840778562
Mem *pIn3 = 0; /* 3rd input operand */
7840878563
Mem *pOut = 0; /* Output operand */
78409
- int *aPermute = 0; /* Permutation of columns for OP_Compare */
78410
- i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */
7841178564
#ifdef VDBE_PROFILE
7841278565
u64 start; /* CPU clock count at start of opcode */
7841378566
#endif
7841478567
/*** INSERT STACK UNION HERE ***/
7841578568
@@ -78420,11 +78573,10 @@
7842078573
** sqlite3_column_text16() failed. */
7842178574
goto no_mem;
7842278575
}
7842378576
assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
7842478577
assert( p->bIsReader || p->readOnly!=0 );
78425
- p->rc = SQLITE_OK;
7842678578
p->iCurrentTime = 0;
7842778579
assert( p->explain==0 );
7842878580
p->pResultSet = 0;
7842978581
db->busyHandler.nBusy = 0;
7843078582
if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
@@ -78781,11 +78933,10 @@
7878178933
pFrame = p->pFrame;
7878278934
p->pFrame = pFrame->pParent;
7878378935
p->nFrame--;
7878478936
sqlite3VdbeSetChanges(db, p->nChange);
7878578937
pcx = sqlite3VdbeFrameRestore(pFrame);
78786
- lastRowid = db->lastRowid;
7878778938
if( pOp->p2==OE_Ignore ){
7878878939
/* Instruction pcx is the OP_Program that invoked the sub-program
7878978940
** currently being halted. If the p2 instruction of this OP_Halt
7879078941
** instruction is set to OE_Ignore, then the sub-program is throwing
7879178942
** an IGNORE exception. In this case jump to the address specified
@@ -79016,11 +79167,11 @@
7901679167
assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
7901779168
pVar = &p->aVar[pOp->p1 - 1];
7901879169
if( sqlite3VdbeMemTooBig(pVar) ){
7901979170
goto too_big;
7902079171
}
79021
- pOut = out2Prerelease(p, pOp);
79172
+ pOut = &aMem[pOp->p2];
7902279173
sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
7902379174
UPDATE_MAX_BLOBSIZE(pOut);
7902479175
break;
7902579176
}
7902679177
@@ -79503,13 +79654,11 @@
7950379654
REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
7950479655
}
7950579656
#endif
7950679657
MemSetTypeFlag(pCtx->pOut, MEM_Null);
7950779658
pCtx->fErrorOrAux = 0;
79508
- db->lastRowid = lastRowid;
7950979659
(*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
79510
- lastRowid = db->lastRowid; /* Remember rowid changes made by xSFunc */
7951179660
7951279661
/* If the function returned an error, throw an exception */
7951379662
if( pCtx->fErrorOrAux ){
7951479663
if( pCtx->isError ){
7951579664
sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
@@ -79961,12 +80110,12 @@
7996180110
}
7996280111
7996380112
7996480113
/* Opcode: Permutation * * * P4 *
7996580114
**
79966
-** Set the permutation used by the OP_Compare operator to be the array
79967
-** of integers in P4.
80115
+** Set the permutation used by the OP_Compare operator in the next
80116
+** instruction. The permutation is stored in the P4 operand.
7996880117
**
7996980118
** The permutation is only valid until the next OP_Compare that has
7997080119
** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
7997180120
** occur immediately prior to the OP_Compare.
7997280121
**
@@ -79974,11 +80123,12 @@
7997480123
** and does not become part of the permutation.
7997580124
*/
7997680125
case OP_Permutation: {
7997780126
assert( pOp->p4type==P4_INTARRAY );
7997880127
assert( pOp->p4.ai );
79979
- aPermute = pOp->p4.ai + 1;
80128
+ assert( pOp[1].opcode==OP_Compare );
80129
+ assert( pOp[1].p5 & OPFLAG_PERMUTE );
7998080130
break;
7998180131
}
7998280132
7998380133
/* Opcode: Compare P1 P2 P3 P4 P5
7998480134
** Synopsis: r[P1@P3] <-> r[P2@P3]
@@ -80007,12 +80157,21 @@
8000780157
int p2;
8000880158
const KeyInfo *pKeyInfo;
8000980159
int idx;
8001080160
CollSeq *pColl; /* Collating sequence to use on this term */
8001180161
int bRev; /* True for DESCENDING sort order */
80162
+ int *aPermute; /* The permutation */
8001280163
80013
- if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
80164
+ if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
80165
+ aPermute = 0;
80166
+ }else{
80167
+ assert( pOp>aOp );
80168
+ assert( pOp[-1].opcode==OP_Permutation );
80169
+ assert( pOp[-1].p4type==P4_INTARRAY );
80170
+ aPermute = pOp[-1].p4.ai + 1;
80171
+ assert( aPermute!=0 );
80172
+ }
8001480173
n = pOp->p3;
8001580174
pKeyInfo = pOp->p4.pKeyInfo;
8001680175
assert( n>0 );
8001780176
assert( pKeyInfo!=0 );
8001880177
p1 = pOp->p1;
@@ -80041,11 +80200,10 @@
8004180200
if( iCompare ){
8004280201
if( bRev ) iCompare = -iCompare;
8004380202
break;
8004480203
}
8004580204
}
80046
- aPermute = 0;
8004780205
break;
8004880206
}
8004980207
8005080208
/* Opcode: Jump P1 P2 P3 * *
8005180209
**
@@ -80597,10 +80755,24 @@
8059780755
do{
8059880756
applyAffinity(pRec++, *(zAffinity++), encoding);
8059980757
assert( zAffinity[0]==0 || pRec<=pLast );
8060080758
}while( zAffinity[0] );
8060180759
}
80760
+
80761
+#ifdef SQLITE_ENABLE_NULL_TRIM
80762
+ /* NULLs can be safely trimmed from the end of the record, as long as
80763
+ ** as the schema format is 2 or more and none of the omitted columns
80764
+ ** have a non-NULL default value. Also, the record must be left with
80765
+ ** at least one field. If P5>0 then it will be one more than the
80766
+ ** index of the right-most column with a non-NULL default value */
80767
+ if( pOp->p5 ){
80768
+ while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
80769
+ pLast--;
80770
+ nField--;
80771
+ }
80772
+ }
80773
+#endif
8060280774
8060380775
/* Loop through the elements that will make up the record to figure
8060480776
** out how much space is required for the new record.
8060580777
*/
8060680778
pRec = pLast;
@@ -82187,11 +82359,11 @@
8218782359
assert( memIsValid(pData) );
8218882360
pC = p->apCsr[pOp->p1];
8218982361
assert( pC!=0 );
8219082362
assert( pC->eCurType==CURTYPE_BTREE );
8219182363
assert( pC->uc.pCursor!=0 );
82192
- assert( pC->isTable );
82364
+ assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
8219382365
assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
8219482366
REGISTER_TRACE(pOp->p2, pData);
8219582367
8219682368
if( pOp->opcode==OP_Insert ){
8219782369
pKey = &aMem[pOp->p3];
@@ -82203,18 +82375,17 @@
8220382375
assert( pOp->opcode==OP_InsertInt );
8220482376
x.nKey = pOp->p3;
8220582377
}
8220682378
8220782379
if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
82208
- assert( pC->isTable );
8220982380
assert( pC->iDb>=0 );
8221082381
zDb = db->aDb[pC->iDb].zDbSName;
8221182382
pTab = pOp->p4.pTab;
82212
- assert( HasRowid(pTab) );
82383
+ assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
8221382384
op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
8221482385
}else{
82215
- pTab = 0; /* Not needed. Silence a comiler warning. */
82386
+ pTab = 0; /* Not needed. Silence a compiler warning. */
8221682387
zDb = 0; /* Not needed. Silence a compiler warning. */
8221782388
}
8221882389
8221982390
#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
8222082391
/* Invoke the pre-update hook, if any */
@@ -82222,14 +82393,15 @@
8222282393
&& pOp->p4type==P4_TABLE
8222382394
&& !(pOp->p5 & OPFLAG_ISUPDATE)
8222482395
){
8222582396
sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey, pOp->p2);
8222682397
}
82398
+ if( pOp->p5 & OPFLAG_ISNOOP ) break;
8222782399
#endif
8222882400
8222982401
if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
82230
- if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = x.nKey;
82402
+ if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
8223182403
if( pData->flags & MEM_Null ){
8223282404
x.pData = 0;
8223382405
x.nData = 0;
8223482406
}else{
8223582407
assert( pData->flags & (MEM_Blob|MEM_Str) );
@@ -82242,11 +82414,11 @@
8224282414
}else{
8224382415
x.nZero = 0;
8224482416
}
8224582417
x.pKey = 0;
8224682418
rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
82247
- (pOp->p5 & OPFLAG_APPEND)!=0, seekResult
82419
+ (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
8224882420
);
8224982421
pC->deferredMoveto = 0;
8225082422
pC->cacheStatus = CACHE_STALE;
8225182423
8225282424
/* Invoke the update-hook if required. */
@@ -82334,12 +82506,15 @@
8233482506
pTab = 0; /* Not needed. Silence a compiler warning. */
8233582507
}
8233682508
8233782509
#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
8233882510
/* Invoke the pre-update-hook if required. */
82339
- if( db->xPreUpdateCallback && pOp->p4.pTab && HasRowid(pTab) ){
82340
- assert( !(opflags & OPFLAG_ISUPDATE) || (aMem[pOp->p3].flags & MEM_Int) );
82511
+ if( db->xPreUpdateCallback && pOp->p4.pTab ){
82512
+ assert( !(opflags & OPFLAG_ISUPDATE)
82513
+ || HasRowid(pTab)==0
82514
+ || (aMem[pOp->p3].flags & MEM_Int)
82515
+ );
8234182516
sqlite3VdbePreUpdateHook(p, pC,
8234282517
(opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
8234382518
zDb, pTab, pC->movetoTarget,
8234482519
pOp->p3
8234582520
);
@@ -82453,11 +82628,11 @@
8245382628
if( rc ) goto abort_due_to_error;
8245482629
p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
8245582630
break;
8245682631
}
8245782632
82458
-/* Opcode: RowData P1 P2 * * *
82633
+/* Opcode: RowData P1 P2 P3 * *
8245982634
** Synopsis: r[P2]=data
8246082635
**
8246182636
** Write into register P2 the complete row content for the row at
8246282637
** which cursor P1 is currently pointing.
8246382638
** There is no interpretation of the data.
@@ -82467,18 +82642,30 @@
8246782642
** If cursor P1 is an index, then the content is the key of the row.
8246882643
** If cursor P2 is a table, then the content extracted is the data.
8246982644
**
8247082645
** If the P1 cursor must be pointing to a valid row (not a NULL row)
8247182646
** of a real table, not a pseudo-table.
82647
+**
82648
+** If P3!=0 then this opcode is allowed to make an ephermeral pointer
82649
+** into the database page. That means that the content of the output
82650
+** register will be invalidated as soon as the cursor moves - including
82651
+** moves caused by other cursors that "save" the the current cursors
82652
+** position in order that they can write to the same table. If P3==0
82653
+** then a copy of the data is made into memory. P3!=0 is faster, but
82654
+** P3==0 is safer.
82655
+**
82656
+** If P3!=0 then the content of the P2 register is unsuitable for use
82657
+** in OP_Result and any OP_Result will invalidate the P2 register content.
82658
+** The P2 register content is invalidated by opcodes like OP_Function or
82659
+** by any use of another cursor pointing to the same table.
8247282660
*/
8247382661
case OP_RowData: {
8247482662
VdbeCursor *pC;
8247582663
BtCursor *pCrsr;
8247682664
u32 n;
8247782665
82478
- pOut = &aMem[pOp->p2];
82479
- memAboutToChange(p, pOut);
82666
+ pOut = out2Prerelease(p, pOp);
8248082667
8248182668
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
8248282669
pC = p->apCsr[pOp->p1];
8248382670
assert( pC!=0 );
8248482671
assert( pC->eCurType==CURTYPE_BTREE );
@@ -82505,18 +82692,13 @@
8250582692
n = sqlite3BtreePayloadSize(pCrsr);
8250682693
if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
8250782694
goto too_big;
8250882695
}
8250982696
testcase( n==0 );
82510
- if( sqlite3VdbeMemClearAndResize(pOut, MAX(n,32)) ){
82511
- goto no_mem;
82512
- }
82513
- pOut->n = n;
82514
- MemSetTypeFlag(pOut, MEM_Blob);
82515
- rc = sqlite3BtreePayload(pCrsr, 0, n, pOut->z);
82697
+ rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut);
8251682698
if( rc ) goto abort_due_to_error;
82517
- pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
82699
+ if( !pOp->p3 ) Deephemeralize(pOut);
8251882700
UPDATE_MAX_BLOBSIZE(pOut);
8251982701
REGISTER_TRACE(pOp->p2, pOut);
8252082702
break;
8252182703
}
8252282704
@@ -82900,11 +83082,11 @@
8290083082
x.nKey = pIn2->n;
8290183083
x.pKey = pIn2->z;
8290283084
x.aMem = aMem + pOp->p3;
8290383085
x.nMem = (u16)pOp->p4.i;
8290483086
rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
82905
- (pOp->p5 & OPFLAG_APPEND)!=0,
83087
+ (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)),
8290683088
((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
8290783089
);
8290883090
assert( pC->deferredMoveto==0 );
8290983091
pC->cacheStatus = CACHE_STALE;
8291083092
}
@@ -83022,11 +83204,10 @@
8302283204
pTabCur->aAltMap = pOp->p4.ai;
8302383205
pTabCur->pAltCursor = pC;
8302483206
}else{
8302583207
pOut = out2Prerelease(p, pOp);
8302683208
pOut->u.i = rowid;
83027
- pOut->flags = MEM_Int;
8302883209
}
8302983210
}else{
8303083211
assert( pOp->opcode==OP_IdxRowid );
8303183212
sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
8303283213
}
@@ -83664,11 +83845,11 @@
8366483845
assert( (int)(pOp - aOp)==pFrame->pc );
8366583846
}
8366683847
8366783848
p->nFrame++;
8366883849
pFrame->pParent = p->pFrame;
83669
- pFrame->lastRowid = lastRowid;
83850
+ pFrame->lastRowid = db->lastRowid;
8367083851
pFrame->nChange = p->nChange;
8367183852
pFrame->nDbChange = p->db->nChange;
8367283853
assert( pFrame->pAuxData==0 );
8367383854
pFrame->pAuxData = p->pAuxData;
8367483855
p->pAuxData = 0;
@@ -84605,11 +84786,11 @@
8460584786
rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
8460684787
db->vtabOnConflict = vtabOnConflict;
8460784788
sqlite3VtabImportErrmsg(p, pVtab);
8460884789
if( rc==SQLITE_OK && pOp->p1 ){
8460984790
assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
84610
- db->lastRowid = lastRowid = rowid;
84791
+ db->lastRowid = rowid;
8461184792
}
8461284793
if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
8461384794
if( pOp->p5==OE_Ignore ){
8461484795
rc = SQLITE_OK;
8461584796
}else{
@@ -84841,11 +85022,10 @@
8484185022
8484285023
/* This is the only way out of this procedure. We have to
8484385024
** release the mutexes on btrees that were acquired at the
8484485025
** top. */
8484585026
vdbe_return:
84846
- db->lastRowid = lastRowid;
8484785027
testcase( nVmStep>0 );
8484885028
p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
8484985029
sqlite3VdbeLeave(p);
8485085030
assert( rc!=SQLITE_OK || nExtraDelete==0
8485185031
|| sqlite3_strlike("DELETE%",p->zSql,0)!=0
@@ -84905,14 +85085,13 @@
8490585085
/*
8490685086
** Valid sqlite3_blob* handles point to Incrblob structures.
8490785087
*/
8490885088
typedef struct Incrblob Incrblob;
8490985089
struct Incrblob {
84910
- int flags; /* Copy of "flags" passed to sqlite3_blob_open() */
8491185090
int nByte; /* Size of open blob, in bytes */
8491285091
int iOffset; /* Byte offset of blob in cursor data */
84913
- int iCol; /* Table column this handle is open on */
85092
+ u16 iCol; /* Table column this handle is open on */
8491485093
BtCursor *pCsr; /* Cursor pointing at blob row */
8491585094
sqlite3_stmt *pStmt; /* Statement holding cursor open */
8491685095
sqlite3 *db; /* The associated database */
8491785096
char *zDb; /* Database name */
8491885097
Table *pTab; /* Table object */
@@ -84939,21 +85118,31 @@
8493985118
static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
8494085119
int rc; /* Error code */
8494185120
char *zErr = 0; /* Error message */
8494285121
Vdbe *v = (Vdbe *)p->pStmt;
8494385122
84944
- /* Set the value of the SQL statements only variable to integer iRow.
84945
- ** This is done directly instead of using sqlite3_bind_int64() to avoid
84946
- ** triggering asserts related to mutexes.
85123
+ /* Set the value of register r[1] in the SQL statement to integer iRow.
85124
+ ** This is done directly as a performance optimization
8494785125
*/
84948
- assert( v->aVar[0].flags&MEM_Int );
84949
- v->aVar[0].u.i = iRow;
85126
+ v->aMem[1].flags = MEM_Int;
85127
+ v->aMem[1].u.i = iRow;
8495085128
84951
- rc = sqlite3_step(p->pStmt);
85129
+ /* If the statement has been run before (and is paused at the OP_ResultRow)
85130
+ ** then back it up to the point where it does the OP_SeekRowid. This could
85131
+ ** have been down with an extra OP_Goto, but simply setting the program
85132
+ ** counter is faster. */
85133
+ if( v->pc>3 ){
85134
+ v->pc = 3;
85135
+ rc = sqlite3VdbeExec(v);
85136
+ }else{
85137
+ rc = sqlite3_step(p->pStmt);
85138
+ }
8495285139
if( rc==SQLITE_ROW ){
8495385140
VdbeCursor *pC = v->apCsr[0];
84954
- u32 type = pC->aType[p->iCol];
85141
+ u32 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
85142
+ testcase( pC->nHdrParsed==p->iCol );
85143
+ testcase( pC->nHdrParsed==p->iCol+1 );
8495585144
if( type<12 ){
8495685145
zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
8495785146
type==0?"null": type==7?"real": "integer"
8495885147
);
8495985148
rc = SQLITE_ERROR;
@@ -84994,11 +85183,11 @@
8499485183
sqlite3* db, /* The database connection */
8499585184
const char *zDb, /* The attached database containing the blob */
8499685185
const char *zTable, /* The table containing the blob */
8499785186
const char *zColumn, /* The column containing the blob */
8499885187
sqlite_int64 iRow, /* The row containing the glob */
84999
- int flags, /* True -> read/write access, false -> read-only */
85188
+ int wrFlag, /* True -> read/write access, false -> read-only */
8500085189
sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
8500185190
){
8500285191
int nAttempt = 0;
8500385192
int iCol; /* Index of zColumn in row-record */
8500485193
int rc = SQLITE_OK;
@@ -85016,11 +85205,11 @@
8501685205
#ifdef SQLITE_ENABLE_API_ARMOR
8501785206
if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
8501885207
return SQLITE_MISUSE_BKPT;
8501985208
}
8502085209
#endif
85021
- flags = !!flags; /* flags = (flags ? 1 : 0); */
85210
+ wrFlag = !!wrFlag; /* wrFlag = (wrFlag ? 1 : 0); */
8502285211
8502385212
sqlite3_mutex_enter(db->mutex);
8502485213
8502585214
pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
8502685215
if( !pBlob ) goto blob_open_out;
@@ -85076,13 +85265,12 @@
8507685265
goto blob_open_out;
8507785266
}
8507885267
8507985268
/* If the value is being opened for writing, check that the
8508085269
** column is not indexed, and that it is not part of a foreign key.
85081
- ** It is against the rules to open a column to which either of these
85082
- ** descriptions applies for writing. */
85083
- if( flags ){
85270
+ */
85271
+ if( wrFlag ){
8508485272
const char *zFault = 0;
8508585273
Index *pIdx;
8508685274
#ifndef SQLITE_OMIT_FOREIGN_KEY
8508785275
if( db->flags&SQLITE_ForeignKeys ){
8508885276
/* Check that the column is not part of an FK child key definition. It
@@ -85139,22 +85327,21 @@
8513985327
*/
8514085328
static const int iLn = VDBE_OFFSET_LINENO(2);
8514185329
static const VdbeOpList openBlob[] = {
8514285330
{OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */
8514385331
{OP_OpenRead, 0, 0, 0}, /* 1: Open a cursor */
85144
- {OP_Variable, 1, 1, 0}, /* 2: Move ?1 into reg[1] */
85145
- {OP_NotExists, 0, 7, 1}, /* 3: Seek the cursor */
85146
- {OP_Column, 0, 0, 1}, /* 4 */
85147
- {OP_ResultRow, 1, 0, 0}, /* 5 */
85148
- {OP_Goto, 0, 2, 0}, /* 6 */
85149
- {OP_Halt, 0, 0, 0}, /* 7 */
85332
+ /* blobSeekToRow() will initialize r[1] to the desired rowid */
85333
+ {OP_NotExists, 0, 5, 1}, /* 2: Seek the cursor to rowid=r[1] */
85334
+ {OP_Column, 0, 0, 1}, /* 3 */
85335
+ {OP_ResultRow, 1, 0, 0}, /* 4 */
85336
+ {OP_Halt, 0, 0, 0}, /* 5 */
8515085337
};
8515185338
Vdbe *v = (Vdbe *)pBlob->pStmt;
8515285339
int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
8515385340
VdbeOp *aOp;
8515485341
85155
- sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, flags,
85342
+ sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag,
8515685343
pTab->pSchema->schema_cookie,
8515785344
pTab->pSchema->iGeneration);
8515885345
sqlite3VdbeChangeP5(v, 1);
8515985346
aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
8516085347
@@ -85167,19 +85354,19 @@
8516785354
#ifdef SQLITE_OMIT_SHARED_CACHE
8516885355
aOp[0].opcode = OP_Noop;
8516985356
#else
8517085357
aOp[0].p1 = iDb;
8517185358
aOp[0].p2 = pTab->tnum;
85172
- aOp[0].p3 = flags;
85359
+ aOp[0].p3 = wrFlag;
8517385360
sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
8517485361
}
8517585362
if( db->mallocFailed==0 ){
8517685363
#endif
8517785364
8517885365
/* Remove either the OP_OpenWrite or OpenRead. Set the P2
8517985366
** parameter of the other to pTab->tnum. */
85180
- if( flags ) aOp[1].opcode = OP_OpenWrite;
85367
+ if( wrFlag ) aOp[1].opcode = OP_OpenWrite;
8518185368
aOp[1].p2 = pTab->tnum;
8518285369
aOp[1].p3 = iDb;
8518385370
8518485371
/* Configure the number of columns. Configure the cursor to
8518585372
** think that the table has one more column than it really
@@ -85188,27 +85375,25 @@
8518885375
** we can invoke OP_Column to fill in the vdbe cursors type
8518985376
** and offset cache without causing any IO.
8519085377
*/
8519185378
aOp[1].p4type = P4_INT32;
8519285379
aOp[1].p4.i = pTab->nCol+1;
85193
- aOp[4].p2 = pTab->nCol;
85380
+ aOp[3].p2 = pTab->nCol;
8519485381
85195
- pParse->nVar = 1;
85382
+ pParse->nVar = 0;
8519685383
pParse->nMem = 1;
8519785384
pParse->nTab = 1;
8519885385
sqlite3VdbeMakeReady(v, pParse);
8519985386
}
8520085387
}
8520185388
85202
- pBlob->flags = flags;
8520385389
pBlob->iCol = iCol;
8520485390
pBlob->db = db;
8520585391
sqlite3BtreeLeaveAll(db);
8520685392
if( db->mallocFailed ){
8520785393
goto blob_open_out;
8520885394
}
85209
- sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
8521085395
rc = blobSeekToRow(pBlob, iRow, &zErr);
8521185396
} while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
8521285397
8521385398
blob_open_out:
8521485399
if( rc==SQLITE_OK && db->mallocFailed==0 ){
@@ -88741,12 +88926,10 @@
8874188926
** This file contains routines used for walking the parser tree and
8874288927
** resolve all identifiers by associating them with a particular
8874388928
** table and column.
8874488929
*/
8874588930
/* #include "sqliteInt.h" */
88746
-/* #include <stdlib.h> */
88747
-/* #include <string.h> */
8874888931
8874988932
/*
8875088933
** Walk the expression tree pExpr and increase the aggregate function
8875188934
** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
8875288935
** This needs to occur when copying a TK_AGG_FUNCTION node from an
@@ -91237,21 +91420,27 @@
9123791420
int doAdd = 0;
9123891421
if( z[0]=='?' ){
9123991422
/* Wildcard of the form "?nnn". Convert "nnn" to an integer and
9124091423
** use it as the variable number */
9124191424
i64 i;
91242
- int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
91243
- x = (ynVar)i;
91425
+ int bOk;
91426
+ if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
91427
+ i = z[1]-'0'; /* The common case of ?N for a single digit N */
91428
+ bOk = 1;
91429
+ }else{
91430
+ bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
91431
+ }
9124491432
testcase( i==0 );
9124591433
testcase( i==1 );
9124691434
testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
9124791435
testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
9124891436
if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
9124991437
sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
9125091438
db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
9125191439
return;
9125291440
}
91441
+ x = (ynVar)i;
9125391442
if( x>pParse->nVar ){
9125491443
pParse->nVar = (int)x;
9125591444
doAdd = 1;
9125691445
}else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
9125791446
doAdd = 1;
@@ -91682,37 +91871,45 @@
9168291871
pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
9168391872
pNewItem->idx = pOldItem->idx;
9168491873
}
9168591874
return pNew;
9168691875
}
91687
-SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
91688
- Select *pNew, *pPrior;
91876
+SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *pDup, int flags){
91877
+ Select *pRet = 0;
91878
+ Select *pNext = 0;
91879
+ Select **pp = &pRet;
91880
+ Select *p;
91881
+
9168991882
assert( db!=0 );
91690
- if( p==0 ) return 0;
91691
- pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
91692
- if( pNew==0 ) return 0;
91693
- pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
91694
- pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
91695
- pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
91696
- pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
91697
- pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
91698
- pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
91699
- pNew->op = p->op;
91700
- pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags);
91701
- if( pPrior ) pPrior->pNext = pNew;
91702
- pNew->pNext = 0;
91703
- pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
91704
- pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
91705
- pNew->iLimit = 0;
91706
- pNew->iOffset = 0;
91707
- pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
91708
- pNew->addrOpenEphm[0] = -1;
91709
- pNew->addrOpenEphm[1] = -1;
91710
- pNew->nSelectRow = p->nSelectRow;
91711
- pNew->pWith = withDup(db, p->pWith);
91712
- sqlite3SelectSetName(pNew, p->zSelName);
91713
- return pNew;
91883
+ for(p=pDup; p; p=p->pPrior){
91884
+ Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
91885
+ if( pNew==0 ) break;
91886
+ pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
91887
+ pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
91888
+ pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
91889
+ pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
91890
+ pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
91891
+ pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
91892
+ pNew->op = p->op;
91893
+ pNew->pNext = pNext;
91894
+ pNew->pPrior = 0;
91895
+ pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
91896
+ pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
91897
+ pNew->iLimit = 0;
91898
+ pNew->iOffset = 0;
91899
+ pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
91900
+ pNew->addrOpenEphm[0] = -1;
91901
+ pNew->addrOpenEphm[1] = -1;
91902
+ pNew->nSelectRow = p->nSelectRow;
91903
+ pNew->pWith = withDup(db, p->pWith);
91904
+ sqlite3SelectSetName(pNew, p->zSelName);
91905
+ *pp = pNew;
91906
+ pp = &pNew->pPrior;
91907
+ pNext = pNew;
91908
+ }
91909
+
91910
+ return pRet;
9171491911
}
9171591912
#else
9171691913
SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
9171791914
assert( p==0 );
9171891915
return 0;
@@ -91773,11 +91970,11 @@
9177391970
**
9177491971
** (a,b,c) = (expr1,expr2,expr3)
9177591972
** Or: (a,b,c) = (SELECT x,y,z FROM ....)
9177691973
**
9177791974
** For each term of the vector assignment, append new entries to the
91778
-** expression list pList. In the case of a subquery on the LHS, append
91975
+** expression list pList. In the case of a subquery on the RHS, append
9177991976
** TK_SELECT_COLUMN expressions.
9178091977
*/
9178191978
SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(
9178291979
Parse *pParse, /* Parsing context */
9178391980
ExprList *pList, /* List to which to append. Might be NULL */
@@ -93882,10 +94079,15 @@
9388294079
int i; /* Loop counter */
9388394080
sqlite3 *db = pParse->db; /* The database connection */
9388494081
u8 enc = ENC(db); /* The text encoding used by this database */
9388594082
CollSeq *pColl = 0; /* A collating sequence */
9388694083
94084
+ if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
94085
+ /* SQL functions can be expensive. So try to move constant functions
94086
+ ** out of the inner loop, even if that means an extra OP_Copy. */
94087
+ return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
94088
+ }
9388794089
assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
9388894090
if( ExprHasProperty(pExpr, EP_TokenOnly) ){
9388994091
pFarg = 0;
9389094092
}else{
9389194093
pFarg = pExpr->x.pList;
@@ -93929,10 +94131,26 @@
9392994131
*/
9393094132
if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
9393194133
assert( nFarg>=1 );
9393294134
return sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
9393394135
}
94136
+
94137
+#ifdef SQLITE_DEBUG
94138
+ /* The AFFINITY() function evaluates to a string that describes
94139
+ ** the type affinity of the argument. This is used for testing of
94140
+ ** the SQLite type logic.
94141
+ */
94142
+ if( pDef->funcFlags & SQLITE_FUNC_AFFINITY ){
94143
+ const char *azAff[] = { "blob", "text", "numeric", "integer", "real" };
94144
+ char aff;
94145
+ assert( nFarg==1 );
94146
+ aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
94147
+ sqlite3VdbeLoadString(v, target,
94148
+ aff ? azAff[aff-SQLITE_AFF_BLOB] : "none");
94149
+ return target;
94150
+ }
94151
+#endif
9393494152
9393594153
for(i=0; i<nFarg; i++){
9393694154
if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
9393794155
testcase( i==31 );
9393894156
constMask |= MASKBIT32(i);
@@ -94246,28 +94464,44 @@
9424694464
return inReg;
9424794465
}
9424894466
9424994467
/*
9425094468
** Factor out the code of the given expression to initialization time.
94469
+**
94470
+** If regDest>=0 then the result is always stored in that register and the
94471
+** result is not reusable. If regDest<0 then this routine is free to
94472
+** store the value whereever it wants. The register where the expression
94473
+** is stored is returned. When regDest<0, two identical expressions will
94474
+** code to the same register.
9425194475
*/
94252
-SQLITE_PRIVATE void sqlite3ExprCodeAtInit(
94476
+SQLITE_PRIVATE int sqlite3ExprCodeAtInit(
9425394477
Parse *pParse, /* Parsing context */
9425494478
Expr *pExpr, /* The expression to code when the VDBE initializes */
94255
- int regDest, /* Store the value in this register */
94256
- u8 reusable /* True if this expression is reusable */
94479
+ int regDest /* Store the value in this register */
9425794480
){
9425894481
ExprList *p;
9425994482
assert( ConstFactorOk(pParse) );
9426094483
p = pParse->pConstExpr;
94484
+ if( regDest<0 && p ){
94485
+ struct ExprList_item *pItem;
94486
+ int i;
94487
+ for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
94488
+ if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){
94489
+ return pItem->u.iConstExprReg;
94490
+ }
94491
+ }
94492
+ }
9426194493
pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
9426294494
p = sqlite3ExprListAppend(pParse, p, pExpr);
9426394495
if( p ){
9426494496
struct ExprList_item *pItem = &p->a[p->nExpr-1];
94497
+ pItem->reusable = regDest<0;
94498
+ if( regDest<0 ) regDest = ++pParse->nMem;
9426594499
pItem->u.iConstExprReg = regDest;
94266
- pItem->reusable = reusable;
9426794500
}
9426894501
pParse->pConstExpr = p;
94502
+ return regDest;
9426994503
}
9427094504
9427194505
/*
9427294506
** Generate code to evaluate an expression and store the results
9427394507
** into a register. Return the register number where the results
@@ -94286,23 +94520,12 @@
9428694520
pExpr = sqlite3ExprSkipCollate(pExpr);
9428794521
if( ConstFactorOk(pParse)
9428894522
&& pExpr->op!=TK_REGISTER
9428994523
&& sqlite3ExprIsConstantNotJoin(pExpr)
9429094524
){
94291
- ExprList *p = pParse->pConstExpr;
94292
- int i;
9429394525
*pReg = 0;
94294
- if( p ){
94295
- struct ExprList_item *pItem;
94296
- for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
94297
- if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){
94298
- return pItem->u.iConstExprReg;
94299
- }
94300
- }
94301
- }
94302
- r2 = ++pParse->nMem;
94303
- sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1);
94526
+ r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1);
9430494527
}else{
9430594528
int r1 = sqlite3GetTempReg(pParse);
9430694529
r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
9430794530
if( r2==r1 ){
9430894531
*pReg = r1;
@@ -94352,11 +94575,11 @@
9435294575
** in register target. If the expression is constant, then this routine
9435394576
** might choose to code the expression at initialization time.
9435494577
*/
9435594578
SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
9435694579
if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
94357
- sqlite3ExprCodeAtInit(pParse, pExpr, target, 0);
94580
+ sqlite3ExprCodeAtInit(pParse, pExpr, target);
9435894581
}else{
9435994582
sqlite3ExprCode(pParse, pExpr, target);
9436094583
}
9436194584
}
9436294585
@@ -94424,11 +94647,11 @@
9442494647
n--;
9442594648
}else{
9442694649
sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
9442794650
}
9442894651
}else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){
94429
- sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0);
94652
+ sqlite3ExprCodeAtInit(pParse, pExpr, target+i);
9443094653
}else{
9443194654
int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
9443294655
if( inReg!=target+i ){
9443394656
VdbeOp *pOp;
9443494657
if( copyOp==OP_Copy
@@ -96968,10 +97191,16 @@
9696897191
** used to query statistical information that has been gathered into
9696997192
** the Stat4Accum object by prior calls to stat_push(). The P parameter
9697097193
** has type BLOB but it is really just a pointer to the Stat4Accum object.
9697197194
** The content to returned is determined by the parameter J
9697297195
** which is one of the STAT_GET_xxxx values defined above.
97196
+**
97197
+** The stat_get(P,J) function is not available to generic SQL. It is
97198
+** inserted as part of a manually constructed bytecode program. (See
97199
+** the callStatGet() routine below.) It is guaranteed that the P
97200
+** parameter will always be a poiner to a Stat4Accum object, never a
97201
+** NULL.
9697397202
**
9697497203
** If neither STAT3 nor STAT4 are enabled, then J is always
9697597204
** STAT_GET_STAT1 and is hence omitted and this routine becomes
9697697205
** a one-parameter function, stat_get(P), that always returns the
9697797206
** stat1 table entry information.
@@ -97787,11 +98016,11 @@
9778798016
sumEq += aSample[i].anEq[iCol];
9778898017
nSum100 += 100;
9778998018
}
9779098019
}
9779198020
97792
- if( nDist100>nSum100 ){
98021
+ if( nDist100>nSum100 && sumEq<nRow ){
9779398022
avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
9779498023
}
9779598024
if( avgEq==0 ) avgEq = 1;
9779698025
pIdx->aAvgEq[iCol] = avgEq;
9779798026
}
@@ -98201,10 +98430,11 @@
9820198430
assert( pVfs );
9820298431
flags |= SQLITE_OPEN_MAIN_DB;
9820398432
rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags);
9820498433
sqlite3_free( zPath );
9820598434
db->nDb++;
98435
+ db->skipBtreeMutex = 0;
9820698436
if( rc==SQLITE_CONSTRAINT ){
9820798437
rc = SQLITE_ERROR;
9820898438
zErrDyn = sqlite3MPrintf(db, "database is already attached");
9820998439
}else if( rc==SQLITE_OK ){
9821098440
Pager *pPager;
@@ -104365,16 +104595,12 @@
104365104595
}
104366104596
}else
104367104597
#endif
104368104598
{
104369104599
int count = (pParse->nested==0); /* True to count changes */
104370
- int iIdxNoSeek = -1;
104371
- if( bComplex==0 && aiCurOnePass[1]!=iDataCur ){
104372
- iIdxNoSeek = aiCurOnePass[1];
104373
- }
104374104600
sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
104375
- iKey, nKey, count, OE_Default, eOnePass, iIdxNoSeek);
104601
+ iKey, nKey, count, OE_Default, eOnePass, aiCurOnePass[1]);
104376104602
}
104377104603
104378104604
/* End of the loop over all rowids/primary-keys. */
104379104605
if( eOnePass!=ONEPASS_OFF ){
104380104606
sqlite3VdbeResolveLabel(v, addrBypass);
@@ -104450,19 +104676,21 @@
104450104676
** then this function must seek iDataCur to the entry identified by iPk
104451104677
** and nPk before reading from it.
104452104678
**
104453104679
** If eMode is ONEPASS_MULTI, then this call is being made as part
104454104680
** of a ONEPASS delete that affects multiple rows. In this case, if
104455
-** iIdxNoSeek is a valid cursor number (>=0), then its position should
104456
-** be preserved following the delete operation. Or, if iIdxNoSeek is not
104457
-** a valid cursor number, the position of iDataCur should be preserved
104458
-** instead.
104681
+** iIdxNoSeek is a valid cursor number (>=0) and is not the same as
104682
+** iDataCur, then its position should be preserved following the delete
104683
+** operation. Or, if iIdxNoSeek is not a valid cursor number, the
104684
+** position of iDataCur should be preserved instead.
104459104685
**
104460104686
** iIdxNoSeek:
104461
-** If iIdxNoSeek is a valid cursor number (>=0), then it identifies an
104462
-** index cursor (from within array of cursors starting at iIdxCur) that
104463
-** already points to the index entry to be deleted.
104687
+** If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
104688
+** then it identifies an index cursor (from within array of cursors
104689
+** starting at iIdxCur) that already points to the index entry to be deleted.
104690
+** Except, this optimization is disabled if there are BEFORE triggers since
104691
+** the trigger body might have moved the cursor.
104464104692
*/
104465104693
SQLITE_PRIVATE void sqlite3GenerateRowDelete(
104466104694
Parse *pParse, /* Parsing context */
104467104695
Table *pTab, /* Table containing the row to be deleted */
104468104696
Trigger *pTrigger, /* List of triggers to (potentially) fire */
@@ -104529,17 +104757,22 @@
104529104757
TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
104530104758
);
104531104759
104532104760
/* If any BEFORE triggers were coded, then seek the cursor to the
104533104761
** row to be deleted again. It may be that the BEFORE triggers moved
104534
- ** the cursor or of already deleted the row that the cursor was
104762
+ ** the cursor or already deleted the row that the cursor was
104535104763
** pointing to.
104764
+ **
104765
+ ** Also disable the iIdxNoSeek optimization since the BEFORE trigger
104766
+ ** may have moved that cursor.
104536104767
*/
104537104768
if( addrStart<sqlite3VdbeCurrentAddr(v) ){
104538104769
sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
104539104770
VdbeCoverageIf(v, opSeek==OP_NotExists);
104540104771
VdbeCoverageIf(v, opSeek==OP_NotFound);
104772
+ testcase( iIdxNoSeek>=0 );
104773
+ iIdxNoSeek = -1;
104541104774
}
104542104775
104543104776
/* Do FK processing. This call checks that any FK constraints that
104544104777
** refer to this table (i.e. constraints attached to other tables)
104545104778
** are not violated by deleting this row. */
@@ -104558,15 +104791,17 @@
104558104791
*/
104559104792
if( pTab->pSelect==0 ){
104560104793
u8 p5 = 0;
104561104794
sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
104562104795
sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
104563
- sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
104796
+ if( pParse->nested==0 ){
104797
+ sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
104798
+ }
104564104799
if( eMode!=ONEPASS_OFF ){
104565104800
sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE);
104566104801
}
104567
- if( iIdxNoSeek>=0 ){
104802
+ if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
104568104803
sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
104569104804
}
104570104805
if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION;
104571104806
sqlite3VdbeChangeP5(v, p5);
104572104807
}
@@ -104716,10 +104951,14 @@
104716104951
** opcode if it is present */
104717104952
sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
104718104953
}
104719104954
if( regOut ){
104720104955
sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
104956
+ if( pIdx->pTable->pSelect ){
104957
+ const char *zAff = sqlite3IndexAffinityStr(pParse->db, pIdx);
104958
+ sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
104959
+ }
104721104960
}
104722104961
sqlite3ReleaseTempRange(pParse, regBase, nCol);
104723104962
return regBase;
104724104963
}
104725104964
@@ -106512,10 +106751,13 @@
106512106751
DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ),
106513106752
#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
106514106753
FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
106515106754
FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
106516106755
FUNCTION2(likely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
106756
+#ifdef SQLITE_DEBUG
106757
+ FUNCTION2(affinity, 1, 0, 0, noopFunc, SQLITE_FUNC_AFFINITY),
106758
+#endif
106517106759
FUNCTION(ltrim, 1, 1, 0, trimFunc ),
106518106760
FUNCTION(ltrim, 2, 1, 0, trimFunc ),
106519106761
FUNCTION(rtrim, 1, 2, 0, trimFunc ),
106520106762
FUNCTION(rtrim, 2, 2, 0, trimFunc ),
106521106763
FUNCTION(trim, 1, 3, 0, trimFunc ),
@@ -109667,11 +109909,11 @@
109667109909
if( db->flags&SQLITE_RecTriggers ){
109668109910
pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
109669109911
}
109670109912
sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
109671109913
regR, nPkField, 0, OE_Replace,
109672
- (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), -1);
109914
+ (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
109673109915
seenReplace = 1;
109674109916
break;
109675109917
}
109676109918
}
109677109919
sqlite3VdbeResolveLabel(v, addrUniqueOk);
@@ -109683,10 +109925,29 @@
109683109925
}
109684109926
109685109927
*pbMayReplace = seenReplace;
109686109928
VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
109687109929
}
109930
+
109931
+#ifdef SQLITE_ENABLE_NULL_TRIM
109932
+/*
109933
+** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
109934
+** to be the number of columns in table pTab that must not be NULL-trimmed.
109935
+**
109936
+** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
109937
+*/
109938
+SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
109939
+ u16 i;
109940
+
109941
+ /* Records with omitted columns are only allowed for schema format
109942
+ ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
109943
+ if( pTab->pSchema->file_format<2 ) return;
109944
+
109945
+ for(i=pTab->nCol; i>1 && pTab->aCol[i-1].pDflt==0; i--){}
109946
+ sqlite3VdbeChangeP5(v, i);
109947
+}
109948
+#endif
109688109949
109689109950
/*
109690109951
** This routine generates code to finish the INSERT or UPDATE operation
109691109952
** that was started by a prior call to sqlite3GenerateConstraintChecks.
109692109953
** A consecutive range of registers starting at regNewData contains the
@@ -109700,11 +109961,11 @@
109700109961
Table *pTab, /* the table into which we are inserting */
109701109962
int iDataCur, /* Cursor of the canonical data source */
109702109963
int iIdxCur, /* First index cursor */
109703109964
int regNewData, /* Range of content */
109704109965
int *aRegIdx, /* Register used by each index. 0 for unused indices */
109705
- int isUpdate, /* True for UPDATE, False for INSERT */
109966
+ int update_flags, /* True for UPDATE, False for INSERT */
109706109967
int appendBias, /* True if this is likely to be an append */
109707109968
int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
109708109969
){
109709109970
Vdbe *v; /* Prepared statements under construction */
109710109971
Index *pIdx; /* An index being inserted or updated */
@@ -109711,10 +109972,15 @@
109711109972
u8 pik_flags; /* flag values passed to the btree insert */
109712109973
int regData; /* Content registers (after the rowid) */
109713109974
int regRec; /* Register holding assembled record for the table */
109714109975
int i; /* Loop counter */
109715109976
u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */
109977
+
109978
+ assert( update_flags==0
109979
+ || update_flags==OPFLAG_ISUPDATE
109980
+ || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)
109981
+ );
109716109982
109717109983
v = sqlite3GetVdbe(pParse);
109718109984
assert( v!=0 );
109719109985
assert( pTab->pSelect==0 ); /* This table is not a VIEW */
109720109986
for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
@@ -109722,34 +109988,43 @@
109722109988
bAffinityDone = 1;
109723109989
if( pIdx->pPartIdxWhere ){
109724109990
sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
109725109991
VdbeCoverage(v);
109726109992
}
109727
- sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
109728
- aRegIdx[i]+1,
109729
- pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
109730
- pik_flags = 0;
109731
- if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT;
109993
+ pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
109732109994
if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
109733109995
assert( pParse->nested==0 );
109734109996
pik_flags |= OPFLAG_NCHANGE;
109997
+ pik_flags |= (update_flags & OPFLAG_SAVEPOSITION);
109998
+#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
109999
+ if( update_flags==0 ){
110000
+ sqlite3VdbeAddOp4(v, OP_InsertInt,
110001
+ iIdxCur+i, aRegIdx[i], 0, (char*)pTab, P4_TABLE
110002
+ );
110003
+ sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP);
110004
+ }
110005
+#endif
109735110006
}
110007
+ sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
110008
+ aRegIdx[i]+1,
110009
+ pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
109736110010
sqlite3VdbeChangeP5(v, pik_flags);
109737110011
}
109738110012
if( !HasRowid(pTab) ) return;
109739110013
regData = regNewData + 1;
109740110014
regRec = sqlite3GetTempReg(pParse);
109741110015
sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
110016
+ sqlite3SetMakeRecordP5(v, pTab);
109742110017
if( !bAffinityDone ){
109743110018
sqlite3TableAffinity(v, pTab, 0);
109744110019
sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
109745110020
}
109746110021
if( pParse->nested ){
109747110022
pik_flags = 0;
109748110023
}else{
109749110024
pik_flags = OPFLAG_NCHANGE;
109750
- pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
110025
+ pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID);
109751110026
}
109752110027
if( appendBias ){
109753110028
pik_flags |= OPFLAG_APPEND;
109754110029
}
109755110030
if( useSeekResult ){
@@ -110154,11 +110429,11 @@
110154110429
addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
110155110430
}else{
110156110431
addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
110157110432
assert( (pDest->tabFlags & TF_Autoincrement)==0 );
110158110433
}
110159
- sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
110434
+ sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
110160110435
if( db->flags & SQLITE_Vacuum ){
110161110436
sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
110162110437
insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|
110163110438
OPFLAG_APPEND|OPFLAG_USESEEKRESULT;
110164110439
}else{
@@ -110186,11 +110461,11 @@
110186110461
sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
110187110462
sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
110188110463
sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
110189110464
VdbeComment((v, "%s", pDestIdx->zName));
110190110465
addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
110191
- sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
110466
+ sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
110192110467
if( db->flags & SQLITE_Vacuum ){
110193110468
/* This INSERT command is part of a VACUUM operation, which guarantees
110194110469
** that the destination table is empty. If all indexed columns use
110195110470
** collation sequence BINARY, then it can also be assumed that the
110196110471
** index will be populated by inserting keys in strictly sorted
@@ -110971,11 +111246,10 @@
110971111246
#endif /* SQLITE3EXT_H */
110972111247
110973111248
/************** End of sqlite3ext.h ******************************************/
110974111249
/************** Continuing where we left off in loadext.c ********************/
110975111250
/* #include "sqliteInt.h" */
110976
-/* #include <string.h> */
110977111251
110978111252
#ifndef SQLITE_OMIT_LOAD_EXTENSION
110979111253
/*
110980111254
** Some API routines are omitted when various features are
110981111255
** excluded from a build of SQLite. Substitute a NULL pointer
@@ -112635,11 +112909,11 @@
112635112909
112636112910
/*
112637112911
** Locate a pragma in the aPragmaName[] array.
112638112912
*/
112639112913
static const PragmaName *pragmaLocate(const char *zName){
112640
- int upr, lwr, mid, rc;
112914
+ int upr, lwr, mid = 0, rc;
112641112915
lwr = 0;
112642112916
upr = ArraySize(aPragmaName)-1;
112643112917
while( lwr<=upr ){
112644112918
mid = (lwr+upr)/2;
112645112919
rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
@@ -116149,10 +116423,11 @@
116149116423
v = pParse->pVdbe;
116150116424
r1 = sqlite3GetTempReg(pParse);
116151116425
sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
116152116426
sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
116153116427
sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, iMem, N);
116428
+ sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
116154116429
sqlite3ReleaseTempReg(pParse, r1);
116155116430
}
116156116431
116157116432
/*
116158116433
** This routine generates the code for the inside of the inner loop
@@ -119677,11 +119952,19 @@
119677119952
assert( pTab->nTabRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nTabRef==2 ));
119678119953
119679119954
pCte->zCteErr = "circular reference: %s";
119680119955
pSavedWith = pParse->pWith;
119681119956
pParse->pWith = pWith;
119682
- sqlite3WalkSelect(pWalker, bMayRecursive ? pSel->pPrior : pSel);
119957
+ if( bMayRecursive ){
119958
+ Select *pPrior = pSel->pPrior;
119959
+ assert( pPrior->pWith==0 );
119960
+ pPrior->pWith = pSel->pWith;
119961
+ sqlite3WalkSelect(pWalker, pPrior);
119962
+ pPrior->pWith = 0;
119963
+ }else{
119964
+ sqlite3WalkSelect(pWalker, pSel);
119965
+ }
119683119966
pParse->pWith = pWith;
119684119967
119685119968
for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
119686119969
pEList = pLeft->pEList;
119687119970
if( pCte->pCols ){
@@ -119721,14 +120004,16 @@
119721120004
** sqlite3SelectExpand() when walking a SELECT tree to resolve table
119722120005
** names and other FROM clause elements.
119723120006
*/
119724120007
static void selectPopWith(Walker *pWalker, Select *p){
119725120008
Parse *pParse = pWalker->pParse;
119726
- With *pWith = findRightmost(p)->pWith;
119727
- if( pWith!=0 ){
119728
- assert( pParse->pWith==pWith );
119729
- pParse->pWith = pWith->pOuter;
120009
+ if( pParse->pWith && p->pPrior==0 ){
120010
+ With *pWith = findRightmost(p)->pWith;
120011
+ if( pWith!=0 ){
120012
+ assert( pParse->pWith==pWith );
120013
+ pParse->pWith = pWith->pOuter;
120014
+ }
119730120015
}
119731120016
}
119732120017
#else
119733120018
#define selectPopWith 0
119734120019
#endif
@@ -119774,12 +120059,12 @@
119774120059
if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
119775120060
return WRC_Prune;
119776120061
}
119777120062
pTabList = p->pSrc;
119778120063
pEList = p->pEList;
119779
- if( pWalker->xSelectCallback2==selectPopWith ){
119780
- sqlite3WithPush(pParse, findRightmost(p)->pWith, 0);
120064
+ if( p->pWith ){
120065
+ sqlite3WithPush(pParse, p->pWith, 0);
119781120066
}
119782120067
119783120068
/* Make sure cursor numbers have been assigned to all entries in
119784120069
** the FROM clause of the SELECT statement.
119785120070
*/
@@ -120062,13 +120347,11 @@
120062120347
if( pParse->hasCompound ){
120063120348
w.xSelectCallback = convertCompoundSelectToSubquery;
120064120349
sqlite3WalkSelect(&w, pSelect);
120065120350
}
120066120351
w.xSelectCallback = selectExpander;
120067
- if( (pSelect->selFlags & SF_MultiValue)==0 ){
120068
- w.xSelectCallback2 = selectPopWith;
120069
- }
120352
+ w.xSelectCallback2 = selectPopWith;
120070120353
sqlite3WalkSelect(&w, pSelect);
120071120354
}
120072120355
120073120356
120074120357
#ifndef SQLITE_OMIT_SUBQUERY
@@ -121143,11 +121426,11 @@
121143121426
/* This case runs if the aggregate has no GROUP BY clause. The
121144121427
** processing is much simpler since there is only a single row
121145121428
** of output.
121146121429
*/
121147121430
resetAccumulator(pParse, &sAggInfo);
121148
- pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0);
121431
+ pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax, 0,flag,0);
121149121432
if( pWInfo==0 ){
121150121433
sqlite3ExprListDelete(db, pDel);
121151121434
goto select_end;
121152121435
}
121153121436
updateAccumulator(pParse, &sAggInfo);
@@ -121232,12 +121515,10 @@
121232121515
**
121233121516
** These routines are in a separate files so that they will not be linked
121234121517
** if they are not used.
121235121518
*/
121236121519
/* #include "sqliteInt.h" */
121237
-/* #include <stdlib.h> */
121238
-/* #include <string.h> */
121239121520
121240121521
#ifndef SQLITE_OMIT_GET_TABLE
121241121522
121242121523
/*
121243121524
** This structure is used to pass data from sqlite3_get_table() through
@@ -122591,16 +122872,16 @@
122591122872
sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
122592122873
pCol->affinity, &pValue);
122593122874
if( pValue ){
122594122875
sqlite3VdbeAppendP4(v, pValue, P4_MEM);
122595122876
}
122877
+ }
122596122878
#ifndef SQLITE_OMIT_FLOATING_POINT
122597
- if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
122598
- sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
122599
- }
122879
+ if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
122880
+ sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
122881
+ }
122600122882
#endif
122601
- }
122602122883
}
122603122884
122604122885
/*
122605122886
** Process an UPDATE statement.
122606122887
**
@@ -122625,11 +122906,11 @@
122625122906
int nIdx; /* Number of indices that need updating */
122626122907
int iBaseCur; /* Base cursor number */
122627122908
int iDataCur; /* Cursor for the canonical data btree */
122628122909
int iIdxCur; /* Cursor for the first index */
122629122910
sqlite3 *db; /* The database structure */
122630
- int *aRegIdx = 0; /* One register assigned to each index to be updated */
122911
+ int *aRegIdx = 0; /* First register in array assigned to each index */
122631122912
int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
122632122913
** an expression for the i-th column of the table.
122633122914
** aXRef[i]==-1 if the i-th column is not changed. */
122634122915
u8 *aToOpen; /* 1 for tables and indices to be opened */
122635122916
u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */
@@ -122637,14 +122918,15 @@
122637122918
u8 chngKey; /* Either chngPk or chngRowid */
122638122919
Expr *pRowidExpr = 0; /* Expression defining the new record number */
122639122920
AuthContext sContext; /* The authorization context */
122640122921
NameContext sNC; /* The name-context to resolve expressions in */
122641122922
int iDb; /* Database containing the table being updated */
122642
- int okOnePass; /* True for one-pass algorithm without the FIFO */
122923
+ int eOnePass; /* ONEPASS_XXX value from where.c */
122643122924
int hasFK; /* True if foreign key processing is required */
122644122925
int labelBreak; /* Jump here to break out of UPDATE loop */
122645122926
int labelContinue; /* Jump here to continue next step of UPDATE loop */
122927
+ int flags; /* Flags for sqlite3WhereBegin() */
122646122928
122647122929
#ifndef SQLITE_OMIT_TRIGGER
122648122930
int isView; /* True when updating a view (INSTEAD OF trigger) */
122649122931
Trigger *pTrigger; /* List of triggers on pTab, if required */
122650122932
int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
@@ -122651,10 +122933,14 @@
122651122933
#endif
122652122934
int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */
122653122935
int iEph = 0; /* Ephemeral table holding all primary key values */
122654122936
int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */
122655122937
int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
122938
+ int addrOpen = 0; /* Address of OP_OpenEphemeral */
122939
+ int iPk = 0; /* First of nPk cells holding PRIMARY KEY value */
122940
+ i16 nPk = 0; /* Number of components of the PRIMARY KEY */
122941
+ int bReplace = 0; /* True if REPLACE conflict resolution might happen */
122656122942
122657122943
/* Register Allocations */
122658122944
int regRowCount = 0; /* A count of rows changed */
122659122945
int regOldRowid = 0; /* The old rowid */
122660122946
int regNewRowid = 0; /* The new rowid */
@@ -122810,17 +123096,27 @@
122810123096
for(i=0; i<pIdx->nKeyCol; i++){
122811123097
i16 iIdxCol = pIdx->aiColumn[i];
122812123098
if( iIdxCol<0 || aXRef[iIdxCol]>=0 ){
122813123099
reg = ++pParse->nMem;
122814123100
pParse->nMem += pIdx->nColumn;
123101
+ if( (onError==OE_Replace)
123102
+ || (onError==OE_Default && pIdx->onError==OE_Replace)
123103
+ ){
123104
+ bReplace = 1;
123105
+ }
122815123106
break;
122816123107
}
122817123108
}
122818123109
}
122819123110
if( reg==0 ) aToOpen[j+1] = 0;
122820123111
aRegIdx[j] = reg;
122821123112
}
123113
+ if( bReplace ){
123114
+ /* If REPLACE conflict resolution might be invoked, open cursors on all
123115
+ ** indexes in case they are needed to delete records. */
123116
+ memset(aToOpen, 1, nIdx+1);
123117
+ }
122822123118
122823123119
/* Begin generating code. */
122824123120
v = sqlite3GetVdbe(pParse);
122825123121
if( v==0 ) goto update_cleanup;
122826123122
if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
@@ -122869,107 +123165,127 @@
122869123165
pWhere, onError);
122870123166
goto update_cleanup;
122871123167
}
122872123168
#endif
122873123169
122874
- /* Begin the database scan
122875
- */
123170
+ /* Initialize the count of updated rows */
123171
+ if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
123172
+ regRowCount = ++pParse->nMem;
123173
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
123174
+ }
123175
+
122876123176
if( HasRowid(pTab) ){
122877123177
sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
122878
- pWInfo = sqlite3WhereBegin(
122879
- pParse, pTabList, pWhere, 0, 0,
122880
- WHERE_ONEPASS_DESIRED | WHERE_SEEK_TABLE, iIdxCur
122881
- );
122882
- if( pWInfo==0 ) goto update_cleanup;
122883
- okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
122884
-
122885
- /* Remember the rowid of every item to be updated.
122886
- */
122887
- sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
122888
- if( !okOnePass ){
122889
- sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
122890
- }
122891
-
122892
- /* End the database scan loop.
122893
- */
122894
- sqlite3WhereEnd(pWInfo);
122895123178
}else{
122896
- int iPk; /* First of nPk memory cells holding PRIMARY KEY value */
122897
- i16 nPk; /* Number of components of the PRIMARY KEY */
122898
- int addrOpen; /* Address of the OpenEphemeral instruction */
122899
-
122900123179
assert( pPk!=0 );
122901123180
nPk = pPk->nKeyCol;
122902123181
iPk = pParse->nMem+1;
122903123182
pParse->nMem += nPk;
122904123183
regKey = ++pParse->nMem;
122905123184
iEph = pParse->nTab++;
123185
+
122906123186
sqlite3VdbeAddOp2(v, OP_Null, 0, iPk);
122907123187
addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
122908123188
sqlite3VdbeSetP4KeyInfo(pParse, pPk);
122909
- pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,
122910
- WHERE_ONEPASS_DESIRED, iIdxCur);
122911
- if( pWInfo==0 ) goto update_cleanup;
122912
- okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
123189
+ }
123190
+
123191
+ /* Begin the database scan.
123192
+ **
123193
+ ** Do not consider a single-pass strategy for a multi-row update if
123194
+ ** there are any triggers or foreign keys to process, or rows may
123195
+ ** be deleted as a result of REPLACE conflict handling. Any of these
123196
+ ** things might disturb a cursor being used to scan through the table
123197
+ ** or index, causing a single-pass approach to malfunction. */
123198
+ flags = WHERE_ONEPASS_DESIRED|WHERE_SEEK_UNIQ_TABLE;
123199
+ if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
123200
+ flags |= WHERE_ONEPASS_MULTIROW;
123201
+ }
123202
+ pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags, iIdxCur);
123203
+ if( pWInfo==0 ) goto update_cleanup;
123204
+
123205
+ /* A one-pass strategy that might update more than one row may not
123206
+ ** be used if any column of the index used for the scan is being
123207
+ ** updated. Otherwise, if there is an index on "b", statements like
123208
+ ** the following could create an infinite loop:
123209
+ **
123210
+ ** UPDATE t1 SET b=b+1 WHERE b>?
123211
+ **
123212
+ ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
123213
+ ** strategy that uses an index for which one or more columns are being
123214
+ ** updated. */
123215
+ eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
123216
+ if( eOnePass==ONEPASS_MULTI ){
123217
+ int iCur = aiCurOnePass[1];
123218
+ if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
123219
+ eOnePass = ONEPASS_OFF;
123220
+ }
123221
+ assert( iCur!=iDataCur || !HasRowid(pTab) );
123222
+ }
123223
+
123224
+ if( HasRowid(pTab) ){
123225
+ /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
123226
+ ** mode, write the rowid into the FIFO. In either of the one-pass modes,
123227
+ ** leave it in register regOldRowid. */
123228
+ sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
123229
+ if( eOnePass==ONEPASS_OFF ){
123230
+ sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
123231
+ }
123232
+ }else{
123233
+ /* Read the PK of the current row into an array of registers. In
123234
+ ** ONEPASS_OFF mode, serialize the array into a record and store it in
123235
+ ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change
123236
+ ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table
123237
+ ** is not required) and leave the PK fields in the array of registers. */
122913123238
for(i=0; i<nPk; i++){
122914123239
assert( pPk->aiColumn[i]>=0 );
122915
- sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pPk->aiColumn[i],
122916
- iPk+i);
123240
+ sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur,pPk->aiColumn[i],iPk+i);
122917123241
}
122918
- if( okOnePass ){
123242
+ if( eOnePass ){
122919123243
sqlite3VdbeChangeToNoop(v, addrOpen);
122920123244
nKey = nPk;
122921123245
regKey = iPk;
122922123246
}else{
122923123247
sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,
122924123248
sqlite3IndexAffinityStr(db, pPk), nPk);
122925123249
sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEph, regKey, iPk, nPk);
122926123250
}
122927
- sqlite3WhereEnd(pWInfo);
122928123251
}
122929123252
122930
- /* Initialize the count of updated rows
122931
- */
122932
- if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
122933
- regRowCount = ++pParse->nMem;
122934
- sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
123253
+ if( eOnePass!=ONEPASS_MULTI ){
123254
+ sqlite3WhereEnd(pWInfo);
122935123255
}
122936123256
122937123257
labelBreak = sqlite3VdbeMakeLabel(v);
122938123258
if( !isView ){
122939
- /*
122940
- ** Open every index that needs updating. Note that if any
122941
- ** index could potentially invoke a REPLACE conflict resolution
122942
- ** action, then we need to open all indices because we might need
122943
- ** to be deleting some records.
122944
- */
122945
- if( onError==OE_Replace ){
122946
- memset(aToOpen, 1, nIdx+1);
122947
- }else{
122948
- for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
122949
- if( pIdx->onError==OE_Replace ){
122950
- memset(aToOpen, 1, nIdx+1);
122951
- break;
122952
- }
122953
- }
122954
- }
122955
- if( okOnePass ){
123259
+ int addrOnce = 0;
123260
+
123261
+ /* Open every index that needs updating. */
123262
+ if( eOnePass!=ONEPASS_OFF ){
122956123263
if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
122957123264
if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
122958123265
}
123266
+
123267
+ if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
123268
+ addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
123269
+ }
122959123270
sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur, aToOpen,
122960123271
0, 0);
123272
+ if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
122961123273
}
122962123274
122963123275
/* Top of the update loop */
122964
- if( okOnePass ){
122965
- if( aToOpen[iDataCur-iBaseCur] && !isView ){
123276
+ if( eOnePass!=ONEPASS_OFF ){
123277
+ if( !isView && aiCurOnePass[0]!=iDataCur && aiCurOnePass[1]!=iDataCur ){
122966123278
assert( pPk );
122967123279
sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
122968123280
VdbeCoverageNeverTaken(v);
122969123281
}
122970
- labelContinue = labelBreak;
123282
+ if( eOnePass==ONEPASS_SINGLE ){
123283
+ labelContinue = labelBreak;
123284
+ }else{
123285
+ labelContinue = sqlite3VdbeMakeLabel(v);
123286
+ }
122971123287
sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
122972123288
VdbeCoverageIf(v, pPk==0);
122973123289
VdbeCoverageIf(v, pPk!=0);
122974123290
}else if( pPk ){
122975123291
labelContinue = sqlite3VdbeMakeLabel(v);
@@ -123090,11 +123406,10 @@
123090123406
}
123091123407
}
123092123408
123093123409
if( !isView ){
123094123410
int addr1 = 0; /* Address of jump instruction */
123095
- int bReplace = 0; /* True if REPLACE conflict resolution might happen */
123096123411
123097123412
/* Do constraint checks. */
123098123413
assert( regOldRowid>0 );
123099123414
sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
123100123415
regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace,
@@ -123126,18 +123441,22 @@
123126123441
** is the column index supplied by the user.
123127123442
*/
123128123443
assert( regNew==regNewRowid+1 );
123129123444
#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
123130123445
sqlite3VdbeAddOp3(v, OP_Delete, iDataCur,
123131
- OPFLAG_ISUPDATE | ((hasFK || chngKey || pPk!=0) ? 0 : OPFLAG_ISNOOP),
123446
+ OPFLAG_ISUPDATE | ((hasFK || chngKey) ? 0 : OPFLAG_ISNOOP),
123132123447
regNewRowid
123133123448
);
123449
+ if( eOnePass==ONEPASS_MULTI ){
123450
+ assert( hasFK==0 && chngKey==0 );
123451
+ sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION);
123452
+ }
123134123453
if( !pParse->nested ){
123135123454
sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
123136123455
}
123137123456
#else
123138
- if( hasFK || chngKey || pPk!=0 ){
123457
+ if( hasFK || chngKey ){
123139123458
sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0);
123140123459
}
123141123460
#endif
123142123461
if( bReplace || chngKey ){
123143123462
sqlite3VdbeJumpHere(v, addr1);
@@ -123146,12 +123465,15 @@
123146123465
if( hasFK ){
123147123466
sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);
123148123467
}
123149123468
123150123469
/* Insert the new index entries and the new record. */
123151
- sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
123152
- regNewRowid, aRegIdx, 1, 0, 0);
123470
+ sqlite3CompleteInsertion(
123471
+ pParse, pTab, iDataCur, iIdxCur, regNewRowid, aRegIdx,
123472
+ OPFLAG_ISUPDATE | (eOnePass==ONEPASS_MULTI ? OPFLAG_SAVEPOSITION : 0),
123473
+ 0, 0
123474
+ );
123153123475
123154123476
/* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
123155123477
** handle rows (possibly in other tables) that refer via a foreign key
123156123478
** to the row just updated. */
123157123479
if( hasFK ){
@@ -123169,12 +123491,15 @@
123169123491
TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue);
123170123492
123171123493
/* Repeat the above with the next record to be updated, until
123172123494
** all record selected by the WHERE clause have been updated.
123173123495
*/
123174
- if( okOnePass ){
123496
+ if( eOnePass==ONEPASS_SINGLE ){
123175123497
/* Nothing to do at end-of-loop for a single-pass */
123498
+ }else if( eOnePass==ONEPASS_MULTI ){
123499
+ sqlite3VdbeResolveLabel(v, labelContinue);
123500
+ sqlite3WhereEnd(pWInfo);
123176123501
}else if( pPk ){
123177123502
sqlite3VdbeResolveLabel(v, labelContinue);
123178123503
sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v);
123179123504
}else{
123180123505
sqlite3VdbeGoto(v, labelContinue);
@@ -127090,11 +127415,14 @@
127090127415
127091127416
/* Seek the table cursor, if required */
127092127417
if( omitTable ){
127093127418
/* pIdx is a covering index. No need to access the main table. */
127094127419
}else if( HasRowid(pIdx->pTable) ){
127095
- if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE)!=0 ){
127420
+ if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE) || (
127421
+ (pWInfo->wctrlFlags & WHERE_SEEK_UNIQ_TABLE)
127422
+ && (pWInfo->eOnePass==ONEPASS_SINGLE)
127423
+ )){
127096127424
iRowidReg = ++pParse->nMem;
127097127425
sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
127098127426
sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
127099127427
sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg);
127100127428
VdbeCoverage(v);
@@ -128454,10 +128782,11 @@
128454128782
int noCase = 0; /* uppercase equivalent to lowercase */
128455128783
int op; /* Top-level operator. pExpr->op */
128456128784
Parse *pParse = pWInfo->pParse; /* Parsing context */
128457128785
sqlite3 *db = pParse->db; /* Database connection */
128458128786
unsigned char eOp2; /* op2 value for LIKE/REGEXP/GLOB */
128787
+ int nLeft; /* Number of elements on left side vector */
128459128788
128460128789
if( db->mallocFailed ){
128461128790
return;
128462128791
}
128463128792
pTerm = &pWC->a[idxTerm];
@@ -128483,10 +128812,14 @@
128483128812
if( ExprHasProperty(pExpr, EP_FromJoin) ){
128484128813
Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
128485128814
prereqAll |= x;
128486128815
extraRight = x-1; /* ON clause terms may not be used with an index
128487128816
** on left table of a LEFT JOIN. Ticket #3015 */
128817
+ if( (prereqAll>>1)>=x ){
128818
+ sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
128819
+ return;
128820
+ }
128488128821
}
128489128822
pTerm->prereqAll = prereqAll;
128490128823
pTerm->leftCursor = -1;
128491128824
pTerm->iParent = -1;
128492128825
pTerm->eOperator = 0;
@@ -128725,17 +129058,16 @@
128725129058
**
128726129059
** This is only required if at least one side of the comparison operation
128727129060
** is not a sub-select. */
128728129061
if( pWC->op==TK_AND
128729129062
&& (pExpr->op==TK_EQ || pExpr->op==TK_IS)
128730
- && sqlite3ExprIsVector(pExpr->pLeft)
129063
+ && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
129064
+ && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
128731129065
&& ( (pExpr->pLeft->flags & EP_xIsSelect)==0
128732
- || (pExpr->pRight->flags & EP_xIsSelect)==0
128733
- )){
128734
- int nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
129066
+ || (pExpr->pRight->flags & EP_xIsSelect)==0)
129067
+ ){
128735129068
int i;
128736
- assert( nLeft==sqlite3ExprVectorSize(pExpr->pRight) );
128737129069
for(i=0; i<nLeft; i++){
128738129070
int idxNew;
128739129071
Expr *pNew;
128740129072
Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i);
128741129073
Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i);
@@ -133934,11 +134266,12 @@
133934134266
x = sqlite3ColumnOfIndex(pIdx, x);
133935134267
if( x>=0 ){
133936134268
pOp->p2 = x;
133937134269
pOp->p1 = pLevel->iIdxCur;
133938134270
}
133939
- assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 );
134271
+ assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0
134272
+ || pWInfo->eOnePass );
133940134273
}else if( pOp->opcode==OP_Rowid ){
133941134274
pOp->p1 = pLevel->iIdxCur;
133942134275
pOp->opcode = OP_IdxRowid;
133943134276
}
133944134277
}
@@ -133998,10 +134331,23 @@
133998134331
** Indicate that sqlite3ParserFree() will never be called with a null
133999134332
** pointer.
134000134333
*/
134001134334
#define YYPARSEFREENEVERNULL 1
134002134335
134336
+/*
134337
+** In the amalgamation, the parse.c file generated by lemon and the
134338
+** tokenize.c file are concatenated. In that case, sqlite3RunParser()
134339
+** has access to the the size of the yyParser object and so the parser
134340
+** engine can be allocated from stack. In that case, only the
134341
+** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
134342
+** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
134343
+** omitted.
134344
+*/
134345
+#ifdef SQLITE_AMALGAMATION
134346
+# define sqlite3Parser_ENGINEALWAYSONSTACK 1
134347
+#endif
134348
+
134003134349
/*
134004134350
** Alternative datatype for the argument to the malloc() routine passed
134005134351
** into sqlite3ParserAlloc(). The default is size_t.
134006134352
*/
134007134353
#define YYMALLOCARGTYPE u64
@@ -135446,10 +135792,35 @@
135446135792
*/
135447135793
#ifndef YYMALLOCARGTYPE
135448135794
# define YYMALLOCARGTYPE size_t
135449135795
#endif
135450135796
135797
+/* Initialize a new parser that has already been allocated.
135798
+*/
135799
+SQLITE_PRIVATE void sqlite3ParserInit(void *yypParser){
135800
+ yyParser *pParser = (yyParser*)yypParser;
135801
+#ifdef YYTRACKMAXSTACKDEPTH
135802
+ pParser->yyhwm = 0;
135803
+#endif
135804
+#if YYSTACKDEPTH<=0
135805
+ pParser->yytos = NULL;
135806
+ pParser->yystack = NULL;
135807
+ pParser->yystksz = 0;
135808
+ if( yyGrowStack(pParser) ){
135809
+ pParser->yystack = &pParser->yystk0;
135810
+ pParser->yystksz = 1;
135811
+ }
135812
+#endif
135813
+#ifndef YYNOERRORRECOVERY
135814
+ pParser->yyerrcnt = -1;
135815
+#endif
135816
+ pParser->yytos = pParser->yystack;
135817
+ pParser->yystack[0].stateno = 0;
135818
+ pParser->yystack[0].major = 0;
135819
+}
135820
+
135821
+#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
135451135822
/*
135452135823
** This function allocates a new parser.
135453135824
** The only argument is a pointer to a function which works like
135454135825
** malloc.
135455135826
**
@@ -135461,32 +135832,15 @@
135461135832
** to sqlite3Parser and sqlite3ParserFree.
135462135833
*/
135463135834
SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPE)){
135464135835
yyParser *pParser;
135465135836
pParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) );
135466
- if( pParser ){
135467
-#ifdef YYTRACKMAXSTACKDEPTH
135468
- pParser->yyhwm = 0;
135469
-#endif
135470
-#if YYSTACKDEPTH<=0
135471
- pParser->yytos = NULL;
135472
- pParser->yystack = NULL;
135473
- pParser->yystksz = 0;
135474
- if( yyGrowStack(pParser) ){
135475
- pParser->yystack = &pParser->yystk0;
135476
- pParser->yystksz = 1;
135477
- }
135478
-#endif
135479
-#ifndef YYNOERRORRECOVERY
135480
- pParser->yyerrcnt = -1;
135481
-#endif
135482
- pParser->yytos = pParser->yystack;
135483
- pParser->yystack[0].stateno = 0;
135484
- pParser->yystack[0].major = 0;
135485
- }
135837
+ if( pParser ) sqlite3ParserInit(pParser);
135486135838
return pParser;
135487135839
}
135840
+#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
135841
+
135488135842
135489135843
/* The following function deletes the "minor type" or semantic value
135490135844
** associated with a symbol. The symbol can be either a terminal
135491135845
** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
135492135846
** a pointer to the value to be deleted. The code used to do the
@@ -135608,10 +135962,22 @@
135608135962
}
135609135963
#endif
135610135964
yy_destructor(pParser, yytos->major, &yytos->minor);
135611135965
}
135612135966
135967
+/*
135968
+** Clear all secondary memory allocations from the parser
135969
+*/
135970
+SQLITE_PRIVATE void sqlite3ParserFinalize(void *p){
135971
+ yyParser *pParser = (yyParser*)p;
135972
+ while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
135973
+#if YYSTACKDEPTH<=0
135974
+ if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
135975
+#endif
135976
+}
135977
+
135978
+#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
135613135979
/*
135614135980
** Deallocate and destroy a parser. Destructors are called for
135615135981
** all stack elements before shutting the parser down.
135616135982
**
135617135983
** If the YYPARSEFREENEVERNULL macro exists (for example because it
@@ -135620,20 +135986,17 @@
135620135986
*/
135621135987
SQLITE_PRIVATE void sqlite3ParserFree(
135622135988
void *p, /* The parser to be deleted */
135623135989
void (*freeProc)(void*) /* Function used to reclaim memory */
135624135990
){
135625
- yyParser *pParser = (yyParser*)p;
135626135991
#ifndef YYPARSEFREENEVERNULL
135627
- if( pParser==0 ) return;
135628
-#endif
135629
- while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
135630
-#if YYSTACKDEPTH<=0
135631
- if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
135632
-#endif
135633
- (*freeProc)((void*)pParser);
135634
-}
135992
+ if( p==0 ) return;
135993
+#endif
135994
+ sqlite3ParserFinalize(p);
135995
+ (*freeProc)(p);
135996
+}
135997
+#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
135635135998
135636135999
/*
135637136000
** Return the peak depth of the stack for a parser.
135638136001
*/
135639136002
#ifdef YYTRACKMAXSTACKDEPTH
@@ -138483,10 +138846,13 @@
138483138846
void *pEngine; /* The LEMON-generated LALR(1) parser */
138484138847
int tokenType; /* type of the next token */
138485138848
int lastTokenParsed = -1; /* type of the previous token */
138486138849
sqlite3 *db = pParse->db; /* The database connection */
138487138850
int mxSqlLen; /* Max length of an SQL string */
138851
+#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
138852
+ unsigned char zSpace[sizeof(yyParser)]; /* Space for parser engine object */
138853
+#endif
138488138854
138489138855
assert( zSql!=0 );
138490138856
mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
138491138857
if( db->nVdbeActive==0 ){
138492138858
db->u1.isInterrupted = 0;
@@ -138494,15 +138860,20 @@
138494138860
pParse->rc = SQLITE_OK;
138495138861
pParse->zTail = zSql;
138496138862
i = 0;
138497138863
assert( pzErrMsg!=0 );
138498138864
/* sqlite3ParserTrace(stdout, "parser: "); */
138865
+#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
138866
+ pEngine = zSpace;
138867
+ sqlite3ParserInit(pEngine);
138868
+#else
138499138869
pEngine = sqlite3ParserAlloc(sqlite3Malloc);
138500138870
if( pEngine==0 ){
138501138871
sqlite3OomFault(db);
138502138872
return SQLITE_NOMEM_BKPT;
138503138873
}
138874
+#endif
138504138875
assert( pParse->pNewTable==0 );
138505138876
assert( pParse->pNewTrigger==0 );
138506138877
assert( pParse->nVar==0 );
138507138878
assert( pParse->pVList==0 );
138508138879
while( 1 ){
@@ -138550,11 +138921,15 @@
138550138921
sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
138551138922
sqlite3ParserStackPeak(pEngine)
138552138923
);
138553138924
sqlite3_mutex_leave(sqlite3MallocMutex());
138554138925
#endif /* YYDEBUG */
138926
+#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
138927
+ sqlite3ParserFinalize(pEngine);
138928
+#else
138555138929
sqlite3ParserFree(pEngine, sqlite3_free);
138930
+#endif
138556138931
if( db->mallocFailed ){
138557138932
pParse->rc = SQLITE_NOMEM_BKPT;
138558138933
}
138559138934
if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
138560138935
pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
@@ -145675,13 +146050,13 @@
145675146050
p->nPendingData = 0;
145676146051
p->azColumn = (char **)&p[1];
145677146052
p->pTokenizer = pTokenizer;
145678146053
p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
145679146054
p->bHasDocsize = (isFts4 && bNoDocsize==0);
145680
- p->bHasStat = isFts4;
145681
- p->bFts4 = isFts4;
145682
- p->bDescIdx = bDescIdx;
146055
+ p->bHasStat = (u8)isFts4;
146056
+ p->bFts4 = (u8)isFts4;
146057
+ p->bDescIdx = (u8)bDescIdx;
145683146058
p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */
145684146059
p->zContentTbl = zContent;
145685146060
p->zLanguageid = zLanguageid;
145686146061
zContent = 0;
145687146062
zLanguageid = 0;
@@ -147734,11 +148109,11 @@
147734148109
sqlite3_stmt *pStmt = 0;
147735148110
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
147736148111
if( rc==SQLITE_OK ){
147737148112
int bHasStat = (sqlite3_step(pStmt)==SQLITE_ROW);
147738148113
rc = sqlite3_finalize(pStmt);
147739
- if( rc==SQLITE_OK ) p->bHasStat = bHasStat;
148114
+ if( rc==SQLITE_OK ) p->bHasStat = (u8)bHasStat;
147740148115
}
147741148116
sqlite3_free(zSql);
147742148117
}else{
147743148118
rc = SQLITE_NOMEM;
147744148119
}
@@ -162638,28 +163013,33 @@
162638163013
struct Rtree {
162639163014
sqlite3_vtab base; /* Base class. Must be first */
162640163015
sqlite3 *db; /* Host database connection */
162641163016
int iNodeSize; /* Size in bytes of each node in the node table */
162642163017
u8 nDim; /* Number of dimensions */
163018
+ u8 nDim2; /* Twice the number of dimensions */
162643163019
u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
162644163020
u8 nBytesPerCell; /* Bytes consumed per cell */
163021
+ u8 inWrTrans; /* True if inside write transaction */
162645163022
int iDepth; /* Current depth of the r-tree structure */
162646163023
char *zDb; /* Name of database containing r-tree table */
162647163024
char *zName; /* Name of r-tree table */
162648
- int nBusy; /* Current number of users of this structure */
163025
+ u32 nBusy; /* Current number of users of this structure */
162649163026
i64 nRowEst; /* Estimated number of rows in this table */
163027
+ u32 nCursor; /* Number of open cursors */
162650163028
162651163029
/* List of nodes removed during a CondenseTree operation. List is
162652163030
** linked together via the pointer normally used for hash chains -
162653163031
** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
162654163032
** headed by the node (leaf nodes have RtreeNode.iNode==0).
162655163033
*/
162656163034
RtreeNode *pDeleted;
162657163035
int iReinsertHeight; /* Height of sub-trees Reinsert() has run on */
163036
+
163037
+ /* Blob I/O on xxx_node */
163038
+ sqlite3_blob *pNodeBlob;
162658163039
162659163040
/* Statements to read/write/delete a record from xxx_node */
162660
- sqlite3_stmt *pReadNode;
162661163041
sqlite3_stmt *pWriteNode;
162662163042
sqlite3_stmt *pDeleteNode;
162663163043
162664163044
/* Statements to read/write/delete a record from xxx_rowid */
162665163045
sqlite3_stmt *pReadRowid;
@@ -162884,26 +163264,110 @@
162884163264
#endif
162885163265
#ifndef MIN
162886163266
# define MIN(x,y) ((x) > (y) ? (y) : (x))
162887163267
#endif
162888163268
163269
+/* What version of GCC is being used. 0 means GCC is not being used */
163270
+#ifndef GCC_VERSION
163271
+#ifdef __GNUC__
163272
+# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
163273
+#else
163274
+# define GCC_VERSION 0
163275
+#endif
163276
+#endif
163277
+
163278
+/* What version of CLANG is being used. 0 means CLANG is not being used */
163279
+#ifndef CLANG_VERSION
163280
+#if defined(__clang__) && !defined(_WIN32)
163281
+# define CLANG_VERSION \
163282
+ (__clang_major__*1000000+__clang_minor__*1000+__clang_patchlevel__)
163283
+#else
163284
+# define CLANG_VERSION 0
163285
+#endif
163286
+#endif
163287
+
163288
+/* The testcase() macro should already be defined in the amalgamation. If
163289
+** it is not, make it a no-op.
163290
+*/
163291
+#ifndef SQLITE_AMALGAMATION
163292
+# define testcase(X)
163293
+#endif
163294
+
163295
+/*
163296
+** Macros to determine whether the machine is big or little endian,
163297
+** and whether or not that determination is run-time or compile-time.
163298
+**
163299
+** For best performance, an attempt is made to guess at the byte-order
163300
+** using C-preprocessor macros. If that is unsuccessful, or if
163301
+** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
163302
+** at run-time.
163303
+*/
163304
+#ifndef SQLITE_BYTEORDER
163305
+#if (defined(i386) || defined(__i386__) || defined(_M_IX86) || \
163306
+ defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
163307
+ defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
163308
+ defined(__arm__)) && !defined(SQLITE_RUNTIME_BYTEORDER)
163309
+# define SQLITE_BYTEORDER 1234
163310
+#elif (defined(sparc) || defined(__ppc__)) \
163311
+ && !defined(SQLITE_RUNTIME_BYTEORDER)
163312
+# define SQLITE_BYTEORDER 4321
163313
+#else
163314
+# define SQLITE_BYTEORDER 0 /* 0 means "unknown at compile-time" */
163315
+#endif
163316
+#endif
163317
+
163318
+
163319
+/* What version of MSVC is being used. 0 means MSVC is not being used */
163320
+#ifndef MSVC_VERSION
163321
+#if defined(_MSC_VER)
163322
+# define MSVC_VERSION _MSC_VER
163323
+#else
163324
+# define MSVC_VERSION 0
163325
+#endif
163326
+#endif
163327
+
162889163328
/*
162890163329
** Functions to deserialize a 16 bit integer, 32 bit real number and
162891163330
** 64 bit integer. The deserialized value is returned.
162892163331
*/
162893163332
static int readInt16(u8 *p){
162894163333
return (p[0]<<8) + p[1];
162895163334
}
162896163335
static void readCoord(u8 *p, RtreeCoord *pCoord){
163336
+ assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */
163337
+#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163338
+ pCoord->u = _byteswap_ulong(*(u32*)p);
163339
+#elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
163340
+ pCoord->u = __builtin_bswap32(*(u32*)p);
163341
+#elif SQLITE_BYTEORDER==1234
163342
+ pCoord->u = ((pCoord->u>>24)&0xff)|((pCoord->u>>8)&0xff00)|
163343
+ ((pCoord->u&0xff)<<24)|((pCoord->u&0xff00)<<8);
163344
+#elif SQLITE_BYTEORDER==4321
163345
+ pCoord->u = *(u32*)p;
163346
+#else
162897163347
pCoord->u = (
162898163348
(((u32)p[0]) << 24) +
162899163349
(((u32)p[1]) << 16) +
162900163350
(((u32)p[2]) << 8) +
162901163351
(((u32)p[3]) << 0)
162902163352
);
163353
+#endif
162903163354
}
162904163355
static i64 readInt64(u8 *p){
163356
+#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163357
+ u64 x;
163358
+ memcpy(&x, p, 8);
163359
+ return (i64)_byteswap_uint64(x);
163360
+#elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
163361
+ u64 x;
163362
+ memcpy(&x, p, 8);
163363
+ return (i64)__builtin_bswap64(x);
163364
+#elif SQLITE_BYTEORDER==4321
163365
+ i64 x;
163366
+ memcpy(&x, p, 8);
163367
+ return x;
163368
+#else
162905163369
return (
162906163370
(((i64)p[0]) << 56) +
162907163371
(((i64)p[1]) << 48) +
162908163372
(((i64)p[2]) << 40) +
162909163373
(((i64)p[3]) << 32) +
@@ -162910,10 +163374,11 @@
162910163374
(((i64)p[4]) << 24) +
162911163375
(((i64)p[5]) << 16) +
162912163376
(((i64)p[6]) << 8) +
162913163377
(((i64)p[7]) << 0)
162914163378
);
163379
+#endif
162915163380
}
162916163381
162917163382
/*
162918163383
** Functions to serialize a 16 bit integer, 32 bit real number and
162919163384
** 64 bit integer. The value returned is the number of bytes written
@@ -162924,28 +163389,50 @@
162924163389
p[1] = (i>> 0)&0xFF;
162925163390
return 2;
162926163391
}
162927163392
static int writeCoord(u8 *p, RtreeCoord *pCoord){
162928163393
u32 i;
163394
+ assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */
162929163395
assert( sizeof(RtreeCoord)==4 );
162930163396
assert( sizeof(u32)==4 );
163397
+#if SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
163398
+ i = __builtin_bswap32(pCoord->u);
163399
+ memcpy(p, &i, 4);
163400
+#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163401
+ i = _byteswap_ulong(pCoord->u);
163402
+ memcpy(p, &i, 4);
163403
+#elif SQLITE_BYTEORDER==4321
163404
+ i = pCoord->u;
163405
+ memcpy(p, &i, 4);
163406
+#else
162931163407
i = pCoord->u;
162932163408
p[0] = (i>>24)&0xFF;
162933163409
p[1] = (i>>16)&0xFF;
162934163410
p[2] = (i>> 8)&0xFF;
162935163411
p[3] = (i>> 0)&0xFF;
163412
+#endif
162936163413
return 4;
162937163414
}
162938163415
static int writeInt64(u8 *p, i64 i){
163416
+#if SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
163417
+ i = (i64)__builtin_bswap64((u64)i);
163418
+ memcpy(p, &i, 8);
163419
+#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163420
+ i = (i64)_byteswap_uint64((u64)i);
163421
+ memcpy(p, &i, 8);
163422
+#elif SQLITE_BYTEORDER==4321
163423
+ memcpy(p, &i, 8);
163424
+#else
162939163425
p[0] = (i>>56)&0xFF;
162940163426
p[1] = (i>>48)&0xFF;
162941163427
p[2] = (i>>40)&0xFF;
162942163428
p[3] = (i>>32)&0xFF;
162943163429
p[4] = (i>>24)&0xFF;
162944163430
p[5] = (i>>16)&0xFF;
162945163431
p[6] = (i>> 8)&0xFF;
162946163432
p[7] = (i>> 0)&0xFF;
163433
+#endif
162947163434
return 8;
162948163435
}
162949163436
162950163437
/*
162951163438
** Increment the reference count of node p.
@@ -163023,10 +163510,21 @@
163023163510
pNode->isDirty = 1;
163024163511
nodeReference(pParent);
163025163512
}
163026163513
return pNode;
163027163514
}
163515
+
163516
+/*
163517
+** Clear the Rtree.pNodeBlob object
163518
+*/
163519
+static void nodeBlobReset(Rtree *pRtree){
163520
+ if( pRtree->pNodeBlob && pRtree->inWrTrans==0 && pRtree->nCursor==0 ){
163521
+ sqlite3_blob *pBlob = pRtree->pNodeBlob;
163522
+ pRtree->pNodeBlob = 0;
163523
+ sqlite3_blob_close(pBlob);
163524
+ }
163525
+}
163028163526
163029163527
/*
163030163528
** Obtain a reference to an r-tree node.
163031163529
*/
163032163530
static int nodeAcquire(
@@ -163033,13 +163531,12 @@
163033163531
Rtree *pRtree, /* R-tree structure */
163034163532
i64 iNode, /* Node number to load */
163035163533
RtreeNode *pParent, /* Either the parent node or NULL */
163036163534
RtreeNode **ppNode /* OUT: Acquired node */
163037163535
){
163038
- int rc;
163039
- int rc2 = SQLITE_OK;
163040
- RtreeNode *pNode;
163536
+ int rc = SQLITE_OK;
163537
+ RtreeNode *pNode = 0;
163041163538
163042163539
/* Check if the requested node is already in the hash table. If so,
163043163540
** increase its reference count and return it.
163044163541
*/
163045163542
if( (pNode = nodeHashLookup(pRtree, iNode)) ){
@@ -163051,32 +163548,49 @@
163051163548
pNode->nRef++;
163052163549
*ppNode = pNode;
163053163550
return SQLITE_OK;
163054163551
}
163055163552
163056
- sqlite3_bind_int64(pRtree->pReadNode, 1, iNode);
163057
- rc = sqlite3_step(pRtree->pReadNode);
163058
- if( rc==SQLITE_ROW ){
163059
- const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0);
163060
- if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){
163061
- pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
163062
- if( !pNode ){
163063
- rc2 = SQLITE_NOMEM;
163064
- }else{
163065
- pNode->pParent = pParent;
163066
- pNode->zData = (u8 *)&pNode[1];
163067
- pNode->nRef = 1;
163068
- pNode->iNode = iNode;
163069
- pNode->isDirty = 0;
163070
- pNode->pNext = 0;
163071
- memcpy(pNode->zData, zBlob, pRtree->iNodeSize);
163072
- nodeReference(pParent);
163073
- }
163074
- }
163075
- }
163076
- rc = sqlite3_reset(pRtree->pReadNode);
163077
- if( rc==SQLITE_OK ) rc = rc2;
163553
+ if( pRtree->pNodeBlob ){
163554
+ sqlite3_blob *pBlob = pRtree->pNodeBlob;
163555
+ pRtree->pNodeBlob = 0;
163556
+ rc = sqlite3_blob_reopen(pBlob, iNode);
163557
+ pRtree->pNodeBlob = pBlob;
163558
+ if( rc ){
163559
+ nodeBlobReset(pRtree);
163560
+ if( rc==SQLITE_NOMEM ) return SQLITE_NOMEM;
163561
+ }
163562
+ }
163563
+ if( pRtree->pNodeBlob==0 ){
163564
+ char *zTab = sqlite3_mprintf("%s_node", pRtree->zName);
163565
+ if( zTab==0 ) return SQLITE_NOMEM;
163566
+ rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, zTab, "data", iNode, 0,
163567
+ &pRtree->pNodeBlob);
163568
+ sqlite3_free(zTab);
163569
+ }
163570
+ if( rc ){
163571
+ nodeBlobReset(pRtree);
163572
+ *ppNode = 0;
163573
+ /* If unable to open an sqlite3_blob on the desired row, that can only
163574
+ ** be because the shadow tables hold erroneous data. */
163575
+ if( rc==SQLITE_ERROR ) rc = SQLITE_CORRUPT_VTAB;
163576
+ }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){
163577
+ pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
163578
+ if( !pNode ){
163579
+ rc = SQLITE_NOMEM;
163580
+ }else{
163581
+ pNode->pParent = pParent;
163582
+ pNode->zData = (u8 *)&pNode[1];
163583
+ pNode->nRef = 1;
163584
+ pNode->iNode = iNode;
163585
+ pNode->isDirty = 0;
163586
+ pNode->pNext = 0;
163587
+ rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
163588
+ pRtree->iNodeSize, 0);
163589
+ nodeReference(pParent);
163590
+ }
163591
+ }
163078163592
163079163593
/* If the root node was just loaded, set pRtree->iDepth to the height
163080163594
** of the r-tree structure. A height of zero means all data is stored on
163081163595
** the root node. A height of one means the children of the root node
163082163596
** are the leaves, and so on. If the depth as specified on the root node
@@ -163124,11 +163638,11 @@
163124163638
int iCell /* Index into pNode into which pCell is written */
163125163639
){
163126163640
int ii;
163127163641
u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
163128163642
p += writeInt64(p, pCell->iRowid);
163129
- for(ii=0; ii<(pRtree->nDim*2); ii++){
163643
+ for(ii=0; ii<pRtree->nDim2; ii++){
163130163644
p += writeCoord(p, &pCell->aCoord[ii]);
163131163645
}
163132163646
pNode->isDirty = 1;
163133163647
}
163134163648
@@ -163258,17 +163772,20 @@
163258163772
int iCell, /* Index of the cell within the node */
163259163773
RtreeCell *pCell /* OUT: Write the cell contents here */
163260163774
){
163261163775
u8 *pData;
163262163776
RtreeCoord *pCoord;
163263
- int ii;
163777
+ int ii = 0;
163264163778
pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
163265163779
pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
163266163780
pCoord = pCell->aCoord;
163267
- for(ii=0; ii<pRtree->nDim*2; ii++){
163268
- readCoord(&pData[ii*4], &pCoord[ii]);
163269
- }
163781
+ do{
163782
+ readCoord(pData, &pCoord[ii]);
163783
+ readCoord(pData+4, &pCoord[ii+1]);
163784
+ pData += 8;
163785
+ ii += 2;
163786
+ }while( ii<pRtree->nDim2 );
163270163787
}
163271163788
163272163789
163273163790
/* Forward declaration for the function that does the work of
163274163791
** the virtual table module xCreate() and xConnect() methods.
@@ -163315,11 +163832,13 @@
163315163832
** zero the structure is deleted.
163316163833
*/
163317163834
static void rtreeRelease(Rtree *pRtree){
163318163835
pRtree->nBusy--;
163319163836
if( pRtree->nBusy==0 ){
163320
- sqlite3_finalize(pRtree->pReadNode);
163837
+ pRtree->inWrTrans = 0;
163838
+ pRtree->nCursor = 0;
163839
+ nodeBlobReset(pRtree);
163321163840
sqlite3_finalize(pRtree->pWriteNode);
163322163841
sqlite3_finalize(pRtree->pDeleteNode);
163323163842
sqlite3_finalize(pRtree->pReadRowid);
163324163843
sqlite3_finalize(pRtree->pWriteRowid);
163325163844
sqlite3_finalize(pRtree->pDeleteRowid);
@@ -163353,10 +163872,11 @@
163353163872
pRtree->zDb, pRtree->zName
163354163873
);
163355163874
if( !zCreate ){
163356163875
rc = SQLITE_NOMEM;
163357163876
}else{
163877
+ nodeBlobReset(pRtree);
163358163878
rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
163359163879
sqlite3_free(zCreate);
163360163880
}
163361163881
if( rc==SQLITE_OK ){
163362163882
rtreeRelease(pRtree);
@@ -163368,17 +163888,19 @@
163368163888
/*
163369163889
** Rtree virtual table module xOpen method.
163370163890
*/
163371163891
static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
163372163892
int rc = SQLITE_NOMEM;
163893
+ Rtree *pRtree = (Rtree *)pVTab;
163373163894
RtreeCursor *pCsr;
163374163895
163375163896
pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
163376163897
if( pCsr ){
163377163898
memset(pCsr, 0, sizeof(RtreeCursor));
163378163899
pCsr->base.pVtab = pVTab;
163379163900
rc = SQLITE_OK;
163901
+ pRtree->nCursor++;
163380163902
}
163381163903
*ppCursor = (sqlite3_vtab_cursor *)pCsr;
163382163904
163383163905
return rc;
163384163906
}
@@ -163407,14 +163929,17 @@
163407163929
*/
163408163930
static int rtreeClose(sqlite3_vtab_cursor *cur){
163409163931
Rtree *pRtree = (Rtree *)(cur->pVtab);
163410163932
int ii;
163411163933
RtreeCursor *pCsr = (RtreeCursor *)cur;
163934
+ assert( pRtree->nCursor>0 );
163412163935
freeCursorConstraints(pCsr);
163413163936
sqlite3_free(pCsr->aPoint);
163414163937
for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
163415163938
sqlite3_free(pCsr);
163939
+ pRtree->nCursor--;
163940
+ nodeBlobReset(pRtree);
163416163941
return SQLITE_OK;
163417163942
}
163418163943
163419163944
/*
163420163945
** Rtree virtual table module xEof method.
@@ -163433,27 +163958,34 @@
163433163958
** endian platforms. The on-disk record stores integer coordinates if
163434163959
** eInt is true and it stores 32-bit floating point records if eInt is
163435163960
** false. a[] is the four bytes of the on-disk record to be decoded.
163436163961
** Store the results in "r".
163437163962
**
163438
-** There are three versions of this macro, one each for little-endian and
163439
-** big-endian processors and a third generic implementation. The endian-
163440
-** specific implementations are much faster and are preferred if the
163441
-** processor endianness is known at compile-time. The SQLITE_BYTEORDER
163442
-** macro is part of sqliteInt.h and hence the endian-specific
163443
-** implementation will only be used if this module is compiled as part
163444
-** of the amalgamation.
163963
+** There are five versions of this macro. The last one is generic. The
163964
+** other four are various architectures-specific optimizations.
163445163965
*/
163446
-#if defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==1234
163966
+#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163967
+#define RTREE_DECODE_COORD(eInt, a, r) { \
163968
+ RtreeCoord c; /* Coordinate decoded */ \
163969
+ c.u = _byteswap_ulong(*(u32*)a); \
163970
+ r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
163971
+}
163972
+#elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
163973
+#define RTREE_DECODE_COORD(eInt, a, r) { \
163974
+ RtreeCoord c; /* Coordinate decoded */ \
163975
+ c.u = __builtin_bswap32(*(u32*)a); \
163976
+ r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
163977
+}
163978
+#elif SQLITE_BYTEORDER==1234
163447163979
#define RTREE_DECODE_COORD(eInt, a, r) { \
163448163980
RtreeCoord c; /* Coordinate decoded */ \
163449163981
memcpy(&c.u,a,4); \
163450163982
c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)| \
163451163983
((c.u&0xff)<<24)|((c.u&0xff00)<<8); \
163452163984
r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
163453163985
}
163454
-#elif defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==4321
163986
+#elif SQLITE_BYTEORDER==4321
163455163987
#define RTREE_DECODE_COORD(eInt, a, r) { \
163456163988
RtreeCoord c; /* Coordinate decoded */ \
163457163989
memcpy(&c.u,a,4); \
163458163990
r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
163459163991
}
@@ -163476,30 +164008,58 @@
163476164008
u8 *pCellData, /* Raw cell content */
163477164009
RtreeSearchPoint *pSearch, /* Container of this cell */
163478164010
sqlite3_rtree_dbl *prScore, /* OUT: score for the cell */
163479164011
int *peWithin /* OUT: visibility of the cell */
163480164012
){
163481
- int i; /* Loop counter */
163482164013
sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
163483164014
int nCoord = pInfo->nCoord; /* No. of coordinates */
163484164015
int rc; /* Callback return code */
164016
+ RtreeCoord c; /* Translator union */
163485164017
sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2]; /* Decoded coordinates */
163486164018
163487164019
assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
163488164020
assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 );
163489164021
163490164022
if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
163491164023
pInfo->iRowid = readInt64(pCellData);
163492164024
}
163493164025
pCellData += 8;
163494
- for(i=0; i<nCoord; i++, pCellData += 4){
163495
- RTREE_DECODE_COORD(eInt, pCellData, aCoord[i]);
164026
+#ifndef SQLITE_RTREE_INT_ONLY
164027
+ if( eInt==0 ){
164028
+ switch( nCoord ){
164029
+ case 10: readCoord(pCellData+36, &c); aCoord[9] = c.f;
164030
+ readCoord(pCellData+32, &c); aCoord[8] = c.f;
164031
+ case 8: readCoord(pCellData+28, &c); aCoord[7] = c.f;
164032
+ readCoord(pCellData+24, &c); aCoord[6] = c.f;
164033
+ case 6: readCoord(pCellData+20, &c); aCoord[5] = c.f;
164034
+ readCoord(pCellData+16, &c); aCoord[4] = c.f;
164035
+ case 4: readCoord(pCellData+12, &c); aCoord[3] = c.f;
164036
+ readCoord(pCellData+8, &c); aCoord[2] = c.f;
164037
+ default: readCoord(pCellData+4, &c); aCoord[1] = c.f;
164038
+ readCoord(pCellData, &c); aCoord[0] = c.f;
164039
+ }
164040
+ }else
164041
+#endif
164042
+ {
164043
+ switch( nCoord ){
164044
+ case 10: readCoord(pCellData+36, &c); aCoord[9] = c.i;
164045
+ readCoord(pCellData+32, &c); aCoord[8] = c.i;
164046
+ case 8: readCoord(pCellData+28, &c); aCoord[7] = c.i;
164047
+ readCoord(pCellData+24, &c); aCoord[6] = c.i;
164048
+ case 6: readCoord(pCellData+20, &c); aCoord[5] = c.i;
164049
+ readCoord(pCellData+16, &c); aCoord[4] = c.i;
164050
+ case 4: readCoord(pCellData+12, &c); aCoord[3] = c.i;
164051
+ readCoord(pCellData+8, &c); aCoord[2] = c.i;
164052
+ default: readCoord(pCellData+4, &c); aCoord[1] = c.i;
164053
+ readCoord(pCellData, &c); aCoord[0] = c.i;
164054
+ }
163496164055
}
163497164056
if( pConstraint->op==RTREE_MATCH ){
164057
+ int eWithin = 0;
163498164058
rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
163499
- nCoord, aCoord, &i);
163500
- if( i==0 ) *peWithin = NOT_WITHIN;
164059
+ nCoord, aCoord, &eWithin);
164060
+ if( eWithin==0 ) *peWithin = NOT_WITHIN;
163501164061
*prScore = RTREE_ZERO;
163502164062
}else{
163503164063
pInfo->aCoord = aCoord;
163504164064
pInfo->iLevel = pSearch->iLevel - 1;
163505164065
pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
@@ -163531,10 +164091,11 @@
163531164091
*/
163532164092
pCellData += 8 + 4*(p->iCoord&0xfe);
163533164093
163534164094
assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
163535164095
|| p->op==RTREE_GT || p->op==RTREE_EQ );
164096
+ assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */
163536164097
switch( p->op ){
163537164098
case RTREE_LE:
163538164099
case RTREE_LT:
163539164100
case RTREE_EQ:
163540164101
RTREE_DECODE_COORD(eInt, pCellData, val);
@@ -163571,10 +164132,11 @@
163571164132
RtreeDValue xN; /* Coordinate value converted to a double */
163572164133
163573164134
assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
163574164135
|| p->op==RTREE_GT || p->op==RTREE_EQ );
163575164136
pCellData += 8 + p->iCoord*4;
164137
+ assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */
163576164138
RTREE_DECODE_COORD(eInt, pCellData, xN);
163577164139
switch( p->op ){
163578164140
case RTREE_LE: if( xN <= p->u.rValue ) return; break;
163579164141
case RTREE_LT: if( xN < p->u.rValue ) return; break;
163580164142
case RTREE_GE: if( xN >= p->u.rValue ) return; break;
@@ -163639,11 +164201,11 @@
163639164201
if( pA->iLevel>pB->iLevel ) return +1;
163640164202
return 0;
163641164203
}
163642164204
163643164205
/*
163644
-** Interchange to search points in a cursor.
164206
+** Interchange two search points in a cursor.
163645164207
*/
163646164208
static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
163647164209
RtreeSearchPoint t = p->aPoint[i];
163648164210
assert( i<j );
163649164211
p->aPoint[i] = p->aPoint[j];
@@ -163887,11 +164449,11 @@
163887164449
rtreeSearchPointPop(pCur);
163888164450
}
163889164451
if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO;
163890164452
p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
163891164453
if( p==0 ) return SQLITE_NOMEM;
163892
- p->eWithin = eWithin;
164454
+ p->eWithin = (u8)eWithin;
163893164455
p->id = x.id;
163894164456
p->iCell = x.iCell;
163895164457
RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
163896164458
break;
163897164459
}
@@ -163946,11 +164508,10 @@
163946164508
if( rc ) return rc;
163947164509
if( p==0 ) return SQLITE_OK;
163948164510
if( i==0 ){
163949164511
sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
163950164512
}else{
163951
- if( rc ) return rc;
163952164513
nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
163953164514
#ifndef SQLITE_RTREE_INT_ONLY
163954164515
if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
163955164516
sqlite3_result_double(ctx, c.f);
163956164517
}else
@@ -164075,11 +164636,11 @@
164075164636
assert( p!=0 ); /* Always returns pCsr->sPoint */
164076164637
pCsr->aNode[0] = pLeaf;
164077164638
p->id = iNode;
164078164639
p->eWithin = PARTLY_WITHIN;
164079164640
rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
164080
- p->iCell = iCell;
164641
+ p->iCell = (u8)iCell;
164081164642
RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
164082164643
}else{
164083164644
pCsr->atEOF = 1;
164084164645
}
164085164646
}else{
@@ -164108,11 +164669,11 @@
164108164669
*/
164109164670
rc = deserializeGeometry(argv[ii], p);
164110164671
if( rc!=SQLITE_OK ){
164111164672
break;
164112164673
}
164113
- p->pInfo->nCoord = pRtree->nDim*2;
164674
+ p->pInfo->nCoord = pRtree->nDim2;
164114164675
p->pInfo->anQueue = pCsr->anQueue;
164115164676
p->pInfo->mxLevel = pRtree->iDepth + 1;
164116164677
}else{
164117164678
#ifdef SQLITE_RTREE_INT_ONLY
164118164679
p->u.rValue = sqlite3_value_int64(argv[ii]);
@@ -164123,11 +164684,11 @@
164123164684
}
164124164685
}
164125164686
}
164126164687
if( rc==SQLITE_OK ){
164127164688
RtreeSearchPoint *pNew;
164128
- pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, pRtree->iDepth+1);
164689
+ pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
164129164690
if( pNew==0 ) return SQLITE_NOMEM;
164130164691
pNew->id = 1;
164131164692
pNew->iCell = 0;
164132164693
pNew->eWithin = PARTLY_WITHIN;
164133164694
assert( pCsr->bPoint==1 );
@@ -164141,23 +164702,10 @@
164141164702
nodeRelease(pRtree, pRoot);
164142164703
rtreeRelease(pRtree);
164143164704
return rc;
164144164705
}
164145164706
164146
-/*
164147
-** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
164148
-** extension is currently being used by a version of SQLite too old to
164149
-** support estimatedRows. In that case this function is a no-op.
164150
-*/
164151
-static void setEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
164152
-#if SQLITE_VERSION_NUMBER>=3008002
164153
- if( sqlite3_libversion_number()>=3008002 ){
164154
- pIdxInfo->estimatedRows = nRow;
164155
- }
164156
-#endif
164157
-}
164158
-
164159164707
/*
164160164708
** Rtree virtual table module xBestIndex method. There are three
164161164709
** table scan strategies to choose from (in order from most to
164162164710
** least desirable):
164163164711
**
@@ -164233,11 +164781,11 @@
164233164781
** considered almost as quick as a direct rowid lookup (for which
164234164782
** sqlite uses an internal cost of 0.0). It is expected to return
164235164783
** a single row.
164236164784
*/
164237164785
pIdxInfo->estimatedCost = 30.0;
164238
- setEstimatedRows(pIdxInfo, 1);
164786
+ pIdxInfo->estimatedRows = 1;
164239164787
return SQLITE_OK;
164240164788
}
164241164789
164242164790
if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
164243164791
u8 op;
@@ -164251,11 +164799,11 @@
164251164799
assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
164252164800
op = RTREE_MATCH;
164253164801
break;
164254164802
}
164255164803
zIdxStr[iIdx++] = op;
164256
- zIdxStr[iIdx++] = p->iColumn - 1 + '0';
164804
+ zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0');
164257164805
pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
164258164806
pIdxInfo->aConstraintUsage[ii].omit = 1;
164259164807
}
164260164808
}
164261164809
@@ -164265,55 +164813,75 @@
164265164813
return SQLITE_NOMEM;
164266164814
}
164267164815
164268164816
nRow = pRtree->nRowEst >> (iIdx/2);
164269164817
pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
164270
- setEstimatedRows(pIdxInfo, nRow);
164818
+ pIdxInfo->estimatedRows = nRow;
164271164819
164272164820
return rc;
164273164821
}
164274164822
164275164823
/*
164276164824
** Return the N-dimensional volumn of the cell stored in *p.
164277164825
*/
164278164826
static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
164279164827
RtreeDValue area = (RtreeDValue)1;
164280
- int ii;
164281
- for(ii=0; ii<(pRtree->nDim*2); ii+=2){
164282
- area = (area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
164828
+ assert( pRtree->nDim>=1 && pRtree->nDim<=5 );
164829
+#ifndef SQLITE_RTREE_INT_ONLY
164830
+ if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
164831
+ switch( pRtree->nDim ){
164832
+ case 5: area = p->aCoord[9].f - p->aCoord[8].f;
164833
+ case 4: area *= p->aCoord[7].f - p->aCoord[6].f;
164834
+ case 3: area *= p->aCoord[5].f - p->aCoord[4].f;
164835
+ case 2: area *= p->aCoord[3].f - p->aCoord[2].f;
164836
+ default: area *= p->aCoord[1].f - p->aCoord[0].f;
164837
+ }
164838
+ }else
164839
+#endif
164840
+ {
164841
+ switch( pRtree->nDim ){
164842
+ case 5: area = p->aCoord[9].i - p->aCoord[8].i;
164843
+ case 4: area *= p->aCoord[7].i - p->aCoord[6].i;
164844
+ case 3: area *= p->aCoord[5].i - p->aCoord[4].i;
164845
+ case 2: area *= p->aCoord[3].i - p->aCoord[2].i;
164846
+ default: area *= p->aCoord[1].i - p->aCoord[0].i;
164847
+ }
164283164848
}
164284164849
return area;
164285164850
}
164286164851
164287164852
/*
164288164853
** Return the margin length of cell p. The margin length is the sum
164289164854
** of the objects size in each dimension.
164290164855
*/
164291164856
static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
164292
- RtreeDValue margin = (RtreeDValue)0;
164293
- int ii;
164294
- for(ii=0; ii<(pRtree->nDim*2); ii+=2){
164857
+ RtreeDValue margin = 0;
164858
+ int ii = pRtree->nDim2 - 2;
164859
+ do{
164295164860
margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
164296
- }
164861
+ ii -= 2;
164862
+ }while( ii>=0 );
164297164863
return margin;
164298164864
}
164299164865
164300164866
/*
164301164867
** Store the union of cells p1 and p2 in p1.
164302164868
*/
164303164869
static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
164304
- int ii;
164870
+ int ii = 0;
164305164871
if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
164306
- for(ii=0; ii<(pRtree->nDim*2); ii+=2){
164872
+ do{
164307164873
p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
164308164874
p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
164309
- }
164875
+ ii += 2;
164876
+ }while( ii<pRtree->nDim2 );
164310164877
}else{
164311
- for(ii=0; ii<(pRtree->nDim*2); ii+=2){
164878
+ do{
164312164879
p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
164313164880
p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
164314
- }
164881
+ ii += 2;
164882
+ }while( ii<pRtree->nDim2 );
164315164883
}
164316164884
}
164317164885
164318164886
/*
164319164887
** Return true if the area covered by p2 is a subset of the area covered
@@ -164320,11 +164888,11 @@
164320164888
** by p1. False otherwise.
164321164889
*/
164322164890
static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
164323164891
int ii;
164324164892
int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
164325
- for(ii=0; ii<(pRtree->nDim*2); ii+=2){
164893
+ for(ii=0; ii<pRtree->nDim2; ii+=2){
164326164894
RtreeCoord *a1 = &p1->aCoord[ii];
164327164895
RtreeCoord *a2 = &p2->aCoord[ii];
164328164896
if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f))
164329164897
|| ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i))
164330164898
){
@@ -164355,11 +164923,11 @@
164355164923
int ii;
164356164924
RtreeDValue overlap = RTREE_ZERO;
164357164925
for(ii=0; ii<nCell; ii++){
164358164926
int jj;
164359164927
RtreeDValue o = (RtreeDValue)1;
164360
- for(jj=0; jj<(pRtree->nDim*2); jj+=2){
164928
+ for(jj=0; jj<pRtree->nDim2; jj+=2){
164361164929
RtreeDValue x1, x2;
164362164930
x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
164363164931
x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
164364164932
if( x2<x1 ){
164365164933
o = (RtreeDValue)0;
@@ -165411,11 +165979,11 @@
165411165979
** with "column" that are interpreted as table constraints.
165412165980
** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
165413165981
** This problem was discovered after years of use, so we silently ignore
165414165982
** these kinds of misdeclared tables to avoid breaking any legacy.
165415165983
*/
165416
- assert( nData<=(pRtree->nDim*2 + 3) );
165984
+ assert( nData<=(pRtree->nDim2 + 3) );
165417165985
165418165986
#ifndef SQLITE_RTREE_INT_ONLY
165419165987
if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
165420165988
for(ii=0; ii<nData-4; ii+=2){
165421165989
cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]);
@@ -165500,10 +166068,31 @@
165500166068
165501166069
constraint:
165502166070
rtreeRelease(pRtree);
165503166071
return rc;
165504166072
}
166073
+
166074
+/*
166075
+** Called when a transaction starts.
166076
+*/
166077
+static int rtreeBeginTransaction(sqlite3_vtab *pVtab){
166078
+ Rtree *pRtree = (Rtree *)pVtab;
166079
+ assert( pRtree->inWrTrans==0 );
166080
+ pRtree->inWrTrans++;
166081
+ return SQLITE_OK;
166082
+}
166083
+
166084
+/*
166085
+** Called when a transaction completes (either by COMMIT or ROLLBACK).
166086
+** The sqlite3_blob object should be released at this point.
166087
+*/
166088
+static int rtreeEndTransaction(sqlite3_vtab *pVtab){
166089
+ Rtree *pRtree = (Rtree *)pVtab;
166090
+ pRtree->inWrTrans = 0;
166091
+ nodeBlobReset(pRtree);
166092
+ return SQLITE_OK;
166093
+}
165505166094
165506166095
/*
165507166096
** The xRename method for rtree module virtual tables.
165508166097
*/
165509166098
static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
@@ -165521,10 +166110,11 @@
165521166110
rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
165522166111
sqlite3_free(zSql);
165523166112
}
165524166113
return rc;
165525166114
}
166115
+
165526166116
165527166117
/*
165528166118
** This function populates the pRtree->nRowEst variable with an estimate
165529166119
** of the number of rows in the virtual table. If possible, this is based
165530166120
** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
@@ -165581,19 +166171,19 @@
165581166171
rtreeNext, /* xNext - advance a cursor */
165582166172
rtreeEof, /* xEof */
165583166173
rtreeColumn, /* xColumn - read data */
165584166174
rtreeRowid, /* xRowid - read data */
165585166175
rtreeUpdate, /* xUpdate - write data */
165586
- 0, /* xBegin - begin transaction */
165587
- 0, /* xSync - sync transaction */
165588
- 0, /* xCommit - commit transaction */
165589
- 0, /* xRollback - rollback transaction */
166176
+ rtreeBeginTransaction, /* xBegin - begin transaction */
166177
+ rtreeEndTransaction, /* xSync - sync transaction */
166178
+ rtreeEndTransaction, /* xCommit - commit transaction */
166179
+ rtreeEndTransaction, /* xRollback - rollback transaction */
165590166180
0, /* xFindFunction - function overloading */
165591166181
rtreeRename, /* xRename - rename the table */
165592166182
0, /* xSavepoint */
165593166183
0, /* xRelease */
165594
- 0 /* xRollbackTo */
166184
+ 0, /* xRollbackTo */
165595166185
};
165596166186
165597166187
static int rtreeSqlInit(
165598166188
Rtree *pRtree,
165599166189
sqlite3 *db,
@@ -165601,14 +166191,13 @@
165601166191
const char *zPrefix,
165602166192
int isCreate
165603166193
){
165604166194
int rc = SQLITE_OK;
165605166195
165606
- #define N_STATEMENT 9
166196
+ #define N_STATEMENT 8
165607166197
static const char *azSql[N_STATEMENT] = {
165608
- /* Read and write the xxx_node table */
165609
- "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1",
166198
+ /* Write the xxx_node table */
165610166199
"INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
165611166200
"DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
165612166201
165613166202
/* Read and write the xxx_rowid table */
165614166203
"SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
@@ -165642,19 +166231,18 @@
165642166231
if( rc!=SQLITE_OK ){
165643166232
return rc;
165644166233
}
165645166234
}
165646166235
165647
- appStmt[0] = &pRtree->pReadNode;
165648
- appStmt[1] = &pRtree->pWriteNode;
165649
- appStmt[2] = &pRtree->pDeleteNode;
165650
- appStmt[3] = &pRtree->pReadRowid;
165651
- appStmt[4] = &pRtree->pWriteRowid;
165652
- appStmt[5] = &pRtree->pDeleteRowid;
165653
- appStmt[6] = &pRtree->pReadParent;
165654
- appStmt[7] = &pRtree->pWriteParent;
165655
- appStmt[8] = &pRtree->pDeleteParent;
166236
+ appStmt[0] = &pRtree->pWriteNode;
166237
+ appStmt[1] = &pRtree->pDeleteNode;
166238
+ appStmt[2] = &pRtree->pReadRowid;
166239
+ appStmt[3] = &pRtree->pWriteRowid;
166240
+ appStmt[4] = &pRtree->pDeleteRowid;
166241
+ appStmt[5] = &pRtree->pReadParent;
166242
+ appStmt[6] = &pRtree->pWriteParent;
166243
+ appStmt[7] = &pRtree->pDeleteParent;
165656166244
165657166245
rc = rtreeQueryStat1(db, pRtree);
165658166246
for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
165659166247
char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
165660166248
if( zSql ){
@@ -165788,13 +166376,14 @@
165788166376
memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
165789166377
pRtree->nBusy = 1;
165790166378
pRtree->base.pModule = &rtreeModule;
165791166379
pRtree->zDb = (char *)&pRtree[1];
165792166380
pRtree->zName = &pRtree->zDb[nDb+1];
165793
- pRtree->nDim = (argc-4)/2;
165794
- pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2;
165795
- pRtree->eCoordType = eCoordType;
166381
+ pRtree->nDim = (u8)((argc-4)/2);
166382
+ pRtree->nDim2 = pRtree->nDim*2;
166383
+ pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
166384
+ pRtree->eCoordType = (u8)eCoordType;
165796166385
memcpy(pRtree->zDb, argv[1], nDb);
165797166386
memcpy(pRtree->zName, argv[2], nName);
165798166387
165799166388
/* Figure out the node size to use. */
165800166389
rc = getNodeSize(db, pRtree, isCreate, pzErr);
@@ -165863,11 +166452,12 @@
165863166452
int ii;
165864166453
165865166454
UNUSED_PARAMETER(nArg);
165866166455
memset(&node, 0, sizeof(RtreeNode));
165867166456
memset(&tree, 0, sizeof(Rtree));
165868
- tree.nDim = sqlite3_value_int(apArg[0]);
166457
+ tree.nDim = (u8)sqlite3_value_int(apArg[0]);
166458
+ tree.nDim2 = tree.nDim*2;
165869166459
tree.nBytesPerCell = 8 + 8 * tree.nDim;
165870166460
node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
165871166461
165872166462
for(ii=0; ii<NCELL(&node); ii++){
165873166463
char zCell[512];
@@ -165876,11 +166466,11 @@
165876166466
int jj;
165877166467
165878166468
nodeGetCell(&tree, &node, ii, &cell);
165879166469
sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
165880166470
nCell = (int)strlen(zCell);
165881
- for(jj=0; jj<tree.nDim*2; jj++){
166471
+ for(jj=0; jj<tree.nDim2; jj++){
165882166472
#ifndef SQLITE_RTREE_INT_ONLY
165883166473
sqlite3_snprintf(512-nCell,&zCell[nCell], " %g",
165884166474
(double)cell.aCoord[jj].f);
165885166475
#else
165886166476
sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
@@ -166584,42 +167174,40 @@
166584167174
166585167175
/*
166586167176
** Register the ICU extension functions with database db.
166587167177
*/
166588167178
SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
166589
- struct IcuScalar {
167179
+ static const struct IcuScalar {
166590167180
const char *zName; /* Function name */
166591
- int nArg; /* Number of arguments */
166592
- int enc; /* Optimal text encoding */
166593
- void *pContext; /* sqlite3_user_data() context */
167181
+ unsigned char nArg; /* Number of arguments */
167182
+ unsigned short enc; /* Optimal text encoding */
167183
+ unsigned char iContext; /* sqlite3_user_data() context */
166594167184
void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
166595167185
} scalars[] = {
166596
- {"regexp", 2, SQLITE_ANY|SQLITE_DETERMINISTIC, 0, icuRegexpFunc},
166597
-
166598
- {"lower", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
166599
- {"lower", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
166600
- {"upper", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16},
166601
- {"upper", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16},
166602
-
166603
- {"lower", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
166604
- {"lower", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
166605
- {"upper", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16},
166606
- {"upper", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16},
166607
-
166608
- {"like", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc},
166609
- {"like", 3, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc},
166610
-
166611
- {"icu_load_collation", 2, SQLITE_UTF8, (void*)db, icuLoadCollation},
167186
+ {"icu_load_collation", 2, SQLITE_UTF8, 1, icuLoadCollation},
167187
+ {"regexp", 2, SQLITE_ANY|SQLITE_DETERMINISTIC, 0, icuRegexpFunc},
167188
+ {"lower", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
167189
+ {"lower", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
167190
+ {"upper", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
167191
+ {"upper", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
167192
+ {"lower", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
167193
+ {"lower", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
167194
+ {"upper", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
167195
+ {"upper", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
167196
+ {"like", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc},
167197
+ {"like", 3, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc},
166612167198
};
166613
-
166614167199
int rc = SQLITE_OK;
166615167200
int i;
166616167201
167202
+
166617167203
for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
166618
- struct IcuScalar *p = &scalars[i];
167204
+ const struct IcuScalar *p = &scalars[i];
166619167205
rc = sqlite3_create_function(
166620
- db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0
167206
+ db, p->zName, p->nArg, p->enc,
167207
+ p->iContext ? (void*)db : (void*)0,
167208
+ p->xFunc, 0, 0
166621167209
);
166622167210
}
166623167211
166624167212
return rc;
166625167213
}
@@ -169823,11 +170411,11 @@
169823170411
169824170412
/*
169825170413
** Open the database handle and attach the RBU database as "rbu". If an
169826170414
** error occurs, leave an error code and message in the RBU handle.
169827170415
*/
169828
-static void rbuOpenDatabase(sqlite3rbu *p){
170416
+static void rbuOpenDatabase(sqlite3rbu *p, int *pbRetry){
169829170417
assert( p->rc || (p->dbMain==0 && p->dbRbu==0) );
169830170418
assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 );
169831170419
169832170420
/* Open the RBU database */
169833170421
p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
@@ -169898,11 +170486,11 @@
169898170486
if( p->eStage>=RBU_STAGE_MOVE ){
169899170487
bOpen = 1;
169900170488
}else{
169901170489
RbuState *pState = rbuLoadState(p);
169902170490
if( pState ){
169903
- bOpen = (pState->eStage>RBU_STAGE_MOVE);
170491
+ bOpen = (pState->eStage>=RBU_STAGE_MOVE);
169904170492
rbuFreeState(pState);
169905170493
}
169906170494
}
169907170495
if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
169908170496
}
@@ -169910,10 +170498,19 @@
169910170498
p->eStage = 0;
169911170499
if( p->rc==SQLITE_OK && p->dbMain==0 ){
169912170500
if( !rbuIsVacuum(p) ){
169913170501
p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
169914170502
}else if( p->pRbuFd->pWalFd ){
170503
+ if( pbRetry ){
170504
+ p->pRbuFd->bNolock = 0;
170505
+ sqlite3_close(p->dbRbu);
170506
+ sqlite3_close(p->dbMain);
170507
+ p->dbMain = 0;
170508
+ p->dbRbu = 0;
170509
+ *pbRetry = 1;
170510
+ return;
170511
+ }
169915170512
p->rc = SQLITE_ERROR;
169916170513
p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
169917170514
}else{
169918170515
char *zTarget;
169919170516
char *zExtra = 0;
@@ -170090,20 +170687,22 @@
170090170687
p->eStage = RBU_STAGE_CAPTURE;
170091170688
rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
170092170689
if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
170093170690
}
170094170691
170095
- if( p->rc==SQLITE_OK ){
170692
+ if( p->rc==SQLITE_OK && p->nFrame>0 ){
170096170693
p->eStage = RBU_STAGE_CKPT;
170097170694
p->nStep = (pState ? pState->nRow : 0);
170098170695
p->aBuf = rbuMalloc(p, p->pgsz);
170099170696
p->iWalCksum = rbuShmChecksum(p);
170100170697
}
170101170698
170102
- if( p->rc==SQLITE_OK && pState && pState->iWalCksum!=p->iWalCksum ){
170103
- p->rc = SQLITE_DONE;
170104
- p->eStage = RBU_STAGE_DONE;
170699
+ if( p->rc==SQLITE_OK ){
170700
+ if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
170701
+ p->rc = SQLITE_DONE;
170702
+ p->eStage = RBU_STAGE_DONE;
170703
+ }
170105170704
}
170106170705
}
170107170706
170108170707
/*
170109170708
** Called when iAmt bytes are read from offset iOff of the wal file while
@@ -170272,11 +170871,11 @@
170272170871
#else
170273170872
p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
170274170873
#endif
170275170874
170276170875
if( p->rc==SQLITE_OK ){
170277
- rbuOpenDatabase(p);
170876
+ rbuOpenDatabase(p, 0);
170278170877
rbuSetupCheckpoint(p, 0);
170279170878
}
170280170879
}
170281170880
}
170282170881
@@ -170983,10 +171582,11 @@
170983171582
rbuCreateVfs(p);
170984171583
170985171584
/* Open the target, RBU and state databases */
170986171585
if( p->rc==SQLITE_OK ){
170987171586
char *pCsr = (char*)&p[1];
171587
+ int bRetry = 0;
170988171588
if( zTarget ){
170989171589
p->zTarget = pCsr;
170990171590
memcpy(p->zTarget, zTarget, nTarget+1);
170991171591
pCsr += nTarget+1;
170992171592
}
@@ -170994,11 +171594,22 @@
170994171594
memcpy(p->zRbu, zRbu, nRbu+1);
170995171595
pCsr += nRbu+1;
170996171596
if( zState ){
170997171597
p->zState = rbuMPrintf(p, "%s", zState);
170998171598
}
170999
- rbuOpenDatabase(p);
171599
+
171600
+ /* If the first attempt to open the database file fails and the bRetry
171601
+ ** flag it set, this means that the db was not opened because it seemed
171602
+ ** to be a wal-mode db. But, this may have happened due to an earlier
171603
+ ** RBU vacuum operation leaving an old wal file in the directory.
171604
+ ** If this is the case, it will have been checkpointed and deleted
171605
+ ** when the handle was closed and a second attempt to open the
171606
+ ** database may succeed. */
171607
+ rbuOpenDatabase(p, &bRetry);
171608
+ if( bRetry ){
171609
+ rbuOpenDatabase(p, 0);
171610
+ }
171000171611
}
171001171612
171002171613
if( p->rc==SQLITE_OK ){
171003171614
pState = rbuLoadState(p);
171004171615
assert( pState || p->rc!=SQLITE_OK );
@@ -175957,11 +176568,11 @@
175957176568
sqlite3_value **ppValue /* OUT: Value from conflicting row */
175958176569
){
175959176570
if( !pIter->pConflict ){
175960176571
return SQLITE_MISUSE;
175961176572
}
175962
- if( iVal<0 || iVal>=sqlite3_column_count(pIter->pConflict) ){
176573
+ if( iVal<0 || iVal>=pIter->nCol ){
175963176574
return SQLITE_RANGE;
175964176575
}
175965176576
*ppValue = sqlite3_column_value(pIter->pConflict, iVal);
175966176577
return SQLITE_OK;
175967176578
}
@@ -176424,11 +177035,17 @@
176424177035
int i;
176425177036
SessionBuffer buf = {0, 0, 0};
176426177037
176427177038
sessionAppendStr(&buf, "INSERT INTO main.", &rc);
176428177039
sessionAppendIdent(&buf, zTab, &rc);
176429
- sessionAppendStr(&buf, " VALUES(?", &rc);
177040
+ sessionAppendStr(&buf, "(", &rc);
177041
+ for(i=0; i<p->nCol; i++){
177042
+ if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
177043
+ sessionAppendIdent(&buf, p->azCol[i], &rc);
177044
+ }
177045
+
177046
+ sessionAppendStr(&buf, ") VALUES(?", &rc);
176430177047
for(i=1; i<p->nCol; i++){
176431177048
sessionAppendStr(&buf, ", ?", &rc);
176432177049
}
176433177050
sessionAppendStr(&buf, ")", &rc);
176434177051
@@ -176970,42 +177587,51 @@
176970177587
break;
176971177588
}
176972177589
nTab = (int)strlen(zTab);
176973177590
sApply.azCol = (const char **)zTab;
176974177591
}else{
177592
+ int nMinCol = 0;
177593
+ int i;
177594
+
176975177595
sqlite3changeset_pk(pIter, &abPK, 0);
176976177596
rc = sessionTableInfo(
176977177597
db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
176978177598
);
176979177599
if( rc!=SQLITE_OK ) break;
177600
+ for(i=0; i<sApply.nCol; i++){
177601
+ if( sApply.abPK[i] ) nMinCol = i+1;
177602
+ }
176980177603
176981177604
if( sApply.nCol==0 ){
176982177605
schemaMismatch = 1;
176983177606
sqlite3_log(SQLITE_SCHEMA,
176984177607
"sqlite3changeset_apply(): no such table: %s", zTab
176985177608
);
176986177609
}
176987
- else if( sApply.nCol!=nCol ){
177610
+ else if( sApply.nCol<nCol ){
176988177611
schemaMismatch = 1;
176989177612
sqlite3_log(SQLITE_SCHEMA,
176990
- "sqlite3changeset_apply(): table %s has %d columns, expected %d",
177613
+ "sqlite3changeset_apply(): table %s has %d columns, "
177614
+ "expected %d or more",
176991177615
zTab, sApply.nCol, nCol
176992177616
);
176993177617
}
176994
- else if( memcmp(sApply.abPK, abPK, nCol)!=0 ){
177618
+ else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
176995177619
schemaMismatch = 1;
176996177620
sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
176997177621
"primary key mismatch for table %s", zTab
176998177622
);
176999177623
}
177000
- else if(
177001
- (rc = sessionSelectRow(db, zTab, &sApply))
177002
- || (rc = sessionUpdateRow(db, zTab, &sApply))
177003
- || (rc = sessionDeleteRow(db, zTab, &sApply))
177004
- || (rc = sessionInsertRow(db, zTab, &sApply))
177005
- ){
177006
- break;
177624
+ else{
177625
+ sApply.nCol = nCol;
177626
+ if((rc = sessionSelectRow(db, zTab, &sApply))
177627
+ || (rc = sessionUpdateRow(db, zTab, &sApply))
177628
+ || (rc = sessionDeleteRow(db, zTab, &sApply))
177629
+ || (rc = sessionInsertRow(db, zTab, &sApply))
177630
+ ){
177631
+ break;
177632
+ }
177007177633
}
177008177634
nTab = sqlite3Strlen30(zTab);
177009177635
}
177010177636
}
177011177637
@@ -177593,11 +178219,11 @@
177593178219
** a BLOB, but there is no support for JSONB in the current implementation.
177594178220
** This implementation parses JSON text at 250 MB/s, so it is hard to see
177595178221
** how JSONB might improve on that.)
177596178222
*/
177597178223
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
177598
-#if !defined(_SQLITEINT_H_)
178224
+#if !defined(SQLITEINT_H)
177599178225
/* #include "sqlite3ext.h" */
177600178226
#endif
177601178227
SQLITE_EXTENSION_INIT1
177602178228
/* #include <assert.h> */
177603178229
/* #include <string.h> */
@@ -181644,10 +182270,35 @@
181644182270
*/
181645182271
#ifndef fts5YYMALLOCARGTYPE
181646182272
# define fts5YYMALLOCARGTYPE size_t
181647182273
#endif
181648182274
182275
+/* Initialize a new parser that has already been allocated.
182276
+*/
182277
+static void sqlite3Fts5ParserInit(void *fts5yypParser){
182278
+ fts5yyParser *pParser = (fts5yyParser*)fts5yypParser;
182279
+#ifdef fts5YYTRACKMAXSTACKDEPTH
182280
+ pParser->fts5yyhwm = 0;
182281
+#endif
182282
+#if fts5YYSTACKDEPTH<=0
182283
+ pParser->fts5yytos = NULL;
182284
+ pParser->fts5yystack = NULL;
182285
+ pParser->fts5yystksz = 0;
182286
+ if( fts5yyGrowStack(pParser) ){
182287
+ pParser->fts5yystack = &pParser->fts5yystk0;
182288
+ pParser->fts5yystksz = 1;
182289
+ }
182290
+#endif
182291
+#ifndef fts5YYNOERRORRECOVERY
182292
+ pParser->fts5yyerrcnt = -1;
182293
+#endif
182294
+ pParser->fts5yytos = pParser->fts5yystack;
182295
+ pParser->fts5yystack[0].stateno = 0;
182296
+ pParser->fts5yystack[0].major = 0;
182297
+}
182298
+
182299
+#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
181649182300
/*
181650182301
** This function allocates a new parser.
181651182302
** The only argument is a pointer to a function which works like
181652182303
** malloc.
181653182304
**
@@ -181659,32 +182310,15 @@
181659182310
** to sqlite3Fts5Parser and sqlite3Fts5ParserFree.
181660182311
*/
181661182312
static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(fts5YYMALLOCARGTYPE)){
181662182313
fts5yyParser *pParser;
181663182314
pParser = (fts5yyParser*)(*mallocProc)( (fts5YYMALLOCARGTYPE)sizeof(fts5yyParser) );
181664
- if( pParser ){
181665
-#ifdef fts5YYTRACKMAXSTACKDEPTH
181666
- pParser->fts5yyhwm = 0;
181667
-#endif
181668
-#if fts5YYSTACKDEPTH<=0
181669
- pParser->fts5yytos = NULL;
181670
- pParser->fts5yystack = NULL;
181671
- pParser->fts5yystksz = 0;
181672
- if( fts5yyGrowStack(pParser) ){
181673
- pParser->fts5yystack = &pParser->fts5yystk0;
181674
- pParser->fts5yystksz = 1;
181675
- }
181676
-#endif
181677
-#ifndef fts5YYNOERRORRECOVERY
181678
- pParser->fts5yyerrcnt = -1;
181679
-#endif
181680
- pParser->fts5yytos = pParser->fts5yystack;
181681
- pParser->fts5yystack[0].stateno = 0;
181682
- pParser->fts5yystack[0].major = 0;
181683
- }
182315
+ if( pParser ) sqlite3Fts5ParserInit(pParser);
181684182316
return pParser;
181685182317
}
182318
+#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
182319
+
181686182320
181687182321
/* The following function deletes the "minor type" or semantic value
181688182322
** associated with a symbol. The symbol can be either a terminal
181689182323
** or nonterminal. "fts5yymajor" is the symbol code, and "fts5yypminor" is
181690182324
** a pointer to the value to be deleted. The code used to do the
@@ -181762,10 +182396,22 @@
181762182396
}
181763182397
#endif
181764182398
fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor);
181765182399
}
181766182400
182401
+/*
182402
+** Clear all secondary memory allocations from the parser
182403
+*/
182404
+static void sqlite3Fts5ParserFinalize(void *p){
182405
+ fts5yyParser *pParser = (fts5yyParser*)p;
182406
+ while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
182407
+#if fts5YYSTACKDEPTH<=0
182408
+ if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
182409
+#endif
182410
+}
182411
+
182412
+#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
181767182413
/*
181768182414
** Deallocate and destroy a parser. Destructors are called for
181769182415
** all stack elements before shutting the parser down.
181770182416
**
181771182417
** If the fts5YYPARSEFREENEVERNULL macro exists (for example because it
@@ -181774,20 +182420,17 @@
181774182420
*/
181775182421
static void sqlite3Fts5ParserFree(
181776182422
void *p, /* The parser to be deleted */
181777182423
void (*freeProc)(void*) /* Function used to reclaim memory */
181778182424
){
181779
- fts5yyParser *pParser = (fts5yyParser*)p;
181780182425
#ifndef fts5YYPARSEFREENEVERNULL
181781
- if( pParser==0 ) return;
181782
-#endif
181783
- while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
181784
-#if fts5YYSTACKDEPTH<=0
181785
- if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
181786
-#endif
181787
- (*freeProc)((void*)pParser);
181788
-}
182426
+ if( p==0 ) return;
182427
+#endif
182428
+ sqlite3Fts5ParserFinalize(p);
182429
+ (*freeProc)(p);
182430
+}
182431
+#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
181789182432
181790182433
/*
181791182434
** Return the peak depth of the stack for a parser.
181792182435
*/
181793182436
#ifdef fts5YYTRACKMAXSTACKDEPTH
@@ -186122,11 +186765,11 @@
186122186765
memset(&sCtx, 0, sizeof(TokenCtx));
186123186766
sCtx.pPhrase = pAppend;
186124186767
186125186768
rc = fts5ParseStringFromToken(pToken, &z);
186126186769
if( rc==SQLITE_OK ){
186127
- int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_QUERY : 0);
186770
+ int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_PREFIX : 0);
186128186771
int n;
186129186772
sqlite3Fts5Dequote(z);
186130186773
n = (int)strlen(z);
186131186774
rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize);
186132186775
}
@@ -196863,11 +197506,11 @@
196863197506
int nArg, /* Number of args */
196864197507
sqlite3_value **apUnused /* Function arguments */
196865197508
){
196866197509
assert( nArg==0 );
196867197510
UNUSED_PARAM2(nArg, apUnused);
196868
- sqlite3_result_text(pCtx, "fts5: 2017-01-06 16:32:41 a65a62893ca8319e89e48b8a38cf8a59c69a8209", -1, SQLITE_TRANSIENT);
197511
+ sqlite3_result_text(pCtx, "fts5: 2017-02-07 12:58:38 07fe6228208684d579c4f6c334c90eb6262a9233", -1, SQLITE_TRANSIENT);
196869197512
}
196870197513
196871197514
static int fts5Init(sqlite3 *db){
196872197515
static const sqlite3_module fts5Mod = {
196873197516
/* iVersion */ 2,
196874197517
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.16.2. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a single translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -208,10 +208,18 @@
208 #ifdef __GNUC__
209 # define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
210 #else
211 # define GCC_VERSION 0
212 #endif
 
 
 
 
 
 
 
 
213
214 /* Needed for various definitions... */
215 #if defined(__GNUC__) && !defined(_GNU_SOURCE)
216 # define _GNU_SOURCE
217 #endif
@@ -379,13 +387,13 @@
379 **
380 ** See also: [sqlite3_libversion()],
381 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
382 ** [sqlite_version()] and [sqlite_source_id()].
383 */
384 #define SQLITE_VERSION "3.16.2"
385 #define SQLITE_VERSION_NUMBER 3016002
386 #define SQLITE_SOURCE_ID "2017-01-06 16:32:41 a65a62893ca8319e89e48b8a38cf8a59c69a8209"
387
388 /*
389 ** CAPI3REF: Run-Time Library Version Numbers
390 ** KEYWORDS: sqlite3_version sqlite3_sourceid
391 **
@@ -517,11 +525,15 @@
517 ** sqlite3_uint64 and sqlite_uint64 types can store integer values
518 ** between 0 and +18446744073709551615 inclusive.
519 */
520 #ifdef SQLITE_INT64_TYPE
521 typedef SQLITE_INT64_TYPE sqlite_int64;
522 typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
 
 
 
 
523 #elif defined(_MSC_VER) || defined(__BORLANDC__)
524 typedef __int64 sqlite_int64;
525 typedef unsigned __int64 sqlite_uint64;
526 #else
527 typedef long long int sqlite_int64;
@@ -830,11 +842,11 @@
830 ** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
831 ** after reboot following a crash or power loss, the only bytes in a
832 ** file that were written at the application level might have changed
833 ** and that adjacent bytes, even bytes within the same sector are
834 ** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
835 ** flag indicate that a file cannot be deleted when open. The
836 ** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
837 ** read-only media and cannot be changed even by processes with
838 ** elevated privileges.
839 */
840 #define SQLITE_IOCAP_ATOMIC 0x00000001
@@ -980,10 +992,13 @@
980 ** <li> [SQLITE_IOCAP_ATOMIC16K]
981 ** <li> [SQLITE_IOCAP_ATOMIC32K]
982 ** <li> [SQLITE_IOCAP_ATOMIC64K]
983 ** <li> [SQLITE_IOCAP_SAFE_APPEND]
984 ** <li> [SQLITE_IOCAP_SEQUENTIAL]
 
 
 
985 ** </ul>
986 **
987 ** The SQLITE_IOCAP_ATOMIC property means that all writes of
988 ** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
989 ** mean that writes of blocks that are nnn bytes in size and
@@ -5668,11 +5683,11 @@
5668 ** ^(The update hook is not invoked when internal system tables are
5669 ** modified (i.e. sqlite_master and sqlite_sequence).)^
5670 ** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
5671 **
5672 ** ^In the current implementation, the update hook
5673 ** is not invoked when duplication rows are deleted because of an
5674 ** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook
5675 ** invoked when rows are deleted using the [truncate optimization].
5676 ** The exceptions defined in this paragraph might change in a future
5677 ** release of SQLite.
5678 **
@@ -6450,10 +6465,16 @@
6450 **
6451 ** ^Unless it returns SQLITE_MISUSE, this function sets the
6452 ** [database connection] error code and message accessible via
6453 ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
6454 **
 
 
 
 
 
 
6455 **
6456 ** ^(If the row that a BLOB handle points to is modified by an
6457 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
6458 ** then the BLOB handle is marked as "expired".
6459 ** This is true if any column of the row is changed, even a column
@@ -6473,10 +6494,14 @@
6473 ** and the built-in [zeroblob] SQL function may be used to create a
6474 ** zero-filled blob to read or write using the incremental-blob interface.
6475 **
6476 ** To avoid a resource leak, every open [BLOB handle] should eventually
6477 ** be released by a call to [sqlite3_blob_close()].
 
 
 
 
6478 */
6479 SQLITE_API int sqlite3_blob_open(
6480 sqlite3*,
6481 const char *zDb,
6482 const char *zTable,
@@ -6488,15 +6513,15 @@
6488
6489 /*
6490 ** CAPI3REF: Move a BLOB Handle to a New Row
6491 ** METHOD: sqlite3_blob
6492 **
6493 ** ^This function is used to move an existing blob handle so that it points
6494 ** to a different row of the same database table. ^The new row is identified
6495 ** by the rowid value passed as the second argument. Only the row can be
6496 ** changed. ^The database, table and column on which the blob handle is open
6497 ** remain the same. Moving an existing blob handle to a new row can be
6498 ** faster than closing the existing handle and opening a new one.
6499 **
6500 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
6501 ** it must exist and there must be either a blob or text value stored in
6502 ** the nominated column.)^ ^If the new row is not present in the table, or if
@@ -8421,22 +8446,22 @@
8421 ** ^These interfaces are only available if SQLite is compiled using the
8422 ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
8423 **
8424 ** ^The [sqlite3_preupdate_hook()] interface registers a callback function
8425 ** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
8426 ** on a [rowid table].
8427 ** ^At most one preupdate hook may be registered at a time on a single
8428 ** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
8429 ** the previous setting.
8430 ** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
8431 ** with a NULL pointer as the second parameter.
8432 ** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
8433 ** the first parameter to callbacks.
8434 **
8435 ** ^The preupdate hook only fires for changes to [rowid tables]; the preupdate
8436 ** hook is not invoked for changes to [virtual tables] or [WITHOUT ROWID]
8437 ** tables.
8438 **
8439 ** ^The second parameter to the preupdate callback is a pointer to
8440 ** the [database connection] that registered the preupdate hook.
8441 ** ^The third parameter to the preupdate callback is one of the constants
8442 ** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
@@ -8446,16 +8471,20 @@
8446 ** will be "main" for the main database or "temp" for TEMP tables or
8447 ** the name given after the AS keyword in the [ATTACH] statement for attached
8448 ** databases.)^
8449 ** ^The fifth parameter to the preupdate callback is the name of the
8450 ** table that is being modified.
8451 ** ^The sixth parameter to the preupdate callback is the initial [rowid] of the
8452 ** row being changes for SQLITE_UPDATE and SQLITE_DELETE changes and is
8453 ** undefined for SQLITE_INSERT changes.
8454 ** ^The seventh parameter to the preupdate callback is the final [rowid] of
8455 ** the row being changed for SQLITE_UPDATE and SQLITE_INSERT changes and is
8456 ** undefined for SQLITE_DELETE changes.
 
 
 
 
8457 **
8458 ** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
8459 ** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
8460 ** provide additional information about a preupdate event. These routines
8461 ** may only be called from within a preupdate callback. Invoking any of
@@ -9155,11 +9184,12 @@
9155 **
9156 ** <li> For each row (primary key) that exists in the to-table but not in
9157 ** the from-table, a DELETE record is added to the session object.
9158 **
9159 ** <li> For each row (primary key) that exists in both tables, but features
9160 ** different in each, an UPDATE record is added to the session.
 
9161 ** </ul>
9162 **
9163 ** To clarify, if this function is called and then a changeset constructed
9164 ** using [sqlite3session_changeset()], then after applying that changeset to
9165 ** database zFrom the contents of the two compatible tables would be
@@ -9740,11 +9770,11 @@
9740 ** considered compatible if all of the following are true:
9741 **
9742 ** <ul>
9743 ** <li> The table has the same name as the name recorded in the
9744 ** changeset, and
9745 ** <li> The table has the same number of columns as recorded in the
9746 ** changeset, and
9747 ** <li> The table has primary key columns in the same position as
9748 ** recorded in the changeset.
9749 ** </ul>
9750 **
@@ -9785,11 +9815,15 @@
9785 ** the changeset the row is deleted from the target database.
9786 **
9787 ** If a row with matching primary key values is found, but one or more of
9788 ** the non-primary key fields contains a value different from the original
9789 ** row value stored in the changeset, the conflict-handler function is
9790 ** invoked with [SQLITE_CHANGESET_DATA] as the second argument.
 
 
 
 
9791 **
9792 ** If no row with matching primary key values is found in the database,
9793 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
9794 ** passed as the second argument.
9795 **
@@ -9800,11 +9834,13 @@
9800 ** operation is attempted because an earlier call to the conflict handler
9801 ** function returned [SQLITE_CHANGESET_REPLACE].
9802 **
9803 ** <dt>INSERT Changes<dd>
9804 ** For each INSERT change, an attempt is made to insert the new row into
9805 ** the database.
 
 
9806 **
9807 ** If the attempt to insert the row fails because the database already
9808 ** contains a row with the same primary key values, the conflict handler
9809 ** function is invoked with the second argument set to
9810 ** [SQLITE_CHANGESET_CONFLICT].
@@ -9818,17 +9854,17 @@
9818 **
9819 ** <dt>UPDATE Changes<dd>
9820 ** For each UPDATE change, this function checks if the target database
9821 ** contains a row with the same primary key value (or values) as the
9822 ** original row values stored in the changeset. If it does, and the values
9823 ** stored in all non-primary key columns also match the values stored in
9824 ** the changeset the row is updated within the target database.
9825 **
9826 ** If a row with matching primary key values is found, but one or more of
9827 ** the non-primary key fields contains a value different from an original
9828 ** row value stored in the changeset, the conflict-handler function is
9829 ** invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
9830 ** UPDATE changes only contain values for non-primary key fields that are
9831 ** to be modified, only those fields need to match the original values to
9832 ** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
9833 **
9834 ** If no row with matching primary key values is found in the database,
@@ -11537,10 +11573,22 @@
11537 #include <stdlib.h>
11538 #include <string.h>
11539 #include <assert.h>
11540 #include <stddef.h>
11541
 
 
 
 
 
 
 
 
 
 
 
 
11542 /*
11543 ** If compiling for a processor that lacks floating point support,
11544 ** substitute integer for floating-point
11545 */
11546 #ifdef SQLITE_OMIT_FLOATING_POINT
@@ -11621,13 +11669,16 @@
11621 /*
11622 ** The default initial allocation for the pagecache when using separate
11623 ** pagecaches for each database connection. A positive number is the
11624 ** number of pages. A negative number N translations means that a buffer
11625 ** of -1024*N bytes is allocated and used for as many pages as it will hold.
 
 
 
11626 */
11627 #ifndef SQLITE_DEFAULT_PCACHE_INITSZ
11628 # define SQLITE_DEFAULT_PCACHE_INITSZ 100
11629 #endif
11630
11631 /*
11632 ** GCC does not define the offsetof() macro so we'll have to do it
11633 ** ourselves.
@@ -12346,13 +12397,14 @@
12346 );
12347 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*);
12348 SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*);
12349 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, u8 flags);
12350
12351 /* Allowed flags for the 2nd argument to sqlite3BtreeDelete() */
12352 #define BTREE_SAVEPOSITION 0x02 /* Leave cursor pointing at NEXT or PREV */
12353 #define BTREE_AUXDELETE 0x04 /* not the primary delete operation */
 
12354
12355 /* An instance of the BtreePayload object describes the content of a single
12356 ** entry in either an index or table btree.
12357 **
12358 ** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
@@ -12379,11 +12431,11 @@
12379 int nData; /* Size of pData. 0 if none. */
12380 int nZero; /* Extra zero data appended after pData,nData */
12381 };
12382
12383 SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
12384 int bias, int seekResult);
12385 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
12386 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
12387 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
12388 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
12389 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
@@ -12512,12 +12564,11 @@
12512 ** as an instance of the following structure:
12513 */
12514 struct VdbeOp {
12515 u8 opcode; /* What operation to perform */
12516 signed char p4type; /* One of the P4_xxx constants for p4 */
12517 u8 notUsed1;
12518 u8 p5; /* Fifth parameter is an unsigned character */
12519 int p1; /* First operand */
12520 int p2; /* Second parameter (often the jump destination) */
12521 int p3; /* The third parameter */
12522 union p4union { /* fourth parameter */
12523 int i; /* Integer value if p4type==P4_INT32 */
@@ -12874,11 +12925,11 @@
12874 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
12875 SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8);
12876 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
12877 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
12878 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
12879 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
12880 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
12881 SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
12882 SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
12883 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
12884 SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
@@ -13176,18 +13227,20 @@
13176 SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
13177 SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
13178 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
13179 SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
13180 SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
13181 SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager);
 
 
13182 # ifdef SQLITE_ENABLE_SNAPSHOT
13183 SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot);
13184 SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot);
13185 SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager);
13186 # endif
13187 #else
13188 # define sqlite3PagerUseWal(x) 0
13189 #endif
13190
13191 #ifdef SQLITE_ENABLE_ZIPVFS
13192 SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager);
13193 #endif
@@ -14007,10 +14060,11 @@
14007 signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
14008 u8 suppressErr; /* Do not issue error messages if true */
14009 u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
14010 u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
14011 u8 mTrace; /* zero or more SQLITE_TRACE flags */
 
14012 int nextPagesize; /* Pagesize after VACUUM if >0 */
14013 u32 magic; /* Magic number for detect library misuse */
14014 int nChange; /* Value returned by sqlite3_changes() */
14015 int nTotalChange; /* Value returned by sqlite3_total_changes() */
14016 int aLimit[SQLITE_N_LIMIT]; /* Limits */
@@ -14272,10 +14326,11 @@
14272 #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
14273 #define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
14274 #define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */
14275 #define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a
14276 ** single query - might change over time */
 
14277
14278 /*
14279 ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
14280 ** used to create the initializers for the FuncDef structures.
14281 **
@@ -15278,11 +15333,11 @@
15278 #define WHERE_DISTINCTBY 0x0080 /* pOrderby is really a DISTINCT clause */
15279 #define WHERE_WANT_DISTINCT 0x0100 /* All output needs to be distinct */
15280 #define WHERE_SORTBYGROUP 0x0200 /* Support sqlite3WhereIsSorted() */
15281 #define WHERE_SEEK_TABLE 0x0400 /* Do not defer seeks on main table */
15282 #define WHERE_ORDERBY_LIMIT 0x0800 /* ORDERBY+LIMIT on the inner loop */
15283 /* 0x1000 not currently used */
15284 /* 0x2000 not currently used */
15285 #define WHERE_USE_LIMIT 0x4000 /* Use the LIMIT in cost estimates */
15286 /* 0x8000 not currently used */
15287
15288 /* Allowed return values from sqlite3WhereIsDistinct()
@@ -15739,25 +15794,23 @@
15739 ** OPFLAG_AUXDELETE == BTREE_AUXDELETE
15740 */
15741 #define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
15742 /* Also used in P2 (not P5) of OP_Delete */
15743 #define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */
15744 #define OPFLAG_LASTROWID 0x02 /* Set to update db->lastRowid */
15745 #define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */
15746 #define OPFLAG_APPEND 0x08 /* This is likely to be an append */
15747 #define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
15748 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
15749 #define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
15750 #endif
15751 #define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */
15752 #define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */
15753 #define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */
15754 #define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */
15755 #define OPFLAG_FORDELETE 0x08 /* OP_Open should use BTREE_FORDELETE */
15756 #define OPFLAG_P2ISREG 0x10 /* P2 to OP_Open** is a register number */
15757 #define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */
15758 #define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete: keep cursor position */
15759 #define OPFLAG_AUXDELETE 0x04 /* OP_Delete: index in a DELETE op */
15760
15761 /*
15762 * Each trigger present in the database schema is stored as an instance of
15763 * struct Trigger.
@@ -16414,11 +16467,11 @@
16414 SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
16415 SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
16416 SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int);
16417 SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, Expr*, int);
16418 SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
16419 SQLITE_PRIVATE void sqlite3ExprCodeAtInit(Parse*, Expr*, int, u8);
16420 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
16421 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
16422 SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
16423 SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
16424 #define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
@@ -16476,10 +16529,15 @@
16476 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
16477 SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
16478 SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
16479 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
16480 u8,u8,int,int*,int*);
 
 
 
 
 
16481 SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
16482 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
16483 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
16484 SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
16485 SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
@@ -16754,12 +16812,14 @@
16754 #endif
16755
16756 /*
16757 ** The interface to the LEMON-generated parser
16758 */
16759 SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64));
16760 SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
 
 
16761 SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
16762 #ifdef YYTRACKMAXSTACKDEPTH
16763 SQLITE_PRIVATE int sqlite3ParserStackPeak(void*);
16764 #endif
16765
@@ -16865,10 +16925,11 @@
16865 #define sqlite3FkActions(a,b,c,d,e,f)
16866 #define sqlite3FkCheck(a,b,c,d,e,f)
16867 #define sqlite3FkDropTable(a,b,c)
16868 #define sqlite3FkOldmask(a,b) 0
16869 #define sqlite3FkRequired(a,b,c,d) 0
 
16870 #endif
16871 #ifndef SQLITE_OMIT_FOREIGN_KEY
16872 SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *, Table*);
16873 SQLITE_PRIVATE int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
16874 #else
@@ -17193,10 +17254,23 @@
17193 ** setting.)
17194 */
17195 #ifndef SQLITE_STMTJRNL_SPILL
17196 # define SQLITE_STMTJRNL_SPILL (64*1024)
17197 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
17198
17199 /*
17200 ** The following singleton contains the global configuration for
17201 ** the SQLite library.
17202 */
@@ -17206,12 +17280,11 @@
17206 SQLITE_THREADSAFE==1, /* bFullMutex */
17207 SQLITE_USE_URI, /* bOpenUri */
17208 SQLITE_ALLOW_COVERING_INDEX_SCAN, /* bUseCis */
17209 0x7ffffffe, /* mxStrlen */
17210 0, /* neverCorrupt */
17211 512, /* szLookaside */
17212 125, /* nLookaside */
17213 SQLITE_STMTJRNL_SPILL, /* nStmtSpill */
17214 {0,0,0,0,0,0,0,0}, /* m */
17215 {0,0,0,0,0,0,0,0,0}, /* mutex */
17216 {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
17217 (void*)0, /* pHeap */
@@ -18219,10 +18292,11 @@
18219 int iNewReg; /* Register for new.* values */
18220 i64 iKey1; /* First key value passed to hook */
18221 i64 iKey2; /* Second key value passed to hook */
18222 Mem *aNew; /* Array of new.* values */
18223 Table *pTab; /* Schema object being upated */
 
18224 };
18225
18226 /*
18227 ** Function prototypes
18228 */
@@ -24283,39 +24357,38 @@
24283
24284 /*
24285 ** Do a memory allocation with statistics and alarms. Assume the
24286 ** lock is already held.
24287 */
24288 static int mallocWithAlarm(int n, void **pp){
24289 int nFull;
24290 void *p;
 
24291 assert( sqlite3_mutex_held(mem0.mutex) );
24292 nFull = sqlite3GlobalConfig.m.xRoundup(n);
24293 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
24294 if( mem0.alarmThreshold>0 ){
24295 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
 
24296 if( nUsed >= mem0.alarmThreshold - nFull ){
24297 mem0.nearlyFull = 1;
24298 sqlite3MallocAlarm(nFull);
24299 }else{
24300 mem0.nearlyFull = 0;
24301 }
24302 }
24303 p = sqlite3GlobalConfig.m.xMalloc(nFull);
24304 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
24305 if( p==0 && mem0.alarmThreshold>0 ){
24306 sqlite3MallocAlarm(nFull);
24307 p = sqlite3GlobalConfig.m.xMalloc(nFull);
24308 }
24309 #endif
24310 if( p ){
24311 nFull = sqlite3MallocSize(p);
24312 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
24313 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
24314 }
24315 *pp = p;
24316 return nFull;
24317 }
24318
24319 /*
24320 ** Allocate memory. This routine is like sqlite3_malloc() except that it
24321 ** assumes the memory subsystem has already been initialized.
@@ -24951,11 +25024,10 @@
24951
24952 /*
24953 ** Allowed values for et_info.flags
24954 */
24955 #define FLAG_SIGNED 1 /* True if the value to convert is signed */
24956 #define FLAG_INTERN 2 /* True if for internal use only */
24957 #define FLAG_STRING 4 /* Allow infinity precision */
24958
24959
24960 /*
24961 ** The following table is searched linearly, so it is good to put the
@@ -24985,15 +25057,14 @@
24985 { 'i', 10, 1, etRADIX, 0, 0 },
24986 { 'n', 0, 0, etSIZE, 0, 0 },
24987 { '%', 0, 0, etPERCENT, 0, 0 },
24988 { 'p', 16, 0, etPOINTER, 0, 1 },
24989
24990 /* All the rest have the FLAG_INTERN bit set and are thus for internal
24991 ** use only */
24992 { 'T', 0, 2, etTOKEN, 0, 0 },
24993 { 'S', 0, 2, etSRCLIST, 0, 0 },
24994 { 'r', 10, 3, etORDINAL, 0, 0 },
24995 };
24996
24997 /*
24998 ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
24999 ** conversions will work.
@@ -25083,11 +25154,10 @@
25083 etByte flag_long; /* True if "l" flag is present */
25084 etByte flag_longlong; /* True if the "ll" flag is present */
25085 etByte done; /* Loop termination flag */
25086 etByte xtype = etINVALID; /* Conversion paradigm */
25087 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
25088 u8 useIntern; /* Ok to use internal conversions (ex: %T) */
25089 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
25090 sqlite_uint64 longvalue; /* Value for integer types */
25091 LONGDOUBLE_TYPE realvalue; /* Value for real types */
25092 const et_info *infop; /* Pointer to the appropriate info structure */
25093 char *zOut; /* Rendering buffer */
@@ -25102,17 +25172,15 @@
25102 #endif
25103 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
25104 char buf[etBUFSIZE]; /* Conversion buffer */
25105
25106 bufpt = 0;
25107 if( pAccum->printfFlags ){
25108 if( (bArgList = (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){
25109 pArgList = va_arg(ap, PrintfArguments*);
25110 }
25111 useIntern = pAccum->printfFlags & SQLITE_PRINTF_INTERNAL;
25112 }else{
25113 bArgList = useIntern = 0;
25114 }
25115 for(; (c=(*fmt))!=0; ++fmt){
25116 if( c!='%' ){
25117 bufpt = (char *)fmt;
25118 #if HAVE_STRCHRNUL
@@ -25220,15 +25288,11 @@
25220 infop = &fmtinfo[0];
25221 xtype = etINVALID;
25222 for(idx=0; idx<ArraySize(fmtinfo); idx++){
25223 if( c==fmtinfo[idx].fmttype ){
25224 infop = &fmtinfo[idx];
25225 if( useIntern || (infop->flags & FLAG_INTERN)==0 ){
25226 xtype = infop->type;
25227 }else{
25228 return;
25229 }
25230 break;
25231 }
25232 }
25233
25234 /*
@@ -25593,22 +25657,28 @@
25593 ** consume, not the length of the output...
25594 ** if( precision>=0 && precision<length ) length = precision; */
25595 break;
25596 }
25597 case etTOKEN: {
25598 Token *pToken = va_arg(ap, Token*);
 
 
25599 assert( bArgList==0 );
25600 if( pToken && pToken->n ){
25601 sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
25602 }
25603 length = width = 0;
25604 break;
25605 }
25606 case etSRCLIST: {
25607 SrcList *pSrc = va_arg(ap, SrcList*);
25608 int k = va_arg(ap, int);
25609 struct SrcList_item *pItem = &pSrc->a[k];
 
 
 
 
25610 assert( bArgList==0 );
25611 assert( k>=0 && k<pSrc->nSrc );
25612 if( pItem->zDatabase ){
25613 sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
25614 sqlite3StrAccumAppend(pAccum, ".", 1);
@@ -25626,13 +25696,17 @@
25626 ** The text of the conversion is pointed to by "bufpt" and is
25627 ** "length" characters long. The field width is "width". Do
25628 ** the output.
25629 */
25630 width -= length;
25631 if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
25632 sqlite3StrAccumAppend(pAccum, bufpt, length);
25633 if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
 
 
 
 
25634
25635 if( zExtra ){
25636 sqlite3DbFree(pAccum->db, zExtra);
25637 zExtra = 0;
25638 }
@@ -28601,11 +28675,11 @@
28601 #if SQLITE_BYTEORDER==4321
28602 u32 x;
28603 memcpy(&x,p,4);
28604 return x;
28605 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
28606 && defined(__GNUC__) && GCC_VERSION>=4003000
28607 u32 x;
28608 memcpy(&x,p,4);
28609 return __builtin_bswap32(x);
28610 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
28611 && defined(_MSC_VER) && _MSC_VER>=1300
@@ -28619,11 +28693,11 @@
28619 }
28620 SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
28621 #if SQLITE_BYTEORDER==4321
28622 memcpy(p,&v,4);
28623 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
28624 && defined(__GNUC__) && GCC_VERSION>=4003000
28625 u32 x = __builtin_bswap32(v);
28626 memcpy(p,&x,4);
28627 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
28628 && defined(_MSC_VER) && _MSC_VER>=1300
28629 u32 x = _byteswap_ulong(v);
@@ -28739,10 +28813,14 @@
28739 ** the other 64-bit signed integer at *pA and store the result in *pA.
28740 ** Return 0 on success. Or if the operation would have resulted in an
28741 ** overflow, leave *pA unchanged and return 1.
28742 */
28743 SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
 
 
 
 
28744 i64 iA = *pA;
28745 testcase( iA==0 ); testcase( iA==1 );
28746 testcase( iB==-1 ); testcase( iB==0 );
28747 if( iB>=0 ){
28748 testcase( iA>0 && LARGEST_INT64 - iA == iB );
@@ -28753,23 +28831,33 @@
28753 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
28754 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
28755 }
28756 *pA += iB;
28757 return 0;
 
28758 }
28759 SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
 
 
 
 
28760 testcase( iB==SMALLEST_INT64+1 );
28761 if( iB==SMALLEST_INT64 ){
28762 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
28763 if( (*pA)>=0 ) return 1;
28764 *pA -= iB;
28765 return 0;
28766 }else{
28767 return sqlite3AddInt64(pA, -iB);
28768 }
 
28769 }
28770 SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
 
 
 
 
28771 i64 iA = *pA;
28772 if( iB>0 ){
28773 if( iA>LARGEST_INT64/iB ) return 1;
28774 if( iA<SMALLEST_INT64/iB ) return 1;
28775 }else if( iB<0 ){
@@ -28781,10 +28869,11 @@
28781 if( -iA>LARGEST_INT64/-iB ) return 1;
28782 }
28783 }
28784 *pA = iA*iB;
28785 return 0;
 
28786 }
28787
28788 /*
28789 ** Compute the absolute value of a 32-bit signed integer, of possible. Or
28790 ** if the integer has a value of -2147483648, return +2147483647
@@ -47485,18 +47574,24 @@
47485 ** if( pPager->jfd->pMethods ){ ...
47486 */
47487 #define isOpen(pFd) ((pFd)->pMethods!=0)
47488
47489 /*
47490 ** Return true if this pager uses a write-ahead log instead of the usual
47491 ** rollback journal. Otherwise false.
47492 */
 
 
 
 
 
 
 
 
 
47493 #ifndef SQLITE_OMIT_WAL
47494 SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager){
47495 return (pPager->pWal!=0);
47496 }
47497 # define pagerUseWal(x) sqlite3PagerUseWal(x)
47498 #else
47499 # define pagerUseWal(x) 0
47500 # define pagerRollbackWal(x) 0
47501 # define pagerWalFrames(v,w,x,y) 0
47502 # define pagerOpenWalIfPresent(z) SQLITE_OK
@@ -58624,27 +58719,38 @@
58624 ** Enter the mutexes in accending order by BtShared pointer address
58625 ** to avoid the possibility of deadlock when two threads with
58626 ** two or more btrees in common both try to lock all their btrees
58627 ** at the same instant.
58628 */
58629 SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
58630 int i;
 
58631 Btree *p;
58632 assert( sqlite3_mutex_held(db->mutex) );
58633 for(i=0; i<db->nDb; i++){
58634 p = db->aDb[i].pBt;
58635 if( p ) sqlite3BtreeEnter(p);
 
 
 
58636 }
 
58637 }
58638 SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
 
 
 
58639 int i;
58640 Btree *p;
58641 assert( sqlite3_mutex_held(db->mutex) );
58642 for(i=0; i<db->nDb; i++){
58643 p = db->aDb[i].pBt;
58644 if( p ) sqlite3BtreeLeave(p);
58645 }
 
 
 
58646 }
58647
58648 #ifndef NDEBUG
58649 /*
58650 ** Return true if the current thread holds the database connection
@@ -62097,16 +62203,18 @@
62097 for(i=0; i<nCell; i++){
62098 u8 *pCell = findCell(pPage, i);
62099 if( eType==PTRMAP_OVERFLOW1 ){
62100 CellInfo info;
62101 pPage->xParseCell(pPage, pCell, &info);
62102 if( info.nLocal<info.nPayload
62103 && pCell+info.nSize-1<=pPage->aData+pPage->maskPage
62104 && iFrom==get4byte(pCell+info.nSize-4)
62105 ){
62106 put4byte(pCell+info.nSize-4, iTo);
62107 break;
 
 
62108 }
62109 }else{
62110 if( get4byte(pCell)==iFrom ){
62111 put4byte(pCell, iTo);
62112 break;
@@ -62777,11 +62885,16 @@
62777 if( p && p->inTrans==TRANS_WRITE ){
62778 BtShared *pBt = p->pBt;
62779 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
62780 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
62781 sqlite3BtreeEnter(p);
62782 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
 
 
 
 
 
62783 if( rc==SQLITE_OK ){
62784 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
62785 pBt->nPage = 0;
62786 }
62787 rc = newDatabase(pBt);
@@ -63163,25 +63276,24 @@
63163 ** for the entry that the pCur cursor is pointing to. The eOp
63164 ** argument is interpreted as follows:
63165 **
63166 ** 0: The operation is a read. Populate the overflow cache.
63167 ** 1: The operation is a write. Populate the overflow cache.
63168 ** 2: The operation is a read. Do not populate the overflow cache.
63169 **
63170 ** A total of "amt" bytes are read or written beginning at "offset".
63171 ** Data is read to or from the buffer pBuf.
63172 **
63173 ** The content being read or written might appear on the main page
63174 ** or be scattered out on multiple overflow pages.
63175 **
63176 ** If the current cursor entry uses one or more overflow pages and the
63177 ** eOp argument is not 2, this function may allocate space for and lazily
63178 ** populates the overflow page-list cache array (BtCursor.aOverflow).
63179 ** Subsequent calls use this cache to make seeking to the supplied offset
63180 ** more efficient.
63181 **
63182 ** Once an overflow page-list cache has been allocated, it may be
63183 ** invalidated if some other cursor writes to the same table, or if
63184 ** the cursor is moved to a different row. Additionally, in auto-vacuum
63185 ** mode, the following events may invalidate an overflow page-list cache.
63186 **
63187 ** * An incremental vacuum,
@@ -63199,25 +63311,21 @@
63199 int rc = SQLITE_OK;
63200 int iIdx = 0;
63201 MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
63202 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
63203 #ifdef SQLITE_DIRECT_OVERFLOW_READ
63204 unsigned char * const pBufStart = pBuf;
63205 int bEnd; /* True if reading to end of data */
63206 #endif
63207
63208 assert( pPage );
 
63209 assert( pCur->eState==CURSOR_VALID );
63210 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
63211 assert( cursorHoldsMutex(pCur) );
63212 assert( eOp!=2 || offset==0 ); /* Always start from beginning for eOp==2 */
63213
63214 getCellInfo(pCur);
63215 aPayload = pCur->info.pPayload;
63216 #ifdef SQLITE_DIRECT_OVERFLOW_READ
63217 bEnd = offset+amt==pCur->info.nPayload;
63218 #endif
63219 assert( offset+amt <= pCur->info.nPayload );
63220
63221 assert( aPayload > pPage->aData );
63222 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
63223 /* Trying to read or write past the end of the data is an error. The
@@ -63232,11 +63340,11 @@
63232 if( offset<pCur->info.nLocal ){
63233 int a = amt;
63234 if( a+offset>pCur->info.nLocal ){
63235 a = pCur->info.nLocal - offset;
63236 }
63237 rc = copyPayload(&aPayload[offset], pBuf, a, (eOp & 0x01), pPage->pDbPage);
63238 offset = 0;
63239 pBuf += a;
63240 amt -= a;
63241 }else{
63242 offset -= pCur->info.nLocal;
@@ -63248,69 +63356,58 @@
63248 Pgno nextPage;
63249
63250 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
63251
63252 /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
63253 ** Except, do not allocate aOverflow[] for eOp==2.
63254 **
63255 ** The aOverflow[] array is sized at one entry for each overflow page
63256 ** in the overflow chain. The page number of the first overflow page is
63257 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
63258 ** means "not yet known" (the cache is lazily populated).
63259 */
63260 if( eOp!=2 && (pCur->curFlags & BTCF_ValidOvfl)==0 ){
63261 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
63262 if( nOvfl>pCur->nOvflAlloc ){
63263 Pgno *aNew = (Pgno*)sqlite3Realloc(
63264 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
63265 );
63266 if( aNew==0 ){
63267 rc = SQLITE_NOMEM_BKPT;
63268 }else{
63269 pCur->nOvflAlloc = nOvfl*2;
63270 pCur->aOverflow = aNew;
63271 }
63272 }
63273 if( rc==SQLITE_OK ){
63274 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
63275 pCur->curFlags |= BTCF_ValidOvfl;
 
 
 
 
 
 
 
 
63276 }
63277 }
63278
63279 /* If the overflow page-list cache has been allocated and the
63280 ** entry for the first required overflow page is valid, skip
63281 ** directly to it.
63282 */
63283 if( (pCur->curFlags & BTCF_ValidOvfl)!=0
63284 && pCur->aOverflow[offset/ovflSize]
63285 ){
63286 iIdx = (offset/ovflSize);
63287 nextPage = pCur->aOverflow[iIdx];
63288 offset = (offset%ovflSize);
63289 }
63290
63291 for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
63292
63293 /* If required, populate the overflow page-list cache. */
63294 if( (pCur->curFlags & BTCF_ValidOvfl)!=0 ){
63295 assert( pCur->aOverflow[iIdx]==0
63296 || pCur->aOverflow[iIdx]==nextPage
63297 || CORRUPT_DB );
63298 pCur->aOverflow[iIdx] = nextPage;
63299 }
63300
63301 if( offset>=ovflSize ){
63302 /* The only reason to read this page is to obtain the page
63303 ** number for the next page in the overflow chain. The page
63304 ** data is not required. So first try to lookup the overflow
63305 ** page-list cache, if any, then fall back to the getOverflowPage()
63306 ** function.
63307 **
63308 ** Note that the aOverflow[] array must be allocated because eOp!=2
63309 ** here. If eOp==2, then offset==0 and this branch is never taken.
63310 */
63311 assert( eOp!=2 );
63312 assert( pCur->curFlags & BTCF_ValidOvfl );
63313 assert( pCur->pBtree->db==pBt->db );
63314 if( pCur->aOverflow[iIdx+1] ){
63315 nextPage = pCur->aOverflow[iIdx+1];
63316 }else{
@@ -63320,11 +63417,11 @@
63320 }else{
63321 /* Need to read this page properly. It contains some of the
63322 ** range of data that is being read (eOp==0) or written (eOp!=0).
63323 */
63324 #ifdef SQLITE_DIRECT_OVERFLOW_READ
63325 sqlite3_file *fd;
63326 #endif
63327 int a = amt;
63328 if( a + offset > ovflSize ){
63329 a = ovflSize - offset;
63330 }
@@ -63332,31 +63429,29 @@
63332 #ifdef SQLITE_DIRECT_OVERFLOW_READ
63333 /* If all the following are true:
63334 **
63335 ** 1) this is a read operation, and
63336 ** 2) data is required from the start of this overflow page, and
63337 ** 3) the database is file-backed, and
63338 ** 4) there is no open write-transaction, and
63339 ** 5) the database is not a WAL database,
63340 ** 6) all data from the page is being read.
63341 ** 7) at least 4 bytes have already been read into the output buffer
63342 **
63343 ** then data can be read directly from the database file into the
63344 ** output buffer, bypassing the page-cache altogether. This speeds
63345 ** up loading large records that span many overflow pages.
63346 */
63347 if( (eOp&0x01)==0 /* (1) */
63348 && offset==0 /* (2) */
63349 && (bEnd || a==ovflSize) /* (6) */
63350 && pBt->inTransaction==TRANS_READ /* (4) */
63351 && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (3) */
63352 && 0==sqlite3PagerUseWal(pBt->pPager) /* (5) */
63353 && &pBuf[-4]>=pBufStart /* (7) */
63354 ){
63355 u8 aSave[4];
63356 u8 *aWrite = &pBuf[-4];
63357 assert( aWrite>=pBufStart ); /* hence (7) */
63358 memcpy(aSave, aWrite, 4);
63359 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
63360 nextPage = get4byte(aWrite);
63361 memcpy(aWrite, aSave, 4);
63362 }else
@@ -63363,28 +63458,31 @@
63363 #endif
63364
63365 {
63366 DbPage *pDbPage;
63367 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
63368 ((eOp&0x01)==0 ? PAGER_GET_READONLY : 0)
63369 );
63370 if( rc==SQLITE_OK ){
63371 aPayload = sqlite3PagerGetData(pDbPage);
63372 nextPage = get4byte(aPayload);
63373 rc = copyPayload(&aPayload[offset+4], pBuf, a, (eOp&0x01), pDbPage);
63374 sqlite3PagerUnref(pDbPage);
63375 offset = 0;
63376 }
63377 }
63378 amt -= a;
 
63379 pBuf += a;
63380 }
 
 
63381 }
63382 }
63383
63384 if( rc==SQLITE_OK && amt>0 ){
63385 return SQLITE_CORRUPT_BKPT;
63386 }
63387 return rc;
63388 }
63389
63390 /*
@@ -63409,25 +63507,38 @@
63409 assert( pCur->eState==CURSOR_VALID );
63410 assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
63411 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
63412 return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
63413 }
 
 
 
 
 
 
63414 #ifndef SQLITE_OMIT_INCRBLOB
63415 SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
 
 
 
 
 
63416 int rc;
63417 if ( pCur->eState==CURSOR_INVALID ){
63418 return SQLITE_ABORT;
63419 }
63420 assert( cursorOwnsBtShared(pCur) );
63421 rc = restoreCursorPosition(pCur);
63422 if( rc==SQLITE_OK ){
63423 assert( pCur->eState==CURSOR_VALID );
63424 assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
63425 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
63426 rc = accessPayload(pCur, offset, amt, pBuf, 0);
63427 }
63428 return rc;
 
 
63429 }
63430 #endif /* SQLITE_OMIT_INCRBLOB */
63431
63432 /*
63433 ** Return a pointer to payload information from the entry that the
@@ -63829,13 +63940,30 @@
63829 ){
63830 if( pCur->info.nKey==intKey ){
63831 *pRes = 0;
63832 return SQLITE_OK;
63833 }
63834 if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKey<intKey ){
63835 *pRes = -1;
63836 return SQLITE_OK;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63837 }
63838 }
63839
63840 if( pIdxKey ){
63841 xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
@@ -63967,11 +64095,12 @@
63967 if( pCellKey==0 ){
63968 rc = SQLITE_NOMEM_BKPT;
63969 goto moveto_finish;
63970 }
63971 pCur->aiIdx[pCur->iPage] = (u16)idx;
63972 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 2);
 
63973 if( rc ){
63974 sqlite3_free(pCellKey);
63975 goto moveto_finish;
63976 }
63977 c = xRecordCompare(nCell, pCellKey, pIdxKey);
@@ -66010,11 +66139,10 @@
66010 */
66011 usableSpace = pBt->usableSize - 12 + leafCorrection;
66012 for(i=0; i<nOld; i++){
66013 MemPage *p = apOld[i];
66014 szNew[i] = usableSpace - p->nFree;
66015 if( szNew[i]<0 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
66016 for(j=0; j<p->nOverflow; j++){
66017 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
66018 }
66019 cntNew[i] = cntOld[i];
66020 }
@@ -66689,11 +66817,11 @@
66689 ** to decode the key.
66690 */
66691 SQLITE_PRIVATE int sqlite3BtreeInsert(
66692 BtCursor *pCur, /* Insert data into the table of this cursor */
66693 const BtreePayload *pX, /* Content of the row to be inserted */
66694 int appendBias, /* True if this is likely an append */
66695 int seekResult /* Result of prior MovetoUnpacked() call */
66696 ){
66697 int rc;
66698 int loc = seekResult; /* -1: before desired location +1: after */
66699 int szNew = 0;
@@ -66701,10 +66829,12 @@
66701 MemPage *pPage;
66702 Btree *p = pCur->pBtree;
66703 BtShared *pBt = p->pBt;
66704 unsigned char *oldCell;
66705 unsigned char *newCell = 0;
 
 
66706
66707 if( pCur->eState==CURSOR_FAULT ){
66708 assert( pCur->skipNext!=SQLITE_OK );
66709 return pCur->skipNext;
66710 }
@@ -66742,23 +66872,28 @@
66742 assert( pX->pKey==0 );
66743 /* If this is an insert into a table b-tree, invalidate any incrblob
66744 ** cursors open on the row being replaced */
66745 invalidateIncrblobCursors(p, pX->nKey, 0);
66746
 
 
 
 
 
66747 /* If the cursor is currently on the last row and we are appending a
66748 ** new row onto the end, set the "loc" to avoid an unnecessary
66749 ** btreeMoveto() call */
66750 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
66751 loc = 0;
66752 }else if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey>0
66753 && pCur->info.nKey==pX->nKey-1 ){
66754 loc = -1;
66755 }else if( loc==0 ){
66756 rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, appendBias, &loc);
66757 if( rc ) return rc;
66758 }
66759 }else if( loc==0 ){
66760 if( pX->nMem ){
66761 UnpackedRecord r;
66762 r.pKeyInfo = pCur->pKeyInfo;
66763 r.aMem = pX->aMem;
66764 r.nField = pX->nMem;
@@ -66765,13 +66900,13 @@
66765 r.default_rc = 0;
66766 r.errCode = 0;
66767 r.r1 = 0;
66768 r.r2 = 0;
66769 r.eqSeen = 0;
66770 rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, appendBias, &loc);
66771 }else{
66772 rc = btreeMoveto(pCur, pX->pKey, pX->nKey, appendBias, &loc);
66773 }
66774 if( rc ) return rc;
66775 }
66776 assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
66777
@@ -66855,10 +66990,24 @@
66855 ** fails. Internal data structure corruption will result otherwise.
66856 ** Also, set the cursor state to invalid. This stops saveCursorPosition()
66857 ** from trying to save the current position of the cursor. */
66858 pCur->apPage[pCur->iPage]->nOverflow = 0;
66859 pCur->eState = CURSOR_INVALID;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66860 }
66861 assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
66862
66863 end_insert:
66864 return rc;
@@ -71765,11 +71914,11 @@
71765 sqlite3VdbeGetOp(p,addr)->p2 = val;
71766 }
71767 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
71768 sqlite3VdbeGetOp(p,addr)->p3 = val;
71769 }
71770 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 p5){
71771 assert( p->nOp>0 || p->db->mallocFailed );
71772 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
71773 }
71774
71775 /*
@@ -73479,64 +73628,63 @@
73479 ** statement transaction is committed.
73480 **
73481 ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
73482 ** Otherwise SQLITE_OK.
73483 */
73484 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
73485 sqlite3 *const db = p->db;
73486 int rc = SQLITE_OK;
73487
73488 /* If p->iStatement is greater than zero, then this Vdbe opened a
73489 ** statement transaction that should be closed here. The only exception
73490 ** is that an IO error may have occurred, causing an emergency rollback.
73491 ** In this case (db->nStatement==0), and there is nothing to do.
73492 */
73493 if( db->nStatement && p->iStatement ){
73494 int i;
73495 const int iSavepoint = p->iStatement-1;
73496
73497 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
73498 assert( db->nStatement>0 );
73499 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
73500
73501 for(i=0; i<db->nDb; i++){
73502 int rc2 = SQLITE_OK;
73503 Btree *pBt = db->aDb[i].pBt;
73504 if( pBt ){
73505 if( eOp==SAVEPOINT_ROLLBACK ){
73506 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
73507 }
73508 if( rc2==SQLITE_OK ){
73509 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
73510 }
73511 if( rc==SQLITE_OK ){
73512 rc = rc2;
73513 }
73514 }
73515 }
73516 db->nStatement--;
73517 p->iStatement = 0;
73518
73519 if( rc==SQLITE_OK ){
73520 if( eOp==SAVEPOINT_ROLLBACK ){
73521 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
73522 }
73523 if( rc==SQLITE_OK ){
73524 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
73525 }
73526 }
73527
73528 /* If the statement transaction is being rolled back, also restore the
73529 ** database handles deferred constraint counter to the value it had when
73530 ** the statement transaction was opened. */
73531 if( eOp==SAVEPOINT_ROLLBACK ){
73532 db->nDeferredCons = p->nStmtDefCons;
73533 db->nDeferredImmCons = p->nStmtDefImmCons;
73534 }
73535 }
73536 return rc;
73537 }
73538
73539 /*
73540 ** This function is called when a transaction opened by the database
73541 ** handle associated with the VM passed as an argument is about to be
73542 ** committed. If there are outstanding deferred foreign key constraint
@@ -75567,14 +75715,14 @@
75567 ** structure itself, using sqlite3DbFree().
75568 **
75569 ** This function is used to free UnpackedRecord structures allocated by
75570 ** the vdbeUnpackRecord() function found in vdbeapi.c.
75571 */
75572 static void vdbeFreeUnpacked(sqlite3 *db, UnpackedRecord *p){
75573 if( p ){
75574 int i;
75575 for(i=0; i<p->nField; i++){
75576 Mem *pMem = &p->aMem[i];
75577 if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
75578 }
75579 sqlite3DbFree(db, p);
75580 }
@@ -75603,14 +75751,19 @@
75603 const char *zTbl = pTab->zName;
75604 static const u8 fakeSortOrder = 0;
75605
75606 assert( db->pPreUpdate==0 );
75607 memset(&preupdate, 0, sizeof(PreUpdate));
75608 if( op==SQLITE_UPDATE ){
75609 iKey2 = v->aMem[iReg].u.i;
 
75610 }else{
75611 iKey2 = iKey1;
 
 
 
 
75612 }
75613
75614 assert( pCsr->nField==pTab->nCol
75615 || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
75616 );
@@ -75629,12 +75782,12 @@
75629
75630 db->pPreUpdate = &preupdate;
75631 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
75632 db->pPreUpdate = 0;
75633 sqlite3DbFree(db, preupdate.aRecord);
75634 vdbeFreeUnpacked(db, preupdate.pUnpacked);
75635 vdbeFreeUnpacked(db, preupdate.pNewUnpacked);
75636 if( preupdate.aNew ){
75637 int i;
75638 for(i=0; i<pCsr->nField; i++){
75639 sqlite3VdbeMemRelease(&preupdate.aNew[i]);
75640 }
@@ -77305,18 +77458,22 @@
77305 ** This function is called from within a pre-update callback to retrieve
77306 ** a field of the row currently being updated or deleted.
77307 */
77308 SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
77309 PreUpdate *p = db->pPreUpdate;
 
77310 int rc = SQLITE_OK;
77311
77312 /* Test that this call is being made from within an SQLITE_DELETE or
77313 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
77314 if( !p || p->op==SQLITE_INSERT ){
77315 rc = SQLITE_MISUSE_BKPT;
77316 goto preupdate_old_out;
77317 }
 
 
 
77318 if( iIdx>=p->pCsr->nField || iIdx<0 ){
77319 rc = SQLITE_RANGE;
77320 goto preupdate_old_out;
77321 }
77322
@@ -77338,21 +77495,18 @@
77338 goto preupdate_old_out;
77339 }
77340 p->aRecord = aRec;
77341 }
77342
77343 if( iIdx>=p->pUnpacked->nField ){
 
 
 
77344 *ppValue = (sqlite3_value *)columnNullValue();
77345 }else{
77346 Mem *pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
77347 *ppValue = &p->pUnpacked->aMem[iIdx];
77348 if( iIdx==p->pTab->iPKey ){
77349 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
77350 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
77351 if( pMem->flags & MEM_Int ){
77352 sqlite3VdbeMemRealify(pMem);
77353 }
77354 }
77355 }
77356
77357 preupdate_old_out:
77358 sqlite3Error(db, rc);
@@ -77401,10 +77555,13 @@
77401
77402 if( !p || p->op==SQLITE_DELETE ){
77403 rc = SQLITE_MISUSE_BKPT;
77404 goto preupdate_new_out;
77405 }
 
 
 
77406 if( iIdx>=p->pCsr->nField || iIdx<0 ){
77407 rc = SQLITE_RANGE;
77408 goto preupdate_new_out;
77409 }
77410
@@ -77421,17 +77578,15 @@
77421 rc = SQLITE_NOMEM;
77422 goto preupdate_new_out;
77423 }
77424 p->pNewUnpacked = pUnpack;
77425 }
77426 if( iIdx>=pUnpack->nField ){
 
 
 
77427 pMem = (sqlite3_value *)columnNullValue();
77428 }else{
77429 pMem = &pUnpack->aMem[iIdx];
77430 if( iIdx==p->pTab->iPKey ){
77431 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
77432 }
77433 }
77434 }else{
77435 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
77436 ** value. Make a copy of the cell contents and return a pointer to it.
77437 ** It is not safe to return a pointer to the memory cell itself as the
@@ -78404,12 +78559,10 @@
78404 Mem *aMem = p->aMem; /* Copy of p->aMem */
78405 Mem *pIn1 = 0; /* 1st input operand */
78406 Mem *pIn2 = 0; /* 2nd input operand */
78407 Mem *pIn3 = 0; /* 3rd input operand */
78408 Mem *pOut = 0; /* Output operand */
78409 int *aPermute = 0; /* Permutation of columns for OP_Compare */
78410 i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */
78411 #ifdef VDBE_PROFILE
78412 u64 start; /* CPU clock count at start of opcode */
78413 #endif
78414 /*** INSERT STACK UNION HERE ***/
78415
@@ -78420,11 +78573,10 @@
78420 ** sqlite3_column_text16() failed. */
78421 goto no_mem;
78422 }
78423 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
78424 assert( p->bIsReader || p->readOnly!=0 );
78425 p->rc = SQLITE_OK;
78426 p->iCurrentTime = 0;
78427 assert( p->explain==0 );
78428 p->pResultSet = 0;
78429 db->busyHandler.nBusy = 0;
78430 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
@@ -78781,11 +78933,10 @@
78781 pFrame = p->pFrame;
78782 p->pFrame = pFrame->pParent;
78783 p->nFrame--;
78784 sqlite3VdbeSetChanges(db, p->nChange);
78785 pcx = sqlite3VdbeFrameRestore(pFrame);
78786 lastRowid = db->lastRowid;
78787 if( pOp->p2==OE_Ignore ){
78788 /* Instruction pcx is the OP_Program that invoked the sub-program
78789 ** currently being halted. If the p2 instruction of this OP_Halt
78790 ** instruction is set to OE_Ignore, then the sub-program is throwing
78791 ** an IGNORE exception. In this case jump to the address specified
@@ -79016,11 +79167,11 @@
79016 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
79017 pVar = &p->aVar[pOp->p1 - 1];
79018 if( sqlite3VdbeMemTooBig(pVar) ){
79019 goto too_big;
79020 }
79021 pOut = out2Prerelease(p, pOp);
79022 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
79023 UPDATE_MAX_BLOBSIZE(pOut);
79024 break;
79025 }
79026
@@ -79503,13 +79654,11 @@
79503 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
79504 }
79505 #endif
79506 MemSetTypeFlag(pCtx->pOut, MEM_Null);
79507 pCtx->fErrorOrAux = 0;
79508 db->lastRowid = lastRowid;
79509 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
79510 lastRowid = db->lastRowid; /* Remember rowid changes made by xSFunc */
79511
79512 /* If the function returned an error, throw an exception */
79513 if( pCtx->fErrorOrAux ){
79514 if( pCtx->isError ){
79515 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
@@ -79961,12 +80110,12 @@
79961 }
79962
79963
79964 /* Opcode: Permutation * * * P4 *
79965 **
79966 ** Set the permutation used by the OP_Compare operator to be the array
79967 ** of integers in P4.
79968 **
79969 ** The permutation is only valid until the next OP_Compare that has
79970 ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
79971 ** occur immediately prior to the OP_Compare.
79972 **
@@ -79974,11 +80123,12 @@
79974 ** and does not become part of the permutation.
79975 */
79976 case OP_Permutation: {
79977 assert( pOp->p4type==P4_INTARRAY );
79978 assert( pOp->p4.ai );
79979 aPermute = pOp->p4.ai + 1;
 
79980 break;
79981 }
79982
79983 /* Opcode: Compare P1 P2 P3 P4 P5
79984 ** Synopsis: r[P1@P3] <-> r[P2@P3]
@@ -80007,12 +80157,21 @@
80007 int p2;
80008 const KeyInfo *pKeyInfo;
80009 int idx;
80010 CollSeq *pColl; /* Collating sequence to use on this term */
80011 int bRev; /* True for DESCENDING sort order */
 
80012
80013 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
 
 
 
 
 
 
 
 
80014 n = pOp->p3;
80015 pKeyInfo = pOp->p4.pKeyInfo;
80016 assert( n>0 );
80017 assert( pKeyInfo!=0 );
80018 p1 = pOp->p1;
@@ -80041,11 +80200,10 @@
80041 if( iCompare ){
80042 if( bRev ) iCompare = -iCompare;
80043 break;
80044 }
80045 }
80046 aPermute = 0;
80047 break;
80048 }
80049
80050 /* Opcode: Jump P1 P2 P3 * *
80051 **
@@ -80597,10 +80755,24 @@
80597 do{
80598 applyAffinity(pRec++, *(zAffinity++), encoding);
80599 assert( zAffinity[0]==0 || pRec<=pLast );
80600 }while( zAffinity[0] );
80601 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80602
80603 /* Loop through the elements that will make up the record to figure
80604 ** out how much space is required for the new record.
80605 */
80606 pRec = pLast;
@@ -82187,11 +82359,11 @@
82187 assert( memIsValid(pData) );
82188 pC = p->apCsr[pOp->p1];
82189 assert( pC!=0 );
82190 assert( pC->eCurType==CURTYPE_BTREE );
82191 assert( pC->uc.pCursor!=0 );
82192 assert( pC->isTable );
82193 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
82194 REGISTER_TRACE(pOp->p2, pData);
82195
82196 if( pOp->opcode==OP_Insert ){
82197 pKey = &aMem[pOp->p3];
@@ -82203,18 +82375,17 @@
82203 assert( pOp->opcode==OP_InsertInt );
82204 x.nKey = pOp->p3;
82205 }
82206
82207 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
82208 assert( pC->isTable );
82209 assert( pC->iDb>=0 );
82210 zDb = db->aDb[pC->iDb].zDbSName;
82211 pTab = pOp->p4.pTab;
82212 assert( HasRowid(pTab) );
82213 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
82214 }else{
82215 pTab = 0; /* Not needed. Silence a comiler warning. */
82216 zDb = 0; /* Not needed. Silence a compiler warning. */
82217 }
82218
82219 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
82220 /* Invoke the pre-update hook, if any */
@@ -82222,14 +82393,15 @@
82222 && pOp->p4type==P4_TABLE
82223 && !(pOp->p5 & OPFLAG_ISUPDATE)
82224 ){
82225 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey, pOp->p2);
82226 }
 
82227 #endif
82228
82229 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
82230 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = x.nKey;
82231 if( pData->flags & MEM_Null ){
82232 x.pData = 0;
82233 x.nData = 0;
82234 }else{
82235 assert( pData->flags & (MEM_Blob|MEM_Str) );
@@ -82242,11 +82414,11 @@
82242 }else{
82243 x.nZero = 0;
82244 }
82245 x.pKey = 0;
82246 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
82247 (pOp->p5 & OPFLAG_APPEND)!=0, seekResult
82248 );
82249 pC->deferredMoveto = 0;
82250 pC->cacheStatus = CACHE_STALE;
82251
82252 /* Invoke the update-hook if required. */
@@ -82334,12 +82506,15 @@
82334 pTab = 0; /* Not needed. Silence a compiler warning. */
82335 }
82336
82337 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
82338 /* Invoke the pre-update-hook if required. */
82339 if( db->xPreUpdateCallback && pOp->p4.pTab && HasRowid(pTab) ){
82340 assert( !(opflags & OPFLAG_ISUPDATE) || (aMem[pOp->p3].flags & MEM_Int) );
 
 
 
82341 sqlite3VdbePreUpdateHook(p, pC,
82342 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
82343 zDb, pTab, pC->movetoTarget,
82344 pOp->p3
82345 );
@@ -82453,11 +82628,11 @@
82453 if( rc ) goto abort_due_to_error;
82454 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
82455 break;
82456 }
82457
82458 /* Opcode: RowData P1 P2 * * *
82459 ** Synopsis: r[P2]=data
82460 **
82461 ** Write into register P2 the complete row content for the row at
82462 ** which cursor P1 is currently pointing.
82463 ** There is no interpretation of the data.
@@ -82467,18 +82642,30 @@
82467 ** If cursor P1 is an index, then the content is the key of the row.
82468 ** If cursor P2 is a table, then the content extracted is the data.
82469 **
82470 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
82471 ** of a real table, not a pseudo-table.
 
 
 
 
 
 
 
 
 
 
 
 
 
82472 */
82473 case OP_RowData: {
82474 VdbeCursor *pC;
82475 BtCursor *pCrsr;
82476 u32 n;
82477
82478 pOut = &aMem[pOp->p2];
82479 memAboutToChange(p, pOut);
82480
82481 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
82482 pC = p->apCsr[pOp->p1];
82483 assert( pC!=0 );
82484 assert( pC->eCurType==CURTYPE_BTREE );
@@ -82505,18 +82692,13 @@
82505 n = sqlite3BtreePayloadSize(pCrsr);
82506 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
82507 goto too_big;
82508 }
82509 testcase( n==0 );
82510 if( sqlite3VdbeMemClearAndResize(pOut, MAX(n,32)) ){
82511 goto no_mem;
82512 }
82513 pOut->n = n;
82514 MemSetTypeFlag(pOut, MEM_Blob);
82515 rc = sqlite3BtreePayload(pCrsr, 0, n, pOut->z);
82516 if( rc ) goto abort_due_to_error;
82517 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
82518 UPDATE_MAX_BLOBSIZE(pOut);
82519 REGISTER_TRACE(pOp->p2, pOut);
82520 break;
82521 }
82522
@@ -82900,11 +83082,11 @@
82900 x.nKey = pIn2->n;
82901 x.pKey = pIn2->z;
82902 x.aMem = aMem + pOp->p3;
82903 x.nMem = (u16)pOp->p4.i;
82904 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
82905 (pOp->p5 & OPFLAG_APPEND)!=0,
82906 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
82907 );
82908 assert( pC->deferredMoveto==0 );
82909 pC->cacheStatus = CACHE_STALE;
82910 }
@@ -83022,11 +83204,10 @@
83022 pTabCur->aAltMap = pOp->p4.ai;
83023 pTabCur->pAltCursor = pC;
83024 }else{
83025 pOut = out2Prerelease(p, pOp);
83026 pOut->u.i = rowid;
83027 pOut->flags = MEM_Int;
83028 }
83029 }else{
83030 assert( pOp->opcode==OP_IdxRowid );
83031 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
83032 }
@@ -83664,11 +83845,11 @@
83664 assert( (int)(pOp - aOp)==pFrame->pc );
83665 }
83666
83667 p->nFrame++;
83668 pFrame->pParent = p->pFrame;
83669 pFrame->lastRowid = lastRowid;
83670 pFrame->nChange = p->nChange;
83671 pFrame->nDbChange = p->db->nChange;
83672 assert( pFrame->pAuxData==0 );
83673 pFrame->pAuxData = p->pAuxData;
83674 p->pAuxData = 0;
@@ -84605,11 +84786,11 @@
84605 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
84606 db->vtabOnConflict = vtabOnConflict;
84607 sqlite3VtabImportErrmsg(p, pVtab);
84608 if( rc==SQLITE_OK && pOp->p1 ){
84609 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
84610 db->lastRowid = lastRowid = rowid;
84611 }
84612 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
84613 if( pOp->p5==OE_Ignore ){
84614 rc = SQLITE_OK;
84615 }else{
@@ -84841,11 +85022,10 @@
84841
84842 /* This is the only way out of this procedure. We have to
84843 ** release the mutexes on btrees that were acquired at the
84844 ** top. */
84845 vdbe_return:
84846 db->lastRowid = lastRowid;
84847 testcase( nVmStep>0 );
84848 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
84849 sqlite3VdbeLeave(p);
84850 assert( rc!=SQLITE_OK || nExtraDelete==0
84851 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
@@ -84905,14 +85085,13 @@
84905 /*
84906 ** Valid sqlite3_blob* handles point to Incrblob structures.
84907 */
84908 typedef struct Incrblob Incrblob;
84909 struct Incrblob {
84910 int flags; /* Copy of "flags" passed to sqlite3_blob_open() */
84911 int nByte; /* Size of open blob, in bytes */
84912 int iOffset; /* Byte offset of blob in cursor data */
84913 int iCol; /* Table column this handle is open on */
84914 BtCursor *pCsr; /* Cursor pointing at blob row */
84915 sqlite3_stmt *pStmt; /* Statement holding cursor open */
84916 sqlite3 *db; /* The associated database */
84917 char *zDb; /* Database name */
84918 Table *pTab; /* Table object */
@@ -84939,21 +85118,31 @@
84939 static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
84940 int rc; /* Error code */
84941 char *zErr = 0; /* Error message */
84942 Vdbe *v = (Vdbe *)p->pStmt;
84943
84944 /* Set the value of the SQL statements only variable to integer iRow.
84945 ** This is done directly instead of using sqlite3_bind_int64() to avoid
84946 ** triggering asserts related to mutexes.
84947 */
84948 assert( v->aVar[0].flags&MEM_Int );
84949 v->aVar[0].u.i = iRow;
84950
84951 rc = sqlite3_step(p->pStmt);
 
 
 
 
 
 
 
 
 
84952 if( rc==SQLITE_ROW ){
84953 VdbeCursor *pC = v->apCsr[0];
84954 u32 type = pC->aType[p->iCol];
 
 
84955 if( type<12 ){
84956 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
84957 type==0?"null": type==7?"real": "integer"
84958 );
84959 rc = SQLITE_ERROR;
@@ -84994,11 +85183,11 @@
84994 sqlite3* db, /* The database connection */
84995 const char *zDb, /* The attached database containing the blob */
84996 const char *zTable, /* The table containing the blob */
84997 const char *zColumn, /* The column containing the blob */
84998 sqlite_int64 iRow, /* The row containing the glob */
84999 int flags, /* True -> read/write access, false -> read-only */
85000 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
85001 ){
85002 int nAttempt = 0;
85003 int iCol; /* Index of zColumn in row-record */
85004 int rc = SQLITE_OK;
@@ -85016,11 +85205,11 @@
85016 #ifdef SQLITE_ENABLE_API_ARMOR
85017 if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
85018 return SQLITE_MISUSE_BKPT;
85019 }
85020 #endif
85021 flags = !!flags; /* flags = (flags ? 1 : 0); */
85022
85023 sqlite3_mutex_enter(db->mutex);
85024
85025 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
85026 if( !pBlob ) goto blob_open_out;
@@ -85076,13 +85265,12 @@
85076 goto blob_open_out;
85077 }
85078
85079 /* If the value is being opened for writing, check that the
85080 ** column is not indexed, and that it is not part of a foreign key.
85081 ** It is against the rules to open a column to which either of these
85082 ** descriptions applies for writing. */
85083 if( flags ){
85084 const char *zFault = 0;
85085 Index *pIdx;
85086 #ifndef SQLITE_OMIT_FOREIGN_KEY
85087 if( db->flags&SQLITE_ForeignKeys ){
85088 /* Check that the column is not part of an FK child key definition. It
@@ -85139,22 +85327,21 @@
85139 */
85140 static const int iLn = VDBE_OFFSET_LINENO(2);
85141 static const VdbeOpList openBlob[] = {
85142 {OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */
85143 {OP_OpenRead, 0, 0, 0}, /* 1: Open a cursor */
85144 {OP_Variable, 1, 1, 0}, /* 2: Move ?1 into reg[1] */
85145 {OP_NotExists, 0, 7, 1}, /* 3: Seek the cursor */
85146 {OP_Column, 0, 0, 1}, /* 4 */
85147 {OP_ResultRow, 1, 0, 0}, /* 5 */
85148 {OP_Goto, 0, 2, 0}, /* 6 */
85149 {OP_Halt, 0, 0, 0}, /* 7 */
85150 };
85151 Vdbe *v = (Vdbe *)pBlob->pStmt;
85152 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
85153 VdbeOp *aOp;
85154
85155 sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, flags,
85156 pTab->pSchema->schema_cookie,
85157 pTab->pSchema->iGeneration);
85158 sqlite3VdbeChangeP5(v, 1);
85159 aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
85160
@@ -85167,19 +85354,19 @@
85167 #ifdef SQLITE_OMIT_SHARED_CACHE
85168 aOp[0].opcode = OP_Noop;
85169 #else
85170 aOp[0].p1 = iDb;
85171 aOp[0].p2 = pTab->tnum;
85172 aOp[0].p3 = flags;
85173 sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
85174 }
85175 if( db->mallocFailed==0 ){
85176 #endif
85177
85178 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
85179 ** parameter of the other to pTab->tnum. */
85180 if( flags ) aOp[1].opcode = OP_OpenWrite;
85181 aOp[1].p2 = pTab->tnum;
85182 aOp[1].p3 = iDb;
85183
85184 /* Configure the number of columns. Configure the cursor to
85185 ** think that the table has one more column than it really
@@ -85188,27 +85375,25 @@
85188 ** we can invoke OP_Column to fill in the vdbe cursors type
85189 ** and offset cache without causing any IO.
85190 */
85191 aOp[1].p4type = P4_INT32;
85192 aOp[1].p4.i = pTab->nCol+1;
85193 aOp[4].p2 = pTab->nCol;
85194
85195 pParse->nVar = 1;
85196 pParse->nMem = 1;
85197 pParse->nTab = 1;
85198 sqlite3VdbeMakeReady(v, pParse);
85199 }
85200 }
85201
85202 pBlob->flags = flags;
85203 pBlob->iCol = iCol;
85204 pBlob->db = db;
85205 sqlite3BtreeLeaveAll(db);
85206 if( db->mallocFailed ){
85207 goto blob_open_out;
85208 }
85209 sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
85210 rc = blobSeekToRow(pBlob, iRow, &zErr);
85211 } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
85212
85213 blob_open_out:
85214 if( rc==SQLITE_OK && db->mallocFailed==0 ){
@@ -88741,12 +88926,10 @@
88741 ** This file contains routines used for walking the parser tree and
88742 ** resolve all identifiers by associating them with a particular
88743 ** table and column.
88744 */
88745 /* #include "sqliteInt.h" */
88746 /* #include <stdlib.h> */
88747 /* #include <string.h> */
88748
88749 /*
88750 ** Walk the expression tree pExpr and increase the aggregate function
88751 ** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
88752 ** This needs to occur when copying a TK_AGG_FUNCTION node from an
@@ -91237,21 +91420,27 @@
91237 int doAdd = 0;
91238 if( z[0]=='?' ){
91239 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
91240 ** use it as the variable number */
91241 i64 i;
91242 int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
91243 x = (ynVar)i;
 
 
 
 
 
91244 testcase( i==0 );
91245 testcase( i==1 );
91246 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
91247 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
91248 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
91249 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
91250 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
91251 return;
91252 }
 
91253 if( x>pParse->nVar ){
91254 pParse->nVar = (int)x;
91255 doAdd = 1;
91256 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
91257 doAdd = 1;
@@ -91682,37 +91871,45 @@
91682 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
91683 pNewItem->idx = pOldItem->idx;
91684 }
91685 return pNew;
91686 }
91687 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
91688 Select *pNew, *pPrior;
 
 
 
 
91689 assert( db!=0 );
91690 if( p==0 ) return 0;
91691 pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
91692 if( pNew==0 ) return 0;
91693 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
91694 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
91695 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
91696 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
91697 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
91698 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
91699 pNew->op = p->op;
91700 pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags);
91701 if( pPrior ) pPrior->pNext = pNew;
91702 pNew->pNext = 0;
91703 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
91704 pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
91705 pNew->iLimit = 0;
91706 pNew->iOffset = 0;
91707 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
91708 pNew->addrOpenEphm[0] = -1;
91709 pNew->addrOpenEphm[1] = -1;
91710 pNew->nSelectRow = p->nSelectRow;
91711 pNew->pWith = withDup(db, p->pWith);
91712 sqlite3SelectSetName(pNew, p->zSelName);
91713 return pNew;
 
 
 
 
91714 }
91715 #else
91716 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
91717 assert( p==0 );
91718 return 0;
@@ -91773,11 +91970,11 @@
91773 **
91774 ** (a,b,c) = (expr1,expr2,expr3)
91775 ** Or: (a,b,c) = (SELECT x,y,z FROM ....)
91776 **
91777 ** For each term of the vector assignment, append new entries to the
91778 ** expression list pList. In the case of a subquery on the LHS, append
91779 ** TK_SELECT_COLUMN expressions.
91780 */
91781 SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(
91782 Parse *pParse, /* Parsing context */
91783 ExprList *pList, /* List to which to append. Might be NULL */
@@ -93882,10 +94079,15 @@
93882 int i; /* Loop counter */
93883 sqlite3 *db = pParse->db; /* The database connection */
93884 u8 enc = ENC(db); /* The text encoding used by this database */
93885 CollSeq *pColl = 0; /* A collating sequence */
93886
 
 
 
 
 
93887 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
93888 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
93889 pFarg = 0;
93890 }else{
93891 pFarg = pExpr->x.pList;
@@ -93929,10 +94131,26 @@
93929 */
93930 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
93931 assert( nFarg>=1 );
93932 return sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
93933 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93934
93935 for(i=0; i<nFarg; i++){
93936 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
93937 testcase( i==31 );
93938 constMask |= MASKBIT32(i);
@@ -94246,28 +94464,44 @@
94246 return inReg;
94247 }
94248
94249 /*
94250 ** Factor out the code of the given expression to initialization time.
 
 
 
 
 
 
94251 */
94252 SQLITE_PRIVATE void sqlite3ExprCodeAtInit(
94253 Parse *pParse, /* Parsing context */
94254 Expr *pExpr, /* The expression to code when the VDBE initializes */
94255 int regDest, /* Store the value in this register */
94256 u8 reusable /* True if this expression is reusable */
94257 ){
94258 ExprList *p;
94259 assert( ConstFactorOk(pParse) );
94260 p = pParse->pConstExpr;
 
 
 
 
 
 
 
 
 
94261 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
94262 p = sqlite3ExprListAppend(pParse, p, pExpr);
94263 if( p ){
94264 struct ExprList_item *pItem = &p->a[p->nExpr-1];
 
 
94265 pItem->u.iConstExprReg = regDest;
94266 pItem->reusable = reusable;
94267 }
94268 pParse->pConstExpr = p;
 
94269 }
94270
94271 /*
94272 ** Generate code to evaluate an expression and store the results
94273 ** into a register. Return the register number where the results
@@ -94286,23 +94520,12 @@
94286 pExpr = sqlite3ExprSkipCollate(pExpr);
94287 if( ConstFactorOk(pParse)
94288 && pExpr->op!=TK_REGISTER
94289 && sqlite3ExprIsConstantNotJoin(pExpr)
94290 ){
94291 ExprList *p = pParse->pConstExpr;
94292 int i;
94293 *pReg = 0;
94294 if( p ){
94295 struct ExprList_item *pItem;
94296 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
94297 if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){
94298 return pItem->u.iConstExprReg;
94299 }
94300 }
94301 }
94302 r2 = ++pParse->nMem;
94303 sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1);
94304 }else{
94305 int r1 = sqlite3GetTempReg(pParse);
94306 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
94307 if( r2==r1 ){
94308 *pReg = r1;
@@ -94352,11 +94575,11 @@
94352 ** in register target. If the expression is constant, then this routine
94353 ** might choose to code the expression at initialization time.
94354 */
94355 SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
94356 if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
94357 sqlite3ExprCodeAtInit(pParse, pExpr, target, 0);
94358 }else{
94359 sqlite3ExprCode(pParse, pExpr, target);
94360 }
94361 }
94362
@@ -94424,11 +94647,11 @@
94424 n--;
94425 }else{
94426 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
94427 }
94428 }else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){
94429 sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0);
94430 }else{
94431 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
94432 if( inReg!=target+i ){
94433 VdbeOp *pOp;
94434 if( copyOp==OP_Copy
@@ -96968,10 +97191,16 @@
96968 ** used to query statistical information that has been gathered into
96969 ** the Stat4Accum object by prior calls to stat_push(). The P parameter
96970 ** has type BLOB but it is really just a pointer to the Stat4Accum object.
96971 ** The content to returned is determined by the parameter J
96972 ** which is one of the STAT_GET_xxxx values defined above.
 
 
 
 
 
 
96973 **
96974 ** If neither STAT3 nor STAT4 are enabled, then J is always
96975 ** STAT_GET_STAT1 and is hence omitted and this routine becomes
96976 ** a one-parameter function, stat_get(P), that always returns the
96977 ** stat1 table entry information.
@@ -97787,11 +98016,11 @@
97787 sumEq += aSample[i].anEq[iCol];
97788 nSum100 += 100;
97789 }
97790 }
97791
97792 if( nDist100>nSum100 ){
97793 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
97794 }
97795 if( avgEq==0 ) avgEq = 1;
97796 pIdx->aAvgEq[iCol] = avgEq;
97797 }
@@ -98201,10 +98430,11 @@
98201 assert( pVfs );
98202 flags |= SQLITE_OPEN_MAIN_DB;
98203 rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags);
98204 sqlite3_free( zPath );
98205 db->nDb++;
 
98206 if( rc==SQLITE_CONSTRAINT ){
98207 rc = SQLITE_ERROR;
98208 zErrDyn = sqlite3MPrintf(db, "database is already attached");
98209 }else if( rc==SQLITE_OK ){
98210 Pager *pPager;
@@ -104365,16 +104595,12 @@
104365 }
104366 }else
104367 #endif
104368 {
104369 int count = (pParse->nested==0); /* True to count changes */
104370 int iIdxNoSeek = -1;
104371 if( bComplex==0 && aiCurOnePass[1]!=iDataCur ){
104372 iIdxNoSeek = aiCurOnePass[1];
104373 }
104374 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
104375 iKey, nKey, count, OE_Default, eOnePass, iIdxNoSeek);
104376 }
104377
104378 /* End of the loop over all rowids/primary-keys. */
104379 if( eOnePass!=ONEPASS_OFF ){
104380 sqlite3VdbeResolveLabel(v, addrBypass);
@@ -104450,19 +104676,21 @@
104450 ** then this function must seek iDataCur to the entry identified by iPk
104451 ** and nPk before reading from it.
104452 **
104453 ** If eMode is ONEPASS_MULTI, then this call is being made as part
104454 ** of a ONEPASS delete that affects multiple rows. In this case, if
104455 ** iIdxNoSeek is a valid cursor number (>=0), then its position should
104456 ** be preserved following the delete operation. Or, if iIdxNoSeek is not
104457 ** a valid cursor number, the position of iDataCur should be preserved
104458 ** instead.
104459 **
104460 ** iIdxNoSeek:
104461 ** If iIdxNoSeek is a valid cursor number (>=0), then it identifies an
104462 ** index cursor (from within array of cursors starting at iIdxCur) that
104463 ** already points to the index entry to be deleted.
 
 
104464 */
104465 SQLITE_PRIVATE void sqlite3GenerateRowDelete(
104466 Parse *pParse, /* Parsing context */
104467 Table *pTab, /* Table containing the row to be deleted */
104468 Trigger *pTrigger, /* List of triggers to (potentially) fire */
@@ -104529,17 +104757,22 @@
104529 TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
104530 );
104531
104532 /* If any BEFORE triggers were coded, then seek the cursor to the
104533 ** row to be deleted again. It may be that the BEFORE triggers moved
104534 ** the cursor or of already deleted the row that the cursor was
104535 ** pointing to.
 
 
 
104536 */
104537 if( addrStart<sqlite3VdbeCurrentAddr(v) ){
104538 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
104539 VdbeCoverageIf(v, opSeek==OP_NotExists);
104540 VdbeCoverageIf(v, opSeek==OP_NotFound);
 
 
104541 }
104542
104543 /* Do FK processing. This call checks that any FK constraints that
104544 ** refer to this table (i.e. constraints attached to other tables)
104545 ** are not violated by deleting this row. */
@@ -104558,15 +104791,17 @@
104558 */
104559 if( pTab->pSelect==0 ){
104560 u8 p5 = 0;
104561 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
104562 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
104563 sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
 
 
104564 if( eMode!=ONEPASS_OFF ){
104565 sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE);
104566 }
104567 if( iIdxNoSeek>=0 ){
104568 sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
104569 }
104570 if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION;
104571 sqlite3VdbeChangeP5(v, p5);
104572 }
@@ -104716,10 +104951,14 @@
104716 ** opcode if it is present */
104717 sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
104718 }
104719 if( regOut ){
104720 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
 
 
 
 
104721 }
104722 sqlite3ReleaseTempRange(pParse, regBase, nCol);
104723 return regBase;
104724 }
104725
@@ -106512,10 +106751,13 @@
106512 DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ),
106513 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
106514 FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
106515 FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
106516 FUNCTION2(likely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
 
 
 
106517 FUNCTION(ltrim, 1, 1, 0, trimFunc ),
106518 FUNCTION(ltrim, 2, 1, 0, trimFunc ),
106519 FUNCTION(rtrim, 1, 2, 0, trimFunc ),
106520 FUNCTION(rtrim, 2, 2, 0, trimFunc ),
106521 FUNCTION(trim, 1, 3, 0, trimFunc ),
@@ -109667,11 +109909,11 @@
109667 if( db->flags&SQLITE_RecTriggers ){
109668 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
109669 }
109670 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
109671 regR, nPkField, 0, OE_Replace,
109672 (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), -1);
109673 seenReplace = 1;
109674 break;
109675 }
109676 }
109677 sqlite3VdbeResolveLabel(v, addrUniqueOk);
@@ -109683,10 +109925,29 @@
109683 }
109684
109685 *pbMayReplace = seenReplace;
109686 VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
109687 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109688
109689 /*
109690 ** This routine generates code to finish the INSERT or UPDATE operation
109691 ** that was started by a prior call to sqlite3GenerateConstraintChecks.
109692 ** A consecutive range of registers starting at regNewData contains the
@@ -109700,11 +109961,11 @@
109700 Table *pTab, /* the table into which we are inserting */
109701 int iDataCur, /* Cursor of the canonical data source */
109702 int iIdxCur, /* First index cursor */
109703 int regNewData, /* Range of content */
109704 int *aRegIdx, /* Register used by each index. 0 for unused indices */
109705 int isUpdate, /* True for UPDATE, False for INSERT */
109706 int appendBias, /* True if this is likely to be an append */
109707 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
109708 ){
109709 Vdbe *v; /* Prepared statements under construction */
109710 Index *pIdx; /* An index being inserted or updated */
@@ -109711,10 +109972,15 @@
109711 u8 pik_flags; /* flag values passed to the btree insert */
109712 int regData; /* Content registers (after the rowid) */
109713 int regRec; /* Register holding assembled record for the table */
109714 int i; /* Loop counter */
109715 u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */
 
 
 
 
 
109716
109717 v = sqlite3GetVdbe(pParse);
109718 assert( v!=0 );
109719 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
109720 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
@@ -109722,34 +109988,43 @@
109722 bAffinityDone = 1;
109723 if( pIdx->pPartIdxWhere ){
109724 sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
109725 VdbeCoverage(v);
109726 }
109727 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
109728 aRegIdx[i]+1,
109729 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
109730 pik_flags = 0;
109731 if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT;
109732 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
109733 assert( pParse->nested==0 );
109734 pik_flags |= OPFLAG_NCHANGE;
 
 
 
 
 
 
 
 
 
109735 }
 
 
 
109736 sqlite3VdbeChangeP5(v, pik_flags);
109737 }
109738 if( !HasRowid(pTab) ) return;
109739 regData = regNewData + 1;
109740 regRec = sqlite3GetTempReg(pParse);
109741 sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
 
109742 if( !bAffinityDone ){
109743 sqlite3TableAffinity(v, pTab, 0);
109744 sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
109745 }
109746 if( pParse->nested ){
109747 pik_flags = 0;
109748 }else{
109749 pik_flags = OPFLAG_NCHANGE;
109750 pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
109751 }
109752 if( appendBias ){
109753 pik_flags |= OPFLAG_APPEND;
109754 }
109755 if( useSeekResult ){
@@ -110154,11 +110429,11 @@
110154 addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
110155 }else{
110156 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
110157 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
110158 }
110159 sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
110160 if( db->flags & SQLITE_Vacuum ){
110161 sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
110162 insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|
110163 OPFLAG_APPEND|OPFLAG_USESEEKRESULT;
110164 }else{
@@ -110186,11 +110461,11 @@
110186 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
110187 sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
110188 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
110189 VdbeComment((v, "%s", pDestIdx->zName));
110190 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
110191 sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
110192 if( db->flags & SQLITE_Vacuum ){
110193 /* This INSERT command is part of a VACUUM operation, which guarantees
110194 ** that the destination table is empty. If all indexed columns use
110195 ** collation sequence BINARY, then it can also be assumed that the
110196 ** index will be populated by inserting keys in strictly sorted
@@ -110971,11 +111246,10 @@
110971 #endif /* SQLITE3EXT_H */
110972
110973 /************** End of sqlite3ext.h ******************************************/
110974 /************** Continuing where we left off in loadext.c ********************/
110975 /* #include "sqliteInt.h" */
110976 /* #include <string.h> */
110977
110978 #ifndef SQLITE_OMIT_LOAD_EXTENSION
110979 /*
110980 ** Some API routines are omitted when various features are
110981 ** excluded from a build of SQLite. Substitute a NULL pointer
@@ -112635,11 +112909,11 @@
112635
112636 /*
112637 ** Locate a pragma in the aPragmaName[] array.
112638 */
112639 static const PragmaName *pragmaLocate(const char *zName){
112640 int upr, lwr, mid, rc;
112641 lwr = 0;
112642 upr = ArraySize(aPragmaName)-1;
112643 while( lwr<=upr ){
112644 mid = (lwr+upr)/2;
112645 rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
@@ -116149,10 +116423,11 @@
116149 v = pParse->pVdbe;
116150 r1 = sqlite3GetTempReg(pParse);
116151 sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
116152 sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
116153 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, iMem, N);
 
116154 sqlite3ReleaseTempReg(pParse, r1);
116155 }
116156
116157 /*
116158 ** This routine generates the code for the inside of the inner loop
@@ -119677,11 +119952,19 @@
119677 assert( pTab->nTabRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nTabRef==2 ));
119678
119679 pCte->zCteErr = "circular reference: %s";
119680 pSavedWith = pParse->pWith;
119681 pParse->pWith = pWith;
119682 sqlite3WalkSelect(pWalker, bMayRecursive ? pSel->pPrior : pSel);
 
 
 
 
 
 
 
 
119683 pParse->pWith = pWith;
119684
119685 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
119686 pEList = pLeft->pEList;
119687 if( pCte->pCols ){
@@ -119721,14 +120004,16 @@
119721 ** sqlite3SelectExpand() when walking a SELECT tree to resolve table
119722 ** names and other FROM clause elements.
119723 */
119724 static void selectPopWith(Walker *pWalker, Select *p){
119725 Parse *pParse = pWalker->pParse;
119726 With *pWith = findRightmost(p)->pWith;
119727 if( pWith!=0 ){
119728 assert( pParse->pWith==pWith );
119729 pParse->pWith = pWith->pOuter;
 
 
119730 }
119731 }
119732 #else
119733 #define selectPopWith 0
119734 #endif
@@ -119774,12 +120059,12 @@
119774 if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
119775 return WRC_Prune;
119776 }
119777 pTabList = p->pSrc;
119778 pEList = p->pEList;
119779 if( pWalker->xSelectCallback2==selectPopWith ){
119780 sqlite3WithPush(pParse, findRightmost(p)->pWith, 0);
119781 }
119782
119783 /* Make sure cursor numbers have been assigned to all entries in
119784 ** the FROM clause of the SELECT statement.
119785 */
@@ -120062,13 +120347,11 @@
120062 if( pParse->hasCompound ){
120063 w.xSelectCallback = convertCompoundSelectToSubquery;
120064 sqlite3WalkSelect(&w, pSelect);
120065 }
120066 w.xSelectCallback = selectExpander;
120067 if( (pSelect->selFlags & SF_MultiValue)==0 ){
120068 w.xSelectCallback2 = selectPopWith;
120069 }
120070 sqlite3WalkSelect(&w, pSelect);
120071 }
120072
120073
120074 #ifndef SQLITE_OMIT_SUBQUERY
@@ -121143,11 +121426,11 @@
121143 /* This case runs if the aggregate has no GROUP BY clause. The
121144 ** processing is much simpler since there is only a single row
121145 ** of output.
121146 */
121147 resetAccumulator(pParse, &sAggInfo);
121148 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0);
121149 if( pWInfo==0 ){
121150 sqlite3ExprListDelete(db, pDel);
121151 goto select_end;
121152 }
121153 updateAccumulator(pParse, &sAggInfo);
@@ -121232,12 +121515,10 @@
121232 **
121233 ** These routines are in a separate files so that they will not be linked
121234 ** if they are not used.
121235 */
121236 /* #include "sqliteInt.h" */
121237 /* #include <stdlib.h> */
121238 /* #include <string.h> */
121239
121240 #ifndef SQLITE_OMIT_GET_TABLE
121241
121242 /*
121243 ** This structure is used to pass data from sqlite3_get_table() through
@@ -122591,16 +122872,16 @@
122591 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
122592 pCol->affinity, &pValue);
122593 if( pValue ){
122594 sqlite3VdbeAppendP4(v, pValue, P4_MEM);
122595 }
 
122596 #ifndef SQLITE_OMIT_FLOATING_POINT
122597 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
122598 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
122599 }
122600 #endif
122601 }
122602 }
122603
122604 /*
122605 ** Process an UPDATE statement.
122606 **
@@ -122625,11 +122906,11 @@
122625 int nIdx; /* Number of indices that need updating */
122626 int iBaseCur; /* Base cursor number */
122627 int iDataCur; /* Cursor for the canonical data btree */
122628 int iIdxCur; /* Cursor for the first index */
122629 sqlite3 *db; /* The database structure */
122630 int *aRegIdx = 0; /* One register assigned to each index to be updated */
122631 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
122632 ** an expression for the i-th column of the table.
122633 ** aXRef[i]==-1 if the i-th column is not changed. */
122634 u8 *aToOpen; /* 1 for tables and indices to be opened */
122635 u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */
@@ -122637,14 +122918,15 @@
122637 u8 chngKey; /* Either chngPk or chngRowid */
122638 Expr *pRowidExpr = 0; /* Expression defining the new record number */
122639 AuthContext sContext; /* The authorization context */
122640 NameContext sNC; /* The name-context to resolve expressions in */
122641 int iDb; /* Database containing the table being updated */
122642 int okOnePass; /* True for one-pass algorithm without the FIFO */
122643 int hasFK; /* True if foreign key processing is required */
122644 int labelBreak; /* Jump here to break out of UPDATE loop */
122645 int labelContinue; /* Jump here to continue next step of UPDATE loop */
 
122646
122647 #ifndef SQLITE_OMIT_TRIGGER
122648 int isView; /* True when updating a view (INSTEAD OF trigger) */
122649 Trigger *pTrigger; /* List of triggers on pTab, if required */
122650 int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
@@ -122651,10 +122933,14 @@
122651 #endif
122652 int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */
122653 int iEph = 0; /* Ephemeral table holding all primary key values */
122654 int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */
122655 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
 
 
 
 
122656
122657 /* Register Allocations */
122658 int regRowCount = 0; /* A count of rows changed */
122659 int regOldRowid = 0; /* The old rowid */
122660 int regNewRowid = 0; /* The new rowid */
@@ -122810,17 +123096,27 @@
122810 for(i=0; i<pIdx->nKeyCol; i++){
122811 i16 iIdxCol = pIdx->aiColumn[i];
122812 if( iIdxCol<0 || aXRef[iIdxCol]>=0 ){
122813 reg = ++pParse->nMem;
122814 pParse->nMem += pIdx->nColumn;
 
 
 
 
 
122815 break;
122816 }
122817 }
122818 }
122819 if( reg==0 ) aToOpen[j+1] = 0;
122820 aRegIdx[j] = reg;
122821 }
 
 
 
 
 
122822
122823 /* Begin generating code. */
122824 v = sqlite3GetVdbe(pParse);
122825 if( v==0 ) goto update_cleanup;
122826 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
@@ -122869,107 +123165,127 @@
122869 pWhere, onError);
122870 goto update_cleanup;
122871 }
122872 #endif
122873
122874 /* Begin the database scan
122875 */
 
 
 
 
122876 if( HasRowid(pTab) ){
122877 sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
122878 pWInfo = sqlite3WhereBegin(
122879 pParse, pTabList, pWhere, 0, 0,
122880 WHERE_ONEPASS_DESIRED | WHERE_SEEK_TABLE, iIdxCur
122881 );
122882 if( pWInfo==0 ) goto update_cleanup;
122883 okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
122884
122885 /* Remember the rowid of every item to be updated.
122886 */
122887 sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
122888 if( !okOnePass ){
122889 sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
122890 }
122891
122892 /* End the database scan loop.
122893 */
122894 sqlite3WhereEnd(pWInfo);
122895 }else{
122896 int iPk; /* First of nPk memory cells holding PRIMARY KEY value */
122897 i16 nPk; /* Number of components of the PRIMARY KEY */
122898 int addrOpen; /* Address of the OpenEphemeral instruction */
122899
122900 assert( pPk!=0 );
122901 nPk = pPk->nKeyCol;
122902 iPk = pParse->nMem+1;
122903 pParse->nMem += nPk;
122904 regKey = ++pParse->nMem;
122905 iEph = pParse->nTab++;
 
122906 sqlite3VdbeAddOp2(v, OP_Null, 0, iPk);
122907 addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
122908 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
122909 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,
122910 WHERE_ONEPASS_DESIRED, iIdxCur);
122911 if( pWInfo==0 ) goto update_cleanup;
122912 okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122913 for(i=0; i<nPk; i++){
122914 assert( pPk->aiColumn[i]>=0 );
122915 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pPk->aiColumn[i],
122916 iPk+i);
122917 }
122918 if( okOnePass ){
122919 sqlite3VdbeChangeToNoop(v, addrOpen);
122920 nKey = nPk;
122921 regKey = iPk;
122922 }else{
122923 sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,
122924 sqlite3IndexAffinityStr(db, pPk), nPk);
122925 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEph, regKey, iPk, nPk);
122926 }
122927 sqlite3WhereEnd(pWInfo);
122928 }
122929
122930 /* Initialize the count of updated rows
122931 */
122932 if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
122933 regRowCount = ++pParse->nMem;
122934 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
122935 }
122936
122937 labelBreak = sqlite3VdbeMakeLabel(v);
122938 if( !isView ){
122939 /*
122940 ** Open every index that needs updating. Note that if any
122941 ** index could potentially invoke a REPLACE conflict resolution
122942 ** action, then we need to open all indices because we might need
122943 ** to be deleting some records.
122944 */
122945 if( onError==OE_Replace ){
122946 memset(aToOpen, 1, nIdx+1);
122947 }else{
122948 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
122949 if( pIdx->onError==OE_Replace ){
122950 memset(aToOpen, 1, nIdx+1);
122951 break;
122952 }
122953 }
122954 }
122955 if( okOnePass ){
122956 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
122957 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
122958 }
 
 
 
 
122959 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur, aToOpen,
122960 0, 0);
 
122961 }
122962
122963 /* Top of the update loop */
122964 if( okOnePass ){
122965 if( aToOpen[iDataCur-iBaseCur] && !isView ){
122966 assert( pPk );
122967 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
122968 VdbeCoverageNeverTaken(v);
122969 }
122970 labelContinue = labelBreak;
 
 
 
 
122971 sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
122972 VdbeCoverageIf(v, pPk==0);
122973 VdbeCoverageIf(v, pPk!=0);
122974 }else if( pPk ){
122975 labelContinue = sqlite3VdbeMakeLabel(v);
@@ -123090,11 +123406,10 @@
123090 }
123091 }
123092
123093 if( !isView ){
123094 int addr1 = 0; /* Address of jump instruction */
123095 int bReplace = 0; /* True if REPLACE conflict resolution might happen */
123096
123097 /* Do constraint checks. */
123098 assert( regOldRowid>0 );
123099 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
123100 regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace,
@@ -123126,18 +123441,22 @@
123126 ** is the column index supplied by the user.
123127 */
123128 assert( regNew==regNewRowid+1 );
123129 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
123130 sqlite3VdbeAddOp3(v, OP_Delete, iDataCur,
123131 OPFLAG_ISUPDATE | ((hasFK || chngKey || pPk!=0) ? 0 : OPFLAG_ISNOOP),
123132 regNewRowid
123133 );
 
 
 
 
123134 if( !pParse->nested ){
123135 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
123136 }
123137 #else
123138 if( hasFK || chngKey || pPk!=0 ){
123139 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0);
123140 }
123141 #endif
123142 if( bReplace || chngKey ){
123143 sqlite3VdbeJumpHere(v, addr1);
@@ -123146,12 +123465,15 @@
123146 if( hasFK ){
123147 sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);
123148 }
123149
123150 /* Insert the new index entries and the new record. */
123151 sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
123152 regNewRowid, aRegIdx, 1, 0, 0);
 
 
 
123153
123154 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
123155 ** handle rows (possibly in other tables) that refer via a foreign key
123156 ** to the row just updated. */
123157 if( hasFK ){
@@ -123169,12 +123491,15 @@
123169 TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue);
123170
123171 /* Repeat the above with the next record to be updated, until
123172 ** all record selected by the WHERE clause have been updated.
123173 */
123174 if( okOnePass ){
123175 /* Nothing to do at end-of-loop for a single-pass */
 
 
 
123176 }else if( pPk ){
123177 sqlite3VdbeResolveLabel(v, labelContinue);
123178 sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v);
123179 }else{
123180 sqlite3VdbeGoto(v, labelContinue);
@@ -127090,11 +127415,14 @@
127090
127091 /* Seek the table cursor, if required */
127092 if( omitTable ){
127093 /* pIdx is a covering index. No need to access the main table. */
127094 }else if( HasRowid(pIdx->pTable) ){
127095 if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE)!=0 ){
 
 
 
127096 iRowidReg = ++pParse->nMem;
127097 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
127098 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
127099 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg);
127100 VdbeCoverage(v);
@@ -128454,10 +128782,11 @@
128454 int noCase = 0; /* uppercase equivalent to lowercase */
128455 int op; /* Top-level operator. pExpr->op */
128456 Parse *pParse = pWInfo->pParse; /* Parsing context */
128457 sqlite3 *db = pParse->db; /* Database connection */
128458 unsigned char eOp2; /* op2 value for LIKE/REGEXP/GLOB */
 
128459
128460 if( db->mallocFailed ){
128461 return;
128462 }
128463 pTerm = &pWC->a[idxTerm];
@@ -128483,10 +128812,14 @@
128483 if( ExprHasProperty(pExpr, EP_FromJoin) ){
128484 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
128485 prereqAll |= x;
128486 extraRight = x-1; /* ON clause terms may not be used with an index
128487 ** on left table of a LEFT JOIN. Ticket #3015 */
 
 
 
 
128488 }
128489 pTerm->prereqAll = prereqAll;
128490 pTerm->leftCursor = -1;
128491 pTerm->iParent = -1;
128492 pTerm->eOperator = 0;
@@ -128725,17 +129058,16 @@
128725 **
128726 ** This is only required if at least one side of the comparison operation
128727 ** is not a sub-select. */
128728 if( pWC->op==TK_AND
128729 && (pExpr->op==TK_EQ || pExpr->op==TK_IS)
128730 && sqlite3ExprIsVector(pExpr->pLeft)
 
128731 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
128732 || (pExpr->pRight->flags & EP_xIsSelect)==0
128733 )){
128734 int nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
128735 int i;
128736 assert( nLeft==sqlite3ExprVectorSize(pExpr->pRight) );
128737 for(i=0; i<nLeft; i++){
128738 int idxNew;
128739 Expr *pNew;
128740 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i);
128741 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i);
@@ -133934,11 +134266,12 @@
133934 x = sqlite3ColumnOfIndex(pIdx, x);
133935 if( x>=0 ){
133936 pOp->p2 = x;
133937 pOp->p1 = pLevel->iIdxCur;
133938 }
133939 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 );
 
133940 }else if( pOp->opcode==OP_Rowid ){
133941 pOp->p1 = pLevel->iIdxCur;
133942 pOp->opcode = OP_IdxRowid;
133943 }
133944 }
@@ -133998,10 +134331,23 @@
133998 ** Indicate that sqlite3ParserFree() will never be called with a null
133999 ** pointer.
134000 */
134001 #define YYPARSEFREENEVERNULL 1
134002
 
 
 
 
 
 
 
 
 
 
 
 
 
134003 /*
134004 ** Alternative datatype for the argument to the malloc() routine passed
134005 ** into sqlite3ParserAlloc(). The default is size_t.
134006 */
134007 #define YYMALLOCARGTYPE u64
@@ -135446,10 +135792,35 @@
135446 */
135447 #ifndef YYMALLOCARGTYPE
135448 # define YYMALLOCARGTYPE size_t
135449 #endif
135450
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135451 /*
135452 ** This function allocates a new parser.
135453 ** The only argument is a pointer to a function which works like
135454 ** malloc.
135455 **
@@ -135461,32 +135832,15 @@
135461 ** to sqlite3Parser and sqlite3ParserFree.
135462 */
135463 SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPE)){
135464 yyParser *pParser;
135465 pParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) );
135466 if( pParser ){
135467 #ifdef YYTRACKMAXSTACKDEPTH
135468 pParser->yyhwm = 0;
135469 #endif
135470 #if YYSTACKDEPTH<=0
135471 pParser->yytos = NULL;
135472 pParser->yystack = NULL;
135473 pParser->yystksz = 0;
135474 if( yyGrowStack(pParser) ){
135475 pParser->yystack = &pParser->yystk0;
135476 pParser->yystksz = 1;
135477 }
135478 #endif
135479 #ifndef YYNOERRORRECOVERY
135480 pParser->yyerrcnt = -1;
135481 #endif
135482 pParser->yytos = pParser->yystack;
135483 pParser->yystack[0].stateno = 0;
135484 pParser->yystack[0].major = 0;
135485 }
135486 return pParser;
135487 }
 
 
135488
135489 /* The following function deletes the "minor type" or semantic value
135490 ** associated with a symbol. The symbol can be either a terminal
135491 ** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
135492 ** a pointer to the value to be deleted. The code used to do the
@@ -135608,10 +135962,22 @@
135608 }
135609 #endif
135610 yy_destructor(pParser, yytos->major, &yytos->minor);
135611 }
135612
 
 
 
 
 
 
 
 
 
 
 
 
135613 /*
135614 ** Deallocate and destroy a parser. Destructors are called for
135615 ** all stack elements before shutting the parser down.
135616 **
135617 ** If the YYPARSEFREENEVERNULL macro exists (for example because it
@@ -135620,20 +135986,17 @@
135620 */
135621 SQLITE_PRIVATE void sqlite3ParserFree(
135622 void *p, /* The parser to be deleted */
135623 void (*freeProc)(void*) /* Function used to reclaim memory */
135624 ){
135625 yyParser *pParser = (yyParser*)p;
135626 #ifndef YYPARSEFREENEVERNULL
135627 if( pParser==0 ) return;
135628 #endif
135629 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
135630 #if YYSTACKDEPTH<=0
135631 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
135632 #endif
135633 (*freeProc)((void*)pParser);
135634 }
135635
135636 /*
135637 ** Return the peak depth of the stack for a parser.
135638 */
135639 #ifdef YYTRACKMAXSTACKDEPTH
@@ -138483,10 +138846,13 @@
138483 void *pEngine; /* The LEMON-generated LALR(1) parser */
138484 int tokenType; /* type of the next token */
138485 int lastTokenParsed = -1; /* type of the previous token */
138486 sqlite3 *db = pParse->db; /* The database connection */
138487 int mxSqlLen; /* Max length of an SQL string */
 
 
 
138488
138489 assert( zSql!=0 );
138490 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
138491 if( db->nVdbeActive==0 ){
138492 db->u1.isInterrupted = 0;
@@ -138494,15 +138860,20 @@
138494 pParse->rc = SQLITE_OK;
138495 pParse->zTail = zSql;
138496 i = 0;
138497 assert( pzErrMsg!=0 );
138498 /* sqlite3ParserTrace(stdout, "parser: "); */
 
 
 
 
138499 pEngine = sqlite3ParserAlloc(sqlite3Malloc);
138500 if( pEngine==0 ){
138501 sqlite3OomFault(db);
138502 return SQLITE_NOMEM_BKPT;
138503 }
 
138504 assert( pParse->pNewTable==0 );
138505 assert( pParse->pNewTrigger==0 );
138506 assert( pParse->nVar==0 );
138507 assert( pParse->pVList==0 );
138508 while( 1 ){
@@ -138550,11 +138921,15 @@
138550 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
138551 sqlite3ParserStackPeak(pEngine)
138552 );
138553 sqlite3_mutex_leave(sqlite3MallocMutex());
138554 #endif /* YYDEBUG */
 
 
 
138555 sqlite3ParserFree(pEngine, sqlite3_free);
 
138556 if( db->mallocFailed ){
138557 pParse->rc = SQLITE_NOMEM_BKPT;
138558 }
138559 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
138560 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
@@ -145675,13 +146050,13 @@
145675 p->nPendingData = 0;
145676 p->azColumn = (char **)&p[1];
145677 p->pTokenizer = pTokenizer;
145678 p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
145679 p->bHasDocsize = (isFts4 && bNoDocsize==0);
145680 p->bHasStat = isFts4;
145681 p->bFts4 = isFts4;
145682 p->bDescIdx = bDescIdx;
145683 p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */
145684 p->zContentTbl = zContent;
145685 p->zLanguageid = zLanguageid;
145686 zContent = 0;
145687 zLanguageid = 0;
@@ -147734,11 +148109,11 @@
147734 sqlite3_stmt *pStmt = 0;
147735 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
147736 if( rc==SQLITE_OK ){
147737 int bHasStat = (sqlite3_step(pStmt)==SQLITE_ROW);
147738 rc = sqlite3_finalize(pStmt);
147739 if( rc==SQLITE_OK ) p->bHasStat = bHasStat;
147740 }
147741 sqlite3_free(zSql);
147742 }else{
147743 rc = SQLITE_NOMEM;
147744 }
@@ -162638,28 +163013,33 @@
162638 struct Rtree {
162639 sqlite3_vtab base; /* Base class. Must be first */
162640 sqlite3 *db; /* Host database connection */
162641 int iNodeSize; /* Size in bytes of each node in the node table */
162642 u8 nDim; /* Number of dimensions */
 
162643 u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
162644 u8 nBytesPerCell; /* Bytes consumed per cell */
 
162645 int iDepth; /* Current depth of the r-tree structure */
162646 char *zDb; /* Name of database containing r-tree table */
162647 char *zName; /* Name of r-tree table */
162648 int nBusy; /* Current number of users of this structure */
162649 i64 nRowEst; /* Estimated number of rows in this table */
 
162650
162651 /* List of nodes removed during a CondenseTree operation. List is
162652 ** linked together via the pointer normally used for hash chains -
162653 ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
162654 ** headed by the node (leaf nodes have RtreeNode.iNode==0).
162655 */
162656 RtreeNode *pDeleted;
162657 int iReinsertHeight; /* Height of sub-trees Reinsert() has run on */
 
 
 
162658
162659 /* Statements to read/write/delete a record from xxx_node */
162660 sqlite3_stmt *pReadNode;
162661 sqlite3_stmt *pWriteNode;
162662 sqlite3_stmt *pDeleteNode;
162663
162664 /* Statements to read/write/delete a record from xxx_rowid */
162665 sqlite3_stmt *pReadRowid;
@@ -162884,26 +163264,110 @@
162884 #endif
162885 #ifndef MIN
162886 # define MIN(x,y) ((x) > (y) ? (y) : (x))
162887 #endif
162888
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162889 /*
162890 ** Functions to deserialize a 16 bit integer, 32 bit real number and
162891 ** 64 bit integer. The deserialized value is returned.
162892 */
162893 static int readInt16(u8 *p){
162894 return (p[0]<<8) + p[1];
162895 }
162896 static void readCoord(u8 *p, RtreeCoord *pCoord){
 
 
 
 
 
 
 
 
 
 
 
162897 pCoord->u = (
162898 (((u32)p[0]) << 24) +
162899 (((u32)p[1]) << 16) +
162900 (((u32)p[2]) << 8) +
162901 (((u32)p[3]) << 0)
162902 );
 
162903 }
162904 static i64 readInt64(u8 *p){
 
 
 
 
 
 
 
 
 
 
 
 
 
162905 return (
162906 (((i64)p[0]) << 56) +
162907 (((i64)p[1]) << 48) +
162908 (((i64)p[2]) << 40) +
162909 (((i64)p[3]) << 32) +
@@ -162910,10 +163374,11 @@
162910 (((i64)p[4]) << 24) +
162911 (((i64)p[5]) << 16) +
162912 (((i64)p[6]) << 8) +
162913 (((i64)p[7]) << 0)
162914 );
 
162915 }
162916
162917 /*
162918 ** Functions to serialize a 16 bit integer, 32 bit real number and
162919 ** 64 bit integer. The value returned is the number of bytes written
@@ -162924,28 +163389,50 @@
162924 p[1] = (i>> 0)&0xFF;
162925 return 2;
162926 }
162927 static int writeCoord(u8 *p, RtreeCoord *pCoord){
162928 u32 i;
 
162929 assert( sizeof(RtreeCoord)==4 );
162930 assert( sizeof(u32)==4 );
 
 
 
 
 
 
 
 
 
 
162931 i = pCoord->u;
162932 p[0] = (i>>24)&0xFF;
162933 p[1] = (i>>16)&0xFF;
162934 p[2] = (i>> 8)&0xFF;
162935 p[3] = (i>> 0)&0xFF;
 
162936 return 4;
162937 }
162938 static int writeInt64(u8 *p, i64 i){
 
 
 
 
 
 
 
 
 
162939 p[0] = (i>>56)&0xFF;
162940 p[1] = (i>>48)&0xFF;
162941 p[2] = (i>>40)&0xFF;
162942 p[3] = (i>>32)&0xFF;
162943 p[4] = (i>>24)&0xFF;
162944 p[5] = (i>>16)&0xFF;
162945 p[6] = (i>> 8)&0xFF;
162946 p[7] = (i>> 0)&0xFF;
 
162947 return 8;
162948 }
162949
162950 /*
162951 ** Increment the reference count of node p.
@@ -163023,10 +163510,21 @@
163023 pNode->isDirty = 1;
163024 nodeReference(pParent);
163025 }
163026 return pNode;
163027 }
 
 
 
 
 
 
 
 
 
 
 
163028
163029 /*
163030 ** Obtain a reference to an r-tree node.
163031 */
163032 static int nodeAcquire(
@@ -163033,13 +163531,12 @@
163033 Rtree *pRtree, /* R-tree structure */
163034 i64 iNode, /* Node number to load */
163035 RtreeNode *pParent, /* Either the parent node or NULL */
163036 RtreeNode **ppNode /* OUT: Acquired node */
163037 ){
163038 int rc;
163039 int rc2 = SQLITE_OK;
163040 RtreeNode *pNode;
163041
163042 /* Check if the requested node is already in the hash table. If so,
163043 ** increase its reference count and return it.
163044 */
163045 if( (pNode = nodeHashLookup(pRtree, iNode)) ){
@@ -163051,32 +163548,49 @@
163051 pNode->nRef++;
163052 *ppNode = pNode;
163053 return SQLITE_OK;
163054 }
163055
163056 sqlite3_bind_int64(pRtree->pReadNode, 1, iNode);
163057 rc = sqlite3_step(pRtree->pReadNode);
163058 if( rc==SQLITE_ROW ){
163059 const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0);
163060 if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){
163061 pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
163062 if( !pNode ){
163063 rc2 = SQLITE_NOMEM;
163064 }else{
163065 pNode->pParent = pParent;
163066 pNode->zData = (u8 *)&pNode[1];
163067 pNode->nRef = 1;
163068 pNode->iNode = iNode;
163069 pNode->isDirty = 0;
163070 pNode->pNext = 0;
163071 memcpy(pNode->zData, zBlob, pRtree->iNodeSize);
163072 nodeReference(pParent);
163073 }
163074 }
163075 }
163076 rc = sqlite3_reset(pRtree->pReadNode);
163077 if( rc==SQLITE_OK ) rc = rc2;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163078
163079 /* If the root node was just loaded, set pRtree->iDepth to the height
163080 ** of the r-tree structure. A height of zero means all data is stored on
163081 ** the root node. A height of one means the children of the root node
163082 ** are the leaves, and so on. If the depth as specified on the root node
@@ -163124,11 +163638,11 @@
163124 int iCell /* Index into pNode into which pCell is written */
163125 ){
163126 int ii;
163127 u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
163128 p += writeInt64(p, pCell->iRowid);
163129 for(ii=0; ii<(pRtree->nDim*2); ii++){
163130 p += writeCoord(p, &pCell->aCoord[ii]);
163131 }
163132 pNode->isDirty = 1;
163133 }
163134
@@ -163258,17 +163772,20 @@
163258 int iCell, /* Index of the cell within the node */
163259 RtreeCell *pCell /* OUT: Write the cell contents here */
163260 ){
163261 u8 *pData;
163262 RtreeCoord *pCoord;
163263 int ii;
163264 pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
163265 pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
163266 pCoord = pCell->aCoord;
163267 for(ii=0; ii<pRtree->nDim*2; ii++){
163268 readCoord(&pData[ii*4], &pCoord[ii]);
163269 }
 
 
 
163270 }
163271
163272
163273 /* Forward declaration for the function that does the work of
163274 ** the virtual table module xCreate() and xConnect() methods.
@@ -163315,11 +163832,13 @@
163315 ** zero the structure is deleted.
163316 */
163317 static void rtreeRelease(Rtree *pRtree){
163318 pRtree->nBusy--;
163319 if( pRtree->nBusy==0 ){
163320 sqlite3_finalize(pRtree->pReadNode);
 
 
163321 sqlite3_finalize(pRtree->pWriteNode);
163322 sqlite3_finalize(pRtree->pDeleteNode);
163323 sqlite3_finalize(pRtree->pReadRowid);
163324 sqlite3_finalize(pRtree->pWriteRowid);
163325 sqlite3_finalize(pRtree->pDeleteRowid);
@@ -163353,10 +163872,11 @@
163353 pRtree->zDb, pRtree->zName
163354 );
163355 if( !zCreate ){
163356 rc = SQLITE_NOMEM;
163357 }else{
 
163358 rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
163359 sqlite3_free(zCreate);
163360 }
163361 if( rc==SQLITE_OK ){
163362 rtreeRelease(pRtree);
@@ -163368,17 +163888,19 @@
163368 /*
163369 ** Rtree virtual table module xOpen method.
163370 */
163371 static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
163372 int rc = SQLITE_NOMEM;
 
163373 RtreeCursor *pCsr;
163374
163375 pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
163376 if( pCsr ){
163377 memset(pCsr, 0, sizeof(RtreeCursor));
163378 pCsr->base.pVtab = pVTab;
163379 rc = SQLITE_OK;
 
163380 }
163381 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
163382
163383 return rc;
163384 }
@@ -163407,14 +163929,17 @@
163407 */
163408 static int rtreeClose(sqlite3_vtab_cursor *cur){
163409 Rtree *pRtree = (Rtree *)(cur->pVtab);
163410 int ii;
163411 RtreeCursor *pCsr = (RtreeCursor *)cur;
 
163412 freeCursorConstraints(pCsr);
163413 sqlite3_free(pCsr->aPoint);
163414 for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
163415 sqlite3_free(pCsr);
 
 
163416 return SQLITE_OK;
163417 }
163418
163419 /*
163420 ** Rtree virtual table module xEof method.
@@ -163433,27 +163958,34 @@
163433 ** endian platforms. The on-disk record stores integer coordinates if
163434 ** eInt is true and it stores 32-bit floating point records if eInt is
163435 ** false. a[] is the four bytes of the on-disk record to be decoded.
163436 ** Store the results in "r".
163437 **
163438 ** There are three versions of this macro, one each for little-endian and
163439 ** big-endian processors and a third generic implementation. The endian-
163440 ** specific implementations are much faster and are preferred if the
163441 ** processor endianness is known at compile-time. The SQLITE_BYTEORDER
163442 ** macro is part of sqliteInt.h and hence the endian-specific
163443 ** implementation will only be used if this module is compiled as part
163444 ** of the amalgamation.
163445 */
163446 #if defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==1234
 
 
 
 
 
 
 
 
 
 
 
 
163447 #define RTREE_DECODE_COORD(eInt, a, r) { \
163448 RtreeCoord c; /* Coordinate decoded */ \
163449 memcpy(&c.u,a,4); \
163450 c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)| \
163451 ((c.u&0xff)<<24)|((c.u&0xff00)<<8); \
163452 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
163453 }
163454 #elif defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==4321
163455 #define RTREE_DECODE_COORD(eInt, a, r) { \
163456 RtreeCoord c; /* Coordinate decoded */ \
163457 memcpy(&c.u,a,4); \
163458 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
163459 }
@@ -163476,30 +164008,58 @@
163476 u8 *pCellData, /* Raw cell content */
163477 RtreeSearchPoint *pSearch, /* Container of this cell */
163478 sqlite3_rtree_dbl *prScore, /* OUT: score for the cell */
163479 int *peWithin /* OUT: visibility of the cell */
163480 ){
163481 int i; /* Loop counter */
163482 sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
163483 int nCoord = pInfo->nCoord; /* No. of coordinates */
163484 int rc; /* Callback return code */
 
163485 sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2]; /* Decoded coordinates */
163486
163487 assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
163488 assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 );
163489
163490 if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
163491 pInfo->iRowid = readInt64(pCellData);
163492 }
163493 pCellData += 8;
163494 for(i=0; i<nCoord; i++, pCellData += 4){
163495 RTREE_DECODE_COORD(eInt, pCellData, aCoord[i]);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163496 }
163497 if( pConstraint->op==RTREE_MATCH ){
 
163498 rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
163499 nCoord, aCoord, &i);
163500 if( i==0 ) *peWithin = NOT_WITHIN;
163501 *prScore = RTREE_ZERO;
163502 }else{
163503 pInfo->aCoord = aCoord;
163504 pInfo->iLevel = pSearch->iLevel - 1;
163505 pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
@@ -163531,10 +164091,11 @@
163531 */
163532 pCellData += 8 + 4*(p->iCoord&0xfe);
163533
163534 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
163535 || p->op==RTREE_GT || p->op==RTREE_EQ );
 
163536 switch( p->op ){
163537 case RTREE_LE:
163538 case RTREE_LT:
163539 case RTREE_EQ:
163540 RTREE_DECODE_COORD(eInt, pCellData, val);
@@ -163571,10 +164132,11 @@
163571 RtreeDValue xN; /* Coordinate value converted to a double */
163572
163573 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
163574 || p->op==RTREE_GT || p->op==RTREE_EQ );
163575 pCellData += 8 + p->iCoord*4;
 
163576 RTREE_DECODE_COORD(eInt, pCellData, xN);
163577 switch( p->op ){
163578 case RTREE_LE: if( xN <= p->u.rValue ) return; break;
163579 case RTREE_LT: if( xN < p->u.rValue ) return; break;
163580 case RTREE_GE: if( xN >= p->u.rValue ) return; break;
@@ -163639,11 +164201,11 @@
163639 if( pA->iLevel>pB->iLevel ) return +1;
163640 return 0;
163641 }
163642
163643 /*
163644 ** Interchange to search points in a cursor.
163645 */
163646 static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
163647 RtreeSearchPoint t = p->aPoint[i];
163648 assert( i<j );
163649 p->aPoint[i] = p->aPoint[j];
@@ -163887,11 +164449,11 @@
163887 rtreeSearchPointPop(pCur);
163888 }
163889 if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO;
163890 p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
163891 if( p==0 ) return SQLITE_NOMEM;
163892 p->eWithin = eWithin;
163893 p->id = x.id;
163894 p->iCell = x.iCell;
163895 RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
163896 break;
163897 }
@@ -163946,11 +164508,10 @@
163946 if( rc ) return rc;
163947 if( p==0 ) return SQLITE_OK;
163948 if( i==0 ){
163949 sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
163950 }else{
163951 if( rc ) return rc;
163952 nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
163953 #ifndef SQLITE_RTREE_INT_ONLY
163954 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
163955 sqlite3_result_double(ctx, c.f);
163956 }else
@@ -164075,11 +164636,11 @@
164075 assert( p!=0 ); /* Always returns pCsr->sPoint */
164076 pCsr->aNode[0] = pLeaf;
164077 p->id = iNode;
164078 p->eWithin = PARTLY_WITHIN;
164079 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
164080 p->iCell = iCell;
164081 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
164082 }else{
164083 pCsr->atEOF = 1;
164084 }
164085 }else{
@@ -164108,11 +164669,11 @@
164108 */
164109 rc = deserializeGeometry(argv[ii], p);
164110 if( rc!=SQLITE_OK ){
164111 break;
164112 }
164113 p->pInfo->nCoord = pRtree->nDim*2;
164114 p->pInfo->anQueue = pCsr->anQueue;
164115 p->pInfo->mxLevel = pRtree->iDepth + 1;
164116 }else{
164117 #ifdef SQLITE_RTREE_INT_ONLY
164118 p->u.rValue = sqlite3_value_int64(argv[ii]);
@@ -164123,11 +164684,11 @@
164123 }
164124 }
164125 }
164126 if( rc==SQLITE_OK ){
164127 RtreeSearchPoint *pNew;
164128 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, pRtree->iDepth+1);
164129 if( pNew==0 ) return SQLITE_NOMEM;
164130 pNew->id = 1;
164131 pNew->iCell = 0;
164132 pNew->eWithin = PARTLY_WITHIN;
164133 assert( pCsr->bPoint==1 );
@@ -164141,23 +164702,10 @@
164141 nodeRelease(pRtree, pRoot);
164142 rtreeRelease(pRtree);
164143 return rc;
164144 }
164145
164146 /*
164147 ** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
164148 ** extension is currently being used by a version of SQLite too old to
164149 ** support estimatedRows. In that case this function is a no-op.
164150 */
164151 static void setEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
164152 #if SQLITE_VERSION_NUMBER>=3008002
164153 if( sqlite3_libversion_number()>=3008002 ){
164154 pIdxInfo->estimatedRows = nRow;
164155 }
164156 #endif
164157 }
164158
164159 /*
164160 ** Rtree virtual table module xBestIndex method. There are three
164161 ** table scan strategies to choose from (in order from most to
164162 ** least desirable):
164163 **
@@ -164233,11 +164781,11 @@
164233 ** considered almost as quick as a direct rowid lookup (for which
164234 ** sqlite uses an internal cost of 0.0). It is expected to return
164235 ** a single row.
164236 */
164237 pIdxInfo->estimatedCost = 30.0;
164238 setEstimatedRows(pIdxInfo, 1);
164239 return SQLITE_OK;
164240 }
164241
164242 if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
164243 u8 op;
@@ -164251,11 +164799,11 @@
164251 assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
164252 op = RTREE_MATCH;
164253 break;
164254 }
164255 zIdxStr[iIdx++] = op;
164256 zIdxStr[iIdx++] = p->iColumn - 1 + '0';
164257 pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
164258 pIdxInfo->aConstraintUsage[ii].omit = 1;
164259 }
164260 }
164261
@@ -164265,55 +164813,75 @@
164265 return SQLITE_NOMEM;
164266 }
164267
164268 nRow = pRtree->nRowEst >> (iIdx/2);
164269 pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
164270 setEstimatedRows(pIdxInfo, nRow);
164271
164272 return rc;
164273 }
164274
164275 /*
164276 ** Return the N-dimensional volumn of the cell stored in *p.
164277 */
164278 static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
164279 RtreeDValue area = (RtreeDValue)1;
164280 int ii;
164281 for(ii=0; ii<(pRtree->nDim*2); ii+=2){
164282 area = (area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164283 }
164284 return area;
164285 }
164286
164287 /*
164288 ** Return the margin length of cell p. The margin length is the sum
164289 ** of the objects size in each dimension.
164290 */
164291 static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
164292 RtreeDValue margin = (RtreeDValue)0;
164293 int ii;
164294 for(ii=0; ii<(pRtree->nDim*2); ii+=2){
164295 margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
164296 }
 
164297 return margin;
164298 }
164299
164300 /*
164301 ** Store the union of cells p1 and p2 in p1.
164302 */
164303 static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
164304 int ii;
164305 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
164306 for(ii=0; ii<(pRtree->nDim*2); ii+=2){
164307 p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
164308 p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
164309 }
 
164310 }else{
164311 for(ii=0; ii<(pRtree->nDim*2); ii+=2){
164312 p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
164313 p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
164314 }
 
164315 }
164316 }
164317
164318 /*
164319 ** Return true if the area covered by p2 is a subset of the area covered
@@ -164320,11 +164888,11 @@
164320 ** by p1. False otherwise.
164321 */
164322 static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
164323 int ii;
164324 int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
164325 for(ii=0; ii<(pRtree->nDim*2); ii+=2){
164326 RtreeCoord *a1 = &p1->aCoord[ii];
164327 RtreeCoord *a2 = &p2->aCoord[ii];
164328 if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f))
164329 || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i))
164330 ){
@@ -164355,11 +164923,11 @@
164355 int ii;
164356 RtreeDValue overlap = RTREE_ZERO;
164357 for(ii=0; ii<nCell; ii++){
164358 int jj;
164359 RtreeDValue o = (RtreeDValue)1;
164360 for(jj=0; jj<(pRtree->nDim*2); jj+=2){
164361 RtreeDValue x1, x2;
164362 x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
164363 x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
164364 if( x2<x1 ){
164365 o = (RtreeDValue)0;
@@ -165411,11 +165979,11 @@
165411 ** with "column" that are interpreted as table constraints.
165412 ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
165413 ** This problem was discovered after years of use, so we silently ignore
165414 ** these kinds of misdeclared tables to avoid breaking any legacy.
165415 */
165416 assert( nData<=(pRtree->nDim*2 + 3) );
165417
165418 #ifndef SQLITE_RTREE_INT_ONLY
165419 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
165420 for(ii=0; ii<nData-4; ii+=2){
165421 cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]);
@@ -165500,10 +166068,31 @@
165500
165501 constraint:
165502 rtreeRelease(pRtree);
165503 return rc;
165504 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165505
165506 /*
165507 ** The xRename method for rtree module virtual tables.
165508 */
165509 static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
@@ -165521,10 +166110,11 @@
165521 rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
165522 sqlite3_free(zSql);
165523 }
165524 return rc;
165525 }
 
165526
165527 /*
165528 ** This function populates the pRtree->nRowEst variable with an estimate
165529 ** of the number of rows in the virtual table. If possible, this is based
165530 ** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
@@ -165581,19 +166171,19 @@
165581 rtreeNext, /* xNext - advance a cursor */
165582 rtreeEof, /* xEof */
165583 rtreeColumn, /* xColumn - read data */
165584 rtreeRowid, /* xRowid - read data */
165585 rtreeUpdate, /* xUpdate - write data */
165586 0, /* xBegin - begin transaction */
165587 0, /* xSync - sync transaction */
165588 0, /* xCommit - commit transaction */
165589 0, /* xRollback - rollback transaction */
165590 0, /* xFindFunction - function overloading */
165591 rtreeRename, /* xRename - rename the table */
165592 0, /* xSavepoint */
165593 0, /* xRelease */
165594 0 /* xRollbackTo */
165595 };
165596
165597 static int rtreeSqlInit(
165598 Rtree *pRtree,
165599 sqlite3 *db,
@@ -165601,14 +166191,13 @@
165601 const char *zPrefix,
165602 int isCreate
165603 ){
165604 int rc = SQLITE_OK;
165605
165606 #define N_STATEMENT 9
165607 static const char *azSql[N_STATEMENT] = {
165608 /* Read and write the xxx_node table */
165609 "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1",
165610 "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
165611 "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
165612
165613 /* Read and write the xxx_rowid table */
165614 "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
@@ -165642,19 +166231,18 @@
165642 if( rc!=SQLITE_OK ){
165643 return rc;
165644 }
165645 }
165646
165647 appStmt[0] = &pRtree->pReadNode;
165648 appStmt[1] = &pRtree->pWriteNode;
165649 appStmt[2] = &pRtree->pDeleteNode;
165650 appStmt[3] = &pRtree->pReadRowid;
165651 appStmt[4] = &pRtree->pWriteRowid;
165652 appStmt[5] = &pRtree->pDeleteRowid;
165653 appStmt[6] = &pRtree->pReadParent;
165654 appStmt[7] = &pRtree->pWriteParent;
165655 appStmt[8] = &pRtree->pDeleteParent;
165656
165657 rc = rtreeQueryStat1(db, pRtree);
165658 for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
165659 char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
165660 if( zSql ){
@@ -165788,13 +166376,14 @@
165788 memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
165789 pRtree->nBusy = 1;
165790 pRtree->base.pModule = &rtreeModule;
165791 pRtree->zDb = (char *)&pRtree[1];
165792 pRtree->zName = &pRtree->zDb[nDb+1];
165793 pRtree->nDim = (argc-4)/2;
165794 pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2;
165795 pRtree->eCoordType = eCoordType;
 
165796 memcpy(pRtree->zDb, argv[1], nDb);
165797 memcpy(pRtree->zName, argv[2], nName);
165798
165799 /* Figure out the node size to use. */
165800 rc = getNodeSize(db, pRtree, isCreate, pzErr);
@@ -165863,11 +166452,12 @@
165863 int ii;
165864
165865 UNUSED_PARAMETER(nArg);
165866 memset(&node, 0, sizeof(RtreeNode));
165867 memset(&tree, 0, sizeof(Rtree));
165868 tree.nDim = sqlite3_value_int(apArg[0]);
 
165869 tree.nBytesPerCell = 8 + 8 * tree.nDim;
165870 node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
165871
165872 for(ii=0; ii<NCELL(&node); ii++){
165873 char zCell[512];
@@ -165876,11 +166466,11 @@
165876 int jj;
165877
165878 nodeGetCell(&tree, &node, ii, &cell);
165879 sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
165880 nCell = (int)strlen(zCell);
165881 for(jj=0; jj<tree.nDim*2; jj++){
165882 #ifndef SQLITE_RTREE_INT_ONLY
165883 sqlite3_snprintf(512-nCell,&zCell[nCell], " %g",
165884 (double)cell.aCoord[jj].f);
165885 #else
165886 sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
@@ -166584,42 +167174,40 @@
166584
166585 /*
166586 ** Register the ICU extension functions with database db.
166587 */
166588 SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
166589 struct IcuScalar {
166590 const char *zName; /* Function name */
166591 int nArg; /* Number of arguments */
166592 int enc; /* Optimal text encoding */
166593 void *pContext; /* sqlite3_user_data() context */
166594 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
166595 } scalars[] = {
166596 {"regexp", 2, SQLITE_ANY|SQLITE_DETERMINISTIC, 0, icuRegexpFunc},
166597
166598 {"lower", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
166599 {"lower", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
166600 {"upper", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16},
166601 {"upper", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16},
166602
166603 {"lower", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
166604 {"lower", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
166605 {"upper", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16},
166606 {"upper", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16},
166607
166608 {"like", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc},
166609 {"like", 3, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc},
166610
166611 {"icu_load_collation", 2, SQLITE_UTF8, (void*)db, icuLoadCollation},
166612 };
166613
166614 int rc = SQLITE_OK;
166615 int i;
166616
 
166617 for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
166618 struct IcuScalar *p = &scalars[i];
166619 rc = sqlite3_create_function(
166620 db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0
 
 
166621 );
166622 }
166623
166624 return rc;
166625 }
@@ -169823,11 +170411,11 @@
169823
169824 /*
169825 ** Open the database handle and attach the RBU database as "rbu". If an
169826 ** error occurs, leave an error code and message in the RBU handle.
169827 */
169828 static void rbuOpenDatabase(sqlite3rbu *p){
169829 assert( p->rc || (p->dbMain==0 && p->dbRbu==0) );
169830 assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 );
169831
169832 /* Open the RBU database */
169833 p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
@@ -169898,11 +170486,11 @@
169898 if( p->eStage>=RBU_STAGE_MOVE ){
169899 bOpen = 1;
169900 }else{
169901 RbuState *pState = rbuLoadState(p);
169902 if( pState ){
169903 bOpen = (pState->eStage>RBU_STAGE_MOVE);
169904 rbuFreeState(pState);
169905 }
169906 }
169907 if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
169908 }
@@ -169910,10 +170498,19 @@
169910 p->eStage = 0;
169911 if( p->rc==SQLITE_OK && p->dbMain==0 ){
169912 if( !rbuIsVacuum(p) ){
169913 p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
169914 }else if( p->pRbuFd->pWalFd ){
 
 
 
 
 
 
 
 
 
169915 p->rc = SQLITE_ERROR;
169916 p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
169917 }else{
169918 char *zTarget;
169919 char *zExtra = 0;
@@ -170090,20 +170687,22 @@
170090 p->eStage = RBU_STAGE_CAPTURE;
170091 rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
170092 if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
170093 }
170094
170095 if( p->rc==SQLITE_OK ){
170096 p->eStage = RBU_STAGE_CKPT;
170097 p->nStep = (pState ? pState->nRow : 0);
170098 p->aBuf = rbuMalloc(p, p->pgsz);
170099 p->iWalCksum = rbuShmChecksum(p);
170100 }
170101
170102 if( p->rc==SQLITE_OK && pState && pState->iWalCksum!=p->iWalCksum ){
170103 p->rc = SQLITE_DONE;
170104 p->eStage = RBU_STAGE_DONE;
 
 
170105 }
170106 }
170107
170108 /*
170109 ** Called when iAmt bytes are read from offset iOff of the wal file while
@@ -170272,11 +170871,11 @@
170272 #else
170273 p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
170274 #endif
170275
170276 if( p->rc==SQLITE_OK ){
170277 rbuOpenDatabase(p);
170278 rbuSetupCheckpoint(p, 0);
170279 }
170280 }
170281 }
170282
@@ -170983,10 +171582,11 @@
170983 rbuCreateVfs(p);
170984
170985 /* Open the target, RBU and state databases */
170986 if( p->rc==SQLITE_OK ){
170987 char *pCsr = (char*)&p[1];
 
170988 if( zTarget ){
170989 p->zTarget = pCsr;
170990 memcpy(p->zTarget, zTarget, nTarget+1);
170991 pCsr += nTarget+1;
170992 }
@@ -170994,11 +171594,22 @@
170994 memcpy(p->zRbu, zRbu, nRbu+1);
170995 pCsr += nRbu+1;
170996 if( zState ){
170997 p->zState = rbuMPrintf(p, "%s", zState);
170998 }
170999 rbuOpenDatabase(p);
 
 
 
 
 
 
 
 
 
 
 
171000 }
171001
171002 if( p->rc==SQLITE_OK ){
171003 pState = rbuLoadState(p);
171004 assert( pState || p->rc!=SQLITE_OK );
@@ -175957,11 +176568,11 @@
175957 sqlite3_value **ppValue /* OUT: Value from conflicting row */
175958 ){
175959 if( !pIter->pConflict ){
175960 return SQLITE_MISUSE;
175961 }
175962 if( iVal<0 || iVal>=sqlite3_column_count(pIter->pConflict) ){
175963 return SQLITE_RANGE;
175964 }
175965 *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
175966 return SQLITE_OK;
175967 }
@@ -176424,11 +177035,17 @@
176424 int i;
176425 SessionBuffer buf = {0, 0, 0};
176426
176427 sessionAppendStr(&buf, "INSERT INTO main.", &rc);
176428 sessionAppendIdent(&buf, zTab, &rc);
176429 sessionAppendStr(&buf, " VALUES(?", &rc);
 
 
 
 
 
 
176430 for(i=1; i<p->nCol; i++){
176431 sessionAppendStr(&buf, ", ?", &rc);
176432 }
176433 sessionAppendStr(&buf, ")", &rc);
176434
@@ -176970,42 +177587,51 @@
176970 break;
176971 }
176972 nTab = (int)strlen(zTab);
176973 sApply.azCol = (const char **)zTab;
176974 }else{
 
 
 
176975 sqlite3changeset_pk(pIter, &abPK, 0);
176976 rc = sessionTableInfo(
176977 db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
176978 );
176979 if( rc!=SQLITE_OK ) break;
 
 
 
176980
176981 if( sApply.nCol==0 ){
176982 schemaMismatch = 1;
176983 sqlite3_log(SQLITE_SCHEMA,
176984 "sqlite3changeset_apply(): no such table: %s", zTab
176985 );
176986 }
176987 else if( sApply.nCol!=nCol ){
176988 schemaMismatch = 1;
176989 sqlite3_log(SQLITE_SCHEMA,
176990 "sqlite3changeset_apply(): table %s has %d columns, expected %d",
 
176991 zTab, sApply.nCol, nCol
176992 );
176993 }
176994 else if( memcmp(sApply.abPK, abPK, nCol)!=0 ){
176995 schemaMismatch = 1;
176996 sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
176997 "primary key mismatch for table %s", zTab
176998 );
176999 }
177000 else if(
177001 (rc = sessionSelectRow(db, zTab, &sApply))
177002 || (rc = sessionUpdateRow(db, zTab, &sApply))
177003 || (rc = sessionDeleteRow(db, zTab, &sApply))
177004 || (rc = sessionInsertRow(db, zTab, &sApply))
177005 ){
177006 break;
 
 
177007 }
177008 nTab = sqlite3Strlen30(zTab);
177009 }
177010 }
177011
@@ -177593,11 +178219,11 @@
177593 ** a BLOB, but there is no support for JSONB in the current implementation.
177594 ** This implementation parses JSON text at 250 MB/s, so it is hard to see
177595 ** how JSONB might improve on that.)
177596 */
177597 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
177598 #if !defined(_SQLITEINT_H_)
177599 /* #include "sqlite3ext.h" */
177600 #endif
177601 SQLITE_EXTENSION_INIT1
177602 /* #include <assert.h> */
177603 /* #include <string.h> */
@@ -181644,10 +182270,35 @@
181644 */
181645 #ifndef fts5YYMALLOCARGTYPE
181646 # define fts5YYMALLOCARGTYPE size_t
181647 #endif
181648
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181649 /*
181650 ** This function allocates a new parser.
181651 ** The only argument is a pointer to a function which works like
181652 ** malloc.
181653 **
@@ -181659,32 +182310,15 @@
181659 ** to sqlite3Fts5Parser and sqlite3Fts5ParserFree.
181660 */
181661 static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(fts5YYMALLOCARGTYPE)){
181662 fts5yyParser *pParser;
181663 pParser = (fts5yyParser*)(*mallocProc)( (fts5YYMALLOCARGTYPE)sizeof(fts5yyParser) );
181664 if( pParser ){
181665 #ifdef fts5YYTRACKMAXSTACKDEPTH
181666 pParser->fts5yyhwm = 0;
181667 #endif
181668 #if fts5YYSTACKDEPTH<=0
181669 pParser->fts5yytos = NULL;
181670 pParser->fts5yystack = NULL;
181671 pParser->fts5yystksz = 0;
181672 if( fts5yyGrowStack(pParser) ){
181673 pParser->fts5yystack = &pParser->fts5yystk0;
181674 pParser->fts5yystksz = 1;
181675 }
181676 #endif
181677 #ifndef fts5YYNOERRORRECOVERY
181678 pParser->fts5yyerrcnt = -1;
181679 #endif
181680 pParser->fts5yytos = pParser->fts5yystack;
181681 pParser->fts5yystack[0].stateno = 0;
181682 pParser->fts5yystack[0].major = 0;
181683 }
181684 return pParser;
181685 }
 
 
181686
181687 /* The following function deletes the "minor type" or semantic value
181688 ** associated with a symbol. The symbol can be either a terminal
181689 ** or nonterminal. "fts5yymajor" is the symbol code, and "fts5yypminor" is
181690 ** a pointer to the value to be deleted. The code used to do the
@@ -181762,10 +182396,22 @@
181762 }
181763 #endif
181764 fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor);
181765 }
181766
 
 
 
 
 
 
 
 
 
 
 
 
181767 /*
181768 ** Deallocate and destroy a parser. Destructors are called for
181769 ** all stack elements before shutting the parser down.
181770 **
181771 ** If the fts5YYPARSEFREENEVERNULL macro exists (for example because it
@@ -181774,20 +182420,17 @@
181774 */
181775 static void sqlite3Fts5ParserFree(
181776 void *p, /* The parser to be deleted */
181777 void (*freeProc)(void*) /* Function used to reclaim memory */
181778 ){
181779 fts5yyParser *pParser = (fts5yyParser*)p;
181780 #ifndef fts5YYPARSEFREENEVERNULL
181781 if( pParser==0 ) return;
181782 #endif
181783 while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
181784 #if fts5YYSTACKDEPTH<=0
181785 if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
181786 #endif
181787 (*freeProc)((void*)pParser);
181788 }
181789
181790 /*
181791 ** Return the peak depth of the stack for a parser.
181792 */
181793 #ifdef fts5YYTRACKMAXSTACKDEPTH
@@ -186122,11 +186765,11 @@
186122 memset(&sCtx, 0, sizeof(TokenCtx));
186123 sCtx.pPhrase = pAppend;
186124
186125 rc = fts5ParseStringFromToken(pToken, &z);
186126 if( rc==SQLITE_OK ){
186127 int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_QUERY : 0);
186128 int n;
186129 sqlite3Fts5Dequote(z);
186130 n = (int)strlen(z);
186131 rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize);
186132 }
@@ -196863,11 +197506,11 @@
196863 int nArg, /* Number of args */
196864 sqlite3_value **apUnused /* Function arguments */
196865 ){
196866 assert( nArg==0 );
196867 UNUSED_PARAM2(nArg, apUnused);
196868 sqlite3_result_text(pCtx, "fts5: 2017-01-06 16:32:41 a65a62893ca8319e89e48b8a38cf8a59c69a8209", -1, SQLITE_TRANSIENT);
196869 }
196870
196871 static int fts5Init(sqlite3 *db){
196872 static const sqlite3_module fts5Mod = {
196873 /* iVersion */ 2,
196874
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.17.0. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a single translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -208,10 +208,18 @@
208 #ifdef __GNUC__
209 # define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
210 #else
211 # define GCC_VERSION 0
212 #endif
213
214 /* What version of CLANG is being used. 0 means CLANG is not being used */
215 #if defined(__clang__) && !defined(_WIN32)
216 # define CLANG_VERSION \
217 (__clang_major__*1000000+__clang_minor__*1000+__clang_patchlevel__)
218 #else
219 # define CLANG_VERSION 0
220 #endif
221
222 /* Needed for various definitions... */
223 #if defined(__GNUC__) && !defined(_GNU_SOURCE)
224 # define _GNU_SOURCE
225 #endif
@@ -379,13 +387,13 @@
387 **
388 ** See also: [sqlite3_libversion()],
389 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
390 ** [sqlite_version()] and [sqlite_source_id()].
391 */
392 #define SQLITE_VERSION "3.17.0"
393 #define SQLITE_VERSION_NUMBER 3017000
394 #define SQLITE_SOURCE_ID "2017-02-07 13:51:48 a136609c98ed3cc673c5a3c2578d49db3f2518d1"
395
396 /*
397 ** CAPI3REF: Run-Time Library Version Numbers
398 ** KEYWORDS: sqlite3_version sqlite3_sourceid
399 **
@@ -517,11 +525,15 @@
525 ** sqlite3_uint64 and sqlite_uint64 types can store integer values
526 ** between 0 and +18446744073709551615 inclusive.
527 */
528 #ifdef SQLITE_INT64_TYPE
529 typedef SQLITE_INT64_TYPE sqlite_int64;
530 # ifdef SQLITE_UINT64_TYPE
531 typedef SQLITE_UINT64_TYPE sqlite_uint64;
532 # else
533 typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
534 # endif
535 #elif defined(_MSC_VER) || defined(__BORLANDC__)
536 typedef __int64 sqlite_int64;
537 typedef unsigned __int64 sqlite_uint64;
538 #else
539 typedef long long int sqlite_int64;
@@ -830,11 +842,11 @@
842 ** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
843 ** after reboot following a crash or power loss, the only bytes in a
844 ** file that were written at the application level might have changed
845 ** and that adjacent bytes, even bytes within the same sector are
846 ** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
847 ** flag indicates that a file cannot be deleted when open. The
848 ** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
849 ** read-only media and cannot be changed even by processes with
850 ** elevated privileges.
851 */
852 #define SQLITE_IOCAP_ATOMIC 0x00000001
@@ -980,10 +992,13 @@
992 ** <li> [SQLITE_IOCAP_ATOMIC16K]
993 ** <li> [SQLITE_IOCAP_ATOMIC32K]
994 ** <li> [SQLITE_IOCAP_ATOMIC64K]
995 ** <li> [SQLITE_IOCAP_SAFE_APPEND]
996 ** <li> [SQLITE_IOCAP_SEQUENTIAL]
997 ** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
998 ** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
999 ** <li> [SQLITE_IOCAP_IMMUTABLE]
1000 ** </ul>
1001 **
1002 ** The SQLITE_IOCAP_ATOMIC property means that all writes of
1003 ** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
1004 ** mean that writes of blocks that are nnn bytes in size and
@@ -5668,11 +5683,11 @@
5683 ** ^(The update hook is not invoked when internal system tables are
5684 ** modified (i.e. sqlite_master and sqlite_sequence).)^
5685 ** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
5686 **
5687 ** ^In the current implementation, the update hook
5688 ** is not invoked when conflicting rows are deleted because of an
5689 ** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook
5690 ** invoked when rows are deleted using the [truncate optimization].
5691 ** The exceptions defined in this paragraph might change in a future
5692 ** release of SQLite.
5693 **
@@ -6450,10 +6465,16 @@
6465 **
6466 ** ^Unless it returns SQLITE_MISUSE, this function sets the
6467 ** [database connection] error code and message accessible via
6468 ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
6469 **
6470 ** A BLOB referenced by sqlite3_blob_open() may be read using the
6471 ** [sqlite3_blob_read()] interface and modified by using
6472 ** [sqlite3_blob_write()]. The [BLOB handle] can be moved to a
6473 ** different row of the same table using the [sqlite3_blob_reopen()]
6474 ** interface. However, the column, table, or database of a [BLOB handle]
6475 ** cannot be changed after the [BLOB handle] is opened.
6476 **
6477 ** ^(If the row that a BLOB handle points to is modified by an
6478 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
6479 ** then the BLOB handle is marked as "expired".
6480 ** This is true if any column of the row is changed, even a column
@@ -6473,10 +6494,14 @@
6494 ** and the built-in [zeroblob] SQL function may be used to create a
6495 ** zero-filled blob to read or write using the incremental-blob interface.
6496 **
6497 ** To avoid a resource leak, every open [BLOB handle] should eventually
6498 ** be released by a call to [sqlite3_blob_close()].
6499 **
6500 ** See also: [sqlite3_blob_close()],
6501 ** [sqlite3_blob_reopen()], [sqlite3_blob_read()],
6502 ** [sqlite3_blob_bytes()], [sqlite3_blob_write()].
6503 */
6504 SQLITE_API int sqlite3_blob_open(
6505 sqlite3*,
6506 const char *zDb,
6507 const char *zTable,
@@ -6488,15 +6513,15 @@
6513
6514 /*
6515 ** CAPI3REF: Move a BLOB Handle to a New Row
6516 ** METHOD: sqlite3_blob
6517 **
6518 ** ^This function is used to move an existing [BLOB handle] so that it points
6519 ** to a different row of the same database table. ^The new row is identified
6520 ** by the rowid value passed as the second argument. Only the row can be
6521 ** changed. ^The database, table and column on which the blob handle is open
6522 ** remain the same. Moving an existing [BLOB handle] to a new row is
6523 ** faster than closing the existing handle and opening a new one.
6524 **
6525 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
6526 ** it must exist and there must be either a blob or text value stored in
6527 ** the nominated column.)^ ^If the new row is not present in the table, or if
@@ -8421,22 +8446,22 @@
8446 ** ^These interfaces are only available if SQLite is compiled using the
8447 ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
8448 **
8449 ** ^The [sqlite3_preupdate_hook()] interface registers a callback function
8450 ** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
8451 ** on a database table.
8452 ** ^At most one preupdate hook may be registered at a time on a single
8453 ** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
8454 ** the previous setting.
8455 ** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
8456 ** with a NULL pointer as the second parameter.
8457 ** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
8458 ** the first parameter to callbacks.
8459 **
8460 ** ^The preupdate hook only fires for changes to real database tables; the
8461 ** preupdate hook is not invoked for changes to [virtual tables] or to
8462 ** system tables like sqlite_master or sqlite_stat1.
8463 **
8464 ** ^The second parameter to the preupdate callback is a pointer to
8465 ** the [database connection] that registered the preupdate hook.
8466 ** ^The third parameter to the preupdate callback is one of the constants
8467 ** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
@@ -8446,16 +8471,20 @@
8471 ** will be "main" for the main database or "temp" for TEMP tables or
8472 ** the name given after the AS keyword in the [ATTACH] statement for attached
8473 ** databases.)^
8474 ** ^The fifth parameter to the preupdate callback is the name of the
8475 ** table that is being modified.
8476 **
8477 ** For an UPDATE or DELETE operation on a [rowid table], the sixth
8478 ** parameter passed to the preupdate callback is the initial [rowid] of the
8479 ** row being modified or deleted. For an INSERT operation on a rowid table,
8480 ** or any operation on a WITHOUT ROWID table, the value of the sixth
8481 ** parameter is undefined. For an INSERT or UPDATE on a rowid table the
8482 ** seventh parameter is the final rowid value of the row being inserted
8483 ** or updated. The value of the seventh parameter passed to the callback
8484 ** function is not defined for operations on WITHOUT ROWID tables, or for
8485 ** INSERT operations on rowid tables.
8486 **
8487 ** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
8488 ** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
8489 ** provide additional information about a preupdate event. These routines
8490 ** may only be called from within a preupdate callback. Invoking any of
@@ -9155,11 +9184,12 @@
9184 **
9185 ** <li> For each row (primary key) that exists in the to-table but not in
9186 ** the from-table, a DELETE record is added to the session object.
9187 **
9188 ** <li> For each row (primary key) that exists in both tables, but features
9189 ** different non-PK values in each, an UPDATE record is added to the
9190 ** session.
9191 ** </ul>
9192 **
9193 ** To clarify, if this function is called and then a changeset constructed
9194 ** using [sqlite3session_changeset()], then after applying that changeset to
9195 ** database zFrom the contents of the two compatible tables would be
@@ -9740,11 +9770,11 @@
9770 ** considered compatible if all of the following are true:
9771 **
9772 ** <ul>
9773 ** <li> The table has the same name as the name recorded in the
9774 ** changeset, and
9775 ** <li> The table has at least as many columns as recorded in the
9776 ** changeset, and
9777 ** <li> The table has primary key columns in the same position as
9778 ** recorded in the changeset.
9779 ** </ul>
9780 **
@@ -9785,11 +9815,15 @@
9815 ** the changeset the row is deleted from the target database.
9816 **
9817 ** If a row with matching primary key values is found, but one or more of
9818 ** the non-primary key fields contains a value different from the original
9819 ** row value stored in the changeset, the conflict-handler function is
9820 ** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
9821 ** database table has more columns than are recorded in the changeset,
9822 ** only the values of those non-primary key fields are compared against
9823 ** the current database contents - any trailing database table columns
9824 ** are ignored.
9825 **
9826 ** If no row with matching primary key values is found in the database,
9827 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
9828 ** passed as the second argument.
9829 **
@@ -9800,11 +9834,13 @@
9834 ** operation is attempted because an earlier call to the conflict handler
9835 ** function returned [SQLITE_CHANGESET_REPLACE].
9836 **
9837 ** <dt>INSERT Changes<dd>
9838 ** For each INSERT change, an attempt is made to insert the new row into
9839 ** the database. If the changeset row contains fewer fields than the
9840 ** database table, the trailing fields are populated with their default
9841 ** values.
9842 **
9843 ** If the attempt to insert the row fails because the database already
9844 ** contains a row with the same primary key values, the conflict handler
9845 ** function is invoked with the second argument set to
9846 ** [SQLITE_CHANGESET_CONFLICT].
@@ -9818,17 +9854,17 @@
9854 **
9855 ** <dt>UPDATE Changes<dd>
9856 ** For each UPDATE change, this function checks if the target database
9857 ** contains a row with the same primary key value (or values) as the
9858 ** original row values stored in the changeset. If it does, and the values
9859 ** stored in all modified non-primary key columns also match the values
9860 ** stored in the changeset the row is updated within the target database.
9861 **
9862 ** If a row with matching primary key values is found, but one or more of
9863 ** the modified non-primary key fields contains a value different from an
9864 ** original row value stored in the changeset, the conflict-handler function
9865 ** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
9866 ** UPDATE changes only contain values for non-primary key fields that are
9867 ** to be modified, only those fields need to match the original values to
9868 ** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
9869 **
9870 ** If no row with matching primary key values is found in the database,
@@ -11537,10 +11573,22 @@
11573 #include <stdlib.h>
11574 #include <string.h>
11575 #include <assert.h>
11576 #include <stddef.h>
11577
11578 /*
11579 ** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY.
11580 ** This allows better measurements of where memcpy() is used when running
11581 ** cachegrind. But this macro version of memcpy() is very slow so it
11582 ** should not be used in production. This is a performance measurement
11583 ** hack only.
11584 */
11585 #ifdef SQLITE_INLINE_MEMCPY
11586 # define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\
11587 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
11588 #endif
11589
11590 /*
11591 ** If compiling for a processor that lacks floating point support,
11592 ** substitute integer for floating-point
11593 */
11594 #ifdef SQLITE_OMIT_FLOATING_POINT
@@ -11621,13 +11669,16 @@
11669 /*
11670 ** The default initial allocation for the pagecache when using separate
11671 ** pagecaches for each database connection. A positive number is the
11672 ** number of pages. A negative number N translations means that a buffer
11673 ** of -1024*N bytes is allocated and used for as many pages as it will hold.
11674 **
11675 ** The default value of "20" was choosen to minimize the run-time of the
11676 ** speedtest1 test program with options: --shrink-memory --reprepare
11677 */
11678 #ifndef SQLITE_DEFAULT_PCACHE_INITSZ
11679 # define SQLITE_DEFAULT_PCACHE_INITSZ 20
11680 #endif
11681
11682 /*
11683 ** GCC does not define the offsetof() macro so we'll have to do it
11684 ** ourselves.
@@ -12346,13 +12397,14 @@
12397 );
12398 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*);
12399 SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*);
12400 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, u8 flags);
12401
12402 /* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */
12403 #define BTREE_SAVEPOSITION 0x02 /* Leave cursor pointing at NEXT or PREV */
12404 #define BTREE_AUXDELETE 0x04 /* not the primary delete operation */
12405 #define BTREE_APPEND 0x08 /* Insert is likely an append */
12406
12407 /* An instance of the BtreePayload object describes the content of a single
12408 ** entry in either an index or table btree.
12409 **
12410 ** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
@@ -12379,11 +12431,11 @@
12431 int nData; /* Size of pData. 0 if none. */
12432 int nZero; /* Extra zero data appended after pData,nData */
12433 };
12434
12435 SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
12436 int flags, int seekResult);
12437 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
12438 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
12439 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
12440 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
12441 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
@@ -12512,12 +12564,11 @@
12564 ** as an instance of the following structure:
12565 */
12566 struct VdbeOp {
12567 u8 opcode; /* What operation to perform */
12568 signed char p4type; /* One of the P4_xxx constants for p4 */
12569 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
 
12570 int p1; /* First operand */
12571 int p2; /* Second parameter (often the jump destination) */
12572 int p3; /* The third parameter */
12573 union p4union { /* fourth parameter */
12574 int i; /* Integer value if p4type==P4_INT32 */
@@ -12874,11 +12925,11 @@
12925 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
12926 SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8);
12927 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
12928 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
12929 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
12930 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
12931 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
12932 SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
12933 SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
12934 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
12935 SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
@@ -13176,18 +13227,20 @@
13227 SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
13228 SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
13229 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
13230 SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
13231 SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
13232 # ifdef SQLITE_DIRECT_OVERFLOW_READ
13233 SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager, Pgno);
13234 # endif
13235 # ifdef SQLITE_ENABLE_SNAPSHOT
13236 SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot);
13237 SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot);
13238 SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager);
13239 # endif
13240 #else
13241 # define sqlite3PagerUseWal(x,y) 0
13242 #endif
13243
13244 #ifdef SQLITE_ENABLE_ZIPVFS
13245 SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager);
13246 #endif
@@ -14007,10 +14060,11 @@
14060 signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
14061 u8 suppressErr; /* Do not issue error messages if true */
14062 u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
14063 u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
14064 u8 mTrace; /* zero or more SQLITE_TRACE flags */
14065 u8 skipBtreeMutex; /* True if no shared-cache backends */
14066 int nextPagesize; /* Pagesize after VACUUM if >0 */
14067 u32 magic; /* Magic number for detect library misuse */
14068 int nChange; /* Value returned by sqlite3_changes() */
14069 int nTotalChange; /* Value returned by sqlite3_total_changes() */
14070 int aLimit[SQLITE_N_LIMIT]; /* Limits */
@@ -14272,10 +14326,11 @@
14326 #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
14327 #define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
14328 #define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */
14329 #define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a
14330 ** single query - might change over time */
14331 #define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
14332
14333 /*
14334 ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
14335 ** used to create the initializers for the FuncDef structures.
14336 **
@@ -15278,11 +15333,11 @@
15333 #define WHERE_DISTINCTBY 0x0080 /* pOrderby is really a DISTINCT clause */
15334 #define WHERE_WANT_DISTINCT 0x0100 /* All output needs to be distinct */
15335 #define WHERE_SORTBYGROUP 0x0200 /* Support sqlite3WhereIsSorted() */
15336 #define WHERE_SEEK_TABLE 0x0400 /* Do not defer seeks on main table */
15337 #define WHERE_ORDERBY_LIMIT 0x0800 /* ORDERBY+LIMIT on the inner loop */
15338 #define WHERE_SEEK_UNIQ_TABLE 0x1000 /* Do not defer seeks if unique */
15339 /* 0x2000 not currently used */
15340 #define WHERE_USE_LIMIT 0x4000 /* Use the LIMIT in cost estimates */
15341 /* 0x8000 not currently used */
15342
15343 /* Allowed return values from sqlite3WhereIsDistinct()
@@ -15739,25 +15794,23 @@
15794 ** OPFLAG_AUXDELETE == BTREE_AUXDELETE
15795 */
15796 #define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
15797 /* Also used in P2 (not P5) of OP_Delete */
15798 #define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */
15799 #define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
15800 #define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */
15801 #define OPFLAG_APPEND 0x08 /* This is likely to be an append */
15802 #define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
 
15803 #define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
 
15804 #define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */
15805 #define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */
15806 #define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */
15807 #define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */
15808 #define OPFLAG_FORDELETE 0x08 /* OP_Open should use BTREE_FORDELETE */
15809 #define OPFLAG_P2ISREG 0x10 /* P2 to OP_Open** is a register number */
15810 #define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */
15811 #define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete/Insert: save cursor pos */
15812 #define OPFLAG_AUXDELETE 0x04 /* OP_Delete: index in a DELETE op */
15813
15814 /*
15815 * Each trigger present in the database schema is stored as an instance of
15816 * struct Trigger.
@@ -16414,11 +16467,11 @@
16467 SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
16468 SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
16469 SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int);
16470 SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, Expr*, int);
16471 SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
16472 SQLITE_PRIVATE int sqlite3ExprCodeAtInit(Parse*, Expr*, int);
16473 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
16474 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
16475 SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
16476 SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
16477 #define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
@@ -16476,10 +16529,15 @@
16529 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
16530 SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
16531 SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
16532 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
16533 u8,u8,int,int*,int*);
16534 #ifdef SQLITE_ENABLE_NULL_TRIM
16535 SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe*,Table*);
16536 #else
16537 # define sqlite3SetMakeRecordP5(A,B)
16538 #endif
16539 SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
16540 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
16541 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
16542 SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
16543 SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
@@ -16754,12 +16812,14 @@
16812 #endif
16813
16814 /*
16815 ** The interface to the LEMON-generated parser
16816 */
16817 #ifndef SQLITE_AMALGAMATION
16818 SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64));
16819 SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
16820 #endif
16821 SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
16822 #ifdef YYTRACKMAXSTACKDEPTH
16823 SQLITE_PRIVATE int sqlite3ParserStackPeak(void*);
16824 #endif
16825
@@ -16865,10 +16925,11 @@
16925 #define sqlite3FkActions(a,b,c,d,e,f)
16926 #define sqlite3FkCheck(a,b,c,d,e,f)
16927 #define sqlite3FkDropTable(a,b,c)
16928 #define sqlite3FkOldmask(a,b) 0
16929 #define sqlite3FkRequired(a,b,c,d) 0
16930 #define sqlite3FkReferences(a) 0
16931 #endif
16932 #ifndef SQLITE_OMIT_FOREIGN_KEY
16933 SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *, Table*);
16934 SQLITE_PRIVATE int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
16935 #else
@@ -17193,10 +17254,23 @@
17254 ** setting.)
17255 */
17256 #ifndef SQLITE_STMTJRNL_SPILL
17257 # define SQLITE_STMTJRNL_SPILL (64*1024)
17258 #endif
17259
17260 /*
17261 ** The default lookaside-configuration, the format "SZ,N". SZ is the
17262 ** number of bytes in each lookaside slot (should be a multiple of 8)
17263 ** and N is the number of slots. The lookaside-configuration can be
17264 ** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
17265 ** or at run-time for an individual database connection using
17266 ** sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE);
17267 */
17268 #ifndef SQLITE_DEFAULT_LOOKASIDE
17269 # define SQLITE_DEFAULT_LOOKASIDE 1200,100
17270 #endif
17271
17272
17273 /*
17274 ** The following singleton contains the global configuration for
17275 ** the SQLite library.
17276 */
@@ -17206,12 +17280,11 @@
17280 SQLITE_THREADSAFE==1, /* bFullMutex */
17281 SQLITE_USE_URI, /* bOpenUri */
17282 SQLITE_ALLOW_COVERING_INDEX_SCAN, /* bUseCis */
17283 0x7ffffffe, /* mxStrlen */
17284 0, /* neverCorrupt */
17285 SQLITE_DEFAULT_LOOKASIDE, /* szLookaside, nLookaside */
 
17286 SQLITE_STMTJRNL_SPILL, /* nStmtSpill */
17287 {0,0,0,0,0,0,0,0}, /* m */
17288 {0,0,0,0,0,0,0,0,0}, /* mutex */
17289 {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
17290 (void*)0, /* pHeap */
@@ -18219,10 +18292,11 @@
18292 int iNewReg; /* Register for new.* values */
18293 i64 iKey1; /* First key value passed to hook */
18294 i64 iKey2; /* Second key value passed to hook */
18295 Mem *aNew; /* Array of new.* values */
18296 Table *pTab; /* Schema object being upated */
18297 Index *pPk; /* PK index if pTab is WITHOUT ROWID */
18298 };
18299
18300 /*
18301 ** Function prototypes
18302 */
@@ -24283,39 +24357,38 @@
24357
24358 /*
24359 ** Do a memory allocation with statistics and alarms. Assume the
24360 ** lock is already held.
24361 */
24362 static void mallocWithAlarm(int n, void **pp){
 
24363 void *p;
24364 int nFull = 0;
24365 assert( sqlite3_mutex_held(mem0.mutex) );
 
24366 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
24367 if( mem0.alarmThreshold>0 ){
24368 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
24369 nFull = sqlite3GlobalConfig.m.xRoundup(n);
24370 if( nUsed >= mem0.alarmThreshold - nFull ){
24371 mem0.nearlyFull = 1;
24372 sqlite3MallocAlarm(nFull);
24373 }else{
24374 mem0.nearlyFull = 0;
24375 }
24376 }
24377 p = sqlite3GlobalConfig.m.xMalloc(n);
24378 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
24379 if( p==0 && mem0.alarmThreshold>0 ){
24380 sqlite3MallocAlarm(nFull);
24381 p = sqlite3GlobalConfig.m.xMalloc(n);
24382 }
24383 #endif
24384 if( p ){
24385 nFull = sqlite3MallocSize(p);
24386 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
24387 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
24388 }
24389 *pp = p;
 
24390 }
24391
24392 /*
24393 ** Allocate memory. This routine is like sqlite3_malloc() except that it
24394 ** assumes the memory subsystem has already been initialized.
@@ -24951,11 +25024,10 @@
25024
25025 /*
25026 ** Allowed values for et_info.flags
25027 */
25028 #define FLAG_SIGNED 1 /* True if the value to convert is signed */
 
25029 #define FLAG_STRING 4 /* Allow infinity precision */
25030
25031
25032 /*
25033 ** The following table is searched linearly, so it is good to put the
@@ -24985,15 +25057,14 @@
25057 { 'i', 10, 1, etRADIX, 0, 0 },
25058 { 'n', 0, 0, etSIZE, 0, 0 },
25059 { '%', 0, 0, etPERCENT, 0, 0 },
25060 { 'p', 16, 0, etPOINTER, 0, 1 },
25061
25062 /* All the rest are undocumented and are for internal use only */
25063 { 'T', 0, 0, etTOKEN, 0, 0 },
25064 { 'S', 0, 0, etSRCLIST, 0, 0 },
25065 { 'r', 10, 1, etORDINAL, 0, 0 },
 
25066 };
25067
25068 /*
25069 ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
25070 ** conversions will work.
@@ -25083,11 +25154,10 @@
25154 etByte flag_long; /* True if "l" flag is present */
25155 etByte flag_longlong; /* True if the "ll" flag is present */
25156 etByte done; /* Loop termination flag */
25157 etByte xtype = etINVALID; /* Conversion paradigm */
25158 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
 
25159 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
25160 sqlite_uint64 longvalue; /* Value for integer types */
25161 LONGDOUBLE_TYPE realvalue; /* Value for real types */
25162 const et_info *infop; /* Pointer to the appropriate info structure */
25163 char *zOut; /* Rendering buffer */
@@ -25102,17 +25172,15 @@
25172 #endif
25173 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
25174 char buf[etBUFSIZE]; /* Conversion buffer */
25175
25176 bufpt = 0;
25177 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
25178 pArgList = va_arg(ap, PrintfArguments*);
25179 bArgList = 1;
 
 
25180 }else{
25181 bArgList = 0;
25182 }
25183 for(; (c=(*fmt))!=0; ++fmt){
25184 if( c!='%' ){
25185 bufpt = (char *)fmt;
25186 #if HAVE_STRCHRNUL
@@ -25220,15 +25288,11 @@
25288 infop = &fmtinfo[0];
25289 xtype = etINVALID;
25290 for(idx=0; idx<ArraySize(fmtinfo); idx++){
25291 if( c==fmtinfo[idx].fmttype ){
25292 infop = &fmtinfo[idx];
25293 xtype = infop->type;
 
 
 
 
25294 break;
25295 }
25296 }
25297
25298 /*
@@ -25593,22 +25657,28 @@
25657 ** consume, not the length of the output...
25658 ** if( precision>=0 && precision<length ) length = precision; */
25659 break;
25660 }
25661 case etTOKEN: {
25662 Token *pToken;
25663 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
25664 pToken = va_arg(ap, Token*);
25665 assert( bArgList==0 );
25666 if( pToken && pToken->n ){
25667 sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
25668 }
25669 length = width = 0;
25670 break;
25671 }
25672 case etSRCLIST: {
25673 SrcList *pSrc;
25674 int k;
25675 struct SrcList_item *pItem;
25676 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
25677 pSrc = va_arg(ap, SrcList*);
25678 k = va_arg(ap, int);
25679 pItem = &pSrc->a[k];
25680 assert( bArgList==0 );
25681 assert( k>=0 && k<pSrc->nSrc );
25682 if( pItem->zDatabase ){
25683 sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
25684 sqlite3StrAccumAppend(pAccum, ".", 1);
@@ -25626,13 +25696,17 @@
25696 ** The text of the conversion is pointed to by "bufpt" and is
25697 ** "length" characters long. The field width is "width". Do
25698 ** the output.
25699 */
25700 width -= length;
25701 if( width>0 ){
25702 if( !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
25703 sqlite3StrAccumAppend(pAccum, bufpt, length);
25704 if( flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
25705 }else{
25706 sqlite3StrAccumAppend(pAccum, bufpt, length);
25707 }
25708
25709 if( zExtra ){
25710 sqlite3DbFree(pAccum->db, zExtra);
25711 zExtra = 0;
25712 }
@@ -28601,11 +28675,11 @@
28675 #if SQLITE_BYTEORDER==4321
28676 u32 x;
28677 memcpy(&x,p,4);
28678 return x;
28679 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
28680 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
28681 u32 x;
28682 memcpy(&x,p,4);
28683 return __builtin_bswap32(x);
28684 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
28685 && defined(_MSC_VER) && _MSC_VER>=1300
@@ -28619,11 +28693,11 @@
28693 }
28694 SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
28695 #if SQLITE_BYTEORDER==4321
28696 memcpy(p,&v,4);
28697 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
28698 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
28699 u32 x = __builtin_bswap32(v);
28700 memcpy(p,&x,4);
28701 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
28702 && defined(_MSC_VER) && _MSC_VER>=1300
28703 u32 x = _byteswap_ulong(v);
@@ -28739,10 +28813,14 @@
28813 ** the other 64-bit signed integer at *pA and store the result in *pA.
28814 ** Return 0 on success. Or if the operation would have resulted in an
28815 ** overflow, leave *pA unchanged and return 1.
28816 */
28817 SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
28818 #if !defined(SQLITE_DISABLE_INTRINSIC) \
28819 && (GCC_VERSION>=5004000 || CLANG_VERSION>=4000000)
28820 return __builtin_add_overflow(*pA, iB, pA);
28821 #else
28822 i64 iA = *pA;
28823 testcase( iA==0 ); testcase( iA==1 );
28824 testcase( iB==-1 ); testcase( iB==0 );
28825 if( iB>=0 ){
28826 testcase( iA>0 && LARGEST_INT64 - iA == iB );
@@ -28753,23 +28831,33 @@
28831 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
28832 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
28833 }
28834 *pA += iB;
28835 return 0;
28836 #endif
28837 }
28838 SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
28839 #if !defined(SQLITE_DISABLE_INTRINSIC) \
28840 && (GCC_VERSION>=5004000 || CLANG_VERSION>=4000000)
28841 return __builtin_sub_overflow(*pA, iB, pA);
28842 #else
28843 testcase( iB==SMALLEST_INT64+1 );
28844 if( iB==SMALLEST_INT64 ){
28845 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
28846 if( (*pA)>=0 ) return 1;
28847 *pA -= iB;
28848 return 0;
28849 }else{
28850 return sqlite3AddInt64(pA, -iB);
28851 }
28852 #endif
28853 }
28854 SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
28855 #if !defined(SQLITE_DISABLE_INTRINSIC) \
28856 && (GCC_VERSION>=5004000 || CLANG_VERSION>=4000000)
28857 return __builtin_mul_overflow(*pA, iB, pA);
28858 #else
28859 i64 iA = *pA;
28860 if( iB>0 ){
28861 if( iA>LARGEST_INT64/iB ) return 1;
28862 if( iA<SMALLEST_INT64/iB ) return 1;
28863 }else if( iB<0 ){
@@ -28781,10 +28869,11 @@
28869 if( -iA>LARGEST_INT64/-iB ) return 1;
28870 }
28871 }
28872 *pA = iA*iB;
28873 return 0;
28874 #endif
28875 }
28876
28877 /*
28878 ** Compute the absolute value of a 32-bit signed integer, of possible. Or
28879 ** if the integer has a value of -2147483648, return +2147483647
@@ -47485,18 +47574,24 @@
47574 ** if( pPager->jfd->pMethods ){ ...
47575 */
47576 #define isOpen(pFd) ((pFd)->pMethods!=0)
47577
47578 /*
47579 ** Return true if this pager uses a write-ahead log to read page pgno.
47580 ** Return false if the pager reads pgno directly from the database.
47581 */
47582 #if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_DIRECT_OVERFLOW_READ)
47583 SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager, Pgno pgno){
47584 u32 iRead = 0;
47585 int rc;
47586 if( pPager->pWal==0 ) return 0;
47587 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
47588 return rc || iRead;
47589 }
47590 #endif
47591 #ifndef SQLITE_OMIT_WAL
47592 # define pagerUseWal(x) ((x)->pWal!=0)
 
 
 
47593 #else
47594 # define pagerUseWal(x) 0
47595 # define pagerRollbackWal(x) 0
47596 # define pagerWalFrames(v,w,x,y) 0
47597 # define pagerOpenWalIfPresent(z) SQLITE_OK
@@ -58624,27 +58719,38 @@
58719 ** Enter the mutexes in accending order by BtShared pointer address
58720 ** to avoid the possibility of deadlock when two threads with
58721 ** two or more btrees in common both try to lock all their btrees
58722 ** at the same instant.
58723 */
58724 static void SQLITE_NOINLINE btreeEnterAll(sqlite3 *db){
58725 int i;
58726 int skipOk = 1;
58727 Btree *p;
58728 assert( sqlite3_mutex_held(db->mutex) );
58729 for(i=0; i<db->nDb; i++){
58730 p = db->aDb[i].pBt;
58731 if( p && p->sharable ){
58732 sqlite3BtreeEnter(p);
58733 skipOk = 0;
58734 }
58735 }
58736 db->skipBtreeMutex = skipOk;
58737 }
58738 SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
58739 if( db->skipBtreeMutex==0 ) btreeEnterAll(db);
58740 }
58741 static void SQLITE_NOINLINE btreeLeaveAll(sqlite3 *db){
58742 int i;
58743 Btree *p;
58744 assert( sqlite3_mutex_held(db->mutex) );
58745 for(i=0; i<db->nDb; i++){
58746 p = db->aDb[i].pBt;
58747 if( p ) sqlite3BtreeLeave(p);
58748 }
58749 }
58750 SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
58751 if( db->skipBtreeMutex==0 ) btreeLeaveAll(db);
58752 }
58753
58754 #ifndef NDEBUG
58755 /*
58756 ** Return true if the current thread holds the database connection
@@ -62097,16 +62203,18 @@
62203 for(i=0; i<nCell; i++){
62204 u8 *pCell = findCell(pPage, i);
62205 if( eType==PTRMAP_OVERFLOW1 ){
62206 CellInfo info;
62207 pPage->xParseCell(pPage, pCell, &info);
62208 if( info.nLocal<info.nPayload ){
62209 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
62210 return SQLITE_CORRUPT_BKPT;
62211 }
62212 if( iFrom==get4byte(pCell+info.nSize-4) ){
62213 put4byte(pCell+info.nSize-4, iTo);
62214 break;
62215 }
62216 }
62217 }else{
62218 if( get4byte(pCell)==iFrom ){
62219 put4byte(pCell, iTo);
62220 break;
@@ -62777,11 +62885,16 @@
62885 if( p && p->inTrans==TRANS_WRITE ){
62886 BtShared *pBt = p->pBt;
62887 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
62888 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
62889 sqlite3BtreeEnter(p);
62890 if( op==SAVEPOINT_ROLLBACK ){
62891 rc = saveAllCursors(pBt, 0, 0);
62892 }
62893 if( rc==SQLITE_OK ){
62894 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
62895 }
62896 if( rc==SQLITE_OK ){
62897 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
62898 pBt->nPage = 0;
62899 }
62900 rc = newDatabase(pBt);
@@ -63163,25 +63276,24 @@
63276 ** for the entry that the pCur cursor is pointing to. The eOp
63277 ** argument is interpreted as follows:
63278 **
63279 ** 0: The operation is a read. Populate the overflow cache.
63280 ** 1: The operation is a write. Populate the overflow cache.
 
63281 **
63282 ** A total of "amt" bytes are read or written beginning at "offset".
63283 ** Data is read to or from the buffer pBuf.
63284 **
63285 ** The content being read or written might appear on the main page
63286 ** or be scattered out on multiple overflow pages.
63287 **
63288 ** If the current cursor entry uses one or more overflow pages
63289 ** this function may allocate space for and lazily populate
63290 ** the overflow page-list cache array (BtCursor.aOverflow).
63291 ** Subsequent calls use this cache to make seeking to the supplied offset
63292 ** more efficient.
63293 **
63294 ** Once an overflow page-list cache has been allocated, it must be
63295 ** invalidated if some other cursor writes to the same table, or if
63296 ** the cursor is moved to a different row. Additionally, in auto-vacuum
63297 ** mode, the following events may invalidate an overflow page-list cache.
63298 **
63299 ** * An incremental vacuum,
@@ -63199,25 +63311,21 @@
63311 int rc = SQLITE_OK;
63312 int iIdx = 0;
63313 MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
63314 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
63315 #ifdef SQLITE_DIRECT_OVERFLOW_READ
63316 unsigned char * const pBufStart = pBuf; /* Start of original out buffer */
 
63317 #endif
63318
63319 assert( pPage );
63320 assert( eOp==0 || eOp==1 );
63321 assert( pCur->eState==CURSOR_VALID );
63322 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
63323 assert( cursorHoldsMutex(pCur) );
 
63324
63325 getCellInfo(pCur);
63326 aPayload = pCur->info.pPayload;
 
 
 
63327 assert( offset+amt <= pCur->info.nPayload );
63328
63329 assert( aPayload > pPage->aData );
63330 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
63331 /* Trying to read or write past the end of the data is an error. The
@@ -63232,11 +63340,11 @@
63340 if( offset<pCur->info.nLocal ){
63341 int a = amt;
63342 if( a+offset>pCur->info.nLocal ){
63343 a = pCur->info.nLocal - offset;
63344 }
63345 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
63346 offset = 0;
63347 pBuf += a;
63348 amt -= a;
63349 }else{
63350 offset -= pCur->info.nLocal;
@@ -63248,69 +63356,58 @@
63356 Pgno nextPage;
63357
63358 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
63359
63360 /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
 
63361 **
63362 ** The aOverflow[] array is sized at one entry for each overflow page
63363 ** in the overflow chain. The page number of the first overflow page is
63364 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
63365 ** means "not yet known" (the cache is lazily populated).
63366 */
63367 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
63368 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
63369 if( nOvfl>pCur->nOvflAlloc ){
63370 Pgno *aNew = (Pgno*)sqlite3Realloc(
63371 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
63372 );
63373 if( aNew==0 ){
63374 return SQLITE_NOMEM_BKPT;
63375 }else{
63376 pCur->nOvflAlloc = nOvfl*2;
63377 pCur->aOverflow = aNew;
63378 }
63379 }
63380 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
63381 pCur->curFlags |= BTCF_ValidOvfl;
63382 }else{
63383 /* If the overflow page-list cache has been allocated and the
63384 ** entry for the first required overflow page is valid, skip
63385 ** directly to it.
63386 */
63387 if( pCur->aOverflow[offset/ovflSize] ){
63388 iIdx = (offset/ovflSize);
63389 nextPage = pCur->aOverflow[iIdx];
63390 offset = (offset%ovflSize);
63391 }
63392 }
63393
63394 assert( rc==SQLITE_OK && amt>0 );
63395 while( nextPage ){
 
 
 
 
 
 
 
 
 
 
 
 
63396 /* If required, populate the overflow page-list cache. */
63397 assert( pCur->aOverflow[iIdx]==0
63398 || pCur->aOverflow[iIdx]==nextPage
63399 || CORRUPT_DB );
63400 pCur->aOverflow[iIdx] = nextPage;
 
 
63401
63402 if( offset>=ovflSize ){
63403 /* The only reason to read this page is to obtain the page
63404 ** number for the next page in the overflow chain. The page
63405 ** data is not required. So first try to lookup the overflow
63406 ** page-list cache, if any, then fall back to the getOverflowPage()
63407 ** function.
 
 
 
63408 */
 
63409 assert( pCur->curFlags & BTCF_ValidOvfl );
63410 assert( pCur->pBtree->db==pBt->db );
63411 if( pCur->aOverflow[iIdx+1] ){
63412 nextPage = pCur->aOverflow[iIdx+1];
63413 }else{
@@ -63320,11 +63417,11 @@
63417 }else{
63418 /* Need to read this page properly. It contains some of the
63419 ** range of data that is being read (eOp==0) or written (eOp!=0).
63420 */
63421 #ifdef SQLITE_DIRECT_OVERFLOW_READ
63422 sqlite3_file *fd; /* File from which to do direct overflow read */
63423 #endif
63424 int a = amt;
63425 if( a + offset > ovflSize ){
63426 a = ovflSize - offset;
63427 }
@@ -63332,31 +63429,29 @@
63429 #ifdef SQLITE_DIRECT_OVERFLOW_READ
63430 /* If all the following are true:
63431 **
63432 ** 1) this is a read operation, and
63433 ** 2) data is required from the start of this overflow page, and
63434 ** 3) there is no open write-transaction, and
63435 ** 4) the database is file-backed, and
63436 ** 5) the page is not in the WAL file
63437 ** 6) at least 4 bytes have already been read into the output buffer
 
63438 **
63439 ** then data can be read directly from the database file into the
63440 ** output buffer, bypassing the page-cache altogether. This speeds
63441 ** up loading large records that span many overflow pages.
63442 */
63443 if( eOp==0 /* (1) */
63444 && offset==0 /* (2) */
63445 && pBt->inTransaction==TRANS_READ /* (3) */
63446 && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (4) */
63447 && 0==sqlite3PagerUseWal(pBt->pPager, nextPage) /* (5) */
63448 && &pBuf[-4]>=pBufStart /* (6) */
 
63449 ){
63450 u8 aSave[4];
63451 u8 *aWrite = &pBuf[-4];
63452 assert( aWrite>=pBufStart ); /* due to (6) */
63453 memcpy(aSave, aWrite, 4);
63454 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
63455 nextPage = get4byte(aWrite);
63456 memcpy(aWrite, aSave, 4);
63457 }else
@@ -63363,28 +63458,31 @@
63458 #endif
63459
63460 {
63461 DbPage *pDbPage;
63462 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
63463 (eOp==0 ? PAGER_GET_READONLY : 0)
63464 );
63465 if( rc==SQLITE_OK ){
63466 aPayload = sqlite3PagerGetData(pDbPage);
63467 nextPage = get4byte(aPayload);
63468 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
63469 sqlite3PagerUnref(pDbPage);
63470 offset = 0;
63471 }
63472 }
63473 amt -= a;
63474 if( amt==0 ) return rc;
63475 pBuf += a;
63476 }
63477 if( rc ) break;
63478 iIdx++;
63479 }
63480 }
63481
63482 if( rc==SQLITE_OK && amt>0 ){
63483 return SQLITE_CORRUPT_BKPT; /* Overflow chain ends prematurely */
63484 }
63485 return rc;
63486 }
63487
63488 /*
@@ -63409,25 +63507,38 @@
63507 assert( pCur->eState==CURSOR_VALID );
63508 assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
63509 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
63510 return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
63511 }
63512
63513 /*
63514 ** This variant of sqlite3BtreePayload() works even if the cursor has not
63515 ** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read()
63516 ** interface.
63517 */
63518 #ifndef SQLITE_OMIT_INCRBLOB
63519 static SQLITE_NOINLINE int accessPayloadChecked(
63520 BtCursor *pCur,
63521 u32 offset,
63522 u32 amt,
63523 void *pBuf
63524 ){
63525 int rc;
63526 if ( pCur->eState==CURSOR_INVALID ){
63527 return SQLITE_ABORT;
63528 }
63529 assert( cursorOwnsBtShared(pCur) );
63530 rc = btreeRestoreCursorPosition(pCur);
63531 return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
63532 }
63533 SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
63534 if( pCur->eState==CURSOR_VALID ){
63535 assert( cursorOwnsBtShared(pCur) );
63536 return accessPayload(pCur, offset, amt, pBuf, 0);
63537 }else{
63538 return accessPayloadChecked(pCur, offset, amt, pBuf);
63539 }
63540 }
63541 #endif /* SQLITE_OMIT_INCRBLOB */
63542
63543 /*
63544 ** Return a pointer to payload information from the entry that the
@@ -63829,13 +63940,30 @@
63940 ){
63941 if( pCur->info.nKey==intKey ){
63942 *pRes = 0;
63943 return SQLITE_OK;
63944 }
63945 if( pCur->info.nKey<intKey ){
63946 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
63947 *pRes = -1;
63948 return SQLITE_OK;
63949 }
63950 /* If the requested key is one more than the previous key, then
63951 ** try to get there using sqlite3BtreeNext() rather than a full
63952 ** binary search. This is an optimization only. The correct answer
63953 ** is still obtained without this ase, only a little more slowely */
63954 if( pCur->info.nKey+1==intKey && !pCur->skipNext ){
63955 *pRes = 0;
63956 rc = sqlite3BtreeNext(pCur, pRes);
63957 if( rc ) return rc;
63958 if( *pRes==0 ){
63959 getCellInfo(pCur);
63960 if( pCur->info.nKey==intKey ){
63961 return SQLITE_OK;
63962 }
63963 }
63964 }
63965 }
63966 }
63967
63968 if( pIdxKey ){
63969 xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
@@ -63967,11 +64095,12 @@
64095 if( pCellKey==0 ){
64096 rc = SQLITE_NOMEM_BKPT;
64097 goto moveto_finish;
64098 }
64099 pCur->aiIdx[pCur->iPage] = (u16)idx;
64100 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
64101 pCur->curFlags &= ~BTCF_ValidOvfl;
64102 if( rc ){
64103 sqlite3_free(pCellKey);
64104 goto moveto_finish;
64105 }
64106 c = xRecordCompare(nCell, pCellKey, pIdxKey);
@@ -66010,11 +66139,10 @@
66139 */
66140 usableSpace = pBt->usableSize - 12 + leafCorrection;
66141 for(i=0; i<nOld; i++){
66142 MemPage *p = apOld[i];
66143 szNew[i] = usableSpace - p->nFree;
 
66144 for(j=0; j<p->nOverflow; j++){
66145 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
66146 }
66147 cntNew[i] = cntOld[i];
66148 }
@@ -66689,11 +66817,11 @@
66817 ** to decode the key.
66818 */
66819 SQLITE_PRIVATE int sqlite3BtreeInsert(
66820 BtCursor *pCur, /* Insert data into the table of this cursor */
66821 const BtreePayload *pX, /* Content of the row to be inserted */
66822 int flags, /* True if this is likely an append */
66823 int seekResult /* Result of prior MovetoUnpacked() call */
66824 ){
66825 int rc;
66826 int loc = seekResult; /* -1: before desired location +1: after */
66827 int szNew = 0;
@@ -66701,10 +66829,12 @@
66829 MemPage *pPage;
66830 Btree *p = pCur->pBtree;
66831 BtShared *pBt = p->pBt;
66832 unsigned char *oldCell;
66833 unsigned char *newCell = 0;
66834
66835 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND))==flags );
66836
66837 if( pCur->eState==CURSOR_FAULT ){
66838 assert( pCur->skipNext!=SQLITE_OK );
66839 return pCur->skipNext;
66840 }
@@ -66742,23 +66872,28 @@
66872 assert( pX->pKey==0 );
66873 /* If this is an insert into a table b-tree, invalidate any incrblob
66874 ** cursors open on the row being replaced */
66875 invalidateIncrblobCursors(p, pX->nKey, 0);
66876
66877 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
66878 ** to a row with the same key as the new entry being inserted. */
66879 assert( (flags & BTREE_SAVEPOSITION)==0 ||
66880 ((pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey) );
66881
66882 /* If the cursor is currently on the last row and we are appending a
66883 ** new row onto the end, set the "loc" to avoid an unnecessary
66884 ** btreeMoveto() call */
66885 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
66886 loc = 0;
66887 }else if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey>0
66888 && pCur->info.nKey==pX->nKey-1 ){
66889 loc = -1;
66890 }else if( loc==0 ){
66891 rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
66892 if( rc ) return rc;
66893 }
66894 }else if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
66895 if( pX->nMem ){
66896 UnpackedRecord r;
66897 r.pKeyInfo = pCur->pKeyInfo;
66898 r.aMem = pX->aMem;
66899 r.nField = pX->nMem;
@@ -66765,13 +66900,13 @@
66900 r.default_rc = 0;
66901 r.errCode = 0;
66902 r.r1 = 0;
66903 r.r2 = 0;
66904 r.eqSeen = 0;
66905 rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc);
66906 }else{
66907 rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
66908 }
66909 if( rc ) return rc;
66910 }
66911 assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
66912
@@ -66855,10 +66990,24 @@
66990 ** fails. Internal data structure corruption will result otherwise.
66991 ** Also, set the cursor state to invalid. This stops saveCursorPosition()
66992 ** from trying to save the current position of the cursor. */
66993 pCur->apPage[pCur->iPage]->nOverflow = 0;
66994 pCur->eState = CURSOR_INVALID;
66995 if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){
66996 rc = moveToRoot(pCur);
66997 if( pCur->pKeyInfo ){
66998 assert( pCur->pKey==0 );
66999 pCur->pKey = sqlite3Malloc( pX->nKey );
67000 if( pCur->pKey==0 ){
67001 rc = SQLITE_NOMEM;
67002 }else{
67003 memcpy(pCur->pKey, pX->pKey, pX->nKey);
67004 }
67005 }
67006 pCur->eState = CURSOR_REQUIRESEEK;
67007 pCur->nKey = pX->nKey;
67008 }
67009 }
67010 assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
67011
67012 end_insert:
67013 return rc;
@@ -71765,11 +71914,11 @@
71914 sqlite3VdbeGetOp(p,addr)->p2 = val;
71915 }
71916 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
71917 sqlite3VdbeGetOp(p,addr)->p3 = val;
71918 }
71919 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
71920 assert( p->nOp>0 || p->db->mallocFailed );
71921 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
71922 }
71923
71924 /*
@@ -73479,64 +73628,63 @@
73628 ** statement transaction is committed.
73629 **
73630 ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
73631 ** Otherwise SQLITE_OK.
73632 */
73633 static SQLITE_NOINLINE int vdbeCloseStatement(Vdbe *p, int eOp){
73634 sqlite3 *const db = p->db;
73635 int rc = SQLITE_OK;
73636 int i;
73637 const int iSavepoint = p->iStatement-1;
73638
73639 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
73640 assert( db->nStatement>0 );
73641 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
73642
73643 for(i=0; i<db->nDb; i++){
73644 int rc2 = SQLITE_OK;
73645 Btree *pBt = db->aDb[i].pBt;
73646 if( pBt ){
73647 if( eOp==SAVEPOINT_ROLLBACK ){
73648 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
73649 }
73650 if( rc2==SQLITE_OK ){
73651 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
73652 }
73653 if( rc==SQLITE_OK ){
73654 rc = rc2;
73655 }
73656 }
73657 }
73658 db->nStatement--;
73659 p->iStatement = 0;
73660
73661 if( rc==SQLITE_OK ){
73662 if( eOp==SAVEPOINT_ROLLBACK ){
73663 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
73664 }
73665 if( rc==SQLITE_OK ){
73666 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
73667 }
73668 }
73669
73670 /* If the statement transaction is being rolled back, also restore the
73671 ** database handles deferred constraint counter to the value it had when
73672 ** the statement transaction was opened. */
73673 if( eOp==SAVEPOINT_ROLLBACK ){
73674 db->nDeferredCons = p->nStmtDefCons;
73675 db->nDeferredImmCons = p->nStmtDefImmCons;
73676 }
73677 return rc;
73678 }
73679 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
73680 if( p->db->nStatement && p->iStatement ){
73681 return vdbeCloseStatement(p, eOp);
73682 }
73683 return SQLITE_OK;
73684 }
73685
 
73686
73687 /*
73688 ** This function is called when a transaction opened by the database
73689 ** handle associated with the VM passed as an argument is about to be
73690 ** committed. If there are outstanding deferred foreign key constraint
@@ -75567,14 +75715,14 @@
75715 ** structure itself, using sqlite3DbFree().
75716 **
75717 ** This function is used to free UnpackedRecord structures allocated by
75718 ** the vdbeUnpackRecord() function found in vdbeapi.c.
75719 */
75720 static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
75721 if( p ){
75722 int i;
75723 for(i=0; i<nField; i++){
75724 Mem *pMem = &p->aMem[i];
75725 if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
75726 }
75727 sqlite3DbFree(db, p);
75728 }
@@ -75603,14 +75751,19 @@
75751 const char *zTbl = pTab->zName;
75752 static const u8 fakeSortOrder = 0;
75753
75754 assert( db->pPreUpdate==0 );
75755 memset(&preupdate, 0, sizeof(PreUpdate));
75756 if( HasRowid(pTab)==0 ){
75757 iKey1 = iKey2 = 0;
75758 preupdate.pPk = sqlite3PrimaryKeyIndex(pTab);
75759 }else{
75760 if( op==SQLITE_UPDATE ){
75761 iKey2 = v->aMem[iReg].u.i;
75762 }else{
75763 iKey2 = iKey1;
75764 }
75765 }
75766
75767 assert( pCsr->nField==pTab->nCol
75768 || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
75769 );
@@ -75629,12 +75782,12 @@
75782
75783 db->pPreUpdate = &preupdate;
75784 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
75785 db->pPreUpdate = 0;
75786 sqlite3DbFree(db, preupdate.aRecord);
75787 vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pUnpacked);
75788 vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pNewUnpacked);
75789 if( preupdate.aNew ){
75790 int i;
75791 for(i=0; i<pCsr->nField; i++){
75792 sqlite3VdbeMemRelease(&preupdate.aNew[i]);
75793 }
@@ -77305,18 +77458,22 @@
77458 ** This function is called from within a pre-update callback to retrieve
77459 ** a field of the row currently being updated or deleted.
77460 */
77461 SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
77462 PreUpdate *p = db->pPreUpdate;
77463 Mem *pMem;
77464 int rc = SQLITE_OK;
77465
77466 /* Test that this call is being made from within an SQLITE_DELETE or
77467 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
77468 if( !p || p->op==SQLITE_INSERT ){
77469 rc = SQLITE_MISUSE_BKPT;
77470 goto preupdate_old_out;
77471 }
77472 if( p->pPk ){
77473 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
77474 }
77475 if( iIdx>=p->pCsr->nField || iIdx<0 ){
77476 rc = SQLITE_RANGE;
77477 goto preupdate_old_out;
77478 }
77479
@@ -77338,21 +77495,18 @@
77495 goto preupdate_old_out;
77496 }
77497 p->aRecord = aRec;
77498 }
77499
77500 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
77501 if( iIdx==p->pTab->iPKey ){
77502 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
77503 }else if( iIdx>=p->pUnpacked->nField ){
77504 *ppValue = (sqlite3_value *)columnNullValue();
77505 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
77506 if( pMem->flags & MEM_Int ){
77507 sqlite3VdbeMemRealify(pMem);
 
 
 
 
 
 
77508 }
77509 }
77510
77511 preupdate_old_out:
77512 sqlite3Error(db, rc);
@@ -77401,10 +77555,13 @@
77555
77556 if( !p || p->op==SQLITE_DELETE ){
77557 rc = SQLITE_MISUSE_BKPT;
77558 goto preupdate_new_out;
77559 }
77560 if( p->pPk && p->op!=SQLITE_UPDATE ){
77561 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
77562 }
77563 if( iIdx>=p->pCsr->nField || iIdx<0 ){
77564 rc = SQLITE_RANGE;
77565 goto preupdate_new_out;
77566 }
77567
@@ -77421,17 +77578,15 @@
77578 rc = SQLITE_NOMEM;
77579 goto preupdate_new_out;
77580 }
77581 p->pNewUnpacked = pUnpack;
77582 }
77583 pMem = &pUnpack->aMem[iIdx];
77584 if( iIdx==p->pTab->iPKey ){
77585 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
77586 }else if( iIdx>=pUnpack->nField ){
77587 pMem = (sqlite3_value *)columnNullValue();
 
 
 
 
 
77588 }
77589 }else{
77590 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
77591 ** value. Make a copy of the cell contents and return a pointer to it.
77592 ** It is not safe to return a pointer to the memory cell itself as the
@@ -78404,12 +78559,10 @@
78559 Mem *aMem = p->aMem; /* Copy of p->aMem */
78560 Mem *pIn1 = 0; /* 1st input operand */
78561 Mem *pIn2 = 0; /* 2nd input operand */
78562 Mem *pIn3 = 0; /* 3rd input operand */
78563 Mem *pOut = 0; /* Output operand */
 
 
78564 #ifdef VDBE_PROFILE
78565 u64 start; /* CPU clock count at start of opcode */
78566 #endif
78567 /*** INSERT STACK UNION HERE ***/
78568
@@ -78420,11 +78573,10 @@
78573 ** sqlite3_column_text16() failed. */
78574 goto no_mem;
78575 }
78576 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
78577 assert( p->bIsReader || p->readOnly!=0 );
 
78578 p->iCurrentTime = 0;
78579 assert( p->explain==0 );
78580 p->pResultSet = 0;
78581 db->busyHandler.nBusy = 0;
78582 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
@@ -78781,11 +78933,10 @@
78933 pFrame = p->pFrame;
78934 p->pFrame = pFrame->pParent;
78935 p->nFrame--;
78936 sqlite3VdbeSetChanges(db, p->nChange);
78937 pcx = sqlite3VdbeFrameRestore(pFrame);
 
78938 if( pOp->p2==OE_Ignore ){
78939 /* Instruction pcx is the OP_Program that invoked the sub-program
78940 ** currently being halted. If the p2 instruction of this OP_Halt
78941 ** instruction is set to OE_Ignore, then the sub-program is throwing
78942 ** an IGNORE exception. In this case jump to the address specified
@@ -79016,11 +79167,11 @@
79167 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
79168 pVar = &p->aVar[pOp->p1 - 1];
79169 if( sqlite3VdbeMemTooBig(pVar) ){
79170 goto too_big;
79171 }
79172 pOut = &aMem[pOp->p2];
79173 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
79174 UPDATE_MAX_BLOBSIZE(pOut);
79175 break;
79176 }
79177
@@ -79503,13 +79654,11 @@
79654 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
79655 }
79656 #endif
79657 MemSetTypeFlag(pCtx->pOut, MEM_Null);
79658 pCtx->fErrorOrAux = 0;
 
79659 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
 
79660
79661 /* If the function returned an error, throw an exception */
79662 if( pCtx->fErrorOrAux ){
79663 if( pCtx->isError ){
79664 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
@@ -79961,12 +80110,12 @@
80110 }
80111
80112
80113 /* Opcode: Permutation * * * P4 *
80114 **
80115 ** Set the permutation used by the OP_Compare operator in the next
80116 ** instruction. The permutation is stored in the P4 operand.
80117 **
80118 ** The permutation is only valid until the next OP_Compare that has
80119 ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
80120 ** occur immediately prior to the OP_Compare.
80121 **
@@ -79974,11 +80123,12 @@
80123 ** and does not become part of the permutation.
80124 */
80125 case OP_Permutation: {
80126 assert( pOp->p4type==P4_INTARRAY );
80127 assert( pOp->p4.ai );
80128 assert( pOp[1].opcode==OP_Compare );
80129 assert( pOp[1].p5 & OPFLAG_PERMUTE );
80130 break;
80131 }
80132
80133 /* Opcode: Compare P1 P2 P3 P4 P5
80134 ** Synopsis: r[P1@P3] <-> r[P2@P3]
@@ -80007,12 +80157,21 @@
80157 int p2;
80158 const KeyInfo *pKeyInfo;
80159 int idx;
80160 CollSeq *pColl; /* Collating sequence to use on this term */
80161 int bRev; /* True for DESCENDING sort order */
80162 int *aPermute; /* The permutation */
80163
80164 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
80165 aPermute = 0;
80166 }else{
80167 assert( pOp>aOp );
80168 assert( pOp[-1].opcode==OP_Permutation );
80169 assert( pOp[-1].p4type==P4_INTARRAY );
80170 aPermute = pOp[-1].p4.ai + 1;
80171 assert( aPermute!=0 );
80172 }
80173 n = pOp->p3;
80174 pKeyInfo = pOp->p4.pKeyInfo;
80175 assert( n>0 );
80176 assert( pKeyInfo!=0 );
80177 p1 = pOp->p1;
@@ -80041,11 +80200,10 @@
80200 if( iCompare ){
80201 if( bRev ) iCompare = -iCompare;
80202 break;
80203 }
80204 }
 
80205 break;
80206 }
80207
80208 /* Opcode: Jump P1 P2 P3 * *
80209 **
@@ -80597,10 +80755,24 @@
80755 do{
80756 applyAffinity(pRec++, *(zAffinity++), encoding);
80757 assert( zAffinity[0]==0 || pRec<=pLast );
80758 }while( zAffinity[0] );
80759 }
80760
80761 #ifdef SQLITE_ENABLE_NULL_TRIM
80762 /* NULLs can be safely trimmed from the end of the record, as long as
80763 ** as the schema format is 2 or more and none of the omitted columns
80764 ** have a non-NULL default value. Also, the record must be left with
80765 ** at least one field. If P5>0 then it will be one more than the
80766 ** index of the right-most column with a non-NULL default value */
80767 if( pOp->p5 ){
80768 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
80769 pLast--;
80770 nField--;
80771 }
80772 }
80773 #endif
80774
80775 /* Loop through the elements that will make up the record to figure
80776 ** out how much space is required for the new record.
80777 */
80778 pRec = pLast;
@@ -82187,11 +82359,11 @@
82359 assert( memIsValid(pData) );
82360 pC = p->apCsr[pOp->p1];
82361 assert( pC!=0 );
82362 assert( pC->eCurType==CURTYPE_BTREE );
82363 assert( pC->uc.pCursor!=0 );
82364 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
82365 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
82366 REGISTER_TRACE(pOp->p2, pData);
82367
82368 if( pOp->opcode==OP_Insert ){
82369 pKey = &aMem[pOp->p3];
@@ -82203,18 +82375,17 @@
82375 assert( pOp->opcode==OP_InsertInt );
82376 x.nKey = pOp->p3;
82377 }
82378
82379 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
 
82380 assert( pC->iDb>=0 );
82381 zDb = db->aDb[pC->iDb].zDbSName;
82382 pTab = pOp->p4.pTab;
82383 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
82384 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
82385 }else{
82386 pTab = 0; /* Not needed. Silence a compiler warning. */
82387 zDb = 0; /* Not needed. Silence a compiler warning. */
82388 }
82389
82390 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
82391 /* Invoke the pre-update hook, if any */
@@ -82222,14 +82393,15 @@
82393 && pOp->p4type==P4_TABLE
82394 && !(pOp->p5 & OPFLAG_ISUPDATE)
82395 ){
82396 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey, pOp->p2);
82397 }
82398 if( pOp->p5 & OPFLAG_ISNOOP ) break;
82399 #endif
82400
82401 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
82402 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
82403 if( pData->flags & MEM_Null ){
82404 x.pData = 0;
82405 x.nData = 0;
82406 }else{
82407 assert( pData->flags & (MEM_Blob|MEM_Str) );
@@ -82242,11 +82414,11 @@
82414 }else{
82415 x.nZero = 0;
82416 }
82417 x.pKey = 0;
82418 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
82419 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
82420 );
82421 pC->deferredMoveto = 0;
82422 pC->cacheStatus = CACHE_STALE;
82423
82424 /* Invoke the update-hook if required. */
@@ -82334,12 +82506,15 @@
82506 pTab = 0; /* Not needed. Silence a compiler warning. */
82507 }
82508
82509 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
82510 /* Invoke the pre-update-hook if required. */
82511 if( db->xPreUpdateCallback && pOp->p4.pTab ){
82512 assert( !(opflags & OPFLAG_ISUPDATE)
82513 || HasRowid(pTab)==0
82514 || (aMem[pOp->p3].flags & MEM_Int)
82515 );
82516 sqlite3VdbePreUpdateHook(p, pC,
82517 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
82518 zDb, pTab, pC->movetoTarget,
82519 pOp->p3
82520 );
@@ -82453,11 +82628,11 @@
82628 if( rc ) goto abort_due_to_error;
82629 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
82630 break;
82631 }
82632
82633 /* Opcode: RowData P1 P2 P3 * *
82634 ** Synopsis: r[P2]=data
82635 **
82636 ** Write into register P2 the complete row content for the row at
82637 ** which cursor P1 is currently pointing.
82638 ** There is no interpretation of the data.
@@ -82467,18 +82642,30 @@
82642 ** If cursor P1 is an index, then the content is the key of the row.
82643 ** If cursor P2 is a table, then the content extracted is the data.
82644 **
82645 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
82646 ** of a real table, not a pseudo-table.
82647 **
82648 ** If P3!=0 then this opcode is allowed to make an ephermeral pointer
82649 ** into the database page. That means that the content of the output
82650 ** register will be invalidated as soon as the cursor moves - including
82651 ** moves caused by other cursors that "save" the the current cursors
82652 ** position in order that they can write to the same table. If P3==0
82653 ** then a copy of the data is made into memory. P3!=0 is faster, but
82654 ** P3==0 is safer.
82655 **
82656 ** If P3!=0 then the content of the P2 register is unsuitable for use
82657 ** in OP_Result and any OP_Result will invalidate the P2 register content.
82658 ** The P2 register content is invalidated by opcodes like OP_Function or
82659 ** by any use of another cursor pointing to the same table.
82660 */
82661 case OP_RowData: {
82662 VdbeCursor *pC;
82663 BtCursor *pCrsr;
82664 u32 n;
82665
82666 pOut = out2Prerelease(p, pOp);
 
82667
82668 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
82669 pC = p->apCsr[pOp->p1];
82670 assert( pC!=0 );
82671 assert( pC->eCurType==CURTYPE_BTREE );
@@ -82505,18 +82692,13 @@
82692 n = sqlite3BtreePayloadSize(pCrsr);
82693 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
82694 goto too_big;
82695 }
82696 testcase( n==0 );
82697 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut);
 
 
 
 
 
82698 if( rc ) goto abort_due_to_error;
82699 if( !pOp->p3 ) Deephemeralize(pOut);
82700 UPDATE_MAX_BLOBSIZE(pOut);
82701 REGISTER_TRACE(pOp->p2, pOut);
82702 break;
82703 }
82704
@@ -82900,11 +83082,11 @@
83082 x.nKey = pIn2->n;
83083 x.pKey = pIn2->z;
83084 x.aMem = aMem + pOp->p3;
83085 x.nMem = (u16)pOp->p4.i;
83086 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
83087 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)),
83088 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
83089 );
83090 assert( pC->deferredMoveto==0 );
83091 pC->cacheStatus = CACHE_STALE;
83092 }
@@ -83022,11 +83204,10 @@
83204 pTabCur->aAltMap = pOp->p4.ai;
83205 pTabCur->pAltCursor = pC;
83206 }else{
83207 pOut = out2Prerelease(p, pOp);
83208 pOut->u.i = rowid;
 
83209 }
83210 }else{
83211 assert( pOp->opcode==OP_IdxRowid );
83212 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
83213 }
@@ -83664,11 +83845,11 @@
83845 assert( (int)(pOp - aOp)==pFrame->pc );
83846 }
83847
83848 p->nFrame++;
83849 pFrame->pParent = p->pFrame;
83850 pFrame->lastRowid = db->lastRowid;
83851 pFrame->nChange = p->nChange;
83852 pFrame->nDbChange = p->db->nChange;
83853 assert( pFrame->pAuxData==0 );
83854 pFrame->pAuxData = p->pAuxData;
83855 p->pAuxData = 0;
@@ -84605,11 +84786,11 @@
84786 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
84787 db->vtabOnConflict = vtabOnConflict;
84788 sqlite3VtabImportErrmsg(p, pVtab);
84789 if( rc==SQLITE_OK && pOp->p1 ){
84790 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
84791 db->lastRowid = rowid;
84792 }
84793 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
84794 if( pOp->p5==OE_Ignore ){
84795 rc = SQLITE_OK;
84796 }else{
@@ -84841,11 +85022,10 @@
85022
85023 /* This is the only way out of this procedure. We have to
85024 ** release the mutexes on btrees that were acquired at the
85025 ** top. */
85026 vdbe_return:
 
85027 testcase( nVmStep>0 );
85028 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
85029 sqlite3VdbeLeave(p);
85030 assert( rc!=SQLITE_OK || nExtraDelete==0
85031 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
@@ -84905,14 +85085,13 @@
85085 /*
85086 ** Valid sqlite3_blob* handles point to Incrblob structures.
85087 */
85088 typedef struct Incrblob Incrblob;
85089 struct Incrblob {
 
85090 int nByte; /* Size of open blob, in bytes */
85091 int iOffset; /* Byte offset of blob in cursor data */
85092 u16 iCol; /* Table column this handle is open on */
85093 BtCursor *pCsr; /* Cursor pointing at blob row */
85094 sqlite3_stmt *pStmt; /* Statement holding cursor open */
85095 sqlite3 *db; /* The associated database */
85096 char *zDb; /* Database name */
85097 Table *pTab; /* Table object */
@@ -84939,21 +85118,31 @@
85118 static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
85119 int rc; /* Error code */
85120 char *zErr = 0; /* Error message */
85121 Vdbe *v = (Vdbe *)p->pStmt;
85122
85123 /* Set the value of register r[1] in the SQL statement to integer iRow.
85124 ** This is done directly as a performance optimization
 
85125 */
85126 v->aMem[1].flags = MEM_Int;
85127 v->aMem[1].u.i = iRow;
85128
85129 /* If the statement has been run before (and is paused at the OP_ResultRow)
85130 ** then back it up to the point where it does the OP_SeekRowid. This could
85131 ** have been down with an extra OP_Goto, but simply setting the program
85132 ** counter is faster. */
85133 if( v->pc>3 ){
85134 v->pc = 3;
85135 rc = sqlite3VdbeExec(v);
85136 }else{
85137 rc = sqlite3_step(p->pStmt);
85138 }
85139 if( rc==SQLITE_ROW ){
85140 VdbeCursor *pC = v->apCsr[0];
85141 u32 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
85142 testcase( pC->nHdrParsed==p->iCol );
85143 testcase( pC->nHdrParsed==p->iCol+1 );
85144 if( type<12 ){
85145 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
85146 type==0?"null": type==7?"real": "integer"
85147 );
85148 rc = SQLITE_ERROR;
@@ -84994,11 +85183,11 @@
85183 sqlite3* db, /* The database connection */
85184 const char *zDb, /* The attached database containing the blob */
85185 const char *zTable, /* The table containing the blob */
85186 const char *zColumn, /* The column containing the blob */
85187 sqlite_int64 iRow, /* The row containing the glob */
85188 int wrFlag, /* True -> read/write access, false -> read-only */
85189 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
85190 ){
85191 int nAttempt = 0;
85192 int iCol; /* Index of zColumn in row-record */
85193 int rc = SQLITE_OK;
@@ -85016,11 +85205,11 @@
85205 #ifdef SQLITE_ENABLE_API_ARMOR
85206 if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
85207 return SQLITE_MISUSE_BKPT;
85208 }
85209 #endif
85210 wrFlag = !!wrFlag; /* wrFlag = (wrFlag ? 1 : 0); */
85211
85212 sqlite3_mutex_enter(db->mutex);
85213
85214 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
85215 if( !pBlob ) goto blob_open_out;
@@ -85076,13 +85265,12 @@
85265 goto blob_open_out;
85266 }
85267
85268 /* If the value is being opened for writing, check that the
85269 ** column is not indexed, and that it is not part of a foreign key.
85270 */
85271 if( wrFlag ){
 
85272 const char *zFault = 0;
85273 Index *pIdx;
85274 #ifndef SQLITE_OMIT_FOREIGN_KEY
85275 if( db->flags&SQLITE_ForeignKeys ){
85276 /* Check that the column is not part of an FK child key definition. It
@@ -85139,22 +85327,21 @@
85327 */
85328 static const int iLn = VDBE_OFFSET_LINENO(2);
85329 static const VdbeOpList openBlob[] = {
85330 {OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */
85331 {OP_OpenRead, 0, 0, 0}, /* 1: Open a cursor */
85332 /* blobSeekToRow() will initialize r[1] to the desired rowid */
85333 {OP_NotExists, 0, 5, 1}, /* 2: Seek the cursor to rowid=r[1] */
85334 {OP_Column, 0, 0, 1}, /* 3 */
85335 {OP_ResultRow, 1, 0, 0}, /* 4 */
85336 {OP_Halt, 0, 0, 0}, /* 5 */
 
85337 };
85338 Vdbe *v = (Vdbe *)pBlob->pStmt;
85339 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
85340 VdbeOp *aOp;
85341
85342 sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag,
85343 pTab->pSchema->schema_cookie,
85344 pTab->pSchema->iGeneration);
85345 sqlite3VdbeChangeP5(v, 1);
85346 aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
85347
@@ -85167,19 +85354,19 @@
85354 #ifdef SQLITE_OMIT_SHARED_CACHE
85355 aOp[0].opcode = OP_Noop;
85356 #else
85357 aOp[0].p1 = iDb;
85358 aOp[0].p2 = pTab->tnum;
85359 aOp[0].p3 = wrFlag;
85360 sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
85361 }
85362 if( db->mallocFailed==0 ){
85363 #endif
85364
85365 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
85366 ** parameter of the other to pTab->tnum. */
85367 if( wrFlag ) aOp[1].opcode = OP_OpenWrite;
85368 aOp[1].p2 = pTab->tnum;
85369 aOp[1].p3 = iDb;
85370
85371 /* Configure the number of columns. Configure the cursor to
85372 ** think that the table has one more column than it really
@@ -85188,27 +85375,25 @@
85375 ** we can invoke OP_Column to fill in the vdbe cursors type
85376 ** and offset cache without causing any IO.
85377 */
85378 aOp[1].p4type = P4_INT32;
85379 aOp[1].p4.i = pTab->nCol+1;
85380 aOp[3].p2 = pTab->nCol;
85381
85382 pParse->nVar = 0;
85383 pParse->nMem = 1;
85384 pParse->nTab = 1;
85385 sqlite3VdbeMakeReady(v, pParse);
85386 }
85387 }
85388
 
85389 pBlob->iCol = iCol;
85390 pBlob->db = db;
85391 sqlite3BtreeLeaveAll(db);
85392 if( db->mallocFailed ){
85393 goto blob_open_out;
85394 }
 
85395 rc = blobSeekToRow(pBlob, iRow, &zErr);
85396 } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
85397
85398 blob_open_out:
85399 if( rc==SQLITE_OK && db->mallocFailed==0 ){
@@ -88741,12 +88926,10 @@
88926 ** This file contains routines used for walking the parser tree and
88927 ** resolve all identifiers by associating them with a particular
88928 ** table and column.
88929 */
88930 /* #include "sqliteInt.h" */
 
 
88931
88932 /*
88933 ** Walk the expression tree pExpr and increase the aggregate function
88934 ** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
88935 ** This needs to occur when copying a TK_AGG_FUNCTION node from an
@@ -91237,21 +91420,27 @@
91420 int doAdd = 0;
91421 if( z[0]=='?' ){
91422 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
91423 ** use it as the variable number */
91424 i64 i;
91425 int bOk;
91426 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
91427 i = z[1]-'0'; /* The common case of ?N for a single digit N */
91428 bOk = 1;
91429 }else{
91430 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
91431 }
91432 testcase( i==0 );
91433 testcase( i==1 );
91434 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
91435 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
91436 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
91437 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
91438 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
91439 return;
91440 }
91441 x = (ynVar)i;
91442 if( x>pParse->nVar ){
91443 pParse->nVar = (int)x;
91444 doAdd = 1;
91445 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
91446 doAdd = 1;
@@ -91682,37 +91871,45 @@
91871 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
91872 pNewItem->idx = pOldItem->idx;
91873 }
91874 return pNew;
91875 }
91876 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *pDup, int flags){
91877 Select *pRet = 0;
91878 Select *pNext = 0;
91879 Select **pp = &pRet;
91880 Select *p;
91881
91882 assert( db!=0 );
91883 for(p=pDup; p; p=p->pPrior){
91884 Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
91885 if( pNew==0 ) break;
91886 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
91887 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
91888 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
91889 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
91890 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
91891 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
91892 pNew->op = p->op;
91893 pNew->pNext = pNext;
91894 pNew->pPrior = 0;
91895 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
91896 pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
91897 pNew->iLimit = 0;
91898 pNew->iOffset = 0;
91899 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
91900 pNew->addrOpenEphm[0] = -1;
91901 pNew->addrOpenEphm[1] = -1;
91902 pNew->nSelectRow = p->nSelectRow;
91903 pNew->pWith = withDup(db, p->pWith);
91904 sqlite3SelectSetName(pNew, p->zSelName);
91905 *pp = pNew;
91906 pp = &pNew->pPrior;
91907 pNext = pNew;
91908 }
91909
91910 return pRet;
91911 }
91912 #else
91913 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
91914 assert( p==0 );
91915 return 0;
@@ -91773,11 +91970,11 @@
91970 **
91971 ** (a,b,c) = (expr1,expr2,expr3)
91972 ** Or: (a,b,c) = (SELECT x,y,z FROM ....)
91973 **
91974 ** For each term of the vector assignment, append new entries to the
91975 ** expression list pList. In the case of a subquery on the RHS, append
91976 ** TK_SELECT_COLUMN expressions.
91977 */
91978 SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(
91979 Parse *pParse, /* Parsing context */
91980 ExprList *pList, /* List to which to append. Might be NULL */
@@ -93882,10 +94079,15 @@
94079 int i; /* Loop counter */
94080 sqlite3 *db = pParse->db; /* The database connection */
94081 u8 enc = ENC(db); /* The text encoding used by this database */
94082 CollSeq *pColl = 0; /* A collating sequence */
94083
94084 if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
94085 /* SQL functions can be expensive. So try to move constant functions
94086 ** out of the inner loop, even if that means an extra OP_Copy. */
94087 return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
94088 }
94089 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
94090 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
94091 pFarg = 0;
94092 }else{
94093 pFarg = pExpr->x.pList;
@@ -93929,10 +94131,26 @@
94131 */
94132 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
94133 assert( nFarg>=1 );
94134 return sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
94135 }
94136
94137 #ifdef SQLITE_DEBUG
94138 /* The AFFINITY() function evaluates to a string that describes
94139 ** the type affinity of the argument. This is used for testing of
94140 ** the SQLite type logic.
94141 */
94142 if( pDef->funcFlags & SQLITE_FUNC_AFFINITY ){
94143 const char *azAff[] = { "blob", "text", "numeric", "integer", "real" };
94144 char aff;
94145 assert( nFarg==1 );
94146 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
94147 sqlite3VdbeLoadString(v, target,
94148 aff ? azAff[aff-SQLITE_AFF_BLOB] : "none");
94149 return target;
94150 }
94151 #endif
94152
94153 for(i=0; i<nFarg; i++){
94154 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
94155 testcase( i==31 );
94156 constMask |= MASKBIT32(i);
@@ -94246,28 +94464,44 @@
94464 return inReg;
94465 }
94466
94467 /*
94468 ** Factor out the code of the given expression to initialization time.
94469 **
94470 ** If regDest>=0 then the result is always stored in that register and the
94471 ** result is not reusable. If regDest<0 then this routine is free to
94472 ** store the value whereever it wants. The register where the expression
94473 ** is stored is returned. When regDest<0, two identical expressions will
94474 ** code to the same register.
94475 */
94476 SQLITE_PRIVATE int sqlite3ExprCodeAtInit(
94477 Parse *pParse, /* Parsing context */
94478 Expr *pExpr, /* The expression to code when the VDBE initializes */
94479 int regDest /* Store the value in this register */
 
94480 ){
94481 ExprList *p;
94482 assert( ConstFactorOk(pParse) );
94483 p = pParse->pConstExpr;
94484 if( regDest<0 && p ){
94485 struct ExprList_item *pItem;
94486 int i;
94487 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
94488 if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){
94489 return pItem->u.iConstExprReg;
94490 }
94491 }
94492 }
94493 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
94494 p = sqlite3ExprListAppend(pParse, p, pExpr);
94495 if( p ){
94496 struct ExprList_item *pItem = &p->a[p->nExpr-1];
94497 pItem->reusable = regDest<0;
94498 if( regDest<0 ) regDest = ++pParse->nMem;
94499 pItem->u.iConstExprReg = regDest;
 
94500 }
94501 pParse->pConstExpr = p;
94502 return regDest;
94503 }
94504
94505 /*
94506 ** Generate code to evaluate an expression and store the results
94507 ** into a register. Return the register number where the results
@@ -94286,23 +94520,12 @@
94520 pExpr = sqlite3ExprSkipCollate(pExpr);
94521 if( ConstFactorOk(pParse)
94522 && pExpr->op!=TK_REGISTER
94523 && sqlite3ExprIsConstantNotJoin(pExpr)
94524 ){
 
 
94525 *pReg = 0;
94526 r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1);
 
 
 
 
 
 
 
 
 
94527 }else{
94528 int r1 = sqlite3GetTempReg(pParse);
94529 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
94530 if( r2==r1 ){
94531 *pReg = r1;
@@ -94352,11 +94575,11 @@
94575 ** in register target. If the expression is constant, then this routine
94576 ** might choose to code the expression at initialization time.
94577 */
94578 SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
94579 if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
94580 sqlite3ExprCodeAtInit(pParse, pExpr, target);
94581 }else{
94582 sqlite3ExprCode(pParse, pExpr, target);
94583 }
94584 }
94585
@@ -94424,11 +94647,11 @@
94647 n--;
94648 }else{
94649 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
94650 }
94651 }else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){
94652 sqlite3ExprCodeAtInit(pParse, pExpr, target+i);
94653 }else{
94654 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
94655 if( inReg!=target+i ){
94656 VdbeOp *pOp;
94657 if( copyOp==OP_Copy
@@ -96968,10 +97191,16 @@
97191 ** used to query statistical information that has been gathered into
97192 ** the Stat4Accum object by prior calls to stat_push(). The P parameter
97193 ** has type BLOB but it is really just a pointer to the Stat4Accum object.
97194 ** The content to returned is determined by the parameter J
97195 ** which is one of the STAT_GET_xxxx values defined above.
97196 **
97197 ** The stat_get(P,J) function is not available to generic SQL. It is
97198 ** inserted as part of a manually constructed bytecode program. (See
97199 ** the callStatGet() routine below.) It is guaranteed that the P
97200 ** parameter will always be a poiner to a Stat4Accum object, never a
97201 ** NULL.
97202 **
97203 ** If neither STAT3 nor STAT4 are enabled, then J is always
97204 ** STAT_GET_STAT1 and is hence omitted and this routine becomes
97205 ** a one-parameter function, stat_get(P), that always returns the
97206 ** stat1 table entry information.
@@ -97787,11 +98016,11 @@
98016 sumEq += aSample[i].anEq[iCol];
98017 nSum100 += 100;
98018 }
98019 }
98020
98021 if( nDist100>nSum100 && sumEq<nRow ){
98022 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
98023 }
98024 if( avgEq==0 ) avgEq = 1;
98025 pIdx->aAvgEq[iCol] = avgEq;
98026 }
@@ -98201,10 +98430,11 @@
98430 assert( pVfs );
98431 flags |= SQLITE_OPEN_MAIN_DB;
98432 rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags);
98433 sqlite3_free( zPath );
98434 db->nDb++;
98435 db->skipBtreeMutex = 0;
98436 if( rc==SQLITE_CONSTRAINT ){
98437 rc = SQLITE_ERROR;
98438 zErrDyn = sqlite3MPrintf(db, "database is already attached");
98439 }else if( rc==SQLITE_OK ){
98440 Pager *pPager;
@@ -104365,16 +104595,12 @@
104595 }
104596 }else
104597 #endif
104598 {
104599 int count = (pParse->nested==0); /* True to count changes */
 
 
 
 
104600 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
104601 iKey, nKey, count, OE_Default, eOnePass, aiCurOnePass[1]);
104602 }
104603
104604 /* End of the loop over all rowids/primary-keys. */
104605 if( eOnePass!=ONEPASS_OFF ){
104606 sqlite3VdbeResolveLabel(v, addrBypass);
@@ -104450,19 +104676,21 @@
104676 ** then this function must seek iDataCur to the entry identified by iPk
104677 ** and nPk before reading from it.
104678 **
104679 ** If eMode is ONEPASS_MULTI, then this call is being made as part
104680 ** of a ONEPASS delete that affects multiple rows. In this case, if
104681 ** iIdxNoSeek is a valid cursor number (>=0) and is not the same as
104682 ** iDataCur, then its position should be preserved following the delete
104683 ** operation. Or, if iIdxNoSeek is not a valid cursor number, the
104684 ** position of iDataCur should be preserved instead.
104685 **
104686 ** iIdxNoSeek:
104687 ** If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
104688 ** then it identifies an index cursor (from within array of cursors
104689 ** starting at iIdxCur) that already points to the index entry to be deleted.
104690 ** Except, this optimization is disabled if there are BEFORE triggers since
104691 ** the trigger body might have moved the cursor.
104692 */
104693 SQLITE_PRIVATE void sqlite3GenerateRowDelete(
104694 Parse *pParse, /* Parsing context */
104695 Table *pTab, /* Table containing the row to be deleted */
104696 Trigger *pTrigger, /* List of triggers to (potentially) fire */
@@ -104529,17 +104757,22 @@
104757 TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
104758 );
104759
104760 /* If any BEFORE triggers were coded, then seek the cursor to the
104761 ** row to be deleted again. It may be that the BEFORE triggers moved
104762 ** the cursor or already deleted the row that the cursor was
104763 ** pointing to.
104764 **
104765 ** Also disable the iIdxNoSeek optimization since the BEFORE trigger
104766 ** may have moved that cursor.
104767 */
104768 if( addrStart<sqlite3VdbeCurrentAddr(v) ){
104769 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
104770 VdbeCoverageIf(v, opSeek==OP_NotExists);
104771 VdbeCoverageIf(v, opSeek==OP_NotFound);
104772 testcase( iIdxNoSeek>=0 );
104773 iIdxNoSeek = -1;
104774 }
104775
104776 /* Do FK processing. This call checks that any FK constraints that
104777 ** refer to this table (i.e. constraints attached to other tables)
104778 ** are not violated by deleting this row. */
@@ -104558,15 +104791,17 @@
104791 */
104792 if( pTab->pSelect==0 ){
104793 u8 p5 = 0;
104794 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
104795 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
104796 if( pParse->nested==0 ){
104797 sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
104798 }
104799 if( eMode!=ONEPASS_OFF ){
104800 sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE);
104801 }
104802 if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
104803 sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
104804 }
104805 if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION;
104806 sqlite3VdbeChangeP5(v, p5);
104807 }
@@ -104716,10 +104951,14 @@
104951 ** opcode if it is present */
104952 sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
104953 }
104954 if( regOut ){
104955 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
104956 if( pIdx->pTable->pSelect ){
104957 const char *zAff = sqlite3IndexAffinityStr(pParse->db, pIdx);
104958 sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
104959 }
104960 }
104961 sqlite3ReleaseTempRange(pParse, regBase, nCol);
104962 return regBase;
104963 }
104964
@@ -106512,10 +106751,13 @@
106751 DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ),
106752 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
106753 FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
106754 FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
106755 FUNCTION2(likely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
106756 #ifdef SQLITE_DEBUG
106757 FUNCTION2(affinity, 1, 0, 0, noopFunc, SQLITE_FUNC_AFFINITY),
106758 #endif
106759 FUNCTION(ltrim, 1, 1, 0, trimFunc ),
106760 FUNCTION(ltrim, 2, 1, 0, trimFunc ),
106761 FUNCTION(rtrim, 1, 2, 0, trimFunc ),
106762 FUNCTION(rtrim, 2, 2, 0, trimFunc ),
106763 FUNCTION(trim, 1, 3, 0, trimFunc ),
@@ -109667,11 +109909,11 @@
109909 if( db->flags&SQLITE_RecTriggers ){
109910 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
109911 }
109912 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
109913 regR, nPkField, 0, OE_Replace,
109914 (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
109915 seenReplace = 1;
109916 break;
109917 }
109918 }
109919 sqlite3VdbeResolveLabel(v, addrUniqueOk);
@@ -109683,10 +109925,29 @@
109925 }
109926
109927 *pbMayReplace = seenReplace;
109928 VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
109929 }
109930
109931 #ifdef SQLITE_ENABLE_NULL_TRIM
109932 /*
109933 ** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
109934 ** to be the number of columns in table pTab that must not be NULL-trimmed.
109935 **
109936 ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
109937 */
109938 SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
109939 u16 i;
109940
109941 /* Records with omitted columns are only allowed for schema format
109942 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
109943 if( pTab->pSchema->file_format<2 ) return;
109944
109945 for(i=pTab->nCol; i>1 && pTab->aCol[i-1].pDflt==0; i--){}
109946 sqlite3VdbeChangeP5(v, i);
109947 }
109948 #endif
109949
109950 /*
109951 ** This routine generates code to finish the INSERT or UPDATE operation
109952 ** that was started by a prior call to sqlite3GenerateConstraintChecks.
109953 ** A consecutive range of registers starting at regNewData contains the
@@ -109700,11 +109961,11 @@
109961 Table *pTab, /* the table into which we are inserting */
109962 int iDataCur, /* Cursor of the canonical data source */
109963 int iIdxCur, /* First index cursor */
109964 int regNewData, /* Range of content */
109965 int *aRegIdx, /* Register used by each index. 0 for unused indices */
109966 int update_flags, /* True for UPDATE, False for INSERT */
109967 int appendBias, /* True if this is likely to be an append */
109968 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
109969 ){
109970 Vdbe *v; /* Prepared statements under construction */
109971 Index *pIdx; /* An index being inserted or updated */
@@ -109711,10 +109972,15 @@
109972 u8 pik_flags; /* flag values passed to the btree insert */
109973 int regData; /* Content registers (after the rowid) */
109974 int regRec; /* Register holding assembled record for the table */
109975 int i; /* Loop counter */
109976 u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */
109977
109978 assert( update_flags==0
109979 || update_flags==OPFLAG_ISUPDATE
109980 || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)
109981 );
109982
109983 v = sqlite3GetVdbe(pParse);
109984 assert( v!=0 );
109985 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
109986 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
@@ -109722,34 +109988,43 @@
109988 bAffinityDone = 1;
109989 if( pIdx->pPartIdxWhere ){
109990 sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
109991 VdbeCoverage(v);
109992 }
109993 pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
 
 
 
 
109994 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
109995 assert( pParse->nested==0 );
109996 pik_flags |= OPFLAG_NCHANGE;
109997 pik_flags |= (update_flags & OPFLAG_SAVEPOSITION);
109998 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
109999 if( update_flags==0 ){
110000 sqlite3VdbeAddOp4(v, OP_InsertInt,
110001 iIdxCur+i, aRegIdx[i], 0, (char*)pTab, P4_TABLE
110002 );
110003 sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP);
110004 }
110005 #endif
110006 }
110007 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
110008 aRegIdx[i]+1,
110009 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
110010 sqlite3VdbeChangeP5(v, pik_flags);
110011 }
110012 if( !HasRowid(pTab) ) return;
110013 regData = regNewData + 1;
110014 regRec = sqlite3GetTempReg(pParse);
110015 sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
110016 sqlite3SetMakeRecordP5(v, pTab);
110017 if( !bAffinityDone ){
110018 sqlite3TableAffinity(v, pTab, 0);
110019 sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
110020 }
110021 if( pParse->nested ){
110022 pik_flags = 0;
110023 }else{
110024 pik_flags = OPFLAG_NCHANGE;
110025 pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID);
110026 }
110027 if( appendBias ){
110028 pik_flags |= OPFLAG_APPEND;
110029 }
110030 if( useSeekResult ){
@@ -110154,11 +110429,11 @@
110429 addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
110430 }else{
110431 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
110432 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
110433 }
110434 sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
110435 if( db->flags & SQLITE_Vacuum ){
110436 sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
110437 insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|
110438 OPFLAG_APPEND|OPFLAG_USESEEKRESULT;
110439 }else{
@@ -110186,11 +110461,11 @@
110461 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
110462 sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
110463 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
110464 VdbeComment((v, "%s", pDestIdx->zName));
110465 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
110466 sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
110467 if( db->flags & SQLITE_Vacuum ){
110468 /* This INSERT command is part of a VACUUM operation, which guarantees
110469 ** that the destination table is empty. If all indexed columns use
110470 ** collation sequence BINARY, then it can also be assumed that the
110471 ** index will be populated by inserting keys in strictly sorted
@@ -110971,11 +111246,10 @@
111246 #endif /* SQLITE3EXT_H */
111247
111248 /************** End of sqlite3ext.h ******************************************/
111249 /************** Continuing where we left off in loadext.c ********************/
111250 /* #include "sqliteInt.h" */
 
111251
111252 #ifndef SQLITE_OMIT_LOAD_EXTENSION
111253 /*
111254 ** Some API routines are omitted when various features are
111255 ** excluded from a build of SQLite. Substitute a NULL pointer
@@ -112635,11 +112909,11 @@
112909
112910 /*
112911 ** Locate a pragma in the aPragmaName[] array.
112912 */
112913 static const PragmaName *pragmaLocate(const char *zName){
112914 int upr, lwr, mid = 0, rc;
112915 lwr = 0;
112916 upr = ArraySize(aPragmaName)-1;
112917 while( lwr<=upr ){
112918 mid = (lwr+upr)/2;
112919 rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
@@ -116149,10 +116423,11 @@
116423 v = pParse->pVdbe;
116424 r1 = sqlite3GetTempReg(pParse);
116425 sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
116426 sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
116427 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, iMem, N);
116428 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
116429 sqlite3ReleaseTempReg(pParse, r1);
116430 }
116431
116432 /*
116433 ** This routine generates the code for the inside of the inner loop
@@ -119677,11 +119952,19 @@
119952 assert( pTab->nTabRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nTabRef==2 ));
119953
119954 pCte->zCteErr = "circular reference: %s";
119955 pSavedWith = pParse->pWith;
119956 pParse->pWith = pWith;
119957 if( bMayRecursive ){
119958 Select *pPrior = pSel->pPrior;
119959 assert( pPrior->pWith==0 );
119960 pPrior->pWith = pSel->pWith;
119961 sqlite3WalkSelect(pWalker, pPrior);
119962 pPrior->pWith = 0;
119963 }else{
119964 sqlite3WalkSelect(pWalker, pSel);
119965 }
119966 pParse->pWith = pWith;
119967
119968 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
119969 pEList = pLeft->pEList;
119970 if( pCte->pCols ){
@@ -119721,14 +120004,16 @@
120004 ** sqlite3SelectExpand() when walking a SELECT tree to resolve table
120005 ** names and other FROM clause elements.
120006 */
120007 static void selectPopWith(Walker *pWalker, Select *p){
120008 Parse *pParse = pWalker->pParse;
120009 if( pParse->pWith && p->pPrior==0 ){
120010 With *pWith = findRightmost(p)->pWith;
120011 if( pWith!=0 ){
120012 assert( pParse->pWith==pWith );
120013 pParse->pWith = pWith->pOuter;
120014 }
120015 }
120016 }
120017 #else
120018 #define selectPopWith 0
120019 #endif
@@ -119774,12 +120059,12 @@
120059 if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
120060 return WRC_Prune;
120061 }
120062 pTabList = p->pSrc;
120063 pEList = p->pEList;
120064 if( p->pWith ){
120065 sqlite3WithPush(pParse, p->pWith, 0);
120066 }
120067
120068 /* Make sure cursor numbers have been assigned to all entries in
120069 ** the FROM clause of the SELECT statement.
120070 */
@@ -120062,13 +120347,11 @@
120347 if( pParse->hasCompound ){
120348 w.xSelectCallback = convertCompoundSelectToSubquery;
120349 sqlite3WalkSelect(&w, pSelect);
120350 }
120351 w.xSelectCallback = selectExpander;
120352 w.xSelectCallback2 = selectPopWith;
 
 
120353 sqlite3WalkSelect(&w, pSelect);
120354 }
120355
120356
120357 #ifndef SQLITE_OMIT_SUBQUERY
@@ -121143,11 +121426,11 @@
121426 /* This case runs if the aggregate has no GROUP BY clause. The
121427 ** processing is much simpler since there is only a single row
121428 ** of output.
121429 */
121430 resetAccumulator(pParse, &sAggInfo);
121431 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax, 0,flag,0);
121432 if( pWInfo==0 ){
121433 sqlite3ExprListDelete(db, pDel);
121434 goto select_end;
121435 }
121436 updateAccumulator(pParse, &sAggInfo);
@@ -121232,12 +121515,10 @@
121515 **
121516 ** These routines are in a separate files so that they will not be linked
121517 ** if they are not used.
121518 */
121519 /* #include "sqliteInt.h" */
 
 
121520
121521 #ifndef SQLITE_OMIT_GET_TABLE
121522
121523 /*
121524 ** This structure is used to pass data from sqlite3_get_table() through
@@ -122591,16 +122872,16 @@
122872 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
122873 pCol->affinity, &pValue);
122874 if( pValue ){
122875 sqlite3VdbeAppendP4(v, pValue, P4_MEM);
122876 }
122877 }
122878 #ifndef SQLITE_OMIT_FLOATING_POINT
122879 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
122880 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
122881 }
122882 #endif
 
122883 }
122884
122885 /*
122886 ** Process an UPDATE statement.
122887 **
@@ -122625,11 +122906,11 @@
122906 int nIdx; /* Number of indices that need updating */
122907 int iBaseCur; /* Base cursor number */
122908 int iDataCur; /* Cursor for the canonical data btree */
122909 int iIdxCur; /* Cursor for the first index */
122910 sqlite3 *db; /* The database structure */
122911 int *aRegIdx = 0; /* First register in array assigned to each index */
122912 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
122913 ** an expression for the i-th column of the table.
122914 ** aXRef[i]==-1 if the i-th column is not changed. */
122915 u8 *aToOpen; /* 1 for tables and indices to be opened */
122916 u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */
@@ -122637,14 +122918,15 @@
122918 u8 chngKey; /* Either chngPk or chngRowid */
122919 Expr *pRowidExpr = 0; /* Expression defining the new record number */
122920 AuthContext sContext; /* The authorization context */
122921 NameContext sNC; /* The name-context to resolve expressions in */
122922 int iDb; /* Database containing the table being updated */
122923 int eOnePass; /* ONEPASS_XXX value from where.c */
122924 int hasFK; /* True if foreign key processing is required */
122925 int labelBreak; /* Jump here to break out of UPDATE loop */
122926 int labelContinue; /* Jump here to continue next step of UPDATE loop */
122927 int flags; /* Flags for sqlite3WhereBegin() */
122928
122929 #ifndef SQLITE_OMIT_TRIGGER
122930 int isView; /* True when updating a view (INSTEAD OF trigger) */
122931 Trigger *pTrigger; /* List of triggers on pTab, if required */
122932 int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
@@ -122651,10 +122933,14 @@
122933 #endif
122934 int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */
122935 int iEph = 0; /* Ephemeral table holding all primary key values */
122936 int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */
122937 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
122938 int addrOpen = 0; /* Address of OP_OpenEphemeral */
122939 int iPk = 0; /* First of nPk cells holding PRIMARY KEY value */
122940 i16 nPk = 0; /* Number of components of the PRIMARY KEY */
122941 int bReplace = 0; /* True if REPLACE conflict resolution might happen */
122942
122943 /* Register Allocations */
122944 int regRowCount = 0; /* A count of rows changed */
122945 int regOldRowid = 0; /* The old rowid */
122946 int regNewRowid = 0; /* The new rowid */
@@ -122810,17 +123096,27 @@
123096 for(i=0; i<pIdx->nKeyCol; i++){
123097 i16 iIdxCol = pIdx->aiColumn[i];
123098 if( iIdxCol<0 || aXRef[iIdxCol]>=0 ){
123099 reg = ++pParse->nMem;
123100 pParse->nMem += pIdx->nColumn;
123101 if( (onError==OE_Replace)
123102 || (onError==OE_Default && pIdx->onError==OE_Replace)
123103 ){
123104 bReplace = 1;
123105 }
123106 break;
123107 }
123108 }
123109 }
123110 if( reg==0 ) aToOpen[j+1] = 0;
123111 aRegIdx[j] = reg;
123112 }
123113 if( bReplace ){
123114 /* If REPLACE conflict resolution might be invoked, open cursors on all
123115 ** indexes in case they are needed to delete records. */
123116 memset(aToOpen, 1, nIdx+1);
123117 }
123118
123119 /* Begin generating code. */
123120 v = sqlite3GetVdbe(pParse);
123121 if( v==0 ) goto update_cleanup;
123122 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
@@ -122869,107 +123165,127 @@
123165 pWhere, onError);
123166 goto update_cleanup;
123167 }
123168 #endif
123169
123170 /* Initialize the count of updated rows */
123171 if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
123172 regRowCount = ++pParse->nMem;
123173 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
123174 }
123175
123176 if( HasRowid(pTab) ){
123177 sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123178 }else{
 
 
 
 
123179 assert( pPk!=0 );
123180 nPk = pPk->nKeyCol;
123181 iPk = pParse->nMem+1;
123182 pParse->nMem += nPk;
123183 regKey = ++pParse->nMem;
123184 iEph = pParse->nTab++;
123185
123186 sqlite3VdbeAddOp2(v, OP_Null, 0, iPk);
123187 addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
123188 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
123189 }
123190
123191 /* Begin the database scan.
123192 **
123193 ** Do not consider a single-pass strategy for a multi-row update if
123194 ** there are any triggers or foreign keys to process, or rows may
123195 ** be deleted as a result of REPLACE conflict handling. Any of these
123196 ** things might disturb a cursor being used to scan through the table
123197 ** or index, causing a single-pass approach to malfunction. */
123198 flags = WHERE_ONEPASS_DESIRED|WHERE_SEEK_UNIQ_TABLE;
123199 if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
123200 flags |= WHERE_ONEPASS_MULTIROW;
123201 }
123202 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags, iIdxCur);
123203 if( pWInfo==0 ) goto update_cleanup;
123204
123205 /* A one-pass strategy that might update more than one row may not
123206 ** be used if any column of the index used for the scan is being
123207 ** updated. Otherwise, if there is an index on "b", statements like
123208 ** the following could create an infinite loop:
123209 **
123210 ** UPDATE t1 SET b=b+1 WHERE b>?
123211 **
123212 ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
123213 ** strategy that uses an index for which one or more columns are being
123214 ** updated. */
123215 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
123216 if( eOnePass==ONEPASS_MULTI ){
123217 int iCur = aiCurOnePass[1];
123218 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
123219 eOnePass = ONEPASS_OFF;
123220 }
123221 assert( iCur!=iDataCur || !HasRowid(pTab) );
123222 }
123223
123224 if( HasRowid(pTab) ){
123225 /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
123226 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
123227 ** leave it in register regOldRowid. */
123228 sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
123229 if( eOnePass==ONEPASS_OFF ){
123230 sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
123231 }
123232 }else{
123233 /* Read the PK of the current row into an array of registers. In
123234 ** ONEPASS_OFF mode, serialize the array into a record and store it in
123235 ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change
123236 ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table
123237 ** is not required) and leave the PK fields in the array of registers. */
123238 for(i=0; i<nPk; i++){
123239 assert( pPk->aiColumn[i]>=0 );
123240 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur,pPk->aiColumn[i],iPk+i);
 
123241 }
123242 if( eOnePass ){
123243 sqlite3VdbeChangeToNoop(v, addrOpen);
123244 nKey = nPk;
123245 regKey = iPk;
123246 }else{
123247 sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,
123248 sqlite3IndexAffinityStr(db, pPk), nPk);
123249 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEph, regKey, iPk, nPk);
123250 }
 
123251 }
123252
123253 if( eOnePass!=ONEPASS_MULTI ){
123254 sqlite3WhereEnd(pWInfo);
 
 
 
123255 }
123256
123257 labelBreak = sqlite3VdbeMakeLabel(v);
123258 if( !isView ){
123259 int addrOnce = 0;
123260
123261 /* Open every index that needs updating. */
123262 if( eOnePass!=ONEPASS_OFF ){
 
 
 
 
 
 
 
 
 
 
 
 
 
123263 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
123264 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
123265 }
123266
123267 if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
123268 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
123269 }
123270 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur, aToOpen,
123271 0, 0);
123272 if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
123273 }
123274
123275 /* Top of the update loop */
123276 if( eOnePass!=ONEPASS_OFF ){
123277 if( !isView && aiCurOnePass[0]!=iDataCur && aiCurOnePass[1]!=iDataCur ){
123278 assert( pPk );
123279 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
123280 VdbeCoverageNeverTaken(v);
123281 }
123282 if( eOnePass==ONEPASS_SINGLE ){
123283 labelContinue = labelBreak;
123284 }else{
123285 labelContinue = sqlite3VdbeMakeLabel(v);
123286 }
123287 sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
123288 VdbeCoverageIf(v, pPk==0);
123289 VdbeCoverageIf(v, pPk!=0);
123290 }else if( pPk ){
123291 labelContinue = sqlite3VdbeMakeLabel(v);
@@ -123090,11 +123406,10 @@
123406 }
123407 }
123408
123409 if( !isView ){
123410 int addr1 = 0; /* Address of jump instruction */
 
123411
123412 /* Do constraint checks. */
123413 assert( regOldRowid>0 );
123414 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
123415 regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace,
@@ -123126,18 +123441,22 @@
123441 ** is the column index supplied by the user.
123442 */
123443 assert( regNew==regNewRowid+1 );
123444 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
123445 sqlite3VdbeAddOp3(v, OP_Delete, iDataCur,
123446 OPFLAG_ISUPDATE | ((hasFK || chngKey) ? 0 : OPFLAG_ISNOOP),
123447 regNewRowid
123448 );
123449 if( eOnePass==ONEPASS_MULTI ){
123450 assert( hasFK==0 && chngKey==0 );
123451 sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION);
123452 }
123453 if( !pParse->nested ){
123454 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
123455 }
123456 #else
123457 if( hasFK || chngKey ){
123458 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0);
123459 }
123460 #endif
123461 if( bReplace || chngKey ){
123462 sqlite3VdbeJumpHere(v, addr1);
@@ -123146,12 +123465,15 @@
123465 if( hasFK ){
123466 sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);
123467 }
123468
123469 /* Insert the new index entries and the new record. */
123470 sqlite3CompleteInsertion(
123471 pParse, pTab, iDataCur, iIdxCur, regNewRowid, aRegIdx,
123472 OPFLAG_ISUPDATE | (eOnePass==ONEPASS_MULTI ? OPFLAG_SAVEPOSITION : 0),
123473 0, 0
123474 );
123475
123476 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
123477 ** handle rows (possibly in other tables) that refer via a foreign key
123478 ** to the row just updated. */
123479 if( hasFK ){
@@ -123169,12 +123491,15 @@
123491 TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue);
123492
123493 /* Repeat the above with the next record to be updated, until
123494 ** all record selected by the WHERE clause have been updated.
123495 */
123496 if( eOnePass==ONEPASS_SINGLE ){
123497 /* Nothing to do at end-of-loop for a single-pass */
123498 }else if( eOnePass==ONEPASS_MULTI ){
123499 sqlite3VdbeResolveLabel(v, labelContinue);
123500 sqlite3WhereEnd(pWInfo);
123501 }else if( pPk ){
123502 sqlite3VdbeResolveLabel(v, labelContinue);
123503 sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v);
123504 }else{
123505 sqlite3VdbeGoto(v, labelContinue);
@@ -127090,11 +127415,14 @@
127415
127416 /* Seek the table cursor, if required */
127417 if( omitTable ){
127418 /* pIdx is a covering index. No need to access the main table. */
127419 }else if( HasRowid(pIdx->pTable) ){
127420 if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE) || (
127421 (pWInfo->wctrlFlags & WHERE_SEEK_UNIQ_TABLE)
127422 && (pWInfo->eOnePass==ONEPASS_SINGLE)
127423 )){
127424 iRowidReg = ++pParse->nMem;
127425 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
127426 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
127427 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg);
127428 VdbeCoverage(v);
@@ -128454,10 +128782,11 @@
128782 int noCase = 0; /* uppercase equivalent to lowercase */
128783 int op; /* Top-level operator. pExpr->op */
128784 Parse *pParse = pWInfo->pParse; /* Parsing context */
128785 sqlite3 *db = pParse->db; /* Database connection */
128786 unsigned char eOp2; /* op2 value for LIKE/REGEXP/GLOB */
128787 int nLeft; /* Number of elements on left side vector */
128788
128789 if( db->mallocFailed ){
128790 return;
128791 }
128792 pTerm = &pWC->a[idxTerm];
@@ -128483,10 +128812,14 @@
128812 if( ExprHasProperty(pExpr, EP_FromJoin) ){
128813 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
128814 prereqAll |= x;
128815 extraRight = x-1; /* ON clause terms may not be used with an index
128816 ** on left table of a LEFT JOIN. Ticket #3015 */
128817 if( (prereqAll>>1)>=x ){
128818 sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
128819 return;
128820 }
128821 }
128822 pTerm->prereqAll = prereqAll;
128823 pTerm->leftCursor = -1;
128824 pTerm->iParent = -1;
128825 pTerm->eOperator = 0;
@@ -128725,17 +129058,16 @@
129058 **
129059 ** This is only required if at least one side of the comparison operation
129060 ** is not a sub-select. */
129061 if( pWC->op==TK_AND
129062 && (pExpr->op==TK_EQ || pExpr->op==TK_IS)
129063 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
129064 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
129065 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
129066 || (pExpr->pRight->flags & EP_xIsSelect)==0)
129067 ){
 
129068 int i;
 
129069 for(i=0; i<nLeft; i++){
129070 int idxNew;
129071 Expr *pNew;
129072 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i);
129073 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i);
@@ -133934,11 +134266,12 @@
134266 x = sqlite3ColumnOfIndex(pIdx, x);
134267 if( x>=0 ){
134268 pOp->p2 = x;
134269 pOp->p1 = pLevel->iIdxCur;
134270 }
134271 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0
134272 || pWInfo->eOnePass );
134273 }else if( pOp->opcode==OP_Rowid ){
134274 pOp->p1 = pLevel->iIdxCur;
134275 pOp->opcode = OP_IdxRowid;
134276 }
134277 }
@@ -133998,10 +134331,23 @@
134331 ** Indicate that sqlite3ParserFree() will never be called with a null
134332 ** pointer.
134333 */
134334 #define YYPARSEFREENEVERNULL 1
134335
134336 /*
134337 ** In the amalgamation, the parse.c file generated by lemon and the
134338 ** tokenize.c file are concatenated. In that case, sqlite3RunParser()
134339 ** has access to the the size of the yyParser object and so the parser
134340 ** engine can be allocated from stack. In that case, only the
134341 ** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
134342 ** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
134343 ** omitted.
134344 */
134345 #ifdef SQLITE_AMALGAMATION
134346 # define sqlite3Parser_ENGINEALWAYSONSTACK 1
134347 #endif
134348
134349 /*
134350 ** Alternative datatype for the argument to the malloc() routine passed
134351 ** into sqlite3ParserAlloc(). The default is size_t.
134352 */
134353 #define YYMALLOCARGTYPE u64
@@ -135446,10 +135792,35 @@
135792 */
135793 #ifndef YYMALLOCARGTYPE
135794 # define YYMALLOCARGTYPE size_t
135795 #endif
135796
135797 /* Initialize a new parser that has already been allocated.
135798 */
135799 SQLITE_PRIVATE void sqlite3ParserInit(void *yypParser){
135800 yyParser *pParser = (yyParser*)yypParser;
135801 #ifdef YYTRACKMAXSTACKDEPTH
135802 pParser->yyhwm = 0;
135803 #endif
135804 #if YYSTACKDEPTH<=0
135805 pParser->yytos = NULL;
135806 pParser->yystack = NULL;
135807 pParser->yystksz = 0;
135808 if( yyGrowStack(pParser) ){
135809 pParser->yystack = &pParser->yystk0;
135810 pParser->yystksz = 1;
135811 }
135812 #endif
135813 #ifndef YYNOERRORRECOVERY
135814 pParser->yyerrcnt = -1;
135815 #endif
135816 pParser->yytos = pParser->yystack;
135817 pParser->yystack[0].stateno = 0;
135818 pParser->yystack[0].major = 0;
135819 }
135820
135821 #ifndef sqlite3Parser_ENGINEALWAYSONSTACK
135822 /*
135823 ** This function allocates a new parser.
135824 ** The only argument is a pointer to a function which works like
135825 ** malloc.
135826 **
@@ -135461,32 +135832,15 @@
135832 ** to sqlite3Parser and sqlite3ParserFree.
135833 */
135834 SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPE)){
135835 yyParser *pParser;
135836 pParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) );
135837 if( pParser ) sqlite3ParserInit(pParser);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135838 return pParser;
135839 }
135840 #endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
135841
135842
135843 /* The following function deletes the "minor type" or semantic value
135844 ** associated with a symbol. The symbol can be either a terminal
135845 ** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
135846 ** a pointer to the value to be deleted. The code used to do the
@@ -135608,10 +135962,22 @@
135962 }
135963 #endif
135964 yy_destructor(pParser, yytos->major, &yytos->minor);
135965 }
135966
135967 /*
135968 ** Clear all secondary memory allocations from the parser
135969 */
135970 SQLITE_PRIVATE void sqlite3ParserFinalize(void *p){
135971 yyParser *pParser = (yyParser*)p;
135972 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
135973 #if YYSTACKDEPTH<=0
135974 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
135975 #endif
135976 }
135977
135978 #ifndef sqlite3Parser_ENGINEALWAYSONSTACK
135979 /*
135980 ** Deallocate and destroy a parser. Destructors are called for
135981 ** all stack elements before shutting the parser down.
135982 **
135983 ** If the YYPARSEFREENEVERNULL macro exists (for example because it
@@ -135620,20 +135986,17 @@
135986 */
135987 SQLITE_PRIVATE void sqlite3ParserFree(
135988 void *p, /* The parser to be deleted */
135989 void (*freeProc)(void*) /* Function used to reclaim memory */
135990 ){
 
135991 #ifndef YYPARSEFREENEVERNULL
135992 if( p==0 ) return;
135993 #endif
135994 sqlite3ParserFinalize(p);
135995 (*freeProc)(p);
135996 }
135997 #endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
 
 
135998
135999 /*
136000 ** Return the peak depth of the stack for a parser.
136001 */
136002 #ifdef YYTRACKMAXSTACKDEPTH
@@ -138483,10 +138846,13 @@
138846 void *pEngine; /* The LEMON-generated LALR(1) parser */
138847 int tokenType; /* type of the next token */
138848 int lastTokenParsed = -1; /* type of the previous token */
138849 sqlite3 *db = pParse->db; /* The database connection */
138850 int mxSqlLen; /* Max length of an SQL string */
138851 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
138852 unsigned char zSpace[sizeof(yyParser)]; /* Space for parser engine object */
138853 #endif
138854
138855 assert( zSql!=0 );
138856 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
138857 if( db->nVdbeActive==0 ){
138858 db->u1.isInterrupted = 0;
@@ -138494,15 +138860,20 @@
138860 pParse->rc = SQLITE_OK;
138861 pParse->zTail = zSql;
138862 i = 0;
138863 assert( pzErrMsg!=0 );
138864 /* sqlite3ParserTrace(stdout, "parser: "); */
138865 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
138866 pEngine = zSpace;
138867 sqlite3ParserInit(pEngine);
138868 #else
138869 pEngine = sqlite3ParserAlloc(sqlite3Malloc);
138870 if( pEngine==0 ){
138871 sqlite3OomFault(db);
138872 return SQLITE_NOMEM_BKPT;
138873 }
138874 #endif
138875 assert( pParse->pNewTable==0 );
138876 assert( pParse->pNewTrigger==0 );
138877 assert( pParse->nVar==0 );
138878 assert( pParse->pVList==0 );
138879 while( 1 ){
@@ -138550,11 +138921,15 @@
138921 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
138922 sqlite3ParserStackPeak(pEngine)
138923 );
138924 sqlite3_mutex_leave(sqlite3MallocMutex());
138925 #endif /* YYDEBUG */
138926 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
138927 sqlite3ParserFinalize(pEngine);
138928 #else
138929 sqlite3ParserFree(pEngine, sqlite3_free);
138930 #endif
138931 if( db->mallocFailed ){
138932 pParse->rc = SQLITE_NOMEM_BKPT;
138933 }
138934 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
138935 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
@@ -145675,13 +146050,13 @@
146050 p->nPendingData = 0;
146051 p->azColumn = (char **)&p[1];
146052 p->pTokenizer = pTokenizer;
146053 p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
146054 p->bHasDocsize = (isFts4 && bNoDocsize==0);
146055 p->bHasStat = (u8)isFts4;
146056 p->bFts4 = (u8)isFts4;
146057 p->bDescIdx = (u8)bDescIdx;
146058 p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */
146059 p->zContentTbl = zContent;
146060 p->zLanguageid = zLanguageid;
146061 zContent = 0;
146062 zLanguageid = 0;
@@ -147734,11 +148109,11 @@
148109 sqlite3_stmt *pStmt = 0;
148110 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
148111 if( rc==SQLITE_OK ){
148112 int bHasStat = (sqlite3_step(pStmt)==SQLITE_ROW);
148113 rc = sqlite3_finalize(pStmt);
148114 if( rc==SQLITE_OK ) p->bHasStat = (u8)bHasStat;
148115 }
148116 sqlite3_free(zSql);
148117 }else{
148118 rc = SQLITE_NOMEM;
148119 }
@@ -162638,28 +163013,33 @@
163013 struct Rtree {
163014 sqlite3_vtab base; /* Base class. Must be first */
163015 sqlite3 *db; /* Host database connection */
163016 int iNodeSize; /* Size in bytes of each node in the node table */
163017 u8 nDim; /* Number of dimensions */
163018 u8 nDim2; /* Twice the number of dimensions */
163019 u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
163020 u8 nBytesPerCell; /* Bytes consumed per cell */
163021 u8 inWrTrans; /* True if inside write transaction */
163022 int iDepth; /* Current depth of the r-tree structure */
163023 char *zDb; /* Name of database containing r-tree table */
163024 char *zName; /* Name of r-tree table */
163025 u32 nBusy; /* Current number of users of this structure */
163026 i64 nRowEst; /* Estimated number of rows in this table */
163027 u32 nCursor; /* Number of open cursors */
163028
163029 /* List of nodes removed during a CondenseTree operation. List is
163030 ** linked together via the pointer normally used for hash chains -
163031 ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
163032 ** headed by the node (leaf nodes have RtreeNode.iNode==0).
163033 */
163034 RtreeNode *pDeleted;
163035 int iReinsertHeight; /* Height of sub-trees Reinsert() has run on */
163036
163037 /* Blob I/O on xxx_node */
163038 sqlite3_blob *pNodeBlob;
163039
163040 /* Statements to read/write/delete a record from xxx_node */
 
163041 sqlite3_stmt *pWriteNode;
163042 sqlite3_stmt *pDeleteNode;
163043
163044 /* Statements to read/write/delete a record from xxx_rowid */
163045 sqlite3_stmt *pReadRowid;
@@ -162884,26 +163264,110 @@
163264 #endif
163265 #ifndef MIN
163266 # define MIN(x,y) ((x) > (y) ? (y) : (x))
163267 #endif
163268
163269 /* What version of GCC is being used. 0 means GCC is not being used */
163270 #ifndef GCC_VERSION
163271 #ifdef __GNUC__
163272 # define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
163273 #else
163274 # define GCC_VERSION 0
163275 #endif
163276 #endif
163277
163278 /* What version of CLANG is being used. 0 means CLANG is not being used */
163279 #ifndef CLANG_VERSION
163280 #if defined(__clang__) && !defined(_WIN32)
163281 # define CLANG_VERSION \
163282 (__clang_major__*1000000+__clang_minor__*1000+__clang_patchlevel__)
163283 #else
163284 # define CLANG_VERSION 0
163285 #endif
163286 #endif
163287
163288 /* The testcase() macro should already be defined in the amalgamation. If
163289 ** it is not, make it a no-op.
163290 */
163291 #ifndef SQLITE_AMALGAMATION
163292 # define testcase(X)
163293 #endif
163294
163295 /*
163296 ** Macros to determine whether the machine is big or little endian,
163297 ** and whether or not that determination is run-time or compile-time.
163298 **
163299 ** For best performance, an attempt is made to guess at the byte-order
163300 ** using C-preprocessor macros. If that is unsuccessful, or if
163301 ** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
163302 ** at run-time.
163303 */
163304 #ifndef SQLITE_BYTEORDER
163305 #if (defined(i386) || defined(__i386__) || defined(_M_IX86) || \
163306 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
163307 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
163308 defined(__arm__)) && !defined(SQLITE_RUNTIME_BYTEORDER)
163309 # define SQLITE_BYTEORDER 1234
163310 #elif (defined(sparc) || defined(__ppc__)) \
163311 && !defined(SQLITE_RUNTIME_BYTEORDER)
163312 # define SQLITE_BYTEORDER 4321
163313 #else
163314 # define SQLITE_BYTEORDER 0 /* 0 means "unknown at compile-time" */
163315 #endif
163316 #endif
163317
163318
163319 /* What version of MSVC is being used. 0 means MSVC is not being used */
163320 #ifndef MSVC_VERSION
163321 #if defined(_MSC_VER)
163322 # define MSVC_VERSION _MSC_VER
163323 #else
163324 # define MSVC_VERSION 0
163325 #endif
163326 #endif
163327
163328 /*
163329 ** Functions to deserialize a 16 bit integer, 32 bit real number and
163330 ** 64 bit integer. The deserialized value is returned.
163331 */
163332 static int readInt16(u8 *p){
163333 return (p[0]<<8) + p[1];
163334 }
163335 static void readCoord(u8 *p, RtreeCoord *pCoord){
163336 assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */
163337 #if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163338 pCoord->u = _byteswap_ulong(*(u32*)p);
163339 #elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
163340 pCoord->u = __builtin_bswap32(*(u32*)p);
163341 #elif SQLITE_BYTEORDER==1234
163342 pCoord->u = ((pCoord->u>>24)&0xff)|((pCoord->u>>8)&0xff00)|
163343 ((pCoord->u&0xff)<<24)|((pCoord->u&0xff00)<<8);
163344 #elif SQLITE_BYTEORDER==4321
163345 pCoord->u = *(u32*)p;
163346 #else
163347 pCoord->u = (
163348 (((u32)p[0]) << 24) +
163349 (((u32)p[1]) << 16) +
163350 (((u32)p[2]) << 8) +
163351 (((u32)p[3]) << 0)
163352 );
163353 #endif
163354 }
163355 static i64 readInt64(u8 *p){
163356 #if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163357 u64 x;
163358 memcpy(&x, p, 8);
163359 return (i64)_byteswap_uint64(x);
163360 #elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
163361 u64 x;
163362 memcpy(&x, p, 8);
163363 return (i64)__builtin_bswap64(x);
163364 #elif SQLITE_BYTEORDER==4321
163365 i64 x;
163366 memcpy(&x, p, 8);
163367 return x;
163368 #else
163369 return (
163370 (((i64)p[0]) << 56) +
163371 (((i64)p[1]) << 48) +
163372 (((i64)p[2]) << 40) +
163373 (((i64)p[3]) << 32) +
@@ -162910,10 +163374,11 @@
163374 (((i64)p[4]) << 24) +
163375 (((i64)p[5]) << 16) +
163376 (((i64)p[6]) << 8) +
163377 (((i64)p[7]) << 0)
163378 );
163379 #endif
163380 }
163381
163382 /*
163383 ** Functions to serialize a 16 bit integer, 32 bit real number and
163384 ** 64 bit integer. The value returned is the number of bytes written
@@ -162924,28 +163389,50 @@
163389 p[1] = (i>> 0)&0xFF;
163390 return 2;
163391 }
163392 static int writeCoord(u8 *p, RtreeCoord *pCoord){
163393 u32 i;
163394 assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */
163395 assert( sizeof(RtreeCoord)==4 );
163396 assert( sizeof(u32)==4 );
163397 #if SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
163398 i = __builtin_bswap32(pCoord->u);
163399 memcpy(p, &i, 4);
163400 #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163401 i = _byteswap_ulong(pCoord->u);
163402 memcpy(p, &i, 4);
163403 #elif SQLITE_BYTEORDER==4321
163404 i = pCoord->u;
163405 memcpy(p, &i, 4);
163406 #else
163407 i = pCoord->u;
163408 p[0] = (i>>24)&0xFF;
163409 p[1] = (i>>16)&0xFF;
163410 p[2] = (i>> 8)&0xFF;
163411 p[3] = (i>> 0)&0xFF;
163412 #endif
163413 return 4;
163414 }
163415 static int writeInt64(u8 *p, i64 i){
163416 #if SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
163417 i = (i64)__builtin_bswap64((u64)i);
163418 memcpy(p, &i, 8);
163419 #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163420 i = (i64)_byteswap_uint64((u64)i);
163421 memcpy(p, &i, 8);
163422 #elif SQLITE_BYTEORDER==4321
163423 memcpy(p, &i, 8);
163424 #else
163425 p[0] = (i>>56)&0xFF;
163426 p[1] = (i>>48)&0xFF;
163427 p[2] = (i>>40)&0xFF;
163428 p[3] = (i>>32)&0xFF;
163429 p[4] = (i>>24)&0xFF;
163430 p[5] = (i>>16)&0xFF;
163431 p[6] = (i>> 8)&0xFF;
163432 p[7] = (i>> 0)&0xFF;
163433 #endif
163434 return 8;
163435 }
163436
163437 /*
163438 ** Increment the reference count of node p.
@@ -163023,10 +163510,21 @@
163510 pNode->isDirty = 1;
163511 nodeReference(pParent);
163512 }
163513 return pNode;
163514 }
163515
163516 /*
163517 ** Clear the Rtree.pNodeBlob object
163518 */
163519 static void nodeBlobReset(Rtree *pRtree){
163520 if( pRtree->pNodeBlob && pRtree->inWrTrans==0 && pRtree->nCursor==0 ){
163521 sqlite3_blob *pBlob = pRtree->pNodeBlob;
163522 pRtree->pNodeBlob = 0;
163523 sqlite3_blob_close(pBlob);
163524 }
163525 }
163526
163527 /*
163528 ** Obtain a reference to an r-tree node.
163529 */
163530 static int nodeAcquire(
@@ -163033,13 +163531,12 @@
163531 Rtree *pRtree, /* R-tree structure */
163532 i64 iNode, /* Node number to load */
163533 RtreeNode *pParent, /* Either the parent node or NULL */
163534 RtreeNode **ppNode /* OUT: Acquired node */
163535 ){
163536 int rc = SQLITE_OK;
163537 RtreeNode *pNode = 0;
 
163538
163539 /* Check if the requested node is already in the hash table. If so,
163540 ** increase its reference count and return it.
163541 */
163542 if( (pNode = nodeHashLookup(pRtree, iNode)) ){
@@ -163051,32 +163548,49 @@
163548 pNode->nRef++;
163549 *ppNode = pNode;
163550 return SQLITE_OK;
163551 }
163552
163553 if( pRtree->pNodeBlob ){
163554 sqlite3_blob *pBlob = pRtree->pNodeBlob;
163555 pRtree->pNodeBlob = 0;
163556 rc = sqlite3_blob_reopen(pBlob, iNode);
163557 pRtree->pNodeBlob = pBlob;
163558 if( rc ){
163559 nodeBlobReset(pRtree);
163560 if( rc==SQLITE_NOMEM ) return SQLITE_NOMEM;
163561 }
163562 }
163563 if( pRtree->pNodeBlob==0 ){
163564 char *zTab = sqlite3_mprintf("%s_node", pRtree->zName);
163565 if( zTab==0 ) return SQLITE_NOMEM;
163566 rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, zTab, "data", iNode, 0,
163567 &pRtree->pNodeBlob);
163568 sqlite3_free(zTab);
163569 }
163570 if( rc ){
163571 nodeBlobReset(pRtree);
163572 *ppNode = 0;
163573 /* If unable to open an sqlite3_blob on the desired row, that can only
163574 ** be because the shadow tables hold erroneous data. */
163575 if( rc==SQLITE_ERROR ) rc = SQLITE_CORRUPT_VTAB;
163576 }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){
163577 pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
163578 if( !pNode ){
163579 rc = SQLITE_NOMEM;
163580 }else{
163581 pNode->pParent = pParent;
163582 pNode->zData = (u8 *)&pNode[1];
163583 pNode->nRef = 1;
163584 pNode->iNode = iNode;
163585 pNode->isDirty = 0;
163586 pNode->pNext = 0;
163587 rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
163588 pRtree->iNodeSize, 0);
163589 nodeReference(pParent);
163590 }
163591 }
163592
163593 /* If the root node was just loaded, set pRtree->iDepth to the height
163594 ** of the r-tree structure. A height of zero means all data is stored on
163595 ** the root node. A height of one means the children of the root node
163596 ** are the leaves, and so on. If the depth as specified on the root node
@@ -163124,11 +163638,11 @@
163638 int iCell /* Index into pNode into which pCell is written */
163639 ){
163640 int ii;
163641 u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
163642 p += writeInt64(p, pCell->iRowid);
163643 for(ii=0; ii<pRtree->nDim2; ii++){
163644 p += writeCoord(p, &pCell->aCoord[ii]);
163645 }
163646 pNode->isDirty = 1;
163647 }
163648
@@ -163258,17 +163772,20 @@
163772 int iCell, /* Index of the cell within the node */
163773 RtreeCell *pCell /* OUT: Write the cell contents here */
163774 ){
163775 u8 *pData;
163776 RtreeCoord *pCoord;
163777 int ii = 0;
163778 pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
163779 pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
163780 pCoord = pCell->aCoord;
163781 do{
163782 readCoord(pData, &pCoord[ii]);
163783 readCoord(pData+4, &pCoord[ii+1]);
163784 pData += 8;
163785 ii += 2;
163786 }while( ii<pRtree->nDim2 );
163787 }
163788
163789
163790 /* Forward declaration for the function that does the work of
163791 ** the virtual table module xCreate() and xConnect() methods.
@@ -163315,11 +163832,13 @@
163832 ** zero the structure is deleted.
163833 */
163834 static void rtreeRelease(Rtree *pRtree){
163835 pRtree->nBusy--;
163836 if( pRtree->nBusy==0 ){
163837 pRtree->inWrTrans = 0;
163838 pRtree->nCursor = 0;
163839 nodeBlobReset(pRtree);
163840 sqlite3_finalize(pRtree->pWriteNode);
163841 sqlite3_finalize(pRtree->pDeleteNode);
163842 sqlite3_finalize(pRtree->pReadRowid);
163843 sqlite3_finalize(pRtree->pWriteRowid);
163844 sqlite3_finalize(pRtree->pDeleteRowid);
@@ -163353,10 +163872,11 @@
163872 pRtree->zDb, pRtree->zName
163873 );
163874 if( !zCreate ){
163875 rc = SQLITE_NOMEM;
163876 }else{
163877 nodeBlobReset(pRtree);
163878 rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
163879 sqlite3_free(zCreate);
163880 }
163881 if( rc==SQLITE_OK ){
163882 rtreeRelease(pRtree);
@@ -163368,17 +163888,19 @@
163888 /*
163889 ** Rtree virtual table module xOpen method.
163890 */
163891 static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
163892 int rc = SQLITE_NOMEM;
163893 Rtree *pRtree = (Rtree *)pVTab;
163894 RtreeCursor *pCsr;
163895
163896 pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
163897 if( pCsr ){
163898 memset(pCsr, 0, sizeof(RtreeCursor));
163899 pCsr->base.pVtab = pVTab;
163900 rc = SQLITE_OK;
163901 pRtree->nCursor++;
163902 }
163903 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
163904
163905 return rc;
163906 }
@@ -163407,14 +163929,17 @@
163929 */
163930 static int rtreeClose(sqlite3_vtab_cursor *cur){
163931 Rtree *pRtree = (Rtree *)(cur->pVtab);
163932 int ii;
163933 RtreeCursor *pCsr = (RtreeCursor *)cur;
163934 assert( pRtree->nCursor>0 );
163935 freeCursorConstraints(pCsr);
163936 sqlite3_free(pCsr->aPoint);
163937 for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
163938 sqlite3_free(pCsr);
163939 pRtree->nCursor--;
163940 nodeBlobReset(pRtree);
163941 return SQLITE_OK;
163942 }
163943
163944 /*
163945 ** Rtree virtual table module xEof method.
@@ -163433,27 +163958,34 @@
163958 ** endian platforms. The on-disk record stores integer coordinates if
163959 ** eInt is true and it stores 32-bit floating point records if eInt is
163960 ** false. a[] is the four bytes of the on-disk record to be decoded.
163961 ** Store the results in "r".
163962 **
163963 ** There are five versions of this macro. The last one is generic. The
163964 ** other four are various architectures-specific optimizations.
 
 
 
 
 
163965 */
163966 #if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163967 #define RTREE_DECODE_COORD(eInt, a, r) { \
163968 RtreeCoord c; /* Coordinate decoded */ \
163969 c.u = _byteswap_ulong(*(u32*)a); \
163970 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
163971 }
163972 #elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
163973 #define RTREE_DECODE_COORD(eInt, a, r) { \
163974 RtreeCoord c; /* Coordinate decoded */ \
163975 c.u = __builtin_bswap32(*(u32*)a); \
163976 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
163977 }
163978 #elif SQLITE_BYTEORDER==1234
163979 #define RTREE_DECODE_COORD(eInt, a, r) { \
163980 RtreeCoord c; /* Coordinate decoded */ \
163981 memcpy(&c.u,a,4); \
163982 c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)| \
163983 ((c.u&0xff)<<24)|((c.u&0xff00)<<8); \
163984 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
163985 }
163986 #elif SQLITE_BYTEORDER==4321
163987 #define RTREE_DECODE_COORD(eInt, a, r) { \
163988 RtreeCoord c; /* Coordinate decoded */ \
163989 memcpy(&c.u,a,4); \
163990 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
163991 }
@@ -163476,30 +164008,58 @@
164008 u8 *pCellData, /* Raw cell content */
164009 RtreeSearchPoint *pSearch, /* Container of this cell */
164010 sqlite3_rtree_dbl *prScore, /* OUT: score for the cell */
164011 int *peWithin /* OUT: visibility of the cell */
164012 ){
 
164013 sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
164014 int nCoord = pInfo->nCoord; /* No. of coordinates */
164015 int rc; /* Callback return code */
164016 RtreeCoord c; /* Translator union */
164017 sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2]; /* Decoded coordinates */
164018
164019 assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
164020 assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 );
164021
164022 if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
164023 pInfo->iRowid = readInt64(pCellData);
164024 }
164025 pCellData += 8;
164026 #ifndef SQLITE_RTREE_INT_ONLY
164027 if( eInt==0 ){
164028 switch( nCoord ){
164029 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.f;
164030 readCoord(pCellData+32, &c); aCoord[8] = c.f;
164031 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.f;
164032 readCoord(pCellData+24, &c); aCoord[6] = c.f;
164033 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.f;
164034 readCoord(pCellData+16, &c); aCoord[4] = c.f;
164035 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.f;
164036 readCoord(pCellData+8, &c); aCoord[2] = c.f;
164037 default: readCoord(pCellData+4, &c); aCoord[1] = c.f;
164038 readCoord(pCellData, &c); aCoord[0] = c.f;
164039 }
164040 }else
164041 #endif
164042 {
164043 switch( nCoord ){
164044 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.i;
164045 readCoord(pCellData+32, &c); aCoord[8] = c.i;
164046 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.i;
164047 readCoord(pCellData+24, &c); aCoord[6] = c.i;
164048 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.i;
164049 readCoord(pCellData+16, &c); aCoord[4] = c.i;
164050 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.i;
164051 readCoord(pCellData+8, &c); aCoord[2] = c.i;
164052 default: readCoord(pCellData+4, &c); aCoord[1] = c.i;
164053 readCoord(pCellData, &c); aCoord[0] = c.i;
164054 }
164055 }
164056 if( pConstraint->op==RTREE_MATCH ){
164057 int eWithin = 0;
164058 rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
164059 nCoord, aCoord, &eWithin);
164060 if( eWithin==0 ) *peWithin = NOT_WITHIN;
164061 *prScore = RTREE_ZERO;
164062 }else{
164063 pInfo->aCoord = aCoord;
164064 pInfo->iLevel = pSearch->iLevel - 1;
164065 pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
@@ -163531,10 +164091,11 @@
164091 */
164092 pCellData += 8 + 4*(p->iCoord&0xfe);
164093
164094 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
164095 || p->op==RTREE_GT || p->op==RTREE_EQ );
164096 assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */
164097 switch( p->op ){
164098 case RTREE_LE:
164099 case RTREE_LT:
164100 case RTREE_EQ:
164101 RTREE_DECODE_COORD(eInt, pCellData, val);
@@ -163571,10 +164132,11 @@
164132 RtreeDValue xN; /* Coordinate value converted to a double */
164133
164134 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
164135 || p->op==RTREE_GT || p->op==RTREE_EQ );
164136 pCellData += 8 + p->iCoord*4;
164137 assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */
164138 RTREE_DECODE_COORD(eInt, pCellData, xN);
164139 switch( p->op ){
164140 case RTREE_LE: if( xN <= p->u.rValue ) return; break;
164141 case RTREE_LT: if( xN < p->u.rValue ) return; break;
164142 case RTREE_GE: if( xN >= p->u.rValue ) return; break;
@@ -163639,11 +164201,11 @@
164201 if( pA->iLevel>pB->iLevel ) return +1;
164202 return 0;
164203 }
164204
164205 /*
164206 ** Interchange two search points in a cursor.
164207 */
164208 static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
164209 RtreeSearchPoint t = p->aPoint[i];
164210 assert( i<j );
164211 p->aPoint[i] = p->aPoint[j];
@@ -163887,11 +164449,11 @@
164449 rtreeSearchPointPop(pCur);
164450 }
164451 if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO;
164452 p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
164453 if( p==0 ) return SQLITE_NOMEM;
164454 p->eWithin = (u8)eWithin;
164455 p->id = x.id;
164456 p->iCell = x.iCell;
164457 RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
164458 break;
164459 }
@@ -163946,11 +164508,10 @@
164508 if( rc ) return rc;
164509 if( p==0 ) return SQLITE_OK;
164510 if( i==0 ){
164511 sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
164512 }else{
 
164513 nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
164514 #ifndef SQLITE_RTREE_INT_ONLY
164515 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
164516 sqlite3_result_double(ctx, c.f);
164517 }else
@@ -164075,11 +164636,11 @@
164636 assert( p!=0 ); /* Always returns pCsr->sPoint */
164637 pCsr->aNode[0] = pLeaf;
164638 p->id = iNode;
164639 p->eWithin = PARTLY_WITHIN;
164640 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
164641 p->iCell = (u8)iCell;
164642 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
164643 }else{
164644 pCsr->atEOF = 1;
164645 }
164646 }else{
@@ -164108,11 +164669,11 @@
164669 */
164670 rc = deserializeGeometry(argv[ii], p);
164671 if( rc!=SQLITE_OK ){
164672 break;
164673 }
164674 p->pInfo->nCoord = pRtree->nDim2;
164675 p->pInfo->anQueue = pCsr->anQueue;
164676 p->pInfo->mxLevel = pRtree->iDepth + 1;
164677 }else{
164678 #ifdef SQLITE_RTREE_INT_ONLY
164679 p->u.rValue = sqlite3_value_int64(argv[ii]);
@@ -164123,11 +164684,11 @@
164684 }
164685 }
164686 }
164687 if( rc==SQLITE_OK ){
164688 RtreeSearchPoint *pNew;
164689 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
164690 if( pNew==0 ) return SQLITE_NOMEM;
164691 pNew->id = 1;
164692 pNew->iCell = 0;
164693 pNew->eWithin = PARTLY_WITHIN;
164694 assert( pCsr->bPoint==1 );
@@ -164141,23 +164702,10 @@
164702 nodeRelease(pRtree, pRoot);
164703 rtreeRelease(pRtree);
164704 return rc;
164705 }
164706
 
 
 
 
 
 
 
 
 
 
 
 
 
164707 /*
164708 ** Rtree virtual table module xBestIndex method. There are three
164709 ** table scan strategies to choose from (in order from most to
164710 ** least desirable):
164711 **
@@ -164233,11 +164781,11 @@
164781 ** considered almost as quick as a direct rowid lookup (for which
164782 ** sqlite uses an internal cost of 0.0). It is expected to return
164783 ** a single row.
164784 */
164785 pIdxInfo->estimatedCost = 30.0;
164786 pIdxInfo->estimatedRows = 1;
164787 return SQLITE_OK;
164788 }
164789
164790 if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
164791 u8 op;
@@ -164251,11 +164799,11 @@
164799 assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
164800 op = RTREE_MATCH;
164801 break;
164802 }
164803 zIdxStr[iIdx++] = op;
164804 zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0');
164805 pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
164806 pIdxInfo->aConstraintUsage[ii].omit = 1;
164807 }
164808 }
164809
@@ -164265,55 +164813,75 @@
164813 return SQLITE_NOMEM;
164814 }
164815
164816 nRow = pRtree->nRowEst >> (iIdx/2);
164817 pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
164818 pIdxInfo->estimatedRows = nRow;
164819
164820 return rc;
164821 }
164822
164823 /*
164824 ** Return the N-dimensional volumn of the cell stored in *p.
164825 */
164826 static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
164827 RtreeDValue area = (RtreeDValue)1;
164828 assert( pRtree->nDim>=1 && pRtree->nDim<=5 );
164829 #ifndef SQLITE_RTREE_INT_ONLY
164830 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
164831 switch( pRtree->nDim ){
164832 case 5: area = p->aCoord[9].f - p->aCoord[8].f;
164833 case 4: area *= p->aCoord[7].f - p->aCoord[6].f;
164834 case 3: area *= p->aCoord[5].f - p->aCoord[4].f;
164835 case 2: area *= p->aCoord[3].f - p->aCoord[2].f;
164836 default: area *= p->aCoord[1].f - p->aCoord[0].f;
164837 }
164838 }else
164839 #endif
164840 {
164841 switch( pRtree->nDim ){
164842 case 5: area = p->aCoord[9].i - p->aCoord[8].i;
164843 case 4: area *= p->aCoord[7].i - p->aCoord[6].i;
164844 case 3: area *= p->aCoord[5].i - p->aCoord[4].i;
164845 case 2: area *= p->aCoord[3].i - p->aCoord[2].i;
164846 default: area *= p->aCoord[1].i - p->aCoord[0].i;
164847 }
164848 }
164849 return area;
164850 }
164851
164852 /*
164853 ** Return the margin length of cell p. The margin length is the sum
164854 ** of the objects size in each dimension.
164855 */
164856 static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
164857 RtreeDValue margin = 0;
164858 int ii = pRtree->nDim2 - 2;
164859 do{
164860 margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
164861 ii -= 2;
164862 }while( ii>=0 );
164863 return margin;
164864 }
164865
164866 /*
164867 ** Store the union of cells p1 and p2 in p1.
164868 */
164869 static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
164870 int ii = 0;
164871 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
164872 do{
164873 p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
164874 p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
164875 ii += 2;
164876 }while( ii<pRtree->nDim2 );
164877 }else{
164878 do{
164879 p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
164880 p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
164881 ii += 2;
164882 }while( ii<pRtree->nDim2 );
164883 }
164884 }
164885
164886 /*
164887 ** Return true if the area covered by p2 is a subset of the area covered
@@ -164320,11 +164888,11 @@
164888 ** by p1. False otherwise.
164889 */
164890 static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
164891 int ii;
164892 int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
164893 for(ii=0; ii<pRtree->nDim2; ii+=2){
164894 RtreeCoord *a1 = &p1->aCoord[ii];
164895 RtreeCoord *a2 = &p2->aCoord[ii];
164896 if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f))
164897 || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i))
164898 ){
@@ -164355,11 +164923,11 @@
164923 int ii;
164924 RtreeDValue overlap = RTREE_ZERO;
164925 for(ii=0; ii<nCell; ii++){
164926 int jj;
164927 RtreeDValue o = (RtreeDValue)1;
164928 for(jj=0; jj<pRtree->nDim2; jj+=2){
164929 RtreeDValue x1, x2;
164930 x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
164931 x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
164932 if( x2<x1 ){
164933 o = (RtreeDValue)0;
@@ -165411,11 +165979,11 @@
165979 ** with "column" that are interpreted as table constraints.
165980 ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
165981 ** This problem was discovered after years of use, so we silently ignore
165982 ** these kinds of misdeclared tables to avoid breaking any legacy.
165983 */
165984 assert( nData<=(pRtree->nDim2 + 3) );
165985
165986 #ifndef SQLITE_RTREE_INT_ONLY
165987 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
165988 for(ii=0; ii<nData-4; ii+=2){
165989 cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]);
@@ -165500,10 +166068,31 @@
166068
166069 constraint:
166070 rtreeRelease(pRtree);
166071 return rc;
166072 }
166073
166074 /*
166075 ** Called when a transaction starts.
166076 */
166077 static int rtreeBeginTransaction(sqlite3_vtab *pVtab){
166078 Rtree *pRtree = (Rtree *)pVtab;
166079 assert( pRtree->inWrTrans==0 );
166080 pRtree->inWrTrans++;
166081 return SQLITE_OK;
166082 }
166083
166084 /*
166085 ** Called when a transaction completes (either by COMMIT or ROLLBACK).
166086 ** The sqlite3_blob object should be released at this point.
166087 */
166088 static int rtreeEndTransaction(sqlite3_vtab *pVtab){
166089 Rtree *pRtree = (Rtree *)pVtab;
166090 pRtree->inWrTrans = 0;
166091 nodeBlobReset(pRtree);
166092 return SQLITE_OK;
166093 }
166094
166095 /*
166096 ** The xRename method for rtree module virtual tables.
166097 */
166098 static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
@@ -165521,10 +166110,11 @@
166110 rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
166111 sqlite3_free(zSql);
166112 }
166113 return rc;
166114 }
166115
166116
166117 /*
166118 ** This function populates the pRtree->nRowEst variable with an estimate
166119 ** of the number of rows in the virtual table. If possible, this is based
166120 ** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
@@ -165581,19 +166171,19 @@
166171 rtreeNext, /* xNext - advance a cursor */
166172 rtreeEof, /* xEof */
166173 rtreeColumn, /* xColumn - read data */
166174 rtreeRowid, /* xRowid - read data */
166175 rtreeUpdate, /* xUpdate - write data */
166176 rtreeBeginTransaction, /* xBegin - begin transaction */
166177 rtreeEndTransaction, /* xSync - sync transaction */
166178 rtreeEndTransaction, /* xCommit - commit transaction */
166179 rtreeEndTransaction, /* xRollback - rollback transaction */
166180 0, /* xFindFunction - function overloading */
166181 rtreeRename, /* xRename - rename the table */
166182 0, /* xSavepoint */
166183 0, /* xRelease */
166184 0, /* xRollbackTo */
166185 };
166186
166187 static int rtreeSqlInit(
166188 Rtree *pRtree,
166189 sqlite3 *db,
@@ -165601,14 +166191,13 @@
166191 const char *zPrefix,
166192 int isCreate
166193 ){
166194 int rc = SQLITE_OK;
166195
166196 #define N_STATEMENT 8
166197 static const char *azSql[N_STATEMENT] = {
166198 /* Write the xxx_node table */
 
166199 "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
166200 "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
166201
166202 /* Read and write the xxx_rowid table */
166203 "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
@@ -165642,19 +166231,18 @@
166231 if( rc!=SQLITE_OK ){
166232 return rc;
166233 }
166234 }
166235
166236 appStmt[0] = &pRtree->pWriteNode;
166237 appStmt[1] = &pRtree->pDeleteNode;
166238 appStmt[2] = &pRtree->pReadRowid;
166239 appStmt[3] = &pRtree->pWriteRowid;
166240 appStmt[4] = &pRtree->pDeleteRowid;
166241 appStmt[5] = &pRtree->pReadParent;
166242 appStmt[6] = &pRtree->pWriteParent;
166243 appStmt[7] = &pRtree->pDeleteParent;
 
166244
166245 rc = rtreeQueryStat1(db, pRtree);
166246 for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
166247 char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
166248 if( zSql ){
@@ -165788,13 +166376,14 @@
166376 memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
166377 pRtree->nBusy = 1;
166378 pRtree->base.pModule = &rtreeModule;
166379 pRtree->zDb = (char *)&pRtree[1];
166380 pRtree->zName = &pRtree->zDb[nDb+1];
166381 pRtree->nDim = (u8)((argc-4)/2);
166382 pRtree->nDim2 = pRtree->nDim*2;
166383 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
166384 pRtree->eCoordType = (u8)eCoordType;
166385 memcpy(pRtree->zDb, argv[1], nDb);
166386 memcpy(pRtree->zName, argv[2], nName);
166387
166388 /* Figure out the node size to use. */
166389 rc = getNodeSize(db, pRtree, isCreate, pzErr);
@@ -165863,11 +166452,12 @@
166452 int ii;
166453
166454 UNUSED_PARAMETER(nArg);
166455 memset(&node, 0, sizeof(RtreeNode));
166456 memset(&tree, 0, sizeof(Rtree));
166457 tree.nDim = (u8)sqlite3_value_int(apArg[0]);
166458 tree.nDim2 = tree.nDim*2;
166459 tree.nBytesPerCell = 8 + 8 * tree.nDim;
166460 node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
166461
166462 for(ii=0; ii<NCELL(&node); ii++){
166463 char zCell[512];
@@ -165876,11 +166466,11 @@
166466 int jj;
166467
166468 nodeGetCell(&tree, &node, ii, &cell);
166469 sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
166470 nCell = (int)strlen(zCell);
166471 for(jj=0; jj<tree.nDim2; jj++){
166472 #ifndef SQLITE_RTREE_INT_ONLY
166473 sqlite3_snprintf(512-nCell,&zCell[nCell], " %g",
166474 (double)cell.aCoord[jj].f);
166475 #else
166476 sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
@@ -166584,42 +167174,40 @@
167174
167175 /*
167176 ** Register the ICU extension functions with database db.
167177 */
167178 SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
167179 static const struct IcuScalar {
167180 const char *zName; /* Function name */
167181 unsigned char nArg; /* Number of arguments */
167182 unsigned short enc; /* Optimal text encoding */
167183 unsigned char iContext; /* sqlite3_user_data() context */
167184 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
167185 } scalars[] = {
167186 {"icu_load_collation", 2, SQLITE_UTF8, 1, icuLoadCollation},
167187 {"regexp", 2, SQLITE_ANY|SQLITE_DETERMINISTIC, 0, icuRegexpFunc},
167188 {"lower", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
167189 {"lower", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
167190 {"upper", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
167191 {"upper", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
167192 {"lower", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
167193 {"lower", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
167194 {"upper", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
167195 {"upper", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
167196 {"like", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc},
167197 {"like", 3, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc},
 
 
 
 
167198 };
 
167199 int rc = SQLITE_OK;
167200 int i;
167201
167202
167203 for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
167204 const struct IcuScalar *p = &scalars[i];
167205 rc = sqlite3_create_function(
167206 db, p->zName, p->nArg, p->enc,
167207 p->iContext ? (void*)db : (void*)0,
167208 p->xFunc, 0, 0
167209 );
167210 }
167211
167212 return rc;
167213 }
@@ -169823,11 +170411,11 @@
170411
170412 /*
170413 ** Open the database handle and attach the RBU database as "rbu". If an
170414 ** error occurs, leave an error code and message in the RBU handle.
170415 */
170416 static void rbuOpenDatabase(sqlite3rbu *p, int *pbRetry){
170417 assert( p->rc || (p->dbMain==0 && p->dbRbu==0) );
170418 assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 );
170419
170420 /* Open the RBU database */
170421 p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
@@ -169898,11 +170486,11 @@
170486 if( p->eStage>=RBU_STAGE_MOVE ){
170487 bOpen = 1;
170488 }else{
170489 RbuState *pState = rbuLoadState(p);
170490 if( pState ){
170491 bOpen = (pState->eStage>=RBU_STAGE_MOVE);
170492 rbuFreeState(pState);
170493 }
170494 }
170495 if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
170496 }
@@ -169910,10 +170498,19 @@
170498 p->eStage = 0;
170499 if( p->rc==SQLITE_OK && p->dbMain==0 ){
170500 if( !rbuIsVacuum(p) ){
170501 p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
170502 }else if( p->pRbuFd->pWalFd ){
170503 if( pbRetry ){
170504 p->pRbuFd->bNolock = 0;
170505 sqlite3_close(p->dbRbu);
170506 sqlite3_close(p->dbMain);
170507 p->dbMain = 0;
170508 p->dbRbu = 0;
170509 *pbRetry = 1;
170510 return;
170511 }
170512 p->rc = SQLITE_ERROR;
170513 p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
170514 }else{
170515 char *zTarget;
170516 char *zExtra = 0;
@@ -170090,20 +170687,22 @@
170687 p->eStage = RBU_STAGE_CAPTURE;
170688 rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
170689 if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
170690 }
170691
170692 if( p->rc==SQLITE_OK && p->nFrame>0 ){
170693 p->eStage = RBU_STAGE_CKPT;
170694 p->nStep = (pState ? pState->nRow : 0);
170695 p->aBuf = rbuMalloc(p, p->pgsz);
170696 p->iWalCksum = rbuShmChecksum(p);
170697 }
170698
170699 if( p->rc==SQLITE_OK ){
170700 if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
170701 p->rc = SQLITE_DONE;
170702 p->eStage = RBU_STAGE_DONE;
170703 }
170704 }
170705 }
170706
170707 /*
170708 ** Called when iAmt bytes are read from offset iOff of the wal file while
@@ -170272,11 +170871,11 @@
170871 #else
170872 p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
170873 #endif
170874
170875 if( p->rc==SQLITE_OK ){
170876 rbuOpenDatabase(p, 0);
170877 rbuSetupCheckpoint(p, 0);
170878 }
170879 }
170880 }
170881
@@ -170983,10 +171582,11 @@
171582 rbuCreateVfs(p);
171583
171584 /* Open the target, RBU and state databases */
171585 if( p->rc==SQLITE_OK ){
171586 char *pCsr = (char*)&p[1];
171587 int bRetry = 0;
171588 if( zTarget ){
171589 p->zTarget = pCsr;
171590 memcpy(p->zTarget, zTarget, nTarget+1);
171591 pCsr += nTarget+1;
171592 }
@@ -170994,11 +171594,22 @@
171594 memcpy(p->zRbu, zRbu, nRbu+1);
171595 pCsr += nRbu+1;
171596 if( zState ){
171597 p->zState = rbuMPrintf(p, "%s", zState);
171598 }
171599
171600 /* If the first attempt to open the database file fails and the bRetry
171601 ** flag it set, this means that the db was not opened because it seemed
171602 ** to be a wal-mode db. But, this may have happened due to an earlier
171603 ** RBU vacuum operation leaving an old wal file in the directory.
171604 ** If this is the case, it will have been checkpointed and deleted
171605 ** when the handle was closed and a second attempt to open the
171606 ** database may succeed. */
171607 rbuOpenDatabase(p, &bRetry);
171608 if( bRetry ){
171609 rbuOpenDatabase(p, 0);
171610 }
171611 }
171612
171613 if( p->rc==SQLITE_OK ){
171614 pState = rbuLoadState(p);
171615 assert( pState || p->rc!=SQLITE_OK );
@@ -175957,11 +176568,11 @@
176568 sqlite3_value **ppValue /* OUT: Value from conflicting row */
176569 ){
176570 if( !pIter->pConflict ){
176571 return SQLITE_MISUSE;
176572 }
176573 if( iVal<0 || iVal>=pIter->nCol ){
176574 return SQLITE_RANGE;
176575 }
176576 *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
176577 return SQLITE_OK;
176578 }
@@ -176424,11 +177035,17 @@
177035 int i;
177036 SessionBuffer buf = {0, 0, 0};
177037
177038 sessionAppendStr(&buf, "INSERT INTO main.", &rc);
177039 sessionAppendIdent(&buf, zTab, &rc);
177040 sessionAppendStr(&buf, "(", &rc);
177041 for(i=0; i<p->nCol; i++){
177042 if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
177043 sessionAppendIdent(&buf, p->azCol[i], &rc);
177044 }
177045
177046 sessionAppendStr(&buf, ") VALUES(?", &rc);
177047 for(i=1; i<p->nCol; i++){
177048 sessionAppendStr(&buf, ", ?", &rc);
177049 }
177050 sessionAppendStr(&buf, ")", &rc);
177051
@@ -176970,42 +177587,51 @@
177587 break;
177588 }
177589 nTab = (int)strlen(zTab);
177590 sApply.azCol = (const char **)zTab;
177591 }else{
177592 int nMinCol = 0;
177593 int i;
177594
177595 sqlite3changeset_pk(pIter, &abPK, 0);
177596 rc = sessionTableInfo(
177597 db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
177598 );
177599 if( rc!=SQLITE_OK ) break;
177600 for(i=0; i<sApply.nCol; i++){
177601 if( sApply.abPK[i] ) nMinCol = i+1;
177602 }
177603
177604 if( sApply.nCol==0 ){
177605 schemaMismatch = 1;
177606 sqlite3_log(SQLITE_SCHEMA,
177607 "sqlite3changeset_apply(): no such table: %s", zTab
177608 );
177609 }
177610 else if( sApply.nCol<nCol ){
177611 schemaMismatch = 1;
177612 sqlite3_log(SQLITE_SCHEMA,
177613 "sqlite3changeset_apply(): table %s has %d columns, "
177614 "expected %d or more",
177615 zTab, sApply.nCol, nCol
177616 );
177617 }
177618 else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
177619 schemaMismatch = 1;
177620 sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
177621 "primary key mismatch for table %s", zTab
177622 );
177623 }
177624 else{
177625 sApply.nCol = nCol;
177626 if((rc = sessionSelectRow(db, zTab, &sApply))
177627 || (rc = sessionUpdateRow(db, zTab, &sApply))
177628 || (rc = sessionDeleteRow(db, zTab, &sApply))
177629 || (rc = sessionInsertRow(db, zTab, &sApply))
177630 ){
177631 break;
177632 }
177633 }
177634 nTab = sqlite3Strlen30(zTab);
177635 }
177636 }
177637
@@ -177593,11 +178219,11 @@
178219 ** a BLOB, but there is no support for JSONB in the current implementation.
178220 ** This implementation parses JSON text at 250 MB/s, so it is hard to see
178221 ** how JSONB might improve on that.)
178222 */
178223 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
178224 #if !defined(SQLITEINT_H)
178225 /* #include "sqlite3ext.h" */
178226 #endif
178227 SQLITE_EXTENSION_INIT1
178228 /* #include <assert.h> */
178229 /* #include <string.h> */
@@ -181644,10 +182270,35 @@
182270 */
182271 #ifndef fts5YYMALLOCARGTYPE
182272 # define fts5YYMALLOCARGTYPE size_t
182273 #endif
182274
182275 /* Initialize a new parser that has already been allocated.
182276 */
182277 static void sqlite3Fts5ParserInit(void *fts5yypParser){
182278 fts5yyParser *pParser = (fts5yyParser*)fts5yypParser;
182279 #ifdef fts5YYTRACKMAXSTACKDEPTH
182280 pParser->fts5yyhwm = 0;
182281 #endif
182282 #if fts5YYSTACKDEPTH<=0
182283 pParser->fts5yytos = NULL;
182284 pParser->fts5yystack = NULL;
182285 pParser->fts5yystksz = 0;
182286 if( fts5yyGrowStack(pParser) ){
182287 pParser->fts5yystack = &pParser->fts5yystk0;
182288 pParser->fts5yystksz = 1;
182289 }
182290 #endif
182291 #ifndef fts5YYNOERRORRECOVERY
182292 pParser->fts5yyerrcnt = -1;
182293 #endif
182294 pParser->fts5yytos = pParser->fts5yystack;
182295 pParser->fts5yystack[0].stateno = 0;
182296 pParser->fts5yystack[0].major = 0;
182297 }
182298
182299 #ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
182300 /*
182301 ** This function allocates a new parser.
182302 ** The only argument is a pointer to a function which works like
182303 ** malloc.
182304 **
@@ -181659,32 +182310,15 @@
182310 ** to sqlite3Fts5Parser and sqlite3Fts5ParserFree.
182311 */
182312 static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(fts5YYMALLOCARGTYPE)){
182313 fts5yyParser *pParser;
182314 pParser = (fts5yyParser*)(*mallocProc)( (fts5YYMALLOCARGTYPE)sizeof(fts5yyParser) );
182315 if( pParser ) sqlite3Fts5ParserInit(pParser);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182316 return pParser;
182317 }
182318 #endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
182319
182320
182321 /* The following function deletes the "minor type" or semantic value
182322 ** associated with a symbol. The symbol can be either a terminal
182323 ** or nonterminal. "fts5yymajor" is the symbol code, and "fts5yypminor" is
182324 ** a pointer to the value to be deleted. The code used to do the
@@ -181762,10 +182396,22 @@
182396 }
182397 #endif
182398 fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor);
182399 }
182400
182401 /*
182402 ** Clear all secondary memory allocations from the parser
182403 */
182404 static void sqlite3Fts5ParserFinalize(void *p){
182405 fts5yyParser *pParser = (fts5yyParser*)p;
182406 while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
182407 #if fts5YYSTACKDEPTH<=0
182408 if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
182409 #endif
182410 }
182411
182412 #ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
182413 /*
182414 ** Deallocate and destroy a parser. Destructors are called for
182415 ** all stack elements before shutting the parser down.
182416 **
182417 ** If the fts5YYPARSEFREENEVERNULL macro exists (for example because it
@@ -181774,20 +182420,17 @@
182420 */
182421 static void sqlite3Fts5ParserFree(
182422 void *p, /* The parser to be deleted */
182423 void (*freeProc)(void*) /* Function used to reclaim memory */
182424 ){
 
182425 #ifndef fts5YYPARSEFREENEVERNULL
182426 if( p==0 ) return;
182427 #endif
182428 sqlite3Fts5ParserFinalize(p);
182429 (*freeProc)(p);
182430 }
182431 #endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
 
 
182432
182433 /*
182434 ** Return the peak depth of the stack for a parser.
182435 */
182436 #ifdef fts5YYTRACKMAXSTACKDEPTH
@@ -186122,11 +186765,11 @@
186765 memset(&sCtx, 0, sizeof(TokenCtx));
186766 sCtx.pPhrase = pAppend;
186767
186768 rc = fts5ParseStringFromToken(pToken, &z);
186769 if( rc==SQLITE_OK ){
186770 int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_PREFIX : 0);
186771 int n;
186772 sqlite3Fts5Dequote(z);
186773 n = (int)strlen(z);
186774 rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize);
186775 }
@@ -196863,11 +197506,11 @@
197506 int nArg, /* Number of args */
197507 sqlite3_value **apUnused /* Function arguments */
197508 ){
197509 assert( nArg==0 );
197510 UNUSED_PARAM2(nArg, apUnused);
197511 sqlite3_result_text(pCtx, "fts5: 2017-02-07 12:58:38 07fe6228208684d579c4f6c334c90eb6262a9233", -1, SQLITE_TRANSIENT);
197512 }
197513
197514 static int fts5Init(sqlite3 *db){
197515 static const sqlite3_module fts5Mod = {
197516 /* iVersion */ 2,
197517
+55 -27
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -119,13 +119,13 @@
119119
**
120120
** See also: [sqlite3_libversion()],
121121
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
122122
** [sqlite_version()] and [sqlite_source_id()].
123123
*/
124
-#define SQLITE_VERSION "3.16.2"
125
-#define SQLITE_VERSION_NUMBER 3016002
126
-#define SQLITE_SOURCE_ID "2017-01-06 16:32:41 a65a62893ca8319e89e48b8a38cf8a59c69a8209"
124
+#define SQLITE_VERSION "3.17.0"
125
+#define SQLITE_VERSION_NUMBER 3017000
126
+#define SQLITE_SOURCE_ID "2017-02-07 13:51:48 a136609c98ed3cc673c5a3c2578d49db3f2518d1"
127127
128128
/*
129129
** CAPI3REF: Run-Time Library Version Numbers
130130
** KEYWORDS: sqlite3_version sqlite3_sourceid
131131
**
@@ -257,11 +257,15 @@
257257
** sqlite3_uint64 and sqlite_uint64 types can store integer values
258258
** between 0 and +18446744073709551615 inclusive.
259259
*/
260260
#ifdef SQLITE_INT64_TYPE
261261
typedef SQLITE_INT64_TYPE sqlite_int64;
262
- typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
262
+# ifdef SQLITE_UINT64_TYPE
263
+ typedef SQLITE_UINT64_TYPE sqlite_uint64;
264
+# else
265
+ typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
266
+# endif
263267
#elif defined(_MSC_VER) || defined(__BORLANDC__)
264268
typedef __int64 sqlite_int64;
265269
typedef unsigned __int64 sqlite_uint64;
266270
#else
267271
typedef long long int sqlite_int64;
@@ -570,11 +574,11 @@
570574
** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
571575
** after reboot following a crash or power loss, the only bytes in a
572576
** file that were written at the application level might have changed
573577
** and that adjacent bytes, even bytes within the same sector are
574578
** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
575
-** flag indicate that a file cannot be deleted when open. The
579
+** flag indicates that a file cannot be deleted when open. The
576580
** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
577581
** read-only media and cannot be changed even by processes with
578582
** elevated privileges.
579583
*/
580584
#define SQLITE_IOCAP_ATOMIC 0x00000001
@@ -720,10 +724,13 @@
720724
** <li> [SQLITE_IOCAP_ATOMIC16K]
721725
** <li> [SQLITE_IOCAP_ATOMIC32K]
722726
** <li> [SQLITE_IOCAP_ATOMIC64K]
723727
** <li> [SQLITE_IOCAP_SAFE_APPEND]
724728
** <li> [SQLITE_IOCAP_SEQUENTIAL]
729
+** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
730
+** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
731
+** <li> [SQLITE_IOCAP_IMMUTABLE]
725732
** </ul>
726733
**
727734
** The SQLITE_IOCAP_ATOMIC property means that all writes of
728735
** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
729736
** mean that writes of blocks that are nnn bytes in size and
@@ -5408,11 +5415,11 @@
54085415
** ^(The update hook is not invoked when internal system tables are
54095416
** modified (i.e. sqlite_master and sqlite_sequence).)^
54105417
** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
54115418
**
54125419
** ^In the current implementation, the update hook
5413
-** is not invoked when duplication rows are deleted because of an
5420
+** is not invoked when conflicting rows are deleted because of an
54145421
** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook
54155422
** invoked when rows are deleted using the [truncate optimization].
54165423
** The exceptions defined in this paragraph might change in a future
54175424
** release of SQLite.
54185425
**
@@ -6190,10 +6197,16 @@
61906197
**
61916198
** ^Unless it returns SQLITE_MISUSE, this function sets the
61926199
** [database connection] error code and message accessible via
61936200
** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
61946201
**
6202
+** A BLOB referenced by sqlite3_blob_open() may be read using the
6203
+** [sqlite3_blob_read()] interface and modified by using
6204
+** [sqlite3_blob_write()]. The [BLOB handle] can be moved to a
6205
+** different row of the same table using the [sqlite3_blob_reopen()]
6206
+** interface. However, the column, table, or database of a [BLOB handle]
6207
+** cannot be changed after the [BLOB handle] is opened.
61956208
**
61966209
** ^(If the row that a BLOB handle points to is modified by an
61976210
** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
61986211
** then the BLOB handle is marked as "expired".
61996212
** This is true if any column of the row is changed, even a column
@@ -6213,10 +6226,14 @@
62136226
** and the built-in [zeroblob] SQL function may be used to create a
62146227
** zero-filled blob to read or write using the incremental-blob interface.
62156228
**
62166229
** To avoid a resource leak, every open [BLOB handle] should eventually
62176230
** be released by a call to [sqlite3_blob_close()].
6231
+**
6232
+** See also: [sqlite3_blob_close()],
6233
+** [sqlite3_blob_reopen()], [sqlite3_blob_read()],
6234
+** [sqlite3_blob_bytes()], [sqlite3_blob_write()].
62186235
*/
62196236
SQLITE_API int sqlite3_blob_open(
62206237
sqlite3*,
62216238
const char *zDb,
62226239
const char *zTable,
@@ -6228,15 +6245,15 @@
62286245
62296246
/*
62306247
** CAPI3REF: Move a BLOB Handle to a New Row
62316248
** METHOD: sqlite3_blob
62326249
**
6233
-** ^This function is used to move an existing blob handle so that it points
6250
+** ^This function is used to move an existing [BLOB handle] so that it points
62346251
** to a different row of the same database table. ^The new row is identified
62356252
** by the rowid value passed as the second argument. Only the row can be
62366253
** changed. ^The database, table and column on which the blob handle is open
6237
-** remain the same. Moving an existing blob handle to a new row can be
6254
+** remain the same. Moving an existing [BLOB handle] to a new row is
62386255
** faster than closing the existing handle and opening a new one.
62396256
**
62406257
** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
62416258
** it must exist and there must be either a blob or text value stored in
62426259
** the nominated column.)^ ^If the new row is not present in the table, or if
@@ -8161,22 +8178,22 @@
81618178
** ^These interfaces are only available if SQLite is compiled using the
81628179
** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
81638180
**
81648181
** ^The [sqlite3_preupdate_hook()] interface registers a callback function
81658182
** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
8166
-** on a [rowid table].
8183
+** on a database table.
81678184
** ^At most one preupdate hook may be registered at a time on a single
81688185
** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
81698186
** the previous setting.
81708187
** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
81718188
** with a NULL pointer as the second parameter.
81728189
** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
81738190
** the first parameter to callbacks.
81748191
**
8175
-** ^The preupdate hook only fires for changes to [rowid tables]; the preupdate
8176
-** hook is not invoked for changes to [virtual tables] or [WITHOUT ROWID]
8177
-** tables.
8192
+** ^The preupdate hook only fires for changes to real database tables; the
8193
+** preupdate hook is not invoked for changes to [virtual tables] or to
8194
+** system tables like sqlite_master or sqlite_stat1.
81788195
**
81798196
** ^The second parameter to the preupdate callback is a pointer to
81808197
** the [database connection] that registered the preupdate hook.
81818198
** ^The third parameter to the preupdate callback is one of the constants
81828199
** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
@@ -8186,16 +8203,20 @@
81868203
** will be "main" for the main database or "temp" for TEMP tables or
81878204
** the name given after the AS keyword in the [ATTACH] statement for attached
81888205
** databases.)^
81898206
** ^The fifth parameter to the preupdate callback is the name of the
81908207
** table that is being modified.
8191
-** ^The sixth parameter to the preupdate callback is the initial [rowid] of the
8192
-** row being changes for SQLITE_UPDATE and SQLITE_DELETE changes and is
8193
-** undefined for SQLITE_INSERT changes.
8194
-** ^The seventh parameter to the preupdate callback is the final [rowid] of
8195
-** the row being changed for SQLITE_UPDATE and SQLITE_INSERT changes and is
8196
-** undefined for SQLITE_DELETE changes.
8208
+**
8209
+** For an UPDATE or DELETE operation on a [rowid table], the sixth
8210
+** parameter passed to the preupdate callback is the initial [rowid] of the
8211
+** row being modified or deleted. For an INSERT operation on a rowid table,
8212
+** or any operation on a WITHOUT ROWID table, the value of the sixth
8213
+** parameter is undefined. For an INSERT or UPDATE on a rowid table the
8214
+** seventh parameter is the final rowid value of the row being inserted
8215
+** or updated. The value of the seventh parameter passed to the callback
8216
+** function is not defined for operations on WITHOUT ROWID tables, or for
8217
+** INSERT operations on rowid tables.
81978218
**
81988219
** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
81998220
** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
82008221
** provide additional information about a preupdate event. These routines
82018222
** may only be called from within a preupdate callback. Invoking any of
@@ -8895,11 +8916,12 @@
88958916
**
88968917
** <li> For each row (primary key) that exists in the to-table but not in
88978918
** the from-table, a DELETE record is added to the session object.
88988919
**
88998920
** <li> For each row (primary key) that exists in both tables, but features
8900
-** different in each, an UPDATE record is added to the session.
8921
+** different non-PK values in each, an UPDATE record is added to the
8922
+** session.
89018923
** </ul>
89028924
**
89038925
** To clarify, if this function is called and then a changeset constructed
89048926
** using [sqlite3session_changeset()], then after applying that changeset to
89058927
** database zFrom the contents of the two compatible tables would be
@@ -9480,11 +9502,11 @@
94809502
** considered compatible if all of the following are true:
94819503
**
94829504
** <ul>
94839505
** <li> The table has the same name as the name recorded in the
94849506
** changeset, and
9485
-** <li> The table has the same number of columns as recorded in the
9507
+** <li> The table has at least as many columns as recorded in the
94869508
** changeset, and
94879509
** <li> The table has primary key columns in the same position as
94889510
** recorded in the changeset.
94899511
** </ul>
94909512
**
@@ -9525,11 +9547,15 @@
95259547
** the changeset the row is deleted from the target database.
95269548
**
95279549
** If a row with matching primary key values is found, but one or more of
95289550
** the non-primary key fields contains a value different from the original
95299551
** row value stored in the changeset, the conflict-handler function is
9530
-** invoked with [SQLITE_CHANGESET_DATA] as the second argument.
9552
+** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
9553
+** database table has more columns than are recorded in the changeset,
9554
+** only the values of those non-primary key fields are compared against
9555
+** the current database contents - any trailing database table columns
9556
+** are ignored.
95319557
**
95329558
** If no row with matching primary key values is found in the database,
95339559
** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
95349560
** passed as the second argument.
95359561
**
@@ -9540,11 +9566,13 @@
95409566
** operation is attempted because an earlier call to the conflict handler
95419567
** function returned [SQLITE_CHANGESET_REPLACE].
95429568
**
95439569
** <dt>INSERT Changes<dd>
95449570
** For each INSERT change, an attempt is made to insert the new row into
9545
-** the database.
9571
+** the database. If the changeset row contains fewer fields than the
9572
+** database table, the trailing fields are populated with their default
9573
+** values.
95469574
**
95479575
** If the attempt to insert the row fails because the database already
95489576
** contains a row with the same primary key values, the conflict handler
95499577
** function is invoked with the second argument set to
95509578
** [SQLITE_CHANGESET_CONFLICT].
@@ -9558,17 +9586,17 @@
95589586
**
95599587
** <dt>UPDATE Changes<dd>
95609588
** For each UPDATE change, this function checks if the target database
95619589
** contains a row with the same primary key value (or values) as the
95629590
** original row values stored in the changeset. If it does, and the values
9563
-** stored in all non-primary key columns also match the values stored in
9564
-** the changeset the row is updated within the target database.
9591
+** stored in all modified non-primary key columns also match the values
9592
+** stored in the changeset the row is updated within the target database.
95659593
**
95669594
** If a row with matching primary key values is found, but one or more of
9567
-** the non-primary key fields contains a value different from an original
9568
-** row value stored in the changeset, the conflict-handler function is
9569
-** invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
9595
+** the modified non-primary key fields contains a value different from an
9596
+** original row value stored in the changeset, the conflict-handler function
9597
+** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
95709598
** UPDATE changes only contain values for non-primary key fields that are
95719599
** to be modified, only those fields need to match the original values to
95729600
** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
95739601
**
95749602
** If no row with matching primary key values is found in the database,
95759603
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -119,13 +119,13 @@
119 **
120 ** See also: [sqlite3_libversion()],
121 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
122 ** [sqlite_version()] and [sqlite_source_id()].
123 */
124 #define SQLITE_VERSION "3.16.2"
125 #define SQLITE_VERSION_NUMBER 3016002
126 #define SQLITE_SOURCE_ID "2017-01-06 16:32:41 a65a62893ca8319e89e48b8a38cf8a59c69a8209"
127
128 /*
129 ** CAPI3REF: Run-Time Library Version Numbers
130 ** KEYWORDS: sqlite3_version sqlite3_sourceid
131 **
@@ -257,11 +257,15 @@
257 ** sqlite3_uint64 and sqlite_uint64 types can store integer values
258 ** between 0 and +18446744073709551615 inclusive.
259 */
260 #ifdef SQLITE_INT64_TYPE
261 typedef SQLITE_INT64_TYPE sqlite_int64;
262 typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
 
 
 
 
263 #elif defined(_MSC_VER) || defined(__BORLANDC__)
264 typedef __int64 sqlite_int64;
265 typedef unsigned __int64 sqlite_uint64;
266 #else
267 typedef long long int sqlite_int64;
@@ -570,11 +574,11 @@
570 ** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
571 ** after reboot following a crash or power loss, the only bytes in a
572 ** file that were written at the application level might have changed
573 ** and that adjacent bytes, even bytes within the same sector are
574 ** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
575 ** flag indicate that a file cannot be deleted when open. The
576 ** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
577 ** read-only media and cannot be changed even by processes with
578 ** elevated privileges.
579 */
580 #define SQLITE_IOCAP_ATOMIC 0x00000001
@@ -720,10 +724,13 @@
720 ** <li> [SQLITE_IOCAP_ATOMIC16K]
721 ** <li> [SQLITE_IOCAP_ATOMIC32K]
722 ** <li> [SQLITE_IOCAP_ATOMIC64K]
723 ** <li> [SQLITE_IOCAP_SAFE_APPEND]
724 ** <li> [SQLITE_IOCAP_SEQUENTIAL]
 
 
 
725 ** </ul>
726 **
727 ** The SQLITE_IOCAP_ATOMIC property means that all writes of
728 ** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
729 ** mean that writes of blocks that are nnn bytes in size and
@@ -5408,11 +5415,11 @@
5408 ** ^(The update hook is not invoked when internal system tables are
5409 ** modified (i.e. sqlite_master and sqlite_sequence).)^
5410 ** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
5411 **
5412 ** ^In the current implementation, the update hook
5413 ** is not invoked when duplication rows are deleted because of an
5414 ** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook
5415 ** invoked when rows are deleted using the [truncate optimization].
5416 ** The exceptions defined in this paragraph might change in a future
5417 ** release of SQLite.
5418 **
@@ -6190,10 +6197,16 @@
6190 **
6191 ** ^Unless it returns SQLITE_MISUSE, this function sets the
6192 ** [database connection] error code and message accessible via
6193 ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
6194 **
 
 
 
 
 
 
6195 **
6196 ** ^(If the row that a BLOB handle points to is modified by an
6197 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
6198 ** then the BLOB handle is marked as "expired".
6199 ** This is true if any column of the row is changed, even a column
@@ -6213,10 +6226,14 @@
6213 ** and the built-in [zeroblob] SQL function may be used to create a
6214 ** zero-filled blob to read or write using the incremental-blob interface.
6215 **
6216 ** To avoid a resource leak, every open [BLOB handle] should eventually
6217 ** be released by a call to [sqlite3_blob_close()].
 
 
 
 
6218 */
6219 SQLITE_API int sqlite3_blob_open(
6220 sqlite3*,
6221 const char *zDb,
6222 const char *zTable,
@@ -6228,15 +6245,15 @@
6228
6229 /*
6230 ** CAPI3REF: Move a BLOB Handle to a New Row
6231 ** METHOD: sqlite3_blob
6232 **
6233 ** ^This function is used to move an existing blob handle so that it points
6234 ** to a different row of the same database table. ^The new row is identified
6235 ** by the rowid value passed as the second argument. Only the row can be
6236 ** changed. ^The database, table and column on which the blob handle is open
6237 ** remain the same. Moving an existing blob handle to a new row can be
6238 ** faster than closing the existing handle and opening a new one.
6239 **
6240 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
6241 ** it must exist and there must be either a blob or text value stored in
6242 ** the nominated column.)^ ^If the new row is not present in the table, or if
@@ -8161,22 +8178,22 @@
8161 ** ^These interfaces are only available if SQLite is compiled using the
8162 ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
8163 **
8164 ** ^The [sqlite3_preupdate_hook()] interface registers a callback function
8165 ** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
8166 ** on a [rowid table].
8167 ** ^At most one preupdate hook may be registered at a time on a single
8168 ** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
8169 ** the previous setting.
8170 ** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
8171 ** with a NULL pointer as the second parameter.
8172 ** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
8173 ** the first parameter to callbacks.
8174 **
8175 ** ^The preupdate hook only fires for changes to [rowid tables]; the preupdate
8176 ** hook is not invoked for changes to [virtual tables] or [WITHOUT ROWID]
8177 ** tables.
8178 **
8179 ** ^The second parameter to the preupdate callback is a pointer to
8180 ** the [database connection] that registered the preupdate hook.
8181 ** ^The third parameter to the preupdate callback is one of the constants
8182 ** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
@@ -8186,16 +8203,20 @@
8186 ** will be "main" for the main database or "temp" for TEMP tables or
8187 ** the name given after the AS keyword in the [ATTACH] statement for attached
8188 ** databases.)^
8189 ** ^The fifth parameter to the preupdate callback is the name of the
8190 ** table that is being modified.
8191 ** ^The sixth parameter to the preupdate callback is the initial [rowid] of the
8192 ** row being changes for SQLITE_UPDATE and SQLITE_DELETE changes and is
8193 ** undefined for SQLITE_INSERT changes.
8194 ** ^The seventh parameter to the preupdate callback is the final [rowid] of
8195 ** the row being changed for SQLITE_UPDATE and SQLITE_INSERT changes and is
8196 ** undefined for SQLITE_DELETE changes.
 
 
 
 
8197 **
8198 ** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
8199 ** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
8200 ** provide additional information about a preupdate event. These routines
8201 ** may only be called from within a preupdate callback. Invoking any of
@@ -8895,11 +8916,12 @@
8895 **
8896 ** <li> For each row (primary key) that exists in the to-table but not in
8897 ** the from-table, a DELETE record is added to the session object.
8898 **
8899 ** <li> For each row (primary key) that exists in both tables, but features
8900 ** different in each, an UPDATE record is added to the session.
 
8901 ** </ul>
8902 **
8903 ** To clarify, if this function is called and then a changeset constructed
8904 ** using [sqlite3session_changeset()], then after applying that changeset to
8905 ** database zFrom the contents of the two compatible tables would be
@@ -9480,11 +9502,11 @@
9480 ** considered compatible if all of the following are true:
9481 **
9482 ** <ul>
9483 ** <li> The table has the same name as the name recorded in the
9484 ** changeset, and
9485 ** <li> The table has the same number of columns as recorded in the
9486 ** changeset, and
9487 ** <li> The table has primary key columns in the same position as
9488 ** recorded in the changeset.
9489 ** </ul>
9490 **
@@ -9525,11 +9547,15 @@
9525 ** the changeset the row is deleted from the target database.
9526 **
9527 ** If a row with matching primary key values is found, but one or more of
9528 ** the non-primary key fields contains a value different from the original
9529 ** row value stored in the changeset, the conflict-handler function is
9530 ** invoked with [SQLITE_CHANGESET_DATA] as the second argument.
 
 
 
 
9531 **
9532 ** If no row with matching primary key values is found in the database,
9533 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
9534 ** passed as the second argument.
9535 **
@@ -9540,11 +9566,13 @@
9540 ** operation is attempted because an earlier call to the conflict handler
9541 ** function returned [SQLITE_CHANGESET_REPLACE].
9542 **
9543 ** <dt>INSERT Changes<dd>
9544 ** For each INSERT change, an attempt is made to insert the new row into
9545 ** the database.
 
 
9546 **
9547 ** If the attempt to insert the row fails because the database already
9548 ** contains a row with the same primary key values, the conflict handler
9549 ** function is invoked with the second argument set to
9550 ** [SQLITE_CHANGESET_CONFLICT].
@@ -9558,17 +9586,17 @@
9558 **
9559 ** <dt>UPDATE Changes<dd>
9560 ** For each UPDATE change, this function checks if the target database
9561 ** contains a row with the same primary key value (or values) as the
9562 ** original row values stored in the changeset. If it does, and the values
9563 ** stored in all non-primary key columns also match the values stored in
9564 ** the changeset the row is updated within the target database.
9565 **
9566 ** If a row with matching primary key values is found, but one or more of
9567 ** the non-primary key fields contains a value different from an original
9568 ** row value stored in the changeset, the conflict-handler function is
9569 ** invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
9570 ** UPDATE changes only contain values for non-primary key fields that are
9571 ** to be modified, only those fields need to match the original values to
9572 ** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
9573 **
9574 ** If no row with matching primary key values is found in the database,
9575
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -119,13 +119,13 @@
119 **
120 ** See also: [sqlite3_libversion()],
121 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
122 ** [sqlite_version()] and [sqlite_source_id()].
123 */
124 #define SQLITE_VERSION "3.17.0"
125 #define SQLITE_VERSION_NUMBER 3017000
126 #define SQLITE_SOURCE_ID "2017-02-07 13:51:48 a136609c98ed3cc673c5a3c2578d49db3f2518d1"
127
128 /*
129 ** CAPI3REF: Run-Time Library Version Numbers
130 ** KEYWORDS: sqlite3_version sqlite3_sourceid
131 **
@@ -257,11 +257,15 @@
257 ** sqlite3_uint64 and sqlite_uint64 types can store integer values
258 ** between 0 and +18446744073709551615 inclusive.
259 */
260 #ifdef SQLITE_INT64_TYPE
261 typedef SQLITE_INT64_TYPE sqlite_int64;
262 # ifdef SQLITE_UINT64_TYPE
263 typedef SQLITE_UINT64_TYPE sqlite_uint64;
264 # else
265 typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
266 # endif
267 #elif defined(_MSC_VER) || defined(__BORLANDC__)
268 typedef __int64 sqlite_int64;
269 typedef unsigned __int64 sqlite_uint64;
270 #else
271 typedef long long int sqlite_int64;
@@ -570,11 +574,11 @@
574 ** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
575 ** after reboot following a crash or power loss, the only bytes in a
576 ** file that were written at the application level might have changed
577 ** and that adjacent bytes, even bytes within the same sector are
578 ** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
579 ** flag indicates that a file cannot be deleted when open. The
580 ** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
581 ** read-only media and cannot be changed even by processes with
582 ** elevated privileges.
583 */
584 #define SQLITE_IOCAP_ATOMIC 0x00000001
@@ -720,10 +724,13 @@
724 ** <li> [SQLITE_IOCAP_ATOMIC16K]
725 ** <li> [SQLITE_IOCAP_ATOMIC32K]
726 ** <li> [SQLITE_IOCAP_ATOMIC64K]
727 ** <li> [SQLITE_IOCAP_SAFE_APPEND]
728 ** <li> [SQLITE_IOCAP_SEQUENTIAL]
729 ** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
730 ** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
731 ** <li> [SQLITE_IOCAP_IMMUTABLE]
732 ** </ul>
733 **
734 ** The SQLITE_IOCAP_ATOMIC property means that all writes of
735 ** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
736 ** mean that writes of blocks that are nnn bytes in size and
@@ -5408,11 +5415,11 @@
5415 ** ^(The update hook is not invoked when internal system tables are
5416 ** modified (i.e. sqlite_master and sqlite_sequence).)^
5417 ** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
5418 **
5419 ** ^In the current implementation, the update hook
5420 ** is not invoked when conflicting rows are deleted because of an
5421 ** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook
5422 ** invoked when rows are deleted using the [truncate optimization].
5423 ** The exceptions defined in this paragraph might change in a future
5424 ** release of SQLite.
5425 **
@@ -6190,10 +6197,16 @@
6197 **
6198 ** ^Unless it returns SQLITE_MISUSE, this function sets the
6199 ** [database connection] error code and message accessible via
6200 ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
6201 **
6202 ** A BLOB referenced by sqlite3_blob_open() may be read using the
6203 ** [sqlite3_blob_read()] interface and modified by using
6204 ** [sqlite3_blob_write()]. The [BLOB handle] can be moved to a
6205 ** different row of the same table using the [sqlite3_blob_reopen()]
6206 ** interface. However, the column, table, or database of a [BLOB handle]
6207 ** cannot be changed after the [BLOB handle] is opened.
6208 **
6209 ** ^(If the row that a BLOB handle points to is modified by an
6210 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
6211 ** then the BLOB handle is marked as "expired".
6212 ** This is true if any column of the row is changed, even a column
@@ -6213,10 +6226,14 @@
6226 ** and the built-in [zeroblob] SQL function may be used to create a
6227 ** zero-filled blob to read or write using the incremental-blob interface.
6228 **
6229 ** To avoid a resource leak, every open [BLOB handle] should eventually
6230 ** be released by a call to [sqlite3_blob_close()].
6231 **
6232 ** See also: [sqlite3_blob_close()],
6233 ** [sqlite3_blob_reopen()], [sqlite3_blob_read()],
6234 ** [sqlite3_blob_bytes()], [sqlite3_blob_write()].
6235 */
6236 SQLITE_API int sqlite3_blob_open(
6237 sqlite3*,
6238 const char *zDb,
6239 const char *zTable,
@@ -6228,15 +6245,15 @@
6245
6246 /*
6247 ** CAPI3REF: Move a BLOB Handle to a New Row
6248 ** METHOD: sqlite3_blob
6249 **
6250 ** ^This function is used to move an existing [BLOB handle] so that it points
6251 ** to a different row of the same database table. ^The new row is identified
6252 ** by the rowid value passed as the second argument. Only the row can be
6253 ** changed. ^The database, table and column on which the blob handle is open
6254 ** remain the same. Moving an existing [BLOB handle] to a new row is
6255 ** faster than closing the existing handle and opening a new one.
6256 **
6257 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
6258 ** it must exist and there must be either a blob or text value stored in
6259 ** the nominated column.)^ ^If the new row is not present in the table, or if
@@ -8161,22 +8178,22 @@
8178 ** ^These interfaces are only available if SQLite is compiled using the
8179 ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
8180 **
8181 ** ^The [sqlite3_preupdate_hook()] interface registers a callback function
8182 ** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
8183 ** on a database table.
8184 ** ^At most one preupdate hook may be registered at a time on a single
8185 ** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
8186 ** the previous setting.
8187 ** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
8188 ** with a NULL pointer as the second parameter.
8189 ** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
8190 ** the first parameter to callbacks.
8191 **
8192 ** ^The preupdate hook only fires for changes to real database tables; the
8193 ** preupdate hook is not invoked for changes to [virtual tables] or to
8194 ** system tables like sqlite_master or sqlite_stat1.
8195 **
8196 ** ^The second parameter to the preupdate callback is a pointer to
8197 ** the [database connection] that registered the preupdate hook.
8198 ** ^The third parameter to the preupdate callback is one of the constants
8199 ** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
@@ -8186,16 +8203,20 @@
8203 ** will be "main" for the main database or "temp" for TEMP tables or
8204 ** the name given after the AS keyword in the [ATTACH] statement for attached
8205 ** databases.)^
8206 ** ^The fifth parameter to the preupdate callback is the name of the
8207 ** table that is being modified.
8208 **
8209 ** For an UPDATE or DELETE operation on a [rowid table], the sixth
8210 ** parameter passed to the preupdate callback is the initial [rowid] of the
8211 ** row being modified or deleted. For an INSERT operation on a rowid table,
8212 ** or any operation on a WITHOUT ROWID table, the value of the sixth
8213 ** parameter is undefined. For an INSERT or UPDATE on a rowid table the
8214 ** seventh parameter is the final rowid value of the row being inserted
8215 ** or updated. The value of the seventh parameter passed to the callback
8216 ** function is not defined for operations on WITHOUT ROWID tables, or for
8217 ** INSERT operations on rowid tables.
8218 **
8219 ** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
8220 ** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
8221 ** provide additional information about a preupdate event. These routines
8222 ** may only be called from within a preupdate callback. Invoking any of
@@ -8895,11 +8916,12 @@
8916 **
8917 ** <li> For each row (primary key) that exists in the to-table but not in
8918 ** the from-table, a DELETE record is added to the session object.
8919 **
8920 ** <li> For each row (primary key) that exists in both tables, but features
8921 ** different non-PK values in each, an UPDATE record is added to the
8922 ** session.
8923 ** </ul>
8924 **
8925 ** To clarify, if this function is called and then a changeset constructed
8926 ** using [sqlite3session_changeset()], then after applying that changeset to
8927 ** database zFrom the contents of the two compatible tables would be
@@ -9480,11 +9502,11 @@
9502 ** considered compatible if all of the following are true:
9503 **
9504 ** <ul>
9505 ** <li> The table has the same name as the name recorded in the
9506 ** changeset, and
9507 ** <li> The table has at least as many columns as recorded in the
9508 ** changeset, and
9509 ** <li> The table has primary key columns in the same position as
9510 ** recorded in the changeset.
9511 ** </ul>
9512 **
@@ -9525,11 +9547,15 @@
9547 ** the changeset the row is deleted from the target database.
9548 **
9549 ** If a row with matching primary key values is found, but one or more of
9550 ** the non-primary key fields contains a value different from the original
9551 ** row value stored in the changeset, the conflict-handler function is
9552 ** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
9553 ** database table has more columns than are recorded in the changeset,
9554 ** only the values of those non-primary key fields are compared against
9555 ** the current database contents - any trailing database table columns
9556 ** are ignored.
9557 **
9558 ** If no row with matching primary key values is found in the database,
9559 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
9560 ** passed as the second argument.
9561 **
@@ -9540,11 +9566,13 @@
9566 ** operation is attempted because an earlier call to the conflict handler
9567 ** function returned [SQLITE_CHANGESET_REPLACE].
9568 **
9569 ** <dt>INSERT Changes<dd>
9570 ** For each INSERT change, an attempt is made to insert the new row into
9571 ** the database. If the changeset row contains fewer fields than the
9572 ** database table, the trailing fields are populated with their default
9573 ** values.
9574 **
9575 ** If the attempt to insert the row fails because the database already
9576 ** contains a row with the same primary key values, the conflict handler
9577 ** function is invoked with the second argument set to
9578 ** [SQLITE_CHANGESET_CONFLICT].
@@ -9558,17 +9586,17 @@
9586 **
9587 ** <dt>UPDATE Changes<dd>
9588 ** For each UPDATE change, this function checks if the target database
9589 ** contains a row with the same primary key value (or values) as the
9590 ** original row values stored in the changeset. If it does, and the values
9591 ** stored in all modified non-primary key columns also match the values
9592 ** stored in the changeset the row is updated within the target database.
9593 **
9594 ** If a row with matching primary key values is found, but one or more of
9595 ** the modified non-primary key fields contains a value different from an
9596 ** original row value stored in the changeset, the conflict-handler function
9597 ** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
9598 ** UPDATE changes only contain values for non-primary key fields that are
9599 ** to be modified, only those fields need to match the original values to
9600 ** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
9601 **
9602 ** If no row with matching primary key values is found in the database,
9603

Keyboard Shortcuts

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