Fossil SCM

(cherry-pick): Update the built-in SQLite to version 3.17.0

jan.nijtmans 2017-02-14 15:41 branch-1.37
Commit 17a33275f02c7f6e131f9c44d25bcf654e0c692d
3 files changed +6 -1 +1436 -760 +83 -55
+6 -1
--- src/shell.c
+++ src/shell.c
@@ -4286,19 +4286,24 @@
42864286
const char *zMode = nArg>=2 ? azArg[1] : "";
42874287
int n2 = (int)strlen(zMode);
42884288
int c2 = zMode[0];
42894289
if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
42904290
p->mode = MODE_Line;
4291
+ sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
42914292
}else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
42924293
p->mode = MODE_Column;
4294
+ sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
42934295
}else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
42944296
p->mode = MODE_List;
4297
+ sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
4298
+ sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
42954299
}else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
42964300
p->mode = MODE_Html;
42974301
}else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
42984302
p->mode = MODE_Tcl;
42994303
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
4304
+ sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
43004305
}else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
43014306
p->mode = MODE_Csv;
43024307
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
43034308
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
43044309
}else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
@@ -5295,11 +5300,11 @@
52955300
sqlite3_libversion(), sqlite3_sourceid());
52965301
}else
52975302
52985303
if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
52995304
const char *zDbName = nArg==2 ? azArg[1] : "main";
5300
- sqlite3_vfs *pVfs;
5305
+ sqlite3_vfs *pVfs = 0;
53015306
if( p->db ){
53025307
sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
53035308
if( pVfs ){
53045309
utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
53055310
raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
53065311
--- src/shell.c
+++ src/shell.c
@@ -4286,19 +4286,24 @@
4286 const char *zMode = nArg>=2 ? azArg[1] : "";
4287 int n2 = (int)strlen(zMode);
4288 int c2 = zMode[0];
4289 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
4290 p->mode = MODE_Line;
 
4291 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
4292 p->mode = MODE_Column;
 
4293 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
4294 p->mode = MODE_List;
 
 
4295 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
4296 p->mode = MODE_Html;
4297 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
4298 p->mode = MODE_Tcl;
4299 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
 
4300 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
4301 p->mode = MODE_Csv;
4302 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
4303 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
4304 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
@@ -5295,11 +5300,11 @@
5295 sqlite3_libversion(), sqlite3_sourceid());
5296 }else
5297
5298 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
5299 const char *zDbName = nArg==2 ? azArg[1] : "main";
5300 sqlite3_vfs *pVfs;
5301 if( p->db ){
5302 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
5303 if( pVfs ){
5304 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
5305 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
5306
--- src/shell.c
+++ src/shell.c
@@ -4286,19 +4286,24 @@
4286 const char *zMode = nArg>=2 ? azArg[1] : "";
4287 int n2 = (int)strlen(zMode);
4288 int c2 = zMode[0];
4289 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
4290 p->mode = MODE_Line;
4291 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
4292 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
4293 p->mode = MODE_Column;
4294 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
4295 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
4296 p->mode = MODE_List;
4297 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
4298 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
4299 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
4300 p->mode = MODE_Html;
4301 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
4302 p->mode = MODE_Tcl;
4303 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
4304 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
4305 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
4306 p->mode = MODE_Csv;
4307 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
4308 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
4309 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
@@ -5295,11 +5300,11 @@
5300 sqlite3_libversion(), sqlite3_sourceid());
5301 }else
5302
5303 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
5304 const char *zDbName = nArg==2 ? azArg[1] : "main";
5305 sqlite3_vfs *pVfs = 0;
5306 if( p->db ){
5307 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
5308 if( pVfs ){
5309 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
5310 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
5311
+1436 -760
--- 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.
@@ -202,15 +202,31 @@
202202
# define _FILE_OFFSET_BITS 64
203203
# endif
204204
# define _LARGEFILE_SOURCE 1
205205
#endif
206206
207
-/* What version of GCC is being used. 0 means GCC is not being used */
208
-#ifdef __GNUC__
207
+/* The GCC_VERSION, CLANG_VERSION, and MSVC_VERSION macros are used to
208
+** conditionally include optimizations for each of these compilers. A
209
+** value of 0 means that compiler is not being used. The
210
+** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
211
+** optimizations, and hence set all compiler macros to 0
212
+*/
213
+#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
209214
# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
210215
#else
211216
# define GCC_VERSION 0
217
+#endif
218
+#if defined(__clang__) && !defined(_WIN32) && !defined(SQLITE_DISABLE_INTRINSIC)
219
+# define CLANG_VERSION \
220
+ (__clang_major__*1000000+__clang_minor__*1000+__clang_patchlevel__)
221
+#else
222
+# define CLANG_VERSION 0
223
+#endif
224
+#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
225
+# define MSVC_VERSION _MSC_VER
226
+#else
227
+# define MSVC_VERSION 0
212228
#endif
213229
214230
/* Needed for various definitions... */
215231
#if defined(__GNUC__) && !defined(_GNU_SOURCE)
216232
# define _GNU_SOURCE
@@ -379,13 +395,13 @@
379395
**
380396
** See also: [sqlite3_libversion()],
381397
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
382398
** [sqlite_version()] and [sqlite_source_id()].
383399
*/
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"
400
+#define SQLITE_VERSION "3.17.0"
401
+#define SQLITE_VERSION_NUMBER 3017000
402
+#define SQLITE_SOURCE_ID "2017-02-13 16:02:40 ada05cfa86ad7f5645450ac7a2a21c9aa6e57d2c"
387403
388404
/*
389405
** CAPI3REF: Run-Time Library Version Numbers
390406
** KEYWORDS: sqlite3_version sqlite3_sourceid
391407
**
@@ -517,11 +533,15 @@
517533
** sqlite3_uint64 and sqlite_uint64 types can store integer values
518534
** between 0 and +18446744073709551615 inclusive.
519535
*/
520536
#ifdef SQLITE_INT64_TYPE
521537
typedef SQLITE_INT64_TYPE sqlite_int64;
522
- typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
538
+# ifdef SQLITE_UINT64_TYPE
539
+ typedef SQLITE_UINT64_TYPE sqlite_uint64;
540
+# else
541
+ typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
542
+# endif
523543
#elif defined(_MSC_VER) || defined(__BORLANDC__)
524544
typedef __int64 sqlite_int64;
525545
typedef unsigned __int64 sqlite_uint64;
526546
#else
527547
typedef long long int sqlite_int64;
@@ -830,11 +850,11 @@
830850
** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
831851
** after reboot following a crash or power loss, the only bytes in a
832852
** file that were written at the application level might have changed
833853
** and that adjacent bytes, even bytes within the same sector are
834854
** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
835
-** flag indicate that a file cannot be deleted when open. The
855
+** flag indicates that a file cannot be deleted when open. The
836856
** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
837857
** read-only media and cannot be changed even by processes with
838858
** elevated privileges.
839859
*/
840860
#define SQLITE_IOCAP_ATOMIC 0x00000001
@@ -980,10 +1000,13 @@
9801000
** <li> [SQLITE_IOCAP_ATOMIC16K]
9811001
** <li> [SQLITE_IOCAP_ATOMIC32K]
9821002
** <li> [SQLITE_IOCAP_ATOMIC64K]
9831003
** <li> [SQLITE_IOCAP_SAFE_APPEND]
9841004
** <li> [SQLITE_IOCAP_SEQUENTIAL]
1005
+** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
1006
+** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
1007
+** <li> [SQLITE_IOCAP_IMMUTABLE]
9851008
** </ul>
9861009
**
9871010
** The SQLITE_IOCAP_ATOMIC property means that all writes of
9881011
** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
9891012
** mean that writes of blocks that are nnn bytes in size and
@@ -5668,11 +5691,11 @@
56685691
** ^(The update hook is not invoked when internal system tables are
56695692
** modified (i.e. sqlite_master and sqlite_sequence).)^
56705693
** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
56715694
**
56725695
** ^In the current implementation, the update hook
5673
-** is not invoked when duplication rows are deleted because of an
5696
+** is not invoked when conflicting rows are deleted because of an
56745697
** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook
56755698
** invoked when rows are deleted using the [truncate optimization].
56765699
** The exceptions defined in this paragraph might change in a future
56775700
** release of SQLite.
56785701
**
@@ -6450,10 +6473,16 @@
64506473
**
64516474
** ^Unless it returns SQLITE_MISUSE, this function sets the
64526475
** [database connection] error code and message accessible via
64536476
** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
64546477
**
6478
+** A BLOB referenced by sqlite3_blob_open() may be read using the
6479
+** [sqlite3_blob_read()] interface and modified by using
6480
+** [sqlite3_blob_write()]. The [BLOB handle] can be moved to a
6481
+** different row of the same table using the [sqlite3_blob_reopen()]
6482
+** interface. However, the column, table, or database of a [BLOB handle]
6483
+** cannot be changed after the [BLOB handle] is opened.
64556484
**
64566485
** ^(If the row that a BLOB handle points to is modified by an
64576486
** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
64586487
** then the BLOB handle is marked as "expired".
64596488
** This is true if any column of the row is changed, even a column
@@ -6473,10 +6502,14 @@
64736502
** and the built-in [zeroblob] SQL function may be used to create a
64746503
** zero-filled blob to read or write using the incremental-blob interface.
64756504
**
64766505
** To avoid a resource leak, every open [BLOB handle] should eventually
64776506
** be released by a call to [sqlite3_blob_close()].
6507
+**
6508
+** See also: [sqlite3_blob_close()],
6509
+** [sqlite3_blob_reopen()], [sqlite3_blob_read()],
6510
+** [sqlite3_blob_bytes()], [sqlite3_blob_write()].
64786511
*/
64796512
SQLITE_API int sqlite3_blob_open(
64806513
sqlite3*,
64816514
const char *zDb,
64826515
const char *zTable,
@@ -6488,15 +6521,15 @@
64886521
64896522
/*
64906523
** CAPI3REF: Move a BLOB Handle to a New Row
64916524
** METHOD: sqlite3_blob
64926525
**
6493
-** ^This function is used to move an existing blob handle so that it points
6526
+** ^This function is used to move an existing [BLOB handle] so that it points
64946527
** to a different row of the same database table. ^The new row is identified
64956528
** by the rowid value passed as the second argument. Only the row can be
64966529
** 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
6530
+** remain the same. Moving an existing [BLOB handle] to a new row is
64986531
** faster than closing the existing handle and opening a new one.
64996532
**
65006533
** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
65016534
** it must exist and there must be either a blob or text value stored in
65026535
** the nominated column.)^ ^If the new row is not present in the table, or if
@@ -8421,22 +8454,22 @@
84218454
** ^These interfaces are only available if SQLite is compiled using the
84228455
** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
84238456
**
84248457
** ^The [sqlite3_preupdate_hook()] interface registers a callback function
84258458
** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
8426
-** on a [rowid table].
8459
+** on a database table.
84278460
** ^At most one preupdate hook may be registered at a time on a single
84288461
** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
84298462
** the previous setting.
84308463
** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
84318464
** with a NULL pointer as the second parameter.
84328465
** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
84338466
** the first parameter to callbacks.
84348467
**
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.
8468
+** ^The preupdate hook only fires for changes to real database tables; the
8469
+** preupdate hook is not invoked for changes to [virtual tables] or to
8470
+** system tables like sqlite_master or sqlite_stat1.
84388471
**
84398472
** ^The second parameter to the preupdate callback is a pointer to
84408473
** the [database connection] that registered the preupdate hook.
84418474
** ^The third parameter to the preupdate callback is one of the constants
84428475
** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
@@ -8446,16 +8479,20 @@
84468479
** will be "main" for the main database or "temp" for TEMP tables or
84478480
** the name given after the AS keyword in the [ATTACH] statement for attached
84488481
** databases.)^
84498482
** ^The fifth parameter to the preupdate callback is the name of the
84508483
** 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.
8484
+**
8485
+** For an UPDATE or DELETE operation on a [rowid table], the sixth
8486
+** parameter passed to the preupdate callback is the initial [rowid] of the
8487
+** row being modified or deleted. For an INSERT operation on a rowid table,
8488
+** or any operation on a WITHOUT ROWID table, the value of the sixth
8489
+** parameter is undefined. For an INSERT or UPDATE on a rowid table the
8490
+** seventh parameter is the final rowid value of the row being inserted
8491
+** or updated. The value of the seventh parameter passed to the callback
8492
+** function is not defined for operations on WITHOUT ROWID tables, or for
8493
+** INSERT operations on rowid tables.
84578494
**
84588495
** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
84598496
** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
84608497
** provide additional information about a preupdate event. These routines
84618498
** may only be called from within a preupdate callback. Invoking any of
@@ -8887,11 +8924,11 @@
88878924
** The session object will be used to create changesets for tables in
88888925
** database zDb, where zDb is either "main", or "temp", or the name of an
88898926
** attached database. It is not an error if database zDb is not attached
88908927
** to the database when the session object is created.
88918928
*/
8892
-int sqlite3session_create(
8929
+SQLITE_API int sqlite3session_create(
88938930
sqlite3 *db, /* Database handle */
88948931
const char *zDb, /* Name of db (e.g. "main") */
88958932
sqlite3_session **ppSession /* OUT: New session object */
88968933
);
88978934
@@ -8905,11 +8942,11 @@
89058942
**
89068943
** Session objects must be deleted before the database handle to which they
89078944
** are attached is closed. Refer to the documentation for
89088945
** [sqlite3session_create()] for details.
89098946
*/
8910
-void sqlite3session_delete(sqlite3_session *pSession);
8947
+SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
89118948
89128949
89138950
/*
89148951
** CAPI3REF: Enable Or Disable A Session Object
89158952
**
@@ -8925,11 +8962,11 @@
89258962
** no-op, and may be used to query the current state of the session.
89268963
**
89278964
** The return value indicates the final state of the session object: 0 if
89288965
** the session is disabled, or 1 if it is enabled.
89298966
*/
8930
-int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
8967
+SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
89318968
89328969
/*
89338970
** CAPI3REF: Set Or Clear the Indirect Change Flag
89348971
**
89358972
** Each change recorded by a session object is marked as either direct or
@@ -8954,11 +8991,11 @@
89548991
** indirect flag for the specified session object.
89558992
**
89568993
** The return value indicates the final state of the indirect flag: 0 if
89578994
** it is clear, or 1 if it is set.
89588995
*/
8959
-int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
8996
+SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
89608997
89618998
/*
89628999
** CAPI3REF: Attach A Table To A Session Object
89639000
**
89649001
** If argument zTab is not NULL, then it is the name of a table to attach
@@ -8984,11 +9021,11 @@
89849021
** in one or more of their PRIMARY KEY columns.
89859022
**
89869023
** SQLITE_OK is returned if the call completes without error. Or, if an error
89879024
** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
89889025
*/
8989
-int sqlite3session_attach(
9026
+SQLITE_API int sqlite3session_attach(
89909027
sqlite3_session *pSession, /* Session object */
89919028
const char *zTab /* Table name */
89929029
);
89939030
89949031
/*
@@ -8998,11 +9035,11 @@
89989035
** in tables that are not attached to the Session object, the filter is called
89999036
** to determine whether changes to the table's rows should be tracked or not.
90009037
** If xFilter returns 0, changes is not tracked. Note that once a table is
90019038
** attached, xFilter will not be called again.
90029039
*/
9003
-void sqlite3session_table_filter(
9040
+SQLITE_API void sqlite3session_table_filter(
90049041
sqlite3_session *pSession, /* Session object */
90059042
int(*xFilter)(
90069043
void *pCtx, /* Copy of third arg to _filter_table() */
90079044
const char *zTab /* Table name */
90089045
),
@@ -9111,11 +9148,11 @@
91119148
** changeset, even though the delete took place while the session was disabled.
91129149
** Or, if one field of a row is updated while a session is disabled, and
91139150
** another field of the same row is updated while the session is enabled, the
91149151
** resulting changeset will contain an UPDATE change that updates both fields.
91159152
*/
9116
-int sqlite3session_changeset(
9153
+SQLITE_API int sqlite3session_changeset(
91179154
sqlite3_session *pSession, /* Session object */
91189155
int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
91199156
void **ppChangeset /* OUT: Buffer containing changeset */
91209157
);
91219158
@@ -9155,11 +9192,12 @@
91559192
**
91569193
** <li> For each row (primary key) that exists in the to-table but not in
91579194
** the from-table, a DELETE record is added to the session object.
91589195
**
91599196
** <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.
9197
+** different non-PK values in each, an UPDATE record is added to the
9198
+** session.
91619199
** </ul>
91629200
**
91639201
** To clarify, if this function is called and then a changeset constructed
91649202
** using [sqlite3session_changeset()], then after applying that changeset to
91659203
** database zFrom the contents of the two compatible tables would be
@@ -9172,11 +9210,11 @@
91729210
** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
91739211
** may be set to point to a buffer containing an English language error
91749212
** message. It is the responsibility of the caller to free this buffer using
91759213
** sqlite3_free().
91769214
*/
9177
-int sqlite3session_diff(
9215
+SQLITE_API int sqlite3session_diff(
91789216
sqlite3_session *pSession,
91799217
const char *zFromDb,
91809218
const char *zTbl,
91819219
char **pzErrMsg
91829220
);
@@ -9208,11 +9246,11 @@
92089246
** Changes within a patchset are ordered in the same way as for changesets
92099247
** generated by the sqlite3session_changeset() function (i.e. all changes for
92109248
** a single table are grouped together, tables appear in the order in which
92119249
** they were attached to the session object).
92129250
*/
9213
-int sqlite3session_patchset(
9251
+SQLITE_API int sqlite3session_patchset(
92149252
sqlite3_session *pSession, /* Session object */
92159253
int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */
92169254
void **ppPatchset /* OUT: Buffer containing changeset */
92179255
);
92189256
@@ -9229,11 +9267,11 @@
92299267
** an attached table is modified and then later on the original values
92309268
** are restored. However, if this function returns non-zero, then it is
92319269
** guaranteed that a call to sqlite3session_changeset() will return a
92329270
** changeset containing zero changes.
92339271
*/
9234
-int sqlite3session_isempty(sqlite3_session *pSession);
9272
+SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
92359273
92369274
/*
92379275
** CAPI3REF: Create An Iterator To Traverse A Changeset
92389276
**
92399277
** Create an iterator used to iterate through the contents of a changeset.
@@ -9264,11 +9302,11 @@
92649302
** this function, all changes that relate to a single table are visited
92659303
** consecutively. There is no chance that the iterator will visit a change
92669304
** the applies to table X, then one for table Y, and then later on visit
92679305
** another change for table X.
92689306
*/
9269
-int sqlite3changeset_start(
9307
+SQLITE_API int sqlite3changeset_start(
92709308
sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
92719309
int nChangeset, /* Size of changeset blob in bytes */
92729310
void *pChangeset /* Pointer to blob containing changeset */
92739311
);
92749312
@@ -9293,11 +9331,11 @@
92939331
**
92949332
** If an error occurs, an SQLite error code is returned. Possible error
92959333
** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or
92969334
** SQLITE_NOMEM.
92979335
*/
9298
-int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
9336
+SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
92999337
93009338
/*
93019339
** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
93029340
**
93039341
** The pIter argument passed to this function may either be an iterator
@@ -9321,11 +9359,11 @@
93219359
**
93229360
** If no error occurs, SQLITE_OK is returned. If an error does occur, an
93239361
** SQLite error code is returned. The values of the output variables may not
93249362
** be trusted in this case.
93259363
*/
9326
-int sqlite3changeset_op(
9364
+SQLITE_API int sqlite3changeset_op(
93279365
sqlite3_changeset_iter *pIter, /* Iterator object */
93289366
const char **pzTab, /* OUT: Pointer to table name */
93299367
int *pnCol, /* OUT: Number of columns in table */
93309368
int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
93319369
int *pbIndirect /* OUT: True for an 'indirect' change */
@@ -9354,11 +9392,11 @@
93549392
** If this function is called when the iterator does not point to a valid
93559393
** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
93569394
** SQLITE_OK is returned and the output variables populated as described
93579395
** above.
93589396
*/
9359
-int sqlite3changeset_pk(
9397
+SQLITE_API int sqlite3changeset_pk(
93609398
sqlite3_changeset_iter *pIter, /* Iterator object */
93619399
unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
93629400
int *pnCol /* OUT: Number of entries in output array */
93639401
);
93649402
@@ -9384,11 +9422,11 @@
93849422
** is similar to the "old.*" columns available to update or delete triggers.
93859423
**
93869424
** If some other error occurs (e.g. an OOM condition), an SQLite error code
93879425
** is returned and *ppValue is set to NULL.
93889426
*/
9389
-int sqlite3changeset_old(
9427
+SQLITE_API int sqlite3changeset_old(
93909428
sqlite3_changeset_iter *pIter, /* Changeset iterator */
93919429
int iVal, /* Column number */
93929430
sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
93939431
);
93949432
@@ -9417,11 +9455,11 @@
94179455
** triggers.
94189456
**
94199457
** If some other error occurs (e.g. an OOM condition), an SQLite error code
94209458
** is returned and *ppValue is set to NULL.
94219459
*/
9422
-int sqlite3changeset_new(
9460
+SQLITE_API int sqlite3changeset_new(
94239461
sqlite3_changeset_iter *pIter, /* Changeset iterator */
94249462
int iVal, /* Column number */
94259463
sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
94269464
);
94279465
@@ -9444,11 +9482,11 @@
94449482
** and returns SQLITE_OK.
94459483
**
94469484
** If some other error occurs (e.g. an OOM condition), an SQLite error code
94479485
** is returned and *ppValue is set to NULL.
94489486
*/
9449
-int sqlite3changeset_conflict(
9487
+SQLITE_API int sqlite3changeset_conflict(
94509488
sqlite3_changeset_iter *pIter, /* Changeset iterator */
94519489
int iVal, /* Column number */
94529490
sqlite3_value **ppValue /* OUT: Value from conflicting row */
94539491
);
94549492
@@ -9460,11 +9498,11 @@
94609498
** it sets the output variable to the total number of known foreign key
94619499
** violations in the destination database and returns SQLITE_OK.
94629500
**
94639501
** In all other cases this function returns SQLITE_MISUSE.
94649502
*/
9465
-int sqlite3changeset_fk_conflicts(
9503
+SQLITE_API int sqlite3changeset_fk_conflicts(
94669504
sqlite3_changeset_iter *pIter, /* Changeset iterator */
94679505
int *pnOut /* OUT: Number of FK violations */
94689506
);
94699507
94709508
@@ -9493,11 +9531,11 @@
94939531
** rc = sqlite3changeset_finalize();
94949532
** if( rc!=SQLITE_OK ){
94959533
** // An error has occurred
94969534
** }
94979535
*/
9498
-int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
9536
+SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
94999537
95009538
/*
95019539
** CAPI3REF: Invert A Changeset
95029540
**
95039541
** This function is used to "invert" a changeset object. Applying an inverted
@@ -9523,11 +9561,11 @@
95239561
** call to this function.
95249562
**
95259563
** WARNING/TODO: This function currently assumes that the input is a valid
95269564
** changeset. If it is not, the results are undefined.
95279565
*/
9528
-int sqlite3changeset_invert(
9566
+SQLITE_API int sqlite3changeset_invert(
95299567
int nIn, const void *pIn, /* Input changeset */
95309568
int *pnOut, void **ppOut /* OUT: Inverse of input */
95319569
);
95329570
95339571
/*
@@ -9552,11 +9590,11 @@
95529590
** *pnOut = 0;
95539591
** }
95549592
**
95559593
** Refer to the sqlite3_changegroup documentation below for details.
95569594
*/
9557
-int sqlite3changeset_concat(
9595
+SQLITE_API int sqlite3changeset_concat(
95589596
int nA, /* Number of bytes in buffer pA */
95599597
void *pA, /* Pointer to buffer containing changeset A */
95609598
int nB, /* Number of bytes in buffer pB */
95619599
void *pB, /* Pointer to buffer containing changeset B */
95629600
int *pnOut, /* OUT: Number of bytes in output changeset */
@@ -9740,11 +9778,11 @@
97409778
** considered compatible if all of the following are true:
97419779
**
97429780
** <ul>
97439781
** <li> The table has the same name as the name recorded in the
97449782
** changeset, and
9745
-** <li> The table has the same number of columns as recorded in the
9783
+** <li> The table has at least as many columns as recorded in the
97469784
** changeset, and
97479785
** <li> The table has primary key columns in the same position as
97489786
** recorded in the changeset.
97499787
** </ul>
97509788
**
@@ -9785,11 +9823,15 @@
97859823
** the changeset the row is deleted from the target database.
97869824
**
97879825
** If a row with matching primary key values is found, but one or more of
97889826
** the non-primary key fields contains a value different from the original
97899827
** row value stored in the changeset, the conflict-handler function is
9790
-** invoked with [SQLITE_CHANGESET_DATA] as the second argument.
9828
+** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
9829
+** database table has more columns than are recorded in the changeset,
9830
+** only the values of those non-primary key fields are compared against
9831
+** the current database contents - any trailing database table columns
9832
+** are ignored.
97919833
**
97929834
** If no row with matching primary key values is found in the database,
97939835
** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
97949836
** passed as the second argument.
97959837
**
@@ -9800,11 +9842,13 @@
98009842
** operation is attempted because an earlier call to the conflict handler
98019843
** function returned [SQLITE_CHANGESET_REPLACE].
98029844
**
98039845
** <dt>INSERT Changes<dd>
98049846
** For each INSERT change, an attempt is made to insert the new row into
9805
-** the database.
9847
+** the database. If the changeset row contains fewer fields than the
9848
+** database table, the trailing fields are populated with their default
9849
+** values.
98069850
**
98079851
** If the attempt to insert the row fails because the database already
98089852
** contains a row with the same primary key values, the conflict handler
98099853
** function is invoked with the second argument set to
98109854
** [SQLITE_CHANGESET_CONFLICT].
@@ -9818,17 +9862,17 @@
98189862
**
98199863
** <dt>UPDATE Changes<dd>
98209864
** For each UPDATE change, this function checks if the target database
98219865
** contains a row with the same primary key value (or values) as the
98229866
** 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.
9867
+** stored in all modified non-primary key columns also match the values
9868
+** stored in the changeset the row is updated within the target database.
98259869
**
98269870
** 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
9871
+** the modified non-primary key fields contains a value different from an
9872
+** original row value stored in the changeset, the conflict-handler function
9873
+** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
98309874
** UPDATE changes only contain values for non-primary key fields that are
98319875
** to be modified, only those fields need to match the original values to
98329876
** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
98339877
**
98349878
** If no row with matching primary key values is found in the database,
@@ -9852,11 +9896,11 @@
98529896
** If any other error (aside from a constraint failure when attempting to
98539897
** write to the target database) occurs, then the savepoint transaction is
98549898
** rolled back, restoring the target database to its original state, and an
98559899
** SQLite error code returned.
98569900
*/
9857
-int sqlite3changeset_apply(
9901
+SQLITE_API int sqlite3changeset_apply(
98589902
sqlite3 *db, /* Apply change to "main" db of this handle */
98599903
int nChangeset, /* Size of changeset in bytes */
98609904
void *pChangeset, /* Changeset blob */
98619905
int(*xFilter)(
98629906
void *pCtx, /* Copy of sixth arg to _apply() */
@@ -10053,11 +10097,11 @@
1005310097
**
1005410098
** The sessions module never invokes an xOutput callback with the third
1005510099
** parameter set to a value less than or equal to zero. Other than this,
1005610100
** no guarantees are made as to the size of the chunks of data returned.
1005710101
*/
10058
-int sqlite3changeset_apply_strm(
10102
+SQLITE_API int sqlite3changeset_apply_strm(
1005910103
sqlite3 *db, /* Apply change to "main" db of this handle */
1006010104
int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
1006110105
void *pIn, /* First arg for xInput */
1006210106
int(*xFilter)(
1006310107
void *pCtx, /* Copy of sixth arg to _apply() */
@@ -10068,35 +10112,35 @@
1006810112
int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
1006910113
sqlite3_changeset_iter *p /* Handle describing change and conflict */
1007010114
),
1007110115
void *pCtx /* First argument passed to xConflict */
1007210116
);
10073
-int sqlite3changeset_concat_strm(
10117
+SQLITE_API int sqlite3changeset_concat_strm(
1007410118
int (*xInputA)(void *pIn, void *pData, int *pnData),
1007510119
void *pInA,
1007610120
int (*xInputB)(void *pIn, void *pData, int *pnData),
1007710121
void *pInB,
1007810122
int (*xOutput)(void *pOut, const void *pData, int nData),
1007910123
void *pOut
1008010124
);
10081
-int sqlite3changeset_invert_strm(
10125
+SQLITE_API int sqlite3changeset_invert_strm(
1008210126
int (*xInput)(void *pIn, void *pData, int *pnData),
1008310127
void *pIn,
1008410128
int (*xOutput)(void *pOut, const void *pData, int nData),
1008510129
void *pOut
1008610130
);
10087
-int sqlite3changeset_start_strm(
10131
+SQLITE_API int sqlite3changeset_start_strm(
1008810132
sqlite3_changeset_iter **pp,
1008910133
int (*xInput)(void *pIn, void *pData, int *pnData),
1009010134
void *pIn
1009110135
);
10092
-int sqlite3session_changeset_strm(
10136
+SQLITE_API int sqlite3session_changeset_strm(
1009310137
sqlite3_session *pSession,
1009410138
int (*xOutput)(void *pOut, const void *pData, int nData),
1009510139
void *pOut
1009610140
);
10097
-int sqlite3session_patchset_strm(
10141
+SQLITE_API int sqlite3session_patchset_strm(
1009810142
sqlite3_session *pSession,
1009910143
int (*xOutput)(void *pOut, const void *pData, int nData),
1010010144
void *pOut
1010110145
);
1010210146
int sqlite3changegroup_add_strm(sqlite3_changegroup*,
@@ -10999,10 +11043,11 @@
1099911043
# if defined(_MSC_VER) && _MSC_VER>=1400
1100011044
# if !defined(_WIN32_WCE)
1100111045
# include <intrin.h>
1100211046
# pragma intrinsic(_byteswap_ushort)
1100311047
# pragma intrinsic(_byteswap_ulong)
11048
+# pragma intrinsic(_byteswap_uint64)
1100411049
# pragma intrinsic(_ReadWriteBarrier)
1100511050
# else
1100611051
# include <cmnintrin.h>
1100711052
# endif
1100811053
# endif
@@ -11537,10 +11582,22 @@
1153711582
#include <stdlib.h>
1153811583
#include <string.h>
1153911584
#include <assert.h>
1154011585
#include <stddef.h>
1154111586
11587
+/*
11588
+** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY.
11589
+** This allows better measurements of where memcpy() is used when running
11590
+** cachegrind. But this macro version of memcpy() is very slow so it
11591
+** should not be used in production. This is a performance measurement
11592
+** hack only.
11593
+*/
11594
+#ifdef SQLITE_INLINE_MEMCPY
11595
+# define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\
11596
+ int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
11597
+#endif
11598
+
1154211599
/*
1154311600
** If compiling for a processor that lacks floating point support,
1154411601
** substitute integer for floating-point
1154511602
*/
1154611603
#ifdef SQLITE_OMIT_FLOATING_POINT
@@ -11621,13 +11678,16 @@
1162111678
/*
1162211679
** The default initial allocation for the pagecache when using separate
1162311680
** pagecaches for each database connection. A positive number is the
1162411681
** number of pages. A negative number N translations means that a buffer
1162511682
** of -1024*N bytes is allocated and used for as many pages as it will hold.
11683
+**
11684
+** The default value of "20" was choosen to minimize the run-time of the
11685
+** speedtest1 test program with options: --shrink-memory --reprepare
1162611686
*/
1162711687
#ifndef SQLITE_DEFAULT_PCACHE_INITSZ
11628
-# define SQLITE_DEFAULT_PCACHE_INITSZ 100
11688
+# define SQLITE_DEFAULT_PCACHE_INITSZ 20
1162911689
#endif
1163011690
1163111691
/*
1163211692
** GCC does not define the offsetof() macro so we'll have to do it
1163311693
** ourselves.
@@ -11798,36 +11858,39 @@
1179811858
** Macros to determine whether the machine is big or little endian,
1179911859
** and whether or not that determination is run-time or compile-time.
1180011860
**
1180111861
** For best performance, an attempt is made to guess at the byte-order
1180211862
** using C-preprocessor macros. If that is unsuccessful, or if
11803
-** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
11863
+** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
1180411864
** at run-time.
1180511865
*/
11806
-#if (defined(i386) || defined(__i386__) || defined(_M_IX86) || \
11866
+#ifndef SQLITE_BYTEORDER
11867
+# if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
1180711868
defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
1180811869
defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
11809
- defined(__arm__)) && !defined(SQLITE_RUNTIME_BYTEORDER)
11810
-# define SQLITE_BYTEORDER 1234
11811
-# define SQLITE_BIGENDIAN 0
11812
-# define SQLITE_LITTLEENDIAN 1
11813
-# define SQLITE_UTF16NATIVE SQLITE_UTF16LE
11870
+ defined(__arm__)
11871
+# define SQLITE_BYTEORDER 1234
11872
+# elif defined(sparc) || defined(__ppc__)
11873
+# define SQLITE_BYTEORDER 4321
11874
+# else
11875
+# define SQLITE_BYTEORDER 0
11876
+# endif
1181411877
#endif
11815
-#if (defined(sparc) || defined(__ppc__)) \
11816
- && !defined(SQLITE_RUNTIME_BYTEORDER)
11817
-# define SQLITE_BYTEORDER 4321
11878
+#if SQLITE_BYTEORDER==4321
1181811879
# define SQLITE_BIGENDIAN 1
1181911880
# define SQLITE_LITTLEENDIAN 0
1182011881
# define SQLITE_UTF16NATIVE SQLITE_UTF16BE
11821
-#endif
11822
-#if !defined(SQLITE_BYTEORDER)
11882
+#elif SQLITE_BYTEORDER==1234
11883
+# define SQLITE_BIGENDIAN 0
11884
+# define SQLITE_LITTLEENDIAN 1
11885
+# define SQLITE_UTF16NATIVE SQLITE_UTF16LE
11886
+#else
1182311887
# ifdef SQLITE_AMALGAMATION
1182411888
const int sqlite3one = 1;
1182511889
# else
1182611890
extern const int sqlite3one;
1182711891
# endif
11828
-# define SQLITE_BYTEORDER 0 /* 0 means "unknown at compile-time" */
1182911892
# define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0)
1183011893
# define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
1183111894
# define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
1183211895
#endif
1183311896
@@ -12346,13 +12409,14 @@
1234612409
);
1234712410
SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*);
1234812411
SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*);
1234912412
SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, u8 flags);
1235012413
12351
-/* Allowed flags for the 2nd argument to sqlite3BtreeDelete() */
12414
+/* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */
1235212415
#define BTREE_SAVEPOSITION 0x02 /* Leave cursor pointing at NEXT or PREV */
1235312416
#define BTREE_AUXDELETE 0x04 /* not the primary delete operation */
12417
+#define BTREE_APPEND 0x08 /* Insert is likely an append */
1235412418
1235512419
/* An instance of the BtreePayload object describes the content of a single
1235612420
** entry in either an index or table btree.
1235712421
**
1235812422
** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
@@ -12379,11 +12443,11 @@
1237912443
int nData; /* Size of pData. 0 if none. */
1238012444
int nZero; /* Extra zero data appended after pData,nData */
1238112445
};
1238212446
1238312447
SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
12384
- int bias, int seekResult);
12448
+ int flags, int seekResult);
1238512449
SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
1238612450
SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
1238712451
SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
1238812452
SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
1238912453
SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
@@ -12512,12 +12576,11 @@
1251212576
** as an instance of the following structure:
1251312577
*/
1251412578
struct VdbeOp {
1251512579
u8 opcode; /* What operation to perform */
1251612580
signed char p4type; /* One of the P4_xxx constants for p4 */
12517
- u8 notUsed1;
12518
- u8 p5; /* Fifth parameter is an unsigned character */
12581
+ u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
1251912582
int p1; /* First operand */
1252012583
int p2; /* Second parameter (often the jump destination) */
1252112584
int p3; /* The third parameter */
1252212585
union p4union { /* fourth parameter */
1252312586
int i; /* Integer value if p4type==P4_INT32 */
@@ -12874,11 +12937,11 @@
1287412937
SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
1287512938
SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8);
1287612939
SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
1287712940
SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
1287812941
SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
12879
-SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
12942
+SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
1288012943
SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
1288112944
SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
1288212945
SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
1288312946
SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
1288412947
SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
@@ -13176,18 +13239,20 @@
1317613239
SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
1317713240
SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
1317813241
SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
1317913242
SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
1318013243
SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
13181
-SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager);
13244
+# ifdef SQLITE_DIRECT_OVERFLOW_READ
13245
+SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager, Pgno);
13246
+# endif
1318213247
# ifdef SQLITE_ENABLE_SNAPSHOT
1318313248
SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot);
1318413249
SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot);
1318513250
SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager);
1318613251
# endif
1318713252
#else
13188
-# define sqlite3PagerUseWal(x) 0
13253
+# define sqlite3PagerUseWal(x,y) 0
1318913254
#endif
1319013255
1319113256
#ifdef SQLITE_ENABLE_ZIPVFS
1319213257
SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager);
1319313258
#endif
@@ -14007,10 +14072,11 @@
1400714072
signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
1400814073
u8 suppressErr; /* Do not issue error messages if true */
1400914074
u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
1401014075
u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
1401114076
u8 mTrace; /* zero or more SQLITE_TRACE flags */
14077
+ u8 skipBtreeMutex; /* True if no shared-cache backends */
1401214078
int nextPagesize; /* Pagesize after VACUUM if >0 */
1401314079
u32 magic; /* Magic number for detect library misuse */
1401414080
int nChange; /* Value returned by sqlite3_changes() */
1401514081
int nTotalChange; /* Value returned by sqlite3_total_changes() */
1401614082
int aLimit[SQLITE_N_LIMIT]; /* Limits */
@@ -14272,10 +14338,11 @@
1427214338
#define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
1427314339
#define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
1427414340
#define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */
1427514341
#define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a
1427614342
** single query - might change over time */
14343
+#define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
1427714344
1427814345
/*
1427914346
** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
1428014347
** used to create the initializers for the FuncDef structures.
1428114348
**
@@ -15278,11 +15345,11 @@
1527815345
#define WHERE_DISTINCTBY 0x0080 /* pOrderby is really a DISTINCT clause */
1527915346
#define WHERE_WANT_DISTINCT 0x0100 /* All output needs to be distinct */
1528015347
#define WHERE_SORTBYGROUP 0x0200 /* Support sqlite3WhereIsSorted() */
1528115348
#define WHERE_SEEK_TABLE 0x0400 /* Do not defer seeks on main table */
1528215349
#define WHERE_ORDERBY_LIMIT 0x0800 /* ORDERBY+LIMIT on the inner loop */
15283
- /* 0x1000 not currently used */
15350
+#define WHERE_SEEK_UNIQ_TABLE 0x1000 /* Do not defer seeks if unique */
1528415351
/* 0x2000 not currently used */
1528515352
#define WHERE_USE_LIMIT 0x4000 /* Use the LIMIT in cost estimates */
1528615353
/* 0x8000 not currently used */
1528715354
1528815355
/* Allowed return values from sqlite3WhereIsDistinct()
@@ -15739,25 +15806,23 @@
1573915806
** OPFLAG_AUXDELETE == BTREE_AUXDELETE
1574015807
*/
1574115808
#define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
1574215809
/* Also used in P2 (not P5) of OP_Delete */
1574315810
#define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */
15744
-#define OPFLAG_LASTROWID 0x02 /* Set to update db->lastRowid */
15811
+#define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
1574515812
#define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */
1574615813
#define OPFLAG_APPEND 0x08 /* This is likely to be an append */
1574715814
#define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
15748
-#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
1574915815
#define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
15750
-#endif
1575115816
#define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */
1575215817
#define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */
1575315818
#define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */
1575415819
#define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */
1575515820
#define OPFLAG_FORDELETE 0x08 /* OP_Open should use BTREE_FORDELETE */
1575615821
#define OPFLAG_P2ISREG 0x10 /* P2 to OP_Open** is a register number */
1575715822
#define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */
15758
-#define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete: keep cursor position */
15823
+#define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete/Insert: save cursor pos */
1575915824
#define OPFLAG_AUXDELETE 0x04 /* OP_Delete: index in a DELETE op */
1576015825
1576115826
/*
1576215827
* Each trigger present in the database schema is stored as an instance of
1576315828
* struct Trigger.
@@ -16414,11 +16479,11 @@
1641416479
SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
1641516480
SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
1641616481
SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int);
1641716482
SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, Expr*, int);
1641816483
SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
16419
-SQLITE_PRIVATE void sqlite3ExprCodeAtInit(Parse*, Expr*, int, u8);
16484
+SQLITE_PRIVATE int sqlite3ExprCodeAtInit(Parse*, Expr*, int);
1642016485
SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
1642116486
SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
1642216487
SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
1642316488
SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
1642416489
#define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
@@ -16476,10 +16541,15 @@
1647616541
SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
1647716542
SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
1647816543
SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
1647916544
SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
1648016545
u8,u8,int,int*,int*);
16546
+#ifdef SQLITE_ENABLE_NULL_TRIM
16547
+SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe*,Table*);
16548
+#else
16549
+# define sqlite3SetMakeRecordP5(A,B)
16550
+#endif
1648116551
SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
1648216552
SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
1648316553
SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
1648416554
SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
1648516555
SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
@@ -16754,12 +16824,14 @@
1675416824
#endif
1675516825
1675616826
/*
1675716827
** The interface to the LEMON-generated parser
1675816828
*/
16759
-SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64));
16760
-SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
16829
+#ifndef SQLITE_AMALGAMATION
16830
+SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64));
16831
+SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
16832
+#endif
1676116833
SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
1676216834
#ifdef YYTRACKMAXSTACKDEPTH
1676316835
SQLITE_PRIVATE int sqlite3ParserStackPeak(void*);
1676416836
#endif
1676516837
@@ -16865,10 +16937,11 @@
1686516937
#define sqlite3FkActions(a,b,c,d,e,f)
1686616938
#define sqlite3FkCheck(a,b,c,d,e,f)
1686716939
#define sqlite3FkDropTable(a,b,c)
1686816940
#define sqlite3FkOldmask(a,b) 0
1686916941
#define sqlite3FkRequired(a,b,c,d) 0
16942
+ #define sqlite3FkReferences(a) 0
1687016943
#endif
1687116944
#ifndef SQLITE_OMIT_FOREIGN_KEY
1687216945
SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *, Table*);
1687316946
SQLITE_PRIVATE int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
1687416947
#else
@@ -17193,10 +17266,23 @@
1719317266
** setting.)
1719417267
*/
1719517268
#ifndef SQLITE_STMTJRNL_SPILL
1719617269
# define SQLITE_STMTJRNL_SPILL (64*1024)
1719717270
#endif
17271
+
17272
+/*
17273
+** The default lookaside-configuration, the format "SZ,N". SZ is the
17274
+** number of bytes in each lookaside slot (should be a multiple of 8)
17275
+** and N is the number of slots. The lookaside-configuration can be
17276
+** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
17277
+** or at run-time for an individual database connection using
17278
+** sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE);
17279
+*/
17280
+#ifndef SQLITE_DEFAULT_LOOKASIDE
17281
+# define SQLITE_DEFAULT_LOOKASIDE 1200,100
17282
+#endif
17283
+
1719817284
1719917285
/*
1720017286
** The following singleton contains the global configuration for
1720117287
** the SQLite library.
1720217288
*/
@@ -17206,12 +17292,11 @@
1720617292
SQLITE_THREADSAFE==1, /* bFullMutex */
1720717293
SQLITE_USE_URI, /* bOpenUri */
1720817294
SQLITE_ALLOW_COVERING_INDEX_SCAN, /* bUseCis */
1720917295
0x7ffffffe, /* mxStrlen */
1721017296
0, /* neverCorrupt */
17211
- 512, /* szLookaside */
17212
- 125, /* nLookaside */
17297
+ SQLITE_DEFAULT_LOOKASIDE, /* szLookaside, nLookaside */
1721317298
SQLITE_STMTJRNL_SPILL, /* nStmtSpill */
1721417299
{0,0,0,0,0,0,0,0}, /* m */
1721517300
{0,0,0,0,0,0,0,0,0}, /* mutex */
1721617301
{0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
1721717302
(void*)0, /* pHeap */
@@ -18219,10 +18304,11 @@
1821918304
int iNewReg; /* Register for new.* values */
1822018305
i64 iKey1; /* First key value passed to hook */
1822118306
i64 iKey2; /* Second key value passed to hook */
1822218307
Mem *aNew; /* Array of new.* values */
1822318308
Table *pTab; /* Schema object being upated */
18309
+ Index *pPk; /* PK index if pTab is WITHOUT ROWID */
1822418310
};
1822518311
1822618312
/*
1822718313
** Function prototypes
1822818314
*/
@@ -20619,20 +20705,22 @@
2061920705
** cases of nByte<=0 will be intercepted and dealt with by higher level
2062020706
** routines.
2062120707
*/
2062220708
static void *sqlite3MemMalloc(int nByte){
2062320709
#ifdef SQLITE_MALLOCSIZE
20624
- void *p = SQLITE_MALLOC( nByte );
20710
+ void *p;
20711
+ testcase( ROUND8(nByte)==nByte );
20712
+ p = SQLITE_MALLOC( nByte );
2062520713
if( p==0 ){
2062620714
testcase( sqlite3GlobalConfig.xLog!=0 );
2062720715
sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
2062820716
}
2062920717
return p;
2063020718
#else
2063120719
sqlite3_int64 *p;
2063220720
assert( nByte>0 );
20633
- nByte = ROUND8(nByte);
20721
+ testcase( ROUND8(nByte)!=nByte );
2063420722
p = SQLITE_MALLOC( nByte+8 );
2063520723
if( p ){
2063620724
p[0] = nByte;
2063720725
p++;
2063820726
}else{
@@ -23750,12 +23838,11 @@
2375023838
SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
2375123839
#if defined(SQLITE_MEMORY_BARRIER)
2375223840
SQLITE_MEMORY_BARRIER;
2375323841
#elif defined(__GNUC__)
2375423842
__sync_synchronize();
23755
-#elif !defined(SQLITE_DISABLE_INTRINSIC) && \
23756
- defined(_MSC_VER) && _MSC_VER>=1300
23843
+#elif MSVC_VERSION>=1300
2375723844
_ReadWriteBarrier();
2375823845
#elif defined(MemoryBarrier)
2375923846
MemoryBarrier();
2376023847
#endif
2376123848
}
@@ -24283,15 +24370,23 @@
2428324370
2428424371
/*
2428524372
** Do a memory allocation with statistics and alarms. Assume the
2428624373
** lock is already held.
2428724374
*/
24288
-static int mallocWithAlarm(int n, void **pp){
24289
- int nFull;
24375
+static void mallocWithAlarm(int n, void **pp){
2429024376
void *p;
24377
+ int nFull;
2429124378
assert( sqlite3_mutex_held(mem0.mutex) );
24379
+ assert( n>0 );
24380
+
24381
+ /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
24382
+ ** implementation of malloc_good_size(), which must be called in debug
24383
+ ** mode and specifically when the DMD "Dark Matter Detector" is enabled
24384
+ ** or else a crash results. Hence, do not attempt to optimize out the
24385
+ ** following xRoundup() call. */
2429224386
nFull = sqlite3GlobalConfig.m.xRoundup(n);
24387
+
2429324388
sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
2429424389
if( mem0.alarmThreshold>0 ){
2429524390
sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
2429624391
if( nUsed >= mem0.alarmThreshold - nFull ){
2429724392
mem0.nearlyFull = 1;
@@ -24311,11 +24406,10 @@
2431124406
nFull = sqlite3MallocSize(p);
2431224407
sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
2431324408
sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
2431424409
}
2431524410
*pp = p;
24316
- return nFull;
2431724411
}
2431824412
2431924413
/*
2432024414
** Allocate memory. This routine is like sqlite3_malloc() except that it
2432124415
** assumes the memory subsystem has already been initialized.
@@ -24951,11 +25045,10 @@
2495125045
2495225046
/*
2495325047
** Allowed values for et_info.flags
2495425048
*/
2495525049
#define FLAG_SIGNED 1 /* True if the value to convert is signed */
24956
-#define FLAG_INTERN 2 /* True if for internal use only */
2495725050
#define FLAG_STRING 4 /* Allow infinity precision */
2495825051
2495925052
2496025053
/*
2496125054
** The following table is searched linearly, so it is good to put the
@@ -24985,15 +25078,14 @@
2498525078
{ 'i', 10, 1, etRADIX, 0, 0 },
2498625079
{ 'n', 0, 0, etSIZE, 0, 0 },
2498725080
{ '%', 0, 0, etPERCENT, 0, 0 },
2498825081
{ 'p', 16, 0, etPOINTER, 0, 1 },
2498925082
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 },
25083
+ /* All the rest are undocumented and are for internal use only */
25084
+ { 'T', 0, 0, etTOKEN, 0, 0 },
25085
+ { 'S', 0, 0, etSRCLIST, 0, 0 },
25086
+ { 'r', 10, 1, etORDINAL, 0, 0 },
2499525087
};
2499625088
2499725089
/*
2499825090
** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
2499925091
** conversions will work.
@@ -25083,11 +25175,10 @@
2508325175
etByte flag_long; /* True if "l" flag is present */
2508425176
etByte flag_longlong; /* True if the "ll" flag is present */
2508525177
etByte done; /* Loop termination flag */
2508625178
etByte xtype = etINVALID; /* Conversion paradigm */
2508725179
u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
25088
- u8 useIntern; /* Ok to use internal conversions (ex: %T) */
2508925180
char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
2509025181
sqlite_uint64 longvalue; /* Value for integer types */
2509125182
LONGDOUBLE_TYPE realvalue; /* Value for real types */
2509225183
const et_info *infop; /* Pointer to the appropriate info structure */
2509325184
char *zOut; /* Rendering buffer */
@@ -25102,17 +25193,15 @@
2510225193
#endif
2510325194
PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
2510425195
char buf[etBUFSIZE]; /* Conversion buffer */
2510525196
2510625197
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;
25198
+ if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
25199
+ pArgList = va_arg(ap, PrintfArguments*);
25200
+ bArgList = 1;
2511225201
}else{
25113
- bArgList = useIntern = 0;
25202
+ bArgList = 0;
2511425203
}
2511525204
for(; (c=(*fmt))!=0; ++fmt){
2511625205
if( c!='%' ){
2511725206
bufpt = (char *)fmt;
2511825207
#if HAVE_STRCHRNUL
@@ -25220,15 +25309,11 @@
2522025309
infop = &fmtinfo[0];
2522125310
xtype = etINVALID;
2522225311
for(idx=0; idx<ArraySize(fmtinfo); idx++){
2522325312
if( c==fmtinfo[idx].fmttype ){
2522425313
infop = &fmtinfo[idx];
25225
- if( useIntern || (infop->flags & FLAG_INTERN)==0 ){
25226
- xtype = infop->type;
25227
- }else{
25228
- return;
25229
- }
25314
+ xtype = infop->type;
2523025315
break;
2523125316
}
2523225317
}
2523325318
2523425319
/*
@@ -25593,22 +25678,28 @@
2559325678
** consume, not the length of the output...
2559425679
** if( precision>=0 && precision<length ) length = precision; */
2559525680
break;
2559625681
}
2559725682
case etTOKEN: {
25598
- Token *pToken = va_arg(ap, Token*);
25683
+ Token *pToken;
25684
+ if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
25685
+ pToken = va_arg(ap, Token*);
2559925686
assert( bArgList==0 );
2560025687
if( pToken && pToken->n ){
2560125688
sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
2560225689
}
2560325690
length = width = 0;
2560425691
break;
2560525692
}
2560625693
case etSRCLIST: {
25607
- SrcList *pSrc = va_arg(ap, SrcList*);
25608
- int k = va_arg(ap, int);
25609
- struct SrcList_item *pItem = &pSrc->a[k];
25694
+ SrcList *pSrc;
25695
+ int k;
25696
+ struct SrcList_item *pItem;
25697
+ if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
25698
+ pSrc = va_arg(ap, SrcList*);
25699
+ k = va_arg(ap, int);
25700
+ pItem = &pSrc->a[k];
2561025701
assert( bArgList==0 );
2561125702
assert( k>=0 && k<pSrc->nSrc );
2561225703
if( pItem->zDatabase ){
2561325704
sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
2561425705
sqlite3StrAccumAppend(pAccum, ".", 1);
@@ -25626,13 +25717,17 @@
2562625717
** The text of the conversion is pointed to by "bufpt" and is
2562725718
** "length" characters long. The field width is "width". Do
2562825719
** the output.
2562925720
*/
2563025721
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, ' ');
25722
+ if( width>0 ){
25723
+ if( !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
25724
+ sqlite3StrAccumAppend(pAccum, bufpt, length);
25725
+ if( flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
25726
+ }else{
25727
+ sqlite3StrAccumAppend(pAccum, bufpt, length);
25728
+ }
2563425729
2563525730
if( zExtra ){
2563625731
sqlite3DbFree(pAccum->db, zExtra);
2563725732
zExtra = 0;
2563825733
}
@@ -28600,17 +28695,15 @@
2860028695
SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
2860128696
#if SQLITE_BYTEORDER==4321
2860228697
u32 x;
2860328698
memcpy(&x,p,4);
2860428699
return x;
28605
-#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
28606
- && defined(__GNUC__) && GCC_VERSION>=4003000
28700
+#elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
2860728701
u32 x;
2860828702
memcpy(&x,p,4);
2860928703
return __builtin_bswap32(x);
28610
-#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
28611
- && defined(_MSC_VER) && _MSC_VER>=1300
28704
+#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
2861228705
u32 x;
2861328706
memcpy(&x,p,4);
2861428707
return _byteswap_ulong(x);
2861528708
#else
2861628709
testcase( p[0]&0x80 );
@@ -28618,16 +28711,14 @@
2861828711
#endif
2861928712
}
2862028713
SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
2862128714
#if SQLITE_BYTEORDER==4321
2862228715
memcpy(p,&v,4);
28623
-#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
28624
- && defined(__GNUC__) && GCC_VERSION>=4003000
28716
+#elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
2862528717
u32 x = __builtin_bswap32(v);
2862628718
memcpy(p,&x,4);
28627
-#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
28628
- && defined(_MSC_VER) && _MSC_VER>=1300
28719
+#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
2862928720
u32 x = _byteswap_ulong(v);
2863028721
memcpy(p,&x,4);
2863128722
#else
2863228723
p[0] = (u8)(v>>24);
2863328724
p[1] = (u8)(v>>16);
@@ -28739,10 +28830,13 @@
2873928830
** the other 64-bit signed integer at *pA and store the result in *pA.
2874028831
** Return 0 on success. Or if the operation would have resulted in an
2874128832
** overflow, leave *pA unchanged and return 1.
2874228833
*/
2874328834
SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
28835
+#if GCC_VERSION>=5004000 || CLANG_VERSION>=4000000
28836
+ return __builtin_add_overflow(*pA, iB, pA);
28837
+#else
2874428838
i64 iA = *pA;
2874528839
testcase( iA==0 ); testcase( iA==1 );
2874628840
testcase( iB==-1 ); testcase( iB==0 );
2874728841
if( iB>=0 ){
2874828842
testcase( iA>0 && LARGEST_INT64 - iA == iB );
@@ -28753,23 +28847,31 @@
2875328847
testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
2875428848
if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
2875528849
}
2875628850
*pA += iB;
2875728851
return 0;
28852
+#endif
2875828853
}
2875928854
SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
28855
+#if GCC_VERSION>=5004000 || CLANG_VERSION>=4000000
28856
+ return __builtin_sub_overflow(*pA, iB, pA);
28857
+#else
2876028858
testcase( iB==SMALLEST_INT64+1 );
2876128859
if( iB==SMALLEST_INT64 ){
2876228860
testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
2876328861
if( (*pA)>=0 ) return 1;
2876428862
*pA -= iB;
2876528863
return 0;
2876628864
}else{
2876728865
return sqlite3AddInt64(pA, -iB);
2876828866
}
28867
+#endif
2876928868
}
2877028869
SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
28870
+#if GCC_VERSION>=5004000 || CLANG_VERSION>=4000000
28871
+ return __builtin_mul_overflow(*pA, iB, pA);
28872
+#else
2877128873
i64 iA = *pA;
2877228874
if( iB>0 ){
2877328875
if( iA>LARGEST_INT64/iB ) return 1;
2877428876
if( iA<SMALLEST_INT64/iB ) return 1;
2877528877
}else if( iB<0 ){
@@ -28781,10 +28883,11 @@
2878128883
if( -iA>LARGEST_INT64/-iB ) return 1;
2878228884
}
2878328885
}
2878428886
*pA = iA*iB;
2878528887
return 0;
28888
+#endif
2878628889
}
2878728890
2878828891
/*
2878928892
** Compute the absolute value of a 32-bit signed integer, of possible. Or
2879028893
** if the integer has a value of -2147483648, return +2147483647
@@ -47485,18 +47588,24 @@
4748547588
** if( pPager->jfd->pMethods ){ ...
4748647589
*/
4748747590
#define isOpen(pFd) ((pFd)->pMethods!=0)
4748847591
4748947592
/*
47490
-** Return true if this pager uses a write-ahead log instead of the usual
47491
-** rollback journal. Otherwise false.
47593
+** Return true if this pager uses a write-ahead log to read page pgno.
47594
+** Return false if the pager reads pgno directly from the database.
4749247595
*/
47596
+#if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_DIRECT_OVERFLOW_READ)
47597
+SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager, Pgno pgno){
47598
+ u32 iRead = 0;
47599
+ int rc;
47600
+ if( pPager->pWal==0 ) return 0;
47601
+ rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
47602
+ return rc || iRead;
47603
+}
47604
+#endif
4749347605
#ifndef SQLITE_OMIT_WAL
47494
-SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager){
47495
- return (pPager->pWal!=0);
47496
-}
47497
-# define pagerUseWal(x) sqlite3PagerUseWal(x)
47606
+# define pagerUseWal(x) ((x)->pWal!=0)
4749847607
#else
4749947608
# define pagerUseWal(x) 0
4750047609
# define pagerRollbackWal(x) 0
4750147610
# define pagerWalFrames(v,w,x,y) 0
4750247611
# define pagerOpenWalIfPresent(z) SQLITE_OK
@@ -58445,15 +58554,13 @@
5844558554
** two-byte aligned address. get2bytea() is only used for accessing the
5844658555
** cell addresses in a btree header.
5844758556
*/
5844858557
#if SQLITE_BYTEORDER==4321
5844958558
# define get2byteAligned(x) (*(u16*)(x))
58450
-#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
58451
- && GCC_VERSION>=4008000
58559
+#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000
5845258560
# define get2byteAligned(x) __builtin_bswap16(*(u16*)(x))
58453
-#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
58454
- && defined(_MSC_VER) && _MSC_VER>=1300
58561
+#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
5845558562
# define get2byteAligned(x) _byteswap_ushort(*(u16*)(x))
5845658563
#else
5845758564
# define get2byteAligned(x) ((x)[0]<<8 | (x)[1])
5845858565
#endif
5845958566
@@ -58624,27 +58731,38 @@
5862458731
** Enter the mutexes in accending order by BtShared pointer address
5862558732
** to avoid the possibility of deadlock when two threads with
5862658733
** two or more btrees in common both try to lock all their btrees
5862758734
** at the same instant.
5862858735
*/
58629
-SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
58736
+static void SQLITE_NOINLINE btreeEnterAll(sqlite3 *db){
5863058737
int i;
58738
+ int skipOk = 1;
5863158739
Btree *p;
5863258740
assert( sqlite3_mutex_held(db->mutex) );
5863358741
for(i=0; i<db->nDb; i++){
5863458742
p = db->aDb[i].pBt;
58635
- if( p ) sqlite3BtreeEnter(p);
58743
+ if( p && p->sharable ){
58744
+ sqlite3BtreeEnter(p);
58745
+ skipOk = 0;
58746
+ }
5863658747
}
58748
+ db->skipBtreeMutex = skipOk;
5863758749
}
58638
-SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
58750
+SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
58751
+ if( db->skipBtreeMutex==0 ) btreeEnterAll(db);
58752
+}
58753
+static void SQLITE_NOINLINE btreeLeaveAll(sqlite3 *db){
5863958754
int i;
5864058755
Btree *p;
5864158756
assert( sqlite3_mutex_held(db->mutex) );
5864258757
for(i=0; i<db->nDb; i++){
5864358758
p = db->aDb[i].pBt;
5864458759
if( p ) sqlite3BtreeLeave(p);
5864558760
}
58761
+}
58762
+SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
58763
+ if( db->skipBtreeMutex==0 ) btreeLeaveAll(db);
5864658764
}
5864758765
5864858766
#ifndef NDEBUG
5864958767
/*
5865058768
** Return true if the current thread holds the database connection
@@ -62097,16 +62215,18 @@
6209762215
for(i=0; i<nCell; i++){
6209862216
u8 *pCell = findCell(pPage, i);
6209962217
if( eType==PTRMAP_OVERFLOW1 ){
6210062218
CellInfo info;
6210162219
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;
62220
+ if( info.nLocal<info.nPayload ){
62221
+ if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
62222
+ return SQLITE_CORRUPT_BKPT;
62223
+ }
62224
+ if( iFrom==get4byte(pCell+info.nSize-4) ){
62225
+ put4byte(pCell+info.nSize-4, iTo);
62226
+ break;
62227
+ }
6210862228
}
6210962229
}else{
6211062230
if( get4byte(pCell)==iFrom ){
6211162231
put4byte(pCell, iTo);
6211262232
break;
@@ -62777,11 +62897,16 @@
6277762897
if( p && p->inTrans==TRANS_WRITE ){
6277862898
BtShared *pBt = p->pBt;
6277962899
assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
6278062900
assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
6278162901
sqlite3BtreeEnter(p);
62782
- rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
62902
+ if( op==SAVEPOINT_ROLLBACK ){
62903
+ rc = saveAllCursors(pBt, 0, 0);
62904
+ }
62905
+ if( rc==SQLITE_OK ){
62906
+ rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
62907
+ }
6278362908
if( rc==SQLITE_OK ){
6278462909
if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
6278562910
pBt->nPage = 0;
6278662911
}
6278762912
rc = newDatabase(pBt);
@@ -63163,25 +63288,24 @@
6316363288
** for the entry that the pCur cursor is pointing to. The eOp
6316463289
** argument is interpreted as follows:
6316563290
**
6316663291
** 0: The operation is a read. Populate the overflow cache.
6316763292
** 1: The operation is a write. Populate the overflow cache.
63168
-** 2: The operation is a read. Do not populate the overflow cache.
6316963293
**
6317063294
** A total of "amt" bytes are read or written beginning at "offset".
6317163295
** Data is read to or from the buffer pBuf.
6317263296
**
6317363297
** The content being read or written might appear on the main page
6317463298
** or be scattered out on multiple overflow pages.
6317563299
**
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).
63300
+** If the current cursor entry uses one or more overflow pages
63301
+** this function may allocate space for and lazily populate
63302
+** the overflow page-list cache array (BtCursor.aOverflow).
6317963303
** Subsequent calls use this cache to make seeking to the supplied offset
6318063304
** more efficient.
6318163305
**
63182
-** Once an overflow page-list cache has been allocated, it may be
63306
+** Once an overflow page-list cache has been allocated, it must be
6318363307
** invalidated if some other cursor writes to the same table, or if
6318463308
** the cursor is moved to a different row. Additionally, in auto-vacuum
6318563309
** mode, the following events may invalidate an overflow page-list cache.
6318663310
**
6318763311
** * An incremental vacuum,
@@ -63199,25 +63323,21 @@
6319963323
int rc = SQLITE_OK;
6320063324
int iIdx = 0;
6320163325
MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
6320263326
BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
6320363327
#ifdef SQLITE_DIRECT_OVERFLOW_READ
63204
- unsigned char * const pBufStart = pBuf;
63205
- int bEnd; /* True if reading to end of data */
63328
+ unsigned char * const pBufStart = pBuf; /* Start of original out buffer */
6320663329
#endif
6320763330
6320863331
assert( pPage );
63332
+ assert( eOp==0 || eOp==1 );
6320963333
assert( pCur->eState==CURSOR_VALID );
6321063334
assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
6321163335
assert( cursorHoldsMutex(pCur) );
63212
- assert( eOp!=2 || offset==0 ); /* Always start from beginning for eOp==2 */
6321363336
6321463337
getCellInfo(pCur);
6321563338
aPayload = pCur->info.pPayload;
63216
-#ifdef SQLITE_DIRECT_OVERFLOW_READ
63217
- bEnd = offset+amt==pCur->info.nPayload;
63218
-#endif
6321963339
assert( offset+amt <= pCur->info.nPayload );
6322063340
6322163341
assert( aPayload > pPage->aData );
6322263342
if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
6322363343
/* Trying to read or write past the end of the data is an error. The
@@ -63232,11 +63352,11 @@
6323263352
if( offset<pCur->info.nLocal ){
6323363353
int a = amt;
6323463354
if( a+offset>pCur->info.nLocal ){
6323563355
a = pCur->info.nLocal - offset;
6323663356
}
63237
- rc = copyPayload(&aPayload[offset], pBuf, a, (eOp & 0x01), pPage->pDbPage);
63357
+ rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
6323863358
offset = 0;
6323963359
pBuf += a;
6324063360
amt -= a;
6324163361
}else{
6324263362
offset -= pCur->info.nLocal;
@@ -63248,69 +63368,58 @@
6324863368
Pgno nextPage;
6324963369
6325063370
nextPage = get4byte(&aPayload[pCur->info.nLocal]);
6325163371
6325263372
/* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
63253
- ** Except, do not allocate aOverflow[] for eOp==2.
6325463373
**
6325563374
** The aOverflow[] array is sized at one entry for each overflow page
6325663375
** in the overflow chain. The page number of the first overflow page is
6325763376
** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
6325863377
** means "not yet known" (the cache is lazily populated).
6325963378
*/
63260
- if( eOp!=2 && (pCur->curFlags & BTCF_ValidOvfl)==0 ){
63379
+ if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
6326163380
int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
6326263381
if( nOvfl>pCur->nOvflAlloc ){
6326363382
Pgno *aNew = (Pgno*)sqlite3Realloc(
6326463383
pCur->aOverflow, nOvfl*2*sizeof(Pgno)
6326563384
);
6326663385
if( aNew==0 ){
63267
- rc = SQLITE_NOMEM_BKPT;
63386
+ return SQLITE_NOMEM_BKPT;
6326863387
}else{
6326963388
pCur->nOvflAlloc = nOvfl*2;
6327063389
pCur->aOverflow = aNew;
6327163390
}
6327263391
}
63273
- if( rc==SQLITE_OK ){
63274
- memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
63275
- pCur->curFlags |= BTCF_ValidOvfl;
63392
+ memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
63393
+ pCur->curFlags |= BTCF_ValidOvfl;
63394
+ }else{
63395
+ /* If the overflow page-list cache has been allocated and the
63396
+ ** entry for the first required overflow page is valid, skip
63397
+ ** directly to it.
63398
+ */
63399
+ if( pCur->aOverflow[offset/ovflSize] ){
63400
+ iIdx = (offset/ovflSize);
63401
+ nextPage = pCur->aOverflow[iIdx];
63402
+ offset = (offset%ovflSize);
6327663403
}
6327763404
}
6327863405
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
-
63406
+ assert( rc==SQLITE_OK && amt>0 );
63407
+ while( nextPage ){
6329363408
/* 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
- }
63409
+ assert( pCur->aOverflow[iIdx]==0
63410
+ || pCur->aOverflow[iIdx]==nextPage
63411
+ || CORRUPT_DB );
63412
+ pCur->aOverflow[iIdx] = nextPage;
6330063413
6330163414
if( offset>=ovflSize ){
6330263415
/* The only reason to read this page is to obtain the page
6330363416
** number for the next page in the overflow chain. The page
6330463417
** data is not required. So first try to lookup the overflow
6330563418
** page-list cache, if any, then fall back to the getOverflowPage()
6330663419
** 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.
6331063420
*/
63311
- assert( eOp!=2 );
6331263421
assert( pCur->curFlags & BTCF_ValidOvfl );
6331363422
assert( pCur->pBtree->db==pBt->db );
6331463423
if( pCur->aOverflow[iIdx+1] ){
6331563424
nextPage = pCur->aOverflow[iIdx+1];
6331663425
}else{
@@ -63320,11 +63429,11 @@
6332063429
}else{
6332163430
/* Need to read this page properly. It contains some of the
6332263431
** range of data that is being read (eOp==0) or written (eOp!=0).
6332363432
*/
6332463433
#ifdef SQLITE_DIRECT_OVERFLOW_READ
63325
- sqlite3_file *fd;
63434
+ sqlite3_file *fd; /* File from which to do direct overflow read */
6332663435
#endif
6332763436
int a = amt;
6332863437
if( a + offset > ovflSize ){
6332963438
a = ovflSize - offset;
6333063439
}
@@ -63332,31 +63441,29 @@
6333263441
#ifdef SQLITE_DIRECT_OVERFLOW_READ
6333363442
/* If all the following are true:
6333463443
**
6333563444
** 1) this is a read operation, and
6333663445
** 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
63446
+ ** 3) there is no open write-transaction, and
63447
+ ** 4) the database is file-backed, and
63448
+ ** 5) the page is not in the WAL file
63449
+ ** 6) at least 4 bytes have already been read into the output buffer
6334263450
**
6334363451
** then data can be read directly from the database file into the
6334463452
** output buffer, bypassing the page-cache altogether. This speeds
6334563453
** up loading large records that span many overflow pages.
6334663454
*/
63347
- if( (eOp&0x01)==0 /* (1) */
63455
+ if( eOp==0 /* (1) */
6334863456
&& 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) */
63457
+ && pBt->inTransaction==TRANS_READ /* (3) */
63458
+ && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (4) */
63459
+ && 0==sqlite3PagerUseWal(pBt->pPager, nextPage) /* (5) */
63460
+ && &pBuf[-4]>=pBufStart /* (6) */
6335463461
){
6335563462
u8 aSave[4];
6335663463
u8 *aWrite = &pBuf[-4];
63357
- assert( aWrite>=pBufStart ); /* hence (7) */
63464
+ assert( aWrite>=pBufStart ); /* due to (6) */
6335863465
memcpy(aSave, aWrite, 4);
6335963466
rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
6336063467
nextPage = get4byte(aWrite);
6336163468
memcpy(aWrite, aSave, 4);
6336263469
}else
@@ -63363,28 +63470,31 @@
6336363470
#endif
6336463471
6336563472
{
6336663473
DbPage *pDbPage;
6336763474
rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
63368
- ((eOp&0x01)==0 ? PAGER_GET_READONLY : 0)
63475
+ (eOp==0 ? PAGER_GET_READONLY : 0)
6336963476
);
6337063477
if( rc==SQLITE_OK ){
6337163478
aPayload = sqlite3PagerGetData(pDbPage);
6337263479
nextPage = get4byte(aPayload);
63373
- rc = copyPayload(&aPayload[offset+4], pBuf, a, (eOp&0x01), pDbPage);
63480
+ rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
6337463481
sqlite3PagerUnref(pDbPage);
6337563482
offset = 0;
6337663483
}
6337763484
}
6337863485
amt -= a;
63486
+ if( amt==0 ) return rc;
6337963487
pBuf += a;
6338063488
}
63489
+ if( rc ) break;
63490
+ iIdx++;
6338163491
}
6338263492
}
6338363493
6338463494
if( rc==SQLITE_OK && amt>0 ){
63385
- return SQLITE_CORRUPT_BKPT;
63495
+ return SQLITE_CORRUPT_BKPT; /* Overflow chain ends prematurely */
6338663496
}
6338763497
return rc;
6338863498
}
6338963499
6339063500
/*
@@ -63409,25 +63519,38 @@
6340963519
assert( pCur->eState==CURSOR_VALID );
6341063520
assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
6341163521
assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
6341263522
return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
6341363523
}
63524
+
63525
+/*
63526
+** This variant of sqlite3BtreePayload() works even if the cursor has not
63527
+** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read()
63528
+** interface.
63529
+*/
6341463530
#ifndef SQLITE_OMIT_INCRBLOB
63415
-SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
63531
+static SQLITE_NOINLINE int accessPayloadChecked(
63532
+ BtCursor *pCur,
63533
+ u32 offset,
63534
+ u32 amt,
63535
+ void *pBuf
63536
+){
6341663537
int rc;
6341763538
if ( pCur->eState==CURSOR_INVALID ){
6341863539
return SQLITE_ABORT;
6341963540
}
6342063541
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;
63542
+ rc = btreeRestoreCursorPosition(pCur);
63543
+ return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
63544
+}
63545
+SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
63546
+ if( pCur->eState==CURSOR_VALID ){
63547
+ assert( cursorOwnsBtShared(pCur) );
63548
+ return accessPayload(pCur, offset, amt, pBuf, 0);
63549
+ }else{
63550
+ return accessPayloadChecked(pCur, offset, amt, pBuf);
63551
+ }
6342963552
}
6343063553
#endif /* SQLITE_OMIT_INCRBLOB */
6343163554
6343263555
/*
6343363556
** Return a pointer to payload information from the entry that the
@@ -63829,13 +63952,30 @@
6382963952
){
6383063953
if( pCur->info.nKey==intKey ){
6383163954
*pRes = 0;
6383263955
return SQLITE_OK;
6383363956
}
63834
- if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKey<intKey ){
63835
- *pRes = -1;
63836
- return SQLITE_OK;
63957
+ if( pCur->info.nKey<intKey ){
63958
+ if( (pCur->curFlags & BTCF_AtLast)!=0 ){
63959
+ *pRes = -1;
63960
+ return SQLITE_OK;
63961
+ }
63962
+ /* If the requested key is one more than the previous key, then
63963
+ ** try to get there using sqlite3BtreeNext() rather than a full
63964
+ ** binary search. This is an optimization only. The correct answer
63965
+ ** is still obtained without this ase, only a little more slowely */
63966
+ if( pCur->info.nKey+1==intKey && !pCur->skipNext ){
63967
+ *pRes = 0;
63968
+ rc = sqlite3BtreeNext(pCur, pRes);
63969
+ if( rc ) return rc;
63970
+ if( *pRes==0 ){
63971
+ getCellInfo(pCur);
63972
+ if( pCur->info.nKey==intKey ){
63973
+ return SQLITE_OK;
63974
+ }
63975
+ }
63976
+ }
6383763977
}
6383863978
}
6383963979
6384063980
if( pIdxKey ){
6384163981
xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
@@ -63967,11 +64107,12 @@
6396764107
if( pCellKey==0 ){
6396864108
rc = SQLITE_NOMEM_BKPT;
6396964109
goto moveto_finish;
6397064110
}
6397164111
pCur->aiIdx[pCur->iPage] = (u16)idx;
63972
- rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 2);
64112
+ rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
64113
+ pCur->curFlags &= ~BTCF_ValidOvfl;
6397364114
if( rc ){
6397464115
sqlite3_free(pCellKey);
6397564116
goto moveto_finish;
6397664117
}
6397764118
c = xRecordCompare(nCell, pCellKey, pIdxKey);
@@ -66010,11 +66151,10 @@
6601066151
*/
6601166152
usableSpace = pBt->usableSize - 12 + leafCorrection;
6601266153
for(i=0; i<nOld; i++){
6601366154
MemPage *p = apOld[i];
6601466155
szNew[i] = usableSpace - p->nFree;
66015
- if( szNew[i]<0 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
6601666156
for(j=0; j<p->nOverflow; j++){
6601766157
szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
6601866158
}
6601966159
cntNew[i] = cntOld[i];
6602066160
}
@@ -66689,11 +66829,11 @@
6668966829
** to decode the key.
6669066830
*/
6669166831
SQLITE_PRIVATE int sqlite3BtreeInsert(
6669266832
BtCursor *pCur, /* Insert data into the table of this cursor */
6669366833
const BtreePayload *pX, /* Content of the row to be inserted */
66694
- int appendBias, /* True if this is likely an append */
66834
+ int flags, /* True if this is likely an append */
6669566835
int seekResult /* Result of prior MovetoUnpacked() call */
6669666836
){
6669766837
int rc;
6669866838
int loc = seekResult; /* -1: before desired location +1: after */
6669966839
int szNew = 0;
@@ -66701,10 +66841,12 @@
6670166841
MemPage *pPage;
6670266842
Btree *p = pCur->pBtree;
6670366843
BtShared *pBt = p->pBt;
6670466844
unsigned char *oldCell;
6670566845
unsigned char *newCell = 0;
66846
+
66847
+ assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND))==flags );
6670666848
6670766849
if( pCur->eState==CURSOR_FAULT ){
6670866850
assert( pCur->skipNext!=SQLITE_OK );
6670966851
return pCur->skipNext;
6671066852
}
@@ -66742,23 +66884,28 @@
6674266884
assert( pX->pKey==0 );
6674366885
/* If this is an insert into a table b-tree, invalidate any incrblob
6674466886
** cursors open on the row being replaced */
6674566887
invalidateIncrblobCursors(p, pX->nKey, 0);
6674666888
66889
+ /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
66890
+ ** to a row with the same key as the new entry being inserted. */
66891
+ assert( (flags & BTREE_SAVEPOSITION)==0 ||
66892
+ ((pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey) );
66893
+
6674766894
/* If the cursor is currently on the last row and we are appending a
6674866895
** new row onto the end, set the "loc" to avoid an unnecessary
6674966896
** btreeMoveto() call */
6675066897
if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
6675166898
loc = 0;
6675266899
}else if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey>0
6675366900
&& pCur->info.nKey==pX->nKey-1 ){
6675466901
loc = -1;
6675566902
}else if( loc==0 ){
66756
- rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, appendBias, &loc);
66903
+ rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
6675766904
if( rc ) return rc;
6675866905
}
66759
- }else if( loc==0 ){
66906
+ }else if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
6676066907
if( pX->nMem ){
6676166908
UnpackedRecord r;
6676266909
r.pKeyInfo = pCur->pKeyInfo;
6676366910
r.aMem = pX->aMem;
6676466911
r.nField = pX->nMem;
@@ -66765,13 +66912,13 @@
6676566912
r.default_rc = 0;
6676666913
r.errCode = 0;
6676766914
r.r1 = 0;
6676866915
r.r2 = 0;
6676966916
r.eqSeen = 0;
66770
- rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, appendBias, &loc);
66917
+ rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc);
6677166918
}else{
66772
- rc = btreeMoveto(pCur, pX->pKey, pX->nKey, appendBias, &loc);
66919
+ rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
6677366920
}
6677466921
if( rc ) return rc;
6677566922
}
6677666923
assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
6677766924
@@ -66855,10 +67002,24 @@
6685567002
** fails. Internal data structure corruption will result otherwise.
6685667003
** Also, set the cursor state to invalid. This stops saveCursorPosition()
6685767004
** from trying to save the current position of the cursor. */
6685867005
pCur->apPage[pCur->iPage]->nOverflow = 0;
6685967006
pCur->eState = CURSOR_INVALID;
67007
+ if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){
67008
+ rc = moveToRoot(pCur);
67009
+ if( pCur->pKeyInfo ){
67010
+ assert( pCur->pKey==0 );
67011
+ pCur->pKey = sqlite3Malloc( pX->nKey );
67012
+ if( pCur->pKey==0 ){
67013
+ rc = SQLITE_NOMEM;
67014
+ }else{
67015
+ memcpy(pCur->pKey, pX->pKey, pX->nKey);
67016
+ }
67017
+ }
67018
+ pCur->eState = CURSOR_REQUIRESEEK;
67019
+ pCur->nKey = pX->nKey;
67020
+ }
6686067021
}
6686167022
assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
6686267023
6686367024
end_insert:
6686467025
return rc;
@@ -71765,11 +71926,11 @@
7176571926
sqlite3VdbeGetOp(p,addr)->p2 = val;
7176671927
}
7176771928
SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
7176871929
sqlite3VdbeGetOp(p,addr)->p3 = val;
7176971930
}
71770
-SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 p5){
71931
+SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
7177171932
assert( p->nOp>0 || p->db->mallocFailed );
7177271933
if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
7177371934
}
7177471935
7177571936
/*
@@ -73479,64 +73640,63 @@
7347973640
** statement transaction is committed.
7348073641
**
7348173642
** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
7348273643
** Otherwise SQLITE_OK.
7348373644
*/
73484
-SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
73645
+static SQLITE_NOINLINE int vdbeCloseStatement(Vdbe *p, int eOp){
7348573646
sqlite3 *const db = p->db;
7348673647
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
-}
73648
+ int i;
73649
+ const int iSavepoint = p->iStatement-1;
73650
+
73651
+ assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
73652
+ assert( db->nStatement>0 );
73653
+ assert( p->iStatement==(db->nStatement+db->nSavepoint) );
73654
+
73655
+ for(i=0; i<db->nDb; i++){
73656
+ int rc2 = SQLITE_OK;
73657
+ Btree *pBt = db->aDb[i].pBt;
73658
+ if( pBt ){
73659
+ if( eOp==SAVEPOINT_ROLLBACK ){
73660
+ rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
73661
+ }
73662
+ if( rc2==SQLITE_OK ){
73663
+ rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
73664
+ }
73665
+ if( rc==SQLITE_OK ){
73666
+ rc = rc2;
73667
+ }
73668
+ }
73669
+ }
73670
+ db->nStatement--;
73671
+ p->iStatement = 0;
73672
+
73673
+ if( rc==SQLITE_OK ){
73674
+ if( eOp==SAVEPOINT_ROLLBACK ){
73675
+ rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
73676
+ }
73677
+ if( rc==SQLITE_OK ){
73678
+ rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
73679
+ }
73680
+ }
73681
+
73682
+ /* If the statement transaction is being rolled back, also restore the
73683
+ ** database handles deferred constraint counter to the value it had when
73684
+ ** the statement transaction was opened. */
73685
+ if( eOp==SAVEPOINT_ROLLBACK ){
73686
+ db->nDeferredCons = p->nStmtDefCons;
73687
+ db->nDeferredImmCons = p->nStmtDefImmCons;
73688
+ }
73689
+ return rc;
73690
+}
73691
+SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
73692
+ if( p->db->nStatement && p->iStatement ){
73693
+ return vdbeCloseStatement(p, eOp);
73694
+ }
73695
+ return SQLITE_OK;
73696
+}
73697
+
7353873698
7353973699
/*
7354073700
** This function is called when a transaction opened by the database
7354173701
** handle associated with the VM passed as an argument is about to be
7354273702
** committed. If there are outstanding deferred foreign key constraint
@@ -75567,14 +75727,14 @@
7556775727
** structure itself, using sqlite3DbFree().
7556875728
**
7556975729
** This function is used to free UnpackedRecord structures allocated by
7557075730
** the vdbeUnpackRecord() function found in vdbeapi.c.
7557175731
*/
75572
-static void vdbeFreeUnpacked(sqlite3 *db, UnpackedRecord *p){
75732
+static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
7557375733
if( p ){
7557475734
int i;
75575
- for(i=0; i<p->nField; i++){
75735
+ for(i=0; i<nField; i++){
7557675736
Mem *pMem = &p->aMem[i];
7557775737
if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
7557875738
}
7557975739
sqlite3DbFree(db, p);
7558075740
}
@@ -75603,14 +75763,19 @@
7560375763
const char *zTbl = pTab->zName;
7560475764
static const u8 fakeSortOrder = 0;
7560575765
7560675766
assert( db->pPreUpdate==0 );
7560775767
memset(&preupdate, 0, sizeof(PreUpdate));
75608
- if( op==SQLITE_UPDATE ){
75609
- iKey2 = v->aMem[iReg].u.i;
75768
+ if( HasRowid(pTab)==0 ){
75769
+ iKey1 = iKey2 = 0;
75770
+ preupdate.pPk = sqlite3PrimaryKeyIndex(pTab);
7561075771
}else{
75611
- iKey2 = iKey1;
75772
+ if( op==SQLITE_UPDATE ){
75773
+ iKey2 = v->aMem[iReg].u.i;
75774
+ }else{
75775
+ iKey2 = iKey1;
75776
+ }
7561275777
}
7561375778
7561475779
assert( pCsr->nField==pTab->nCol
7561575780
|| (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
7561675781
);
@@ -75629,12 +75794,12 @@
7562975794
7563075795
db->pPreUpdate = &preupdate;
7563175796
db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
7563275797
db->pPreUpdate = 0;
7563375798
sqlite3DbFree(db, preupdate.aRecord);
75634
- vdbeFreeUnpacked(db, preupdate.pUnpacked);
75635
- vdbeFreeUnpacked(db, preupdate.pNewUnpacked);
75799
+ vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pUnpacked);
75800
+ vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pNewUnpacked);
7563675801
if( preupdate.aNew ){
7563775802
int i;
7563875803
for(i=0; i<pCsr->nField; i++){
7563975804
sqlite3VdbeMemRelease(&preupdate.aNew[i]);
7564075805
}
@@ -77305,18 +77470,22 @@
7730577470
** This function is called from within a pre-update callback to retrieve
7730677471
** a field of the row currently being updated or deleted.
7730777472
*/
7730877473
SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
7730977474
PreUpdate *p = db->pPreUpdate;
77475
+ Mem *pMem;
7731077476
int rc = SQLITE_OK;
7731177477
7731277478
/* Test that this call is being made from within an SQLITE_DELETE or
7731377479
** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
7731477480
if( !p || p->op==SQLITE_INSERT ){
7731577481
rc = SQLITE_MISUSE_BKPT;
7731677482
goto preupdate_old_out;
7731777483
}
77484
+ if( p->pPk ){
77485
+ iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
77486
+ }
7731877487
if( iIdx>=p->pCsr->nField || iIdx<0 ){
7731977488
rc = SQLITE_RANGE;
7732077489
goto preupdate_old_out;
7732177490
}
7732277491
@@ -77338,21 +77507,18 @@
7733877507
goto preupdate_old_out;
7733977508
}
7734077509
p->aRecord = aRec;
7734177510
}
7734277511
77343
- if( iIdx>=p->pUnpacked->nField ){
77512
+ pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
77513
+ if( iIdx==p->pTab->iPKey ){
77514
+ sqlite3VdbeMemSetInt64(pMem, p->iKey1);
77515
+ }else if( iIdx>=p->pUnpacked->nField ){
7734477516
*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
- }
77517
+ }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
77518
+ if( pMem->flags & MEM_Int ){
77519
+ sqlite3VdbeMemRealify(pMem);
7735477520
}
7735577521
}
7735677522
7735777523
preupdate_old_out:
7735877524
sqlite3Error(db, rc);
@@ -77401,10 +77567,13 @@
7740177567
7740277568
if( !p || p->op==SQLITE_DELETE ){
7740377569
rc = SQLITE_MISUSE_BKPT;
7740477570
goto preupdate_new_out;
7740577571
}
77572
+ if( p->pPk && p->op!=SQLITE_UPDATE ){
77573
+ iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
77574
+ }
7740677575
if( iIdx>=p->pCsr->nField || iIdx<0 ){
7740777576
rc = SQLITE_RANGE;
7740877577
goto preupdate_new_out;
7740977578
}
7741077579
@@ -77421,17 +77590,15 @@
7742177590
rc = SQLITE_NOMEM;
7742277591
goto preupdate_new_out;
7742377592
}
7742477593
p->pNewUnpacked = pUnpack;
7742577594
}
77426
- if( iIdx>=pUnpack->nField ){
77595
+ pMem = &pUnpack->aMem[iIdx];
77596
+ if( iIdx==p->pTab->iPKey ){
77597
+ sqlite3VdbeMemSetInt64(pMem, p->iKey2);
77598
+ }else if( iIdx>=pUnpack->nField ){
7742777599
pMem = (sqlite3_value *)columnNullValue();
77428
- }else{
77429
- pMem = &pUnpack->aMem[iIdx];
77430
- if( iIdx==p->pTab->iPKey ){
77431
- sqlite3VdbeMemSetInt64(pMem, p->iKey2);
77432
- }
7743377600
}
7743477601
}else{
7743577602
/* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
7743677603
** value. Make a copy of the cell contents and return a pointer to it.
7743777604
** It is not safe to return a pointer to the memory cell itself as the
@@ -78404,12 +78571,10 @@
7840478571
Mem *aMem = p->aMem; /* Copy of p->aMem */
7840578572
Mem *pIn1 = 0; /* 1st input operand */
7840678573
Mem *pIn2 = 0; /* 2nd input operand */
7840778574
Mem *pIn3 = 0; /* 3rd input operand */
7840878575
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 */
7841178576
#ifdef VDBE_PROFILE
7841278577
u64 start; /* CPU clock count at start of opcode */
7841378578
#endif
7841478579
/*** INSERT STACK UNION HERE ***/
7841578580
@@ -78420,11 +78585,10 @@
7842078585
** sqlite3_column_text16() failed. */
7842178586
goto no_mem;
7842278587
}
7842378588
assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
7842478589
assert( p->bIsReader || p->readOnly!=0 );
78425
- p->rc = SQLITE_OK;
7842678590
p->iCurrentTime = 0;
7842778591
assert( p->explain==0 );
7842878592
p->pResultSet = 0;
7842978593
db->busyHandler.nBusy = 0;
7843078594
if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
@@ -78781,11 +78945,10 @@
7878178945
pFrame = p->pFrame;
7878278946
p->pFrame = pFrame->pParent;
7878378947
p->nFrame--;
7878478948
sqlite3VdbeSetChanges(db, p->nChange);
7878578949
pcx = sqlite3VdbeFrameRestore(pFrame);
78786
- lastRowid = db->lastRowid;
7878778950
if( pOp->p2==OE_Ignore ){
7878878951
/* Instruction pcx is the OP_Program that invoked the sub-program
7878978952
** currently being halted. If the p2 instruction of this OP_Halt
7879078953
** instruction is set to OE_Ignore, then the sub-program is throwing
7879178954
** an IGNORE exception. In this case jump to the address specified
@@ -79016,11 +79179,11 @@
7901679179
assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
7901779180
pVar = &p->aVar[pOp->p1 - 1];
7901879181
if( sqlite3VdbeMemTooBig(pVar) ){
7901979182
goto too_big;
7902079183
}
79021
- pOut = out2Prerelease(p, pOp);
79184
+ pOut = &aMem[pOp->p2];
7902279185
sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
7902379186
UPDATE_MAX_BLOBSIZE(pOut);
7902479187
break;
7902579188
}
7902679189
@@ -79503,13 +79666,11 @@
7950379666
REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
7950479667
}
7950579668
#endif
7950679669
MemSetTypeFlag(pCtx->pOut, MEM_Null);
7950779670
pCtx->fErrorOrAux = 0;
79508
- db->lastRowid = lastRowid;
7950979671
(*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
79510
- lastRowid = db->lastRowid; /* Remember rowid changes made by xSFunc */
7951179672
7951279673
/* If the function returned an error, throw an exception */
7951379674
if( pCtx->fErrorOrAux ){
7951479675
if( pCtx->isError ){
7951579676
sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
@@ -79961,12 +80122,12 @@
7996180122
}
7996280123
7996380124
7996480125
/* Opcode: Permutation * * * P4 *
7996580126
**
79966
-** Set the permutation used by the OP_Compare operator to be the array
79967
-** of integers in P4.
80127
+** Set the permutation used by the OP_Compare operator in the next
80128
+** instruction. The permutation is stored in the P4 operand.
7996880129
**
7996980130
** The permutation is only valid until the next OP_Compare that has
7997080131
** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
7997180132
** occur immediately prior to the OP_Compare.
7997280133
**
@@ -79974,11 +80135,12 @@
7997480135
** and does not become part of the permutation.
7997580136
*/
7997680137
case OP_Permutation: {
7997780138
assert( pOp->p4type==P4_INTARRAY );
7997880139
assert( pOp->p4.ai );
79979
- aPermute = pOp->p4.ai + 1;
80140
+ assert( pOp[1].opcode==OP_Compare );
80141
+ assert( pOp[1].p5 & OPFLAG_PERMUTE );
7998080142
break;
7998180143
}
7998280144
7998380145
/* Opcode: Compare P1 P2 P3 P4 P5
7998480146
** Synopsis: r[P1@P3] <-> r[P2@P3]
@@ -80007,12 +80169,21 @@
8000780169
int p2;
8000880170
const KeyInfo *pKeyInfo;
8000980171
int idx;
8001080172
CollSeq *pColl; /* Collating sequence to use on this term */
8001180173
int bRev; /* True for DESCENDING sort order */
80174
+ int *aPermute; /* The permutation */
8001280175
80013
- if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
80176
+ if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
80177
+ aPermute = 0;
80178
+ }else{
80179
+ assert( pOp>aOp );
80180
+ assert( pOp[-1].opcode==OP_Permutation );
80181
+ assert( pOp[-1].p4type==P4_INTARRAY );
80182
+ aPermute = pOp[-1].p4.ai + 1;
80183
+ assert( aPermute!=0 );
80184
+ }
8001480185
n = pOp->p3;
8001580186
pKeyInfo = pOp->p4.pKeyInfo;
8001680187
assert( n>0 );
8001780188
assert( pKeyInfo!=0 );
8001880189
p1 = pOp->p1;
@@ -80041,11 +80212,10 @@
8004180212
if( iCompare ){
8004280213
if( bRev ) iCompare = -iCompare;
8004380214
break;
8004480215
}
8004580216
}
80046
- aPermute = 0;
8004780217
break;
8004880218
}
8004980219
8005080220
/* Opcode: Jump P1 P2 P3 * *
8005180221
**
@@ -80597,10 +80767,24 @@
8059780767
do{
8059880768
applyAffinity(pRec++, *(zAffinity++), encoding);
8059980769
assert( zAffinity[0]==0 || pRec<=pLast );
8060080770
}while( zAffinity[0] );
8060180771
}
80772
+
80773
+#ifdef SQLITE_ENABLE_NULL_TRIM
80774
+ /* NULLs can be safely trimmed from the end of the record, as long as
80775
+ ** as the schema format is 2 or more and none of the omitted columns
80776
+ ** have a non-NULL default value. Also, the record must be left with
80777
+ ** at least one field. If P5>0 then it will be one more than the
80778
+ ** index of the right-most column with a non-NULL default value */
80779
+ if( pOp->p5 ){
80780
+ while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
80781
+ pLast--;
80782
+ nField--;
80783
+ }
80784
+ }
80785
+#endif
8060280786
8060380787
/* Loop through the elements that will make up the record to figure
8060480788
** out how much space is required for the new record.
8060580789
*/
8060680790
pRec = pLast;
@@ -82187,11 +82371,11 @@
8218782371
assert( memIsValid(pData) );
8218882372
pC = p->apCsr[pOp->p1];
8218982373
assert( pC!=0 );
8219082374
assert( pC->eCurType==CURTYPE_BTREE );
8219182375
assert( pC->uc.pCursor!=0 );
82192
- assert( pC->isTable );
82376
+ assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
8219382377
assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
8219482378
REGISTER_TRACE(pOp->p2, pData);
8219582379
8219682380
if( pOp->opcode==OP_Insert ){
8219782381
pKey = &aMem[pOp->p3];
@@ -82203,18 +82387,17 @@
8220382387
assert( pOp->opcode==OP_InsertInt );
8220482388
x.nKey = pOp->p3;
8220582389
}
8220682390
8220782391
if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
82208
- assert( pC->isTable );
8220982392
assert( pC->iDb>=0 );
8221082393
zDb = db->aDb[pC->iDb].zDbSName;
8221182394
pTab = pOp->p4.pTab;
82212
- assert( HasRowid(pTab) );
82395
+ assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
8221382396
op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
8221482397
}else{
82215
- pTab = 0; /* Not needed. Silence a comiler warning. */
82398
+ pTab = 0; /* Not needed. Silence a compiler warning. */
8221682399
zDb = 0; /* Not needed. Silence a compiler warning. */
8221782400
}
8221882401
8221982402
#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
8222082403
/* Invoke the pre-update hook, if any */
@@ -82222,14 +82405,15 @@
8222282405
&& pOp->p4type==P4_TABLE
8222382406
&& !(pOp->p5 & OPFLAG_ISUPDATE)
8222482407
){
8222582408
sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey, pOp->p2);
8222682409
}
82410
+ if( pOp->p5 & OPFLAG_ISNOOP ) break;
8222782411
#endif
8222882412
8222982413
if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
82230
- if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = x.nKey;
82414
+ if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
8223182415
if( pData->flags & MEM_Null ){
8223282416
x.pData = 0;
8223382417
x.nData = 0;
8223482418
}else{
8223582419
assert( pData->flags & (MEM_Blob|MEM_Str) );
@@ -82242,11 +82426,11 @@
8224282426
}else{
8224382427
x.nZero = 0;
8224482428
}
8224582429
x.pKey = 0;
8224682430
rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
82247
- (pOp->p5 & OPFLAG_APPEND)!=0, seekResult
82431
+ (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
8224882432
);
8224982433
pC->deferredMoveto = 0;
8225082434
pC->cacheStatus = CACHE_STALE;
8225182435
8225282436
/* Invoke the update-hook if required. */
@@ -82334,12 +82518,15 @@
8233482518
pTab = 0; /* Not needed. Silence a compiler warning. */
8233582519
}
8233682520
8233782521
#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
8233882522
/* 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) );
82523
+ if( db->xPreUpdateCallback && pOp->p4.pTab ){
82524
+ assert( !(opflags & OPFLAG_ISUPDATE)
82525
+ || HasRowid(pTab)==0
82526
+ || (aMem[pOp->p3].flags & MEM_Int)
82527
+ );
8234182528
sqlite3VdbePreUpdateHook(p, pC,
8234282529
(opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
8234382530
zDb, pTab, pC->movetoTarget,
8234482531
pOp->p3
8234582532
);
@@ -82453,11 +82640,11 @@
8245382640
if( rc ) goto abort_due_to_error;
8245482641
p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
8245582642
break;
8245682643
}
8245782644
82458
-/* Opcode: RowData P1 P2 * * *
82645
+/* Opcode: RowData P1 P2 P3 * *
8245982646
** Synopsis: r[P2]=data
8246082647
**
8246182648
** Write into register P2 the complete row content for the row at
8246282649
** which cursor P1 is currently pointing.
8246382650
** There is no interpretation of the data.
@@ -82467,18 +82654,30 @@
8246782654
** If cursor P1 is an index, then the content is the key of the row.
8246882655
** If cursor P2 is a table, then the content extracted is the data.
8246982656
**
8247082657
** If the P1 cursor must be pointing to a valid row (not a NULL row)
8247182658
** of a real table, not a pseudo-table.
82659
+**
82660
+** If P3!=0 then this opcode is allowed to make an ephermeral pointer
82661
+** into the database page. That means that the content of the output
82662
+** register will be invalidated as soon as the cursor moves - including
82663
+** moves caused by other cursors that "save" the the current cursors
82664
+** position in order that they can write to the same table. If P3==0
82665
+** then a copy of the data is made into memory. P3!=0 is faster, but
82666
+** P3==0 is safer.
82667
+**
82668
+** If P3!=0 then the content of the P2 register is unsuitable for use
82669
+** in OP_Result and any OP_Result will invalidate the P2 register content.
82670
+** The P2 register content is invalidated by opcodes like OP_Function or
82671
+** by any use of another cursor pointing to the same table.
8247282672
*/
8247382673
case OP_RowData: {
8247482674
VdbeCursor *pC;
8247582675
BtCursor *pCrsr;
8247682676
u32 n;
8247782677
82478
- pOut = &aMem[pOp->p2];
82479
- memAboutToChange(p, pOut);
82678
+ pOut = out2Prerelease(p, pOp);
8248082679
8248182680
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
8248282681
pC = p->apCsr[pOp->p1];
8248382682
assert( pC!=0 );
8248482683
assert( pC->eCurType==CURTYPE_BTREE );
@@ -82505,18 +82704,13 @@
8250582704
n = sqlite3BtreePayloadSize(pCrsr);
8250682705
if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
8250782706
goto too_big;
8250882707
}
8250982708
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);
82709
+ rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut);
8251682710
if( rc ) goto abort_due_to_error;
82517
- pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
82711
+ if( !pOp->p3 ) Deephemeralize(pOut);
8251882712
UPDATE_MAX_BLOBSIZE(pOut);
8251982713
REGISTER_TRACE(pOp->p2, pOut);
8252082714
break;
8252182715
}
8252282716
@@ -82900,11 +83094,11 @@
8290083094
x.nKey = pIn2->n;
8290183095
x.pKey = pIn2->z;
8290283096
x.aMem = aMem + pOp->p3;
8290383097
x.nMem = (u16)pOp->p4.i;
8290483098
rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
82905
- (pOp->p5 & OPFLAG_APPEND)!=0,
83099
+ (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)),
8290683100
((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
8290783101
);
8290883102
assert( pC->deferredMoveto==0 );
8290983103
pC->cacheStatus = CACHE_STALE;
8291083104
}
@@ -83022,11 +83216,10 @@
8302283216
pTabCur->aAltMap = pOp->p4.ai;
8302383217
pTabCur->pAltCursor = pC;
8302483218
}else{
8302583219
pOut = out2Prerelease(p, pOp);
8302683220
pOut->u.i = rowid;
83027
- pOut->flags = MEM_Int;
8302883221
}
8302983222
}else{
8303083223
assert( pOp->opcode==OP_IdxRowid );
8303183224
sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
8303283225
}
@@ -83664,11 +83857,11 @@
8366483857
assert( (int)(pOp - aOp)==pFrame->pc );
8366583858
}
8366683859
8366783860
p->nFrame++;
8366883861
pFrame->pParent = p->pFrame;
83669
- pFrame->lastRowid = lastRowid;
83862
+ pFrame->lastRowid = db->lastRowid;
8367083863
pFrame->nChange = p->nChange;
8367183864
pFrame->nDbChange = p->db->nChange;
8367283865
assert( pFrame->pAuxData==0 );
8367383866
pFrame->pAuxData = p->pAuxData;
8367483867
p->pAuxData = 0;
@@ -84605,11 +84798,11 @@
8460584798
rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
8460684799
db->vtabOnConflict = vtabOnConflict;
8460784800
sqlite3VtabImportErrmsg(p, pVtab);
8460884801
if( rc==SQLITE_OK && pOp->p1 ){
8460984802
assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
84610
- db->lastRowid = lastRowid = rowid;
84803
+ db->lastRowid = rowid;
8461184804
}
8461284805
if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
8461384806
if( pOp->p5==OE_Ignore ){
8461484807
rc = SQLITE_OK;
8461584808
}else{
@@ -84841,11 +85034,10 @@
8484185034
8484285035
/* This is the only way out of this procedure. We have to
8484385036
** release the mutexes on btrees that were acquired at the
8484485037
** top. */
8484585038
vdbe_return:
84846
- db->lastRowid = lastRowid;
8484785039
testcase( nVmStep>0 );
8484885040
p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
8484985041
sqlite3VdbeLeave(p);
8485085042
assert( rc!=SQLITE_OK || nExtraDelete==0
8485185043
|| sqlite3_strlike("DELETE%",p->zSql,0)!=0
@@ -84905,14 +85097,13 @@
8490585097
/*
8490685098
** Valid sqlite3_blob* handles point to Incrblob structures.
8490785099
*/
8490885100
typedef struct Incrblob Incrblob;
8490985101
struct Incrblob {
84910
- int flags; /* Copy of "flags" passed to sqlite3_blob_open() */
8491185102
int nByte; /* Size of open blob, in bytes */
8491285103
int iOffset; /* Byte offset of blob in cursor data */
84913
- int iCol; /* Table column this handle is open on */
85104
+ u16 iCol; /* Table column this handle is open on */
8491485105
BtCursor *pCsr; /* Cursor pointing at blob row */
8491585106
sqlite3_stmt *pStmt; /* Statement holding cursor open */
8491685107
sqlite3 *db; /* The associated database */
8491785108
char *zDb; /* Database name */
8491885109
Table *pTab; /* Table object */
@@ -84939,21 +85130,31 @@
8493985130
static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
8494085131
int rc; /* Error code */
8494185132
char *zErr = 0; /* Error message */
8494285133
Vdbe *v = (Vdbe *)p->pStmt;
8494385134
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.
85135
+ /* Set the value of register r[1] in the SQL statement to integer iRow.
85136
+ ** This is done directly as a performance optimization
8494785137
*/
84948
- assert( v->aVar[0].flags&MEM_Int );
84949
- v->aVar[0].u.i = iRow;
85138
+ v->aMem[1].flags = MEM_Int;
85139
+ v->aMem[1].u.i = iRow;
8495085140
84951
- rc = sqlite3_step(p->pStmt);
85141
+ /* If the statement has been run before (and is paused at the OP_ResultRow)
85142
+ ** then back it up to the point where it does the OP_SeekRowid. This could
85143
+ ** have been down with an extra OP_Goto, but simply setting the program
85144
+ ** counter is faster. */
85145
+ if( v->pc>3 ){
85146
+ v->pc = 3;
85147
+ rc = sqlite3VdbeExec(v);
85148
+ }else{
85149
+ rc = sqlite3_step(p->pStmt);
85150
+ }
8495285151
if( rc==SQLITE_ROW ){
8495385152
VdbeCursor *pC = v->apCsr[0];
84954
- u32 type = pC->aType[p->iCol];
85153
+ u32 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
85154
+ testcase( pC->nHdrParsed==p->iCol );
85155
+ testcase( pC->nHdrParsed==p->iCol+1 );
8495585156
if( type<12 ){
8495685157
zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
8495785158
type==0?"null": type==7?"real": "integer"
8495885159
);
8495985160
rc = SQLITE_ERROR;
@@ -84994,11 +85195,11 @@
8499485195
sqlite3* db, /* The database connection */
8499585196
const char *zDb, /* The attached database containing the blob */
8499685197
const char *zTable, /* The table containing the blob */
8499785198
const char *zColumn, /* The column containing the blob */
8499885199
sqlite_int64 iRow, /* The row containing the glob */
84999
- int flags, /* True -> read/write access, false -> read-only */
85200
+ int wrFlag, /* True -> read/write access, false -> read-only */
8500085201
sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
8500185202
){
8500285203
int nAttempt = 0;
8500385204
int iCol; /* Index of zColumn in row-record */
8500485205
int rc = SQLITE_OK;
@@ -85016,11 +85217,11 @@
8501685217
#ifdef SQLITE_ENABLE_API_ARMOR
8501785218
if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
8501885219
return SQLITE_MISUSE_BKPT;
8501985220
}
8502085221
#endif
85021
- flags = !!flags; /* flags = (flags ? 1 : 0); */
85222
+ wrFlag = !!wrFlag; /* wrFlag = (wrFlag ? 1 : 0); */
8502285223
8502385224
sqlite3_mutex_enter(db->mutex);
8502485225
8502585226
pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
8502685227
if( !pBlob ) goto blob_open_out;
@@ -85076,13 +85277,12 @@
8507685277
goto blob_open_out;
8507785278
}
8507885279
8507985280
/* If the value is being opened for writing, check that the
8508085281
** 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 ){
85282
+ */
85283
+ if( wrFlag ){
8508485284
const char *zFault = 0;
8508585285
Index *pIdx;
8508685286
#ifndef SQLITE_OMIT_FOREIGN_KEY
8508785287
if( db->flags&SQLITE_ForeignKeys ){
8508885288
/* Check that the column is not part of an FK child key definition. It
@@ -85139,22 +85339,21 @@
8513985339
*/
8514085340
static const int iLn = VDBE_OFFSET_LINENO(2);
8514185341
static const VdbeOpList openBlob[] = {
8514285342
{OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */
8514385343
{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 */
85344
+ /* blobSeekToRow() will initialize r[1] to the desired rowid */
85345
+ {OP_NotExists, 0, 5, 1}, /* 2: Seek the cursor to rowid=r[1] */
85346
+ {OP_Column, 0, 0, 1}, /* 3 */
85347
+ {OP_ResultRow, 1, 0, 0}, /* 4 */
85348
+ {OP_Halt, 0, 0, 0}, /* 5 */
8515085349
};
8515185350
Vdbe *v = (Vdbe *)pBlob->pStmt;
8515285351
int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
8515385352
VdbeOp *aOp;
8515485353
85155
- sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, flags,
85354
+ sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag,
8515685355
pTab->pSchema->schema_cookie,
8515785356
pTab->pSchema->iGeneration);
8515885357
sqlite3VdbeChangeP5(v, 1);
8515985358
aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
8516085359
@@ -85167,19 +85366,19 @@
8516785366
#ifdef SQLITE_OMIT_SHARED_CACHE
8516885367
aOp[0].opcode = OP_Noop;
8516985368
#else
8517085369
aOp[0].p1 = iDb;
8517185370
aOp[0].p2 = pTab->tnum;
85172
- aOp[0].p3 = flags;
85371
+ aOp[0].p3 = wrFlag;
8517385372
sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
8517485373
}
8517585374
if( db->mallocFailed==0 ){
8517685375
#endif
8517785376
8517885377
/* Remove either the OP_OpenWrite or OpenRead. Set the P2
8517985378
** parameter of the other to pTab->tnum. */
85180
- if( flags ) aOp[1].opcode = OP_OpenWrite;
85379
+ if( wrFlag ) aOp[1].opcode = OP_OpenWrite;
8518185380
aOp[1].p2 = pTab->tnum;
8518285381
aOp[1].p3 = iDb;
8518385382
8518485383
/* Configure the number of columns. Configure the cursor to
8518585384
** think that the table has one more column than it really
@@ -85188,27 +85387,25 @@
8518885387
** we can invoke OP_Column to fill in the vdbe cursors type
8518985388
** and offset cache without causing any IO.
8519085389
*/
8519185390
aOp[1].p4type = P4_INT32;
8519285391
aOp[1].p4.i = pTab->nCol+1;
85193
- aOp[4].p2 = pTab->nCol;
85392
+ aOp[3].p2 = pTab->nCol;
8519485393
85195
- pParse->nVar = 1;
85394
+ pParse->nVar = 0;
8519685395
pParse->nMem = 1;
8519785396
pParse->nTab = 1;
8519885397
sqlite3VdbeMakeReady(v, pParse);
8519985398
}
8520085399
}
8520185400
85202
- pBlob->flags = flags;
8520385401
pBlob->iCol = iCol;
8520485402
pBlob->db = db;
8520585403
sqlite3BtreeLeaveAll(db);
8520685404
if( db->mallocFailed ){
8520785405
goto blob_open_out;
8520885406
}
85209
- sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
8521085407
rc = blobSeekToRow(pBlob, iRow, &zErr);
8521185408
} while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
8521285409
8521385410
blob_open_out:
8521485411
if( rc==SQLITE_OK && db->mallocFailed==0 ){
@@ -88741,12 +88938,10 @@
8874188938
** This file contains routines used for walking the parser tree and
8874288939
** resolve all identifiers by associating them with a particular
8874388940
** table and column.
8874488941
*/
8874588942
/* #include "sqliteInt.h" */
88746
-/* #include <stdlib.h> */
88747
-/* #include <string.h> */
8874888943
8874988944
/*
8875088945
** Walk the expression tree pExpr and increase the aggregate function
8875188946
** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
8875288947
** This needs to occur when copying a TK_AGG_FUNCTION node from an
@@ -90501,11 +90696,11 @@
9050190696
aff = sqlite3ExprAffinity(pExpr->pLeft);
9050290697
if( pExpr->pRight ){
9050390698
aff = sqlite3CompareAffinity(pExpr->pRight, aff);
9050490699
}else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
9050590700
aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
90506
- }else if( NEVER(aff==0) ){
90701
+ }else if( aff==0 ){
9050790702
aff = SQLITE_AFF_BLOB;
9050890703
}
9050990704
return aff;
9051090705
}
9051190706
@@ -91237,21 +91432,27 @@
9123791432
int doAdd = 0;
9123891433
if( z[0]=='?' ){
9123991434
/* Wildcard of the form "?nnn". Convert "nnn" to an integer and
9124091435
** use it as the variable number */
9124191436
i64 i;
91242
- int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
91243
- x = (ynVar)i;
91437
+ int bOk;
91438
+ if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
91439
+ i = z[1]-'0'; /* The common case of ?N for a single digit N */
91440
+ bOk = 1;
91441
+ }else{
91442
+ bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
91443
+ }
9124491444
testcase( i==0 );
9124591445
testcase( i==1 );
9124691446
testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
9124791447
testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
9124891448
if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
9124991449
sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
9125091450
db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
9125191451
return;
9125291452
}
91453
+ x = (ynVar)i;
9125391454
if( x>pParse->nVar ){
9125491455
pParse->nVar = (int)x;
9125591456
doAdd = 1;
9125691457
}else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
9125791458
doAdd = 1;
@@ -91682,37 +91883,45 @@
9168291883
pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
9168391884
pNewItem->idx = pOldItem->idx;
9168491885
}
9168591886
return pNew;
9168691887
}
91687
-SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
91688
- Select *pNew, *pPrior;
91888
+SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *pDup, int flags){
91889
+ Select *pRet = 0;
91890
+ Select *pNext = 0;
91891
+ Select **pp = &pRet;
91892
+ Select *p;
91893
+
9168991894
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;
91895
+ for(p=pDup; p; p=p->pPrior){
91896
+ Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
91897
+ if( pNew==0 ) break;
91898
+ pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
91899
+ pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
91900
+ pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
91901
+ pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
91902
+ pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
91903
+ pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
91904
+ pNew->op = p->op;
91905
+ pNew->pNext = pNext;
91906
+ pNew->pPrior = 0;
91907
+ pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
91908
+ pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
91909
+ pNew->iLimit = 0;
91910
+ pNew->iOffset = 0;
91911
+ pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
91912
+ pNew->addrOpenEphm[0] = -1;
91913
+ pNew->addrOpenEphm[1] = -1;
91914
+ pNew->nSelectRow = p->nSelectRow;
91915
+ pNew->pWith = withDup(db, p->pWith);
91916
+ sqlite3SelectSetName(pNew, p->zSelName);
91917
+ *pp = pNew;
91918
+ pp = &pNew->pPrior;
91919
+ pNext = pNew;
91920
+ }
91921
+
91922
+ return pRet;
9171491923
}
9171591924
#else
9171691925
SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
9171791926
assert( p==0 );
9171891927
return 0;
@@ -91773,11 +91982,11 @@
9177391982
**
9177491983
** (a,b,c) = (expr1,expr2,expr3)
9177591984
** Or: (a,b,c) = (SELECT x,y,z FROM ....)
9177691985
**
9177791986
** 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
91987
+** expression list pList. In the case of a subquery on the RHS, append
9177991988
** TK_SELECT_COLUMN expressions.
9178091989
*/
9178191990
SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(
9178291991
Parse *pParse, /* Parsing context */
9178391992
ExprList *pList, /* List to which to append. Might be NULL */
@@ -93882,10 +94091,15 @@
9388294091
int i; /* Loop counter */
9388394092
sqlite3 *db = pParse->db; /* The database connection */
9388494093
u8 enc = ENC(db); /* The text encoding used by this database */
9388594094
CollSeq *pColl = 0; /* A collating sequence */
9388694095
94096
+ if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
94097
+ /* SQL functions can be expensive. So try to move constant functions
94098
+ ** out of the inner loop, even if that means an extra OP_Copy. */
94099
+ return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
94100
+ }
9388794101
assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
9388894102
if( ExprHasProperty(pExpr, EP_TokenOnly) ){
9388994103
pFarg = 0;
9389094104
}else{
9389194105
pFarg = pExpr->x.pList;
@@ -93929,10 +94143,26 @@
9392994143
*/
9393094144
if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
9393194145
assert( nFarg>=1 );
9393294146
return sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
9393394147
}
94148
+
94149
+#ifdef SQLITE_DEBUG
94150
+ /* The AFFINITY() function evaluates to a string that describes
94151
+ ** the type affinity of the argument. This is used for testing of
94152
+ ** the SQLite type logic.
94153
+ */
94154
+ if( pDef->funcFlags & SQLITE_FUNC_AFFINITY ){
94155
+ const char *azAff[] = { "blob", "text", "numeric", "integer", "real" };
94156
+ char aff;
94157
+ assert( nFarg==1 );
94158
+ aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
94159
+ sqlite3VdbeLoadString(v, target,
94160
+ aff ? azAff[aff-SQLITE_AFF_BLOB] : "none");
94161
+ return target;
94162
+ }
94163
+#endif
9393494164
9393594165
for(i=0; i<nFarg; i++){
9393694166
if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
9393794167
testcase( i==31 );
9393894168
constMask |= MASKBIT32(i);
@@ -94246,28 +94476,44 @@
9424694476
return inReg;
9424794477
}
9424894478
9424994479
/*
9425094480
** Factor out the code of the given expression to initialization time.
94481
+**
94482
+** If regDest>=0 then the result is always stored in that register and the
94483
+** result is not reusable. If regDest<0 then this routine is free to
94484
+** store the value whereever it wants. The register where the expression
94485
+** is stored is returned. When regDest<0, two identical expressions will
94486
+** code to the same register.
9425194487
*/
94252
-SQLITE_PRIVATE void sqlite3ExprCodeAtInit(
94488
+SQLITE_PRIVATE int sqlite3ExprCodeAtInit(
9425394489
Parse *pParse, /* Parsing context */
9425494490
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 */
94491
+ int regDest /* Store the value in this register */
9425794492
){
9425894493
ExprList *p;
9425994494
assert( ConstFactorOk(pParse) );
9426094495
p = pParse->pConstExpr;
94496
+ if( regDest<0 && p ){
94497
+ struct ExprList_item *pItem;
94498
+ int i;
94499
+ for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
94500
+ if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){
94501
+ return pItem->u.iConstExprReg;
94502
+ }
94503
+ }
94504
+ }
9426194505
pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
9426294506
p = sqlite3ExprListAppend(pParse, p, pExpr);
9426394507
if( p ){
9426494508
struct ExprList_item *pItem = &p->a[p->nExpr-1];
94509
+ pItem->reusable = regDest<0;
94510
+ if( regDest<0 ) regDest = ++pParse->nMem;
9426594511
pItem->u.iConstExprReg = regDest;
94266
- pItem->reusable = reusable;
9426794512
}
9426894513
pParse->pConstExpr = p;
94514
+ return regDest;
9426994515
}
9427094516
9427194517
/*
9427294518
** Generate code to evaluate an expression and store the results
9427394519
** into a register. Return the register number where the results
@@ -94286,23 +94532,12 @@
9428694532
pExpr = sqlite3ExprSkipCollate(pExpr);
9428794533
if( ConstFactorOk(pParse)
9428894534
&& pExpr->op!=TK_REGISTER
9428994535
&& sqlite3ExprIsConstantNotJoin(pExpr)
9429094536
){
94291
- ExprList *p = pParse->pConstExpr;
94292
- int i;
9429394537
*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);
94538
+ r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1);
9430494539
}else{
9430594540
int r1 = sqlite3GetTempReg(pParse);
9430694541
r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
9430794542
if( r2==r1 ){
9430894543
*pReg = r1;
@@ -94352,11 +94587,11 @@
9435294587
** in register target. If the expression is constant, then this routine
9435394588
** might choose to code the expression at initialization time.
9435494589
*/
9435594590
SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
9435694591
if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
94357
- sqlite3ExprCodeAtInit(pParse, pExpr, target, 0);
94592
+ sqlite3ExprCodeAtInit(pParse, pExpr, target);
9435894593
}else{
9435994594
sqlite3ExprCode(pParse, pExpr, target);
9436094595
}
9436194596
}
9436294597
@@ -94424,11 +94659,11 @@
9442494659
n--;
9442594660
}else{
9442694661
sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
9442794662
}
9442894663
}else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){
94429
- sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0);
94664
+ sqlite3ExprCodeAtInit(pParse, pExpr, target+i);
9443094665
}else{
9443194666
int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
9443294667
if( inReg!=target+i ){
9443394668
VdbeOp *pOp;
9443494669
if( copyOp==OP_Copy
@@ -96968,10 +97203,16 @@
9696897203
** used to query statistical information that has been gathered into
9696997204
** the Stat4Accum object by prior calls to stat_push(). The P parameter
9697097205
** has type BLOB but it is really just a pointer to the Stat4Accum object.
9697197206
** The content to returned is determined by the parameter J
9697297207
** which is one of the STAT_GET_xxxx values defined above.
97208
+**
97209
+** The stat_get(P,J) function is not available to generic SQL. It is
97210
+** inserted as part of a manually constructed bytecode program. (See
97211
+** the callStatGet() routine below.) It is guaranteed that the P
97212
+** parameter will always be a poiner to a Stat4Accum object, never a
97213
+** NULL.
9697397214
**
9697497215
** If neither STAT3 nor STAT4 are enabled, then J is always
9697597216
** STAT_GET_STAT1 and is hence omitted and this routine becomes
9697697217
** a one-parameter function, stat_get(P), that always returns the
9697797218
** stat1 table entry information.
@@ -97787,11 +98028,11 @@
9778798028
sumEq += aSample[i].anEq[iCol];
9778898029
nSum100 += 100;
9778998030
}
9779098031
}
9779198032
97792
- if( nDist100>nSum100 ){
98033
+ if( nDist100>nSum100 && sumEq<nRow ){
9779398034
avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
9779498035
}
9779598036
if( avgEq==0 ) avgEq = 1;
9779698037
pIdx->aAvgEq[iCol] = avgEq;
9779798038
}
@@ -98201,10 +98442,11 @@
9820198442
assert( pVfs );
9820298443
flags |= SQLITE_OPEN_MAIN_DB;
9820398444
rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags);
9820498445
sqlite3_free( zPath );
9820598446
db->nDb++;
98447
+ db->skipBtreeMutex = 0;
9820698448
if( rc==SQLITE_CONSTRAINT ){
9820798449
rc = SQLITE_ERROR;
9820898450
zErrDyn = sqlite3MPrintf(db, "database is already attached");
9820998451
}else if( rc==SQLITE_OK ){
9821098452
Pager *pPager;
@@ -104365,16 +104607,12 @@
104365104607
}
104366104608
}else
104367104609
#endif
104368104610
{
104369104611
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
- }
104374104612
sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
104375
- iKey, nKey, count, OE_Default, eOnePass, iIdxNoSeek);
104613
+ iKey, nKey, count, OE_Default, eOnePass, aiCurOnePass[1]);
104376104614
}
104377104615
104378104616
/* End of the loop over all rowids/primary-keys. */
104379104617
if( eOnePass!=ONEPASS_OFF ){
104380104618
sqlite3VdbeResolveLabel(v, addrBypass);
@@ -104450,19 +104688,21 @@
104450104688
** then this function must seek iDataCur to the entry identified by iPk
104451104689
** and nPk before reading from it.
104452104690
**
104453104691
** If eMode is ONEPASS_MULTI, then this call is being made as part
104454104692
** 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.
104693
+** iIdxNoSeek is a valid cursor number (>=0) and is not the same as
104694
+** iDataCur, then its position should be preserved following the delete
104695
+** operation. Or, if iIdxNoSeek is not a valid cursor number, the
104696
+** position of iDataCur should be preserved instead.
104459104697
**
104460104698
** 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.
104699
+** If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
104700
+** then it identifies an index cursor (from within array of cursors
104701
+** starting at iIdxCur) that already points to the index entry to be deleted.
104702
+** Except, this optimization is disabled if there are BEFORE triggers since
104703
+** the trigger body might have moved the cursor.
104464104704
*/
104465104705
SQLITE_PRIVATE void sqlite3GenerateRowDelete(
104466104706
Parse *pParse, /* Parsing context */
104467104707
Table *pTab, /* Table containing the row to be deleted */
104468104708
Trigger *pTrigger, /* List of triggers to (potentially) fire */
@@ -104529,17 +104769,22 @@
104529104769
TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
104530104770
);
104531104771
104532104772
/* If any BEFORE triggers were coded, then seek the cursor to the
104533104773
** 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
104774
+ ** the cursor or already deleted the row that the cursor was
104535104775
** pointing to.
104776
+ **
104777
+ ** Also disable the iIdxNoSeek optimization since the BEFORE trigger
104778
+ ** may have moved that cursor.
104536104779
*/
104537104780
if( addrStart<sqlite3VdbeCurrentAddr(v) ){
104538104781
sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
104539104782
VdbeCoverageIf(v, opSeek==OP_NotExists);
104540104783
VdbeCoverageIf(v, opSeek==OP_NotFound);
104784
+ testcase( iIdxNoSeek>=0 );
104785
+ iIdxNoSeek = -1;
104541104786
}
104542104787
104543104788
/* Do FK processing. This call checks that any FK constraints that
104544104789
** refer to this table (i.e. constraints attached to other tables)
104545104790
** are not violated by deleting this row. */
@@ -104558,15 +104803,17 @@
104558104803
*/
104559104804
if( pTab->pSelect==0 ){
104560104805
u8 p5 = 0;
104561104806
sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
104562104807
sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
104563
- sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
104808
+ if( pParse->nested==0 ){
104809
+ sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
104810
+ }
104564104811
if( eMode!=ONEPASS_OFF ){
104565104812
sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE);
104566104813
}
104567
- if( iIdxNoSeek>=0 ){
104814
+ if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
104568104815
sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
104569104816
}
104570104817
if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION;
104571104818
sqlite3VdbeChangeP5(v, p5);
104572104819
}
@@ -104716,10 +104963,14 @@
104716104963
** opcode if it is present */
104717104964
sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
104718104965
}
104719104966
if( regOut ){
104720104967
sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
104968
+ if( pIdx->pTable->pSelect ){
104969
+ const char *zAff = sqlite3IndexAffinityStr(pParse->db, pIdx);
104970
+ sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
104971
+ }
104721104972
}
104722104973
sqlite3ReleaseTempRange(pParse, regBase, nCol);
104723104974
return regBase;
104724104975
}
104725104976
@@ -106512,10 +106763,13 @@
106512106763
DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ),
106513106764
#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
106514106765
FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
106515106766
FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
106516106767
FUNCTION2(likely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
106768
+#ifdef SQLITE_DEBUG
106769
+ FUNCTION2(affinity, 1, 0, 0, noopFunc, SQLITE_FUNC_AFFINITY),
106770
+#endif
106517106771
FUNCTION(ltrim, 1, 1, 0, trimFunc ),
106518106772
FUNCTION(ltrim, 2, 1, 0, trimFunc ),
106519106773
FUNCTION(rtrim, 1, 2, 0, trimFunc ),
106520106774
FUNCTION(rtrim, 2, 2, 0, trimFunc ),
106521106775
FUNCTION(trim, 1, 3, 0, trimFunc ),
@@ -109667,11 +109921,11 @@
109667109921
if( db->flags&SQLITE_RecTriggers ){
109668109922
pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
109669109923
}
109670109924
sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
109671109925
regR, nPkField, 0, OE_Replace,
109672
- (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), -1);
109926
+ (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
109673109927
seenReplace = 1;
109674109928
break;
109675109929
}
109676109930
}
109677109931
sqlite3VdbeResolveLabel(v, addrUniqueOk);
@@ -109683,10 +109937,29 @@
109683109937
}
109684109938
109685109939
*pbMayReplace = seenReplace;
109686109940
VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
109687109941
}
109942
+
109943
+#ifdef SQLITE_ENABLE_NULL_TRIM
109944
+/*
109945
+** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
109946
+** to be the number of columns in table pTab that must not be NULL-trimmed.
109947
+**
109948
+** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
109949
+*/
109950
+SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
109951
+ u16 i;
109952
+
109953
+ /* Records with omitted columns are only allowed for schema format
109954
+ ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
109955
+ if( pTab->pSchema->file_format<2 ) return;
109956
+
109957
+ for(i=pTab->nCol; i>1 && pTab->aCol[i-1].pDflt==0; i--){}
109958
+ sqlite3VdbeChangeP5(v, i);
109959
+}
109960
+#endif
109688109961
109689109962
/*
109690109963
** This routine generates code to finish the INSERT or UPDATE operation
109691109964
** that was started by a prior call to sqlite3GenerateConstraintChecks.
109692109965
** A consecutive range of registers starting at regNewData contains the
@@ -109700,11 +109973,11 @@
109700109973
Table *pTab, /* the table into which we are inserting */
109701109974
int iDataCur, /* Cursor of the canonical data source */
109702109975
int iIdxCur, /* First index cursor */
109703109976
int regNewData, /* Range of content */
109704109977
int *aRegIdx, /* Register used by each index. 0 for unused indices */
109705
- int isUpdate, /* True for UPDATE, False for INSERT */
109978
+ int update_flags, /* True for UPDATE, False for INSERT */
109706109979
int appendBias, /* True if this is likely to be an append */
109707109980
int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
109708109981
){
109709109982
Vdbe *v; /* Prepared statements under construction */
109710109983
Index *pIdx; /* An index being inserted or updated */
@@ -109711,10 +109984,15 @@
109711109984
u8 pik_flags; /* flag values passed to the btree insert */
109712109985
int regData; /* Content registers (after the rowid) */
109713109986
int regRec; /* Register holding assembled record for the table */
109714109987
int i; /* Loop counter */
109715109988
u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */
109989
+
109990
+ assert( update_flags==0
109991
+ || update_flags==OPFLAG_ISUPDATE
109992
+ || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)
109993
+ );
109716109994
109717109995
v = sqlite3GetVdbe(pParse);
109718109996
assert( v!=0 );
109719109997
assert( pTab->pSelect==0 ); /* This table is not a VIEW */
109720109998
for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
@@ -109722,34 +110000,43 @@
109722110000
bAffinityDone = 1;
109723110001
if( pIdx->pPartIdxWhere ){
109724110002
sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
109725110003
VdbeCoverage(v);
109726110004
}
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;
110005
+ pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
109732110006
if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
109733110007
assert( pParse->nested==0 );
109734110008
pik_flags |= OPFLAG_NCHANGE;
110009
+ pik_flags |= (update_flags & OPFLAG_SAVEPOSITION);
110010
+#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
110011
+ if( update_flags==0 ){
110012
+ sqlite3VdbeAddOp4(v, OP_InsertInt,
110013
+ iIdxCur+i, aRegIdx[i], 0, (char*)pTab, P4_TABLE
110014
+ );
110015
+ sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP);
110016
+ }
110017
+#endif
109735110018
}
110019
+ sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
110020
+ aRegIdx[i]+1,
110021
+ pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
109736110022
sqlite3VdbeChangeP5(v, pik_flags);
109737110023
}
109738110024
if( !HasRowid(pTab) ) return;
109739110025
regData = regNewData + 1;
109740110026
regRec = sqlite3GetTempReg(pParse);
109741110027
sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
110028
+ sqlite3SetMakeRecordP5(v, pTab);
109742110029
if( !bAffinityDone ){
109743110030
sqlite3TableAffinity(v, pTab, 0);
109744110031
sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
109745110032
}
109746110033
if( pParse->nested ){
109747110034
pik_flags = 0;
109748110035
}else{
109749110036
pik_flags = OPFLAG_NCHANGE;
109750
- pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
110037
+ pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID);
109751110038
}
109752110039
if( appendBias ){
109753110040
pik_flags |= OPFLAG_APPEND;
109754110041
}
109755110042
if( useSeekResult ){
@@ -110154,11 +110441,11 @@
110154110441
addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
110155110442
}else{
110156110443
addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
110157110444
assert( (pDest->tabFlags & TF_Autoincrement)==0 );
110158110445
}
110159
- sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
110446
+ sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
110160110447
if( db->flags & SQLITE_Vacuum ){
110161110448
sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
110162110449
insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|
110163110450
OPFLAG_APPEND|OPFLAG_USESEEKRESULT;
110164110451
}else{
@@ -110186,11 +110473,11 @@
110186110473
sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
110187110474
sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
110188110475
sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
110189110476
VdbeComment((v, "%s", pDestIdx->zName));
110190110477
addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
110191
- sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
110478
+ sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
110192110479
if( db->flags & SQLITE_Vacuum ){
110193110480
/* This INSERT command is part of a VACUUM operation, which guarantees
110194110481
** that the destination table is empty. If all indexed columns use
110195110482
** collation sequence BINARY, then it can also be assumed that the
110196110483
** index will be populated by inserting keys in strictly sorted
@@ -110971,11 +111258,10 @@
110971111258
#endif /* SQLITE3EXT_H */
110972111259
110973111260
/************** End of sqlite3ext.h ******************************************/
110974111261
/************** Continuing where we left off in loadext.c ********************/
110975111262
/* #include "sqliteInt.h" */
110976
-/* #include <string.h> */
110977111263
110978111264
#ifndef SQLITE_OMIT_LOAD_EXTENSION
110979111265
/*
110980111266
** Some API routines are omitted when various features are
110981111267
** excluded from a build of SQLite. Substitute a NULL pointer
@@ -112635,11 +112921,11 @@
112635112921
112636112922
/*
112637112923
** Locate a pragma in the aPragmaName[] array.
112638112924
*/
112639112925
static const PragmaName *pragmaLocate(const char *zName){
112640
- int upr, lwr, mid, rc;
112926
+ int upr, lwr, mid = 0, rc;
112641112927
lwr = 0;
112642112928
upr = ArraySize(aPragmaName)-1;
112643112929
while( lwr<=upr ){
112644112930
mid = (lwr+upr)/2;
112645112931
rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
@@ -116149,10 +116435,11 @@
116149116435
v = pParse->pVdbe;
116150116436
r1 = sqlite3GetTempReg(pParse);
116151116437
sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
116152116438
sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
116153116439
sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, iMem, N);
116440
+ sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
116154116441
sqlite3ReleaseTempReg(pParse, r1);
116155116442
}
116156116443
116157116444
/*
116158116445
** This routine generates the code for the inside of the inner loop
@@ -119677,11 +119964,19 @@
119677119964
assert( pTab->nTabRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nTabRef==2 ));
119678119965
119679119966
pCte->zCteErr = "circular reference: %s";
119680119967
pSavedWith = pParse->pWith;
119681119968
pParse->pWith = pWith;
119682
- sqlite3WalkSelect(pWalker, bMayRecursive ? pSel->pPrior : pSel);
119969
+ if( bMayRecursive ){
119970
+ Select *pPrior = pSel->pPrior;
119971
+ assert( pPrior->pWith==0 );
119972
+ pPrior->pWith = pSel->pWith;
119973
+ sqlite3WalkSelect(pWalker, pPrior);
119974
+ pPrior->pWith = 0;
119975
+ }else{
119976
+ sqlite3WalkSelect(pWalker, pSel);
119977
+ }
119683119978
pParse->pWith = pWith;
119684119979
119685119980
for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
119686119981
pEList = pLeft->pEList;
119687119982
if( pCte->pCols ){
@@ -119721,14 +120016,16 @@
119721120016
** sqlite3SelectExpand() when walking a SELECT tree to resolve table
119722120017
** names and other FROM clause elements.
119723120018
*/
119724120019
static void selectPopWith(Walker *pWalker, Select *p){
119725120020
Parse *pParse = pWalker->pParse;
119726
- With *pWith = findRightmost(p)->pWith;
119727
- if( pWith!=0 ){
119728
- assert( pParse->pWith==pWith );
119729
- pParse->pWith = pWith->pOuter;
120021
+ if( pParse->pWith && p->pPrior==0 ){
120022
+ With *pWith = findRightmost(p)->pWith;
120023
+ if( pWith!=0 ){
120024
+ assert( pParse->pWith==pWith );
120025
+ pParse->pWith = pWith->pOuter;
120026
+ }
119730120027
}
119731120028
}
119732120029
#else
119733120030
#define selectPopWith 0
119734120031
#endif
@@ -119774,12 +120071,12 @@
119774120071
if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
119775120072
return WRC_Prune;
119776120073
}
119777120074
pTabList = p->pSrc;
119778120075
pEList = p->pEList;
119779
- if( pWalker->xSelectCallback2==selectPopWith ){
119780
- sqlite3WithPush(pParse, findRightmost(p)->pWith, 0);
120076
+ if( p->pWith ){
120077
+ sqlite3WithPush(pParse, p->pWith, 0);
119781120078
}
119782120079
119783120080
/* Make sure cursor numbers have been assigned to all entries in
119784120081
** the FROM clause of the SELECT statement.
119785120082
*/
@@ -120062,13 +120359,11 @@
120062120359
if( pParse->hasCompound ){
120063120360
w.xSelectCallback = convertCompoundSelectToSubquery;
120064120361
sqlite3WalkSelect(&w, pSelect);
120065120362
}
120066120363
w.xSelectCallback = selectExpander;
120067
- if( (pSelect->selFlags & SF_MultiValue)==0 ){
120068
- w.xSelectCallback2 = selectPopWith;
120069
- }
120364
+ w.xSelectCallback2 = selectPopWith;
120070120365
sqlite3WalkSelect(&w, pSelect);
120071120366
}
120072120367
120073120368
120074120369
#ifndef SQLITE_OMIT_SUBQUERY
@@ -121143,11 +121438,11 @@
121143121438
/* This case runs if the aggregate has no GROUP BY clause. The
121144121439
** processing is much simpler since there is only a single row
121145121440
** of output.
121146121441
*/
121147121442
resetAccumulator(pParse, &sAggInfo);
121148
- pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0);
121443
+ pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax, 0,flag,0);
121149121444
if( pWInfo==0 ){
121150121445
sqlite3ExprListDelete(db, pDel);
121151121446
goto select_end;
121152121447
}
121153121448
updateAccumulator(pParse, &sAggInfo);
@@ -121232,12 +121527,10 @@
121232121527
**
121233121528
** These routines are in a separate files so that they will not be linked
121234121529
** if they are not used.
121235121530
*/
121236121531
/* #include "sqliteInt.h" */
121237
-/* #include <stdlib.h> */
121238
-/* #include <string.h> */
121239121532
121240121533
#ifndef SQLITE_OMIT_GET_TABLE
121241121534
121242121535
/*
121243121536
** This structure is used to pass data from sqlite3_get_table() through
@@ -122591,16 +122884,16 @@
122591122884
sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
122592122885
pCol->affinity, &pValue);
122593122886
if( pValue ){
122594122887
sqlite3VdbeAppendP4(v, pValue, P4_MEM);
122595122888
}
122889
+ }
122596122890
#ifndef SQLITE_OMIT_FLOATING_POINT
122597
- if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
122598
- sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
122599
- }
122891
+ if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
122892
+ sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
122893
+ }
122600122894
#endif
122601
- }
122602122895
}
122603122896
122604122897
/*
122605122898
** Process an UPDATE statement.
122606122899
**
@@ -122625,11 +122918,11 @@
122625122918
int nIdx; /* Number of indices that need updating */
122626122919
int iBaseCur; /* Base cursor number */
122627122920
int iDataCur; /* Cursor for the canonical data btree */
122628122921
int iIdxCur; /* Cursor for the first index */
122629122922
sqlite3 *db; /* The database structure */
122630
- int *aRegIdx = 0; /* One register assigned to each index to be updated */
122923
+ int *aRegIdx = 0; /* First register in array assigned to each index */
122631122924
int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
122632122925
** an expression for the i-th column of the table.
122633122926
** aXRef[i]==-1 if the i-th column is not changed. */
122634122927
u8 *aToOpen; /* 1 for tables and indices to be opened */
122635122928
u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */
@@ -122637,14 +122930,15 @@
122637122930
u8 chngKey; /* Either chngPk or chngRowid */
122638122931
Expr *pRowidExpr = 0; /* Expression defining the new record number */
122639122932
AuthContext sContext; /* The authorization context */
122640122933
NameContext sNC; /* The name-context to resolve expressions in */
122641122934
int iDb; /* Database containing the table being updated */
122642
- int okOnePass; /* True for one-pass algorithm without the FIFO */
122935
+ int eOnePass; /* ONEPASS_XXX value from where.c */
122643122936
int hasFK; /* True if foreign key processing is required */
122644122937
int labelBreak; /* Jump here to break out of UPDATE loop */
122645122938
int labelContinue; /* Jump here to continue next step of UPDATE loop */
122939
+ int flags; /* Flags for sqlite3WhereBegin() */
122646122940
122647122941
#ifndef SQLITE_OMIT_TRIGGER
122648122942
int isView; /* True when updating a view (INSTEAD OF trigger) */
122649122943
Trigger *pTrigger; /* List of triggers on pTab, if required */
122650122944
int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
@@ -122651,10 +122945,14 @@
122651122945
#endif
122652122946
int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */
122653122947
int iEph = 0; /* Ephemeral table holding all primary key values */
122654122948
int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */
122655122949
int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
122950
+ int addrOpen = 0; /* Address of OP_OpenEphemeral */
122951
+ int iPk = 0; /* First of nPk cells holding PRIMARY KEY value */
122952
+ i16 nPk = 0; /* Number of components of the PRIMARY KEY */
122953
+ int bReplace = 0; /* True if REPLACE conflict resolution might happen */
122656122954
122657122955
/* Register Allocations */
122658122956
int regRowCount = 0; /* A count of rows changed */
122659122957
int regOldRowid = 0; /* The old rowid */
122660122958
int regNewRowid = 0; /* The new rowid */
@@ -122810,17 +123108,27 @@
122810123108
for(i=0; i<pIdx->nKeyCol; i++){
122811123109
i16 iIdxCol = pIdx->aiColumn[i];
122812123110
if( iIdxCol<0 || aXRef[iIdxCol]>=0 ){
122813123111
reg = ++pParse->nMem;
122814123112
pParse->nMem += pIdx->nColumn;
123113
+ if( (onError==OE_Replace)
123114
+ || (onError==OE_Default && pIdx->onError==OE_Replace)
123115
+ ){
123116
+ bReplace = 1;
123117
+ }
122815123118
break;
122816123119
}
122817123120
}
122818123121
}
122819123122
if( reg==0 ) aToOpen[j+1] = 0;
122820123123
aRegIdx[j] = reg;
122821123124
}
123125
+ if( bReplace ){
123126
+ /* If REPLACE conflict resolution might be invoked, open cursors on all
123127
+ ** indexes in case they are needed to delete records. */
123128
+ memset(aToOpen, 1, nIdx+1);
123129
+ }
122822123130
122823123131
/* Begin generating code. */
122824123132
v = sqlite3GetVdbe(pParse);
122825123133
if( v==0 ) goto update_cleanup;
122826123134
if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
@@ -122869,107 +123177,127 @@
122869123177
pWhere, onError);
122870123178
goto update_cleanup;
122871123179
}
122872123180
#endif
122873123181
122874
- /* Begin the database scan
122875
- */
123182
+ /* Initialize the count of updated rows */
123183
+ if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
123184
+ regRowCount = ++pParse->nMem;
123185
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
123186
+ }
123187
+
122876123188
if( HasRowid(pTab) ){
122877123189
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);
122895123190
}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
-
122900123191
assert( pPk!=0 );
122901123192
nPk = pPk->nKeyCol;
122902123193
iPk = pParse->nMem+1;
122903123194
pParse->nMem += nPk;
122904123195
regKey = ++pParse->nMem;
122905123196
iEph = pParse->nTab++;
123197
+
122906123198
sqlite3VdbeAddOp2(v, OP_Null, 0, iPk);
122907123199
addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
122908123200
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);
123201
+ }
123202
+
123203
+ /* Begin the database scan.
123204
+ **
123205
+ ** Do not consider a single-pass strategy for a multi-row update if
123206
+ ** there are any triggers or foreign keys to process, or rows may
123207
+ ** be deleted as a result of REPLACE conflict handling. Any of these
123208
+ ** things might disturb a cursor being used to scan through the table
123209
+ ** or index, causing a single-pass approach to malfunction. */
123210
+ flags = WHERE_ONEPASS_DESIRED|WHERE_SEEK_UNIQ_TABLE;
123211
+ if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
123212
+ flags |= WHERE_ONEPASS_MULTIROW;
123213
+ }
123214
+ pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags, iIdxCur);
123215
+ if( pWInfo==0 ) goto update_cleanup;
123216
+
123217
+ /* A one-pass strategy that might update more than one row may not
123218
+ ** be used if any column of the index used for the scan is being
123219
+ ** updated. Otherwise, if there is an index on "b", statements like
123220
+ ** the following could create an infinite loop:
123221
+ **
123222
+ ** UPDATE t1 SET b=b+1 WHERE b>?
123223
+ **
123224
+ ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
123225
+ ** strategy that uses an index for which one or more columns are being
123226
+ ** updated. */
123227
+ eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
123228
+ if( eOnePass==ONEPASS_MULTI ){
123229
+ int iCur = aiCurOnePass[1];
123230
+ if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
123231
+ eOnePass = ONEPASS_OFF;
123232
+ }
123233
+ assert( iCur!=iDataCur || !HasRowid(pTab) );
123234
+ }
123235
+
123236
+ if( HasRowid(pTab) ){
123237
+ /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
123238
+ ** mode, write the rowid into the FIFO. In either of the one-pass modes,
123239
+ ** leave it in register regOldRowid. */
123240
+ sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
123241
+ if( eOnePass==ONEPASS_OFF ){
123242
+ sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
123243
+ }
123244
+ }else{
123245
+ /* Read the PK of the current row into an array of registers. In
123246
+ ** ONEPASS_OFF mode, serialize the array into a record and store it in
123247
+ ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change
123248
+ ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table
123249
+ ** is not required) and leave the PK fields in the array of registers. */
122913123250
for(i=0; i<nPk; i++){
122914123251
assert( pPk->aiColumn[i]>=0 );
122915
- sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pPk->aiColumn[i],
122916
- iPk+i);
123252
+ sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur,pPk->aiColumn[i],iPk+i);
122917123253
}
122918
- if( okOnePass ){
123254
+ if( eOnePass ){
122919123255
sqlite3VdbeChangeToNoop(v, addrOpen);
122920123256
nKey = nPk;
122921123257
regKey = iPk;
122922123258
}else{
122923123259
sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,
122924123260
sqlite3IndexAffinityStr(db, pPk), nPk);
122925123261
sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEph, regKey, iPk, nPk);
122926123262
}
122927
- sqlite3WhereEnd(pWInfo);
122928123263
}
122929123264
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);
123265
+ if( eOnePass!=ONEPASS_MULTI ){
123266
+ sqlite3WhereEnd(pWInfo);
122935123267
}
122936123268
122937123269
labelBreak = sqlite3VdbeMakeLabel(v);
122938123270
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 ){
123271
+ int addrOnce = 0;
123272
+
123273
+ /* Open every index that needs updating. */
123274
+ if( eOnePass!=ONEPASS_OFF ){
122956123275
if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
122957123276
if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
122958123277
}
123278
+
123279
+ if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
123280
+ addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
123281
+ }
122959123282
sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur, aToOpen,
122960123283
0, 0);
123284
+ if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
122961123285
}
122962123286
122963123287
/* Top of the update loop */
122964
- if( okOnePass ){
122965
- if( aToOpen[iDataCur-iBaseCur] && !isView ){
123288
+ if( eOnePass!=ONEPASS_OFF ){
123289
+ if( !isView && aiCurOnePass[0]!=iDataCur && aiCurOnePass[1]!=iDataCur ){
122966123290
assert( pPk );
122967123291
sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
122968123292
VdbeCoverageNeverTaken(v);
122969123293
}
122970
- labelContinue = labelBreak;
123294
+ if( eOnePass==ONEPASS_SINGLE ){
123295
+ labelContinue = labelBreak;
123296
+ }else{
123297
+ labelContinue = sqlite3VdbeMakeLabel(v);
123298
+ }
122971123299
sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
122972123300
VdbeCoverageIf(v, pPk==0);
122973123301
VdbeCoverageIf(v, pPk!=0);
122974123302
}else if( pPk ){
122975123303
labelContinue = sqlite3VdbeMakeLabel(v);
@@ -123090,11 +123418,10 @@
123090123418
}
123091123419
}
123092123420
123093123421
if( !isView ){
123094123422
int addr1 = 0; /* Address of jump instruction */
123095
- int bReplace = 0; /* True if REPLACE conflict resolution might happen */
123096123423
123097123424
/* Do constraint checks. */
123098123425
assert( regOldRowid>0 );
123099123426
sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
123100123427
regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace,
@@ -123126,18 +123453,22 @@
123126123453
** is the column index supplied by the user.
123127123454
*/
123128123455
assert( regNew==regNewRowid+1 );
123129123456
#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
123130123457
sqlite3VdbeAddOp3(v, OP_Delete, iDataCur,
123131
- OPFLAG_ISUPDATE | ((hasFK || chngKey || pPk!=0) ? 0 : OPFLAG_ISNOOP),
123458
+ OPFLAG_ISUPDATE | ((hasFK || chngKey) ? 0 : OPFLAG_ISNOOP),
123132123459
regNewRowid
123133123460
);
123461
+ if( eOnePass==ONEPASS_MULTI ){
123462
+ assert( hasFK==0 && chngKey==0 );
123463
+ sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION);
123464
+ }
123134123465
if( !pParse->nested ){
123135123466
sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
123136123467
}
123137123468
#else
123138
- if( hasFK || chngKey || pPk!=0 ){
123469
+ if( hasFK || chngKey ){
123139123470
sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0);
123140123471
}
123141123472
#endif
123142123473
if( bReplace || chngKey ){
123143123474
sqlite3VdbeJumpHere(v, addr1);
@@ -123146,12 +123477,15 @@
123146123477
if( hasFK ){
123147123478
sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);
123148123479
}
123149123480
123150123481
/* Insert the new index entries and the new record. */
123151
- sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
123152
- regNewRowid, aRegIdx, 1, 0, 0);
123482
+ sqlite3CompleteInsertion(
123483
+ pParse, pTab, iDataCur, iIdxCur, regNewRowid, aRegIdx,
123484
+ OPFLAG_ISUPDATE | (eOnePass==ONEPASS_MULTI ? OPFLAG_SAVEPOSITION : 0),
123485
+ 0, 0
123486
+ );
123153123487
123154123488
/* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
123155123489
** handle rows (possibly in other tables) that refer via a foreign key
123156123490
** to the row just updated. */
123157123491
if( hasFK ){
@@ -123169,12 +123503,15 @@
123169123503
TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue);
123170123504
123171123505
/* Repeat the above with the next record to be updated, until
123172123506
** all record selected by the WHERE clause have been updated.
123173123507
*/
123174
- if( okOnePass ){
123508
+ if( eOnePass==ONEPASS_SINGLE ){
123175123509
/* Nothing to do at end-of-loop for a single-pass */
123510
+ }else if( eOnePass==ONEPASS_MULTI ){
123511
+ sqlite3VdbeResolveLabel(v, labelContinue);
123512
+ sqlite3WhereEnd(pWInfo);
123176123513
}else if( pPk ){
123177123514
sqlite3VdbeResolveLabel(v, labelContinue);
123178123515
sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v);
123179123516
}else{
123180123517
sqlite3VdbeGoto(v, labelContinue);
@@ -127090,11 +127427,14 @@
127090127427
127091127428
/* Seek the table cursor, if required */
127092127429
if( omitTable ){
127093127430
/* pIdx is a covering index. No need to access the main table. */
127094127431
}else if( HasRowid(pIdx->pTable) ){
127095
- if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE)!=0 ){
127432
+ if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE) || (
127433
+ (pWInfo->wctrlFlags & WHERE_SEEK_UNIQ_TABLE)
127434
+ && (pWInfo->eOnePass==ONEPASS_SINGLE)
127435
+ )){
127096127436
iRowidReg = ++pParse->nMem;
127097127437
sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
127098127438
sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
127099127439
sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg);
127100127440
VdbeCoverage(v);
@@ -128454,10 +128794,11 @@
128454128794
int noCase = 0; /* uppercase equivalent to lowercase */
128455128795
int op; /* Top-level operator. pExpr->op */
128456128796
Parse *pParse = pWInfo->pParse; /* Parsing context */
128457128797
sqlite3 *db = pParse->db; /* Database connection */
128458128798
unsigned char eOp2; /* op2 value for LIKE/REGEXP/GLOB */
128799
+ int nLeft; /* Number of elements on left side vector */
128459128800
128460128801
if( db->mallocFailed ){
128461128802
return;
128462128803
}
128463128804
pTerm = &pWC->a[idxTerm];
@@ -128483,10 +128824,14 @@
128483128824
if( ExprHasProperty(pExpr, EP_FromJoin) ){
128484128825
Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
128485128826
prereqAll |= x;
128486128827
extraRight = x-1; /* ON clause terms may not be used with an index
128487128828
** on left table of a LEFT JOIN. Ticket #3015 */
128829
+ if( (prereqAll>>1)>=x ){
128830
+ sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
128831
+ return;
128832
+ }
128488128833
}
128489128834
pTerm->prereqAll = prereqAll;
128490128835
pTerm->leftCursor = -1;
128491128836
pTerm->iParent = -1;
128492128837
pTerm->eOperator = 0;
@@ -128725,17 +129070,16 @@
128725129070
**
128726129071
** This is only required if at least one side of the comparison operation
128727129072
** is not a sub-select. */
128728129073
if( pWC->op==TK_AND
128729129074
&& (pExpr->op==TK_EQ || pExpr->op==TK_IS)
128730
- && sqlite3ExprIsVector(pExpr->pLeft)
129075
+ && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
129076
+ && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
128731129077
&& ( (pExpr->pLeft->flags & EP_xIsSelect)==0
128732
- || (pExpr->pRight->flags & EP_xIsSelect)==0
128733
- )){
128734
- int nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
129078
+ || (pExpr->pRight->flags & EP_xIsSelect)==0)
129079
+ ){
128735129080
int i;
128736
- assert( nLeft==sqlite3ExprVectorSize(pExpr->pRight) );
128737129081
for(i=0; i<nLeft; i++){
128738129082
int idxNew;
128739129083
Expr *pNew;
128740129084
Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i);
128741129085
Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i);
@@ -129293,10 +129637,11 @@
129293129637
if( pIdx ){
129294129638
int j = iColumn;
129295129639
iColumn = pIdx->aiColumn[j];
129296129640
if( iColumn==XN_EXPR ){
129297129641
pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
129642
+ pScan->zCollName = pIdx->azColl[j];
129298129643
}else if( iColumn==pIdx->pTable->iPKey ){
129299129644
iColumn = XN_ROWID;
129300129645
}else if( iColumn>=0 ){
129301129646
pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
129302129647
pScan->zCollName = pIdx->azColl[j];
@@ -133934,11 +134279,12 @@
133934134279
x = sqlite3ColumnOfIndex(pIdx, x);
133935134280
if( x>=0 ){
133936134281
pOp->p2 = x;
133937134282
pOp->p1 = pLevel->iIdxCur;
133938134283
}
133939
- assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 );
134284
+ assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0
134285
+ || pWInfo->eOnePass );
133940134286
}else if( pOp->opcode==OP_Rowid ){
133941134287
pOp->p1 = pLevel->iIdxCur;
133942134288
pOp->opcode = OP_IdxRowid;
133943134289
}
133944134290
}
@@ -133998,10 +134344,23 @@
133998134344
** Indicate that sqlite3ParserFree() will never be called with a null
133999134345
** pointer.
134000134346
*/
134001134347
#define YYPARSEFREENEVERNULL 1
134002134348
134349
+/*
134350
+** In the amalgamation, the parse.c file generated by lemon and the
134351
+** tokenize.c file are concatenated. In that case, sqlite3RunParser()
134352
+** has access to the the size of the yyParser object and so the parser
134353
+** engine can be allocated from stack. In that case, only the
134354
+** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
134355
+** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
134356
+** omitted.
134357
+*/
134358
+#ifdef SQLITE_AMALGAMATION
134359
+# define sqlite3Parser_ENGINEALWAYSONSTACK 1
134360
+#endif
134361
+
134003134362
/*
134004134363
** Alternative datatype for the argument to the malloc() routine passed
134005134364
** into sqlite3ParserAlloc(). The default is size_t.
134006134365
*/
134007134366
#define YYMALLOCARGTYPE u64
@@ -135446,10 +135805,35 @@
135446135805
*/
135447135806
#ifndef YYMALLOCARGTYPE
135448135807
# define YYMALLOCARGTYPE size_t
135449135808
#endif
135450135809
135810
+/* Initialize a new parser that has already been allocated.
135811
+*/
135812
+SQLITE_PRIVATE void sqlite3ParserInit(void *yypParser){
135813
+ yyParser *pParser = (yyParser*)yypParser;
135814
+#ifdef YYTRACKMAXSTACKDEPTH
135815
+ pParser->yyhwm = 0;
135816
+#endif
135817
+#if YYSTACKDEPTH<=0
135818
+ pParser->yytos = NULL;
135819
+ pParser->yystack = NULL;
135820
+ pParser->yystksz = 0;
135821
+ if( yyGrowStack(pParser) ){
135822
+ pParser->yystack = &pParser->yystk0;
135823
+ pParser->yystksz = 1;
135824
+ }
135825
+#endif
135826
+#ifndef YYNOERRORRECOVERY
135827
+ pParser->yyerrcnt = -1;
135828
+#endif
135829
+ pParser->yytos = pParser->yystack;
135830
+ pParser->yystack[0].stateno = 0;
135831
+ pParser->yystack[0].major = 0;
135832
+}
135833
+
135834
+#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
135451135835
/*
135452135836
** This function allocates a new parser.
135453135837
** The only argument is a pointer to a function which works like
135454135838
** malloc.
135455135839
**
@@ -135461,32 +135845,15 @@
135461135845
** to sqlite3Parser and sqlite3ParserFree.
135462135846
*/
135463135847
SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPE)){
135464135848
yyParser *pParser;
135465135849
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
- }
135850
+ if( pParser ) sqlite3ParserInit(pParser);
135486135851
return pParser;
135487135852
}
135853
+#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
135854
+
135488135855
135489135856
/* The following function deletes the "minor type" or semantic value
135490135857
** associated with a symbol. The symbol can be either a terminal
135491135858
** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
135492135859
** a pointer to the value to be deleted. The code used to do the
@@ -135608,10 +135975,22 @@
135608135975
}
135609135976
#endif
135610135977
yy_destructor(pParser, yytos->major, &yytos->minor);
135611135978
}
135612135979
135980
+/*
135981
+** Clear all secondary memory allocations from the parser
135982
+*/
135983
+SQLITE_PRIVATE void sqlite3ParserFinalize(void *p){
135984
+ yyParser *pParser = (yyParser*)p;
135985
+ while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
135986
+#if YYSTACKDEPTH<=0
135987
+ if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
135988
+#endif
135989
+}
135990
+
135991
+#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
135613135992
/*
135614135993
** Deallocate and destroy a parser. Destructors are called for
135615135994
** all stack elements before shutting the parser down.
135616135995
**
135617135996
** If the YYPARSEFREENEVERNULL macro exists (for example because it
@@ -135620,20 +135999,17 @@
135620135999
*/
135621136000
SQLITE_PRIVATE void sqlite3ParserFree(
135622136001
void *p, /* The parser to be deleted */
135623136002
void (*freeProc)(void*) /* Function used to reclaim memory */
135624136003
){
135625
- yyParser *pParser = (yyParser*)p;
135626136004
#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
-}
136005
+ if( p==0 ) return;
136006
+#endif
136007
+ sqlite3ParserFinalize(p);
136008
+ (*freeProc)(p);
136009
+}
136010
+#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
135635136011
135636136012
/*
135637136013
** Return the peak depth of the stack for a parser.
135638136014
*/
135639136015
#ifdef YYTRACKMAXSTACKDEPTH
@@ -138483,10 +138859,13 @@
138483138859
void *pEngine; /* The LEMON-generated LALR(1) parser */
138484138860
int tokenType; /* type of the next token */
138485138861
int lastTokenParsed = -1; /* type of the previous token */
138486138862
sqlite3 *db = pParse->db; /* The database connection */
138487138863
int mxSqlLen; /* Max length of an SQL string */
138864
+#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
138865
+ unsigned char zSpace[sizeof(yyParser)]; /* Space for parser engine object */
138866
+#endif
138488138867
138489138868
assert( zSql!=0 );
138490138869
mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
138491138870
if( db->nVdbeActive==0 ){
138492138871
db->u1.isInterrupted = 0;
@@ -138494,15 +138873,20 @@
138494138873
pParse->rc = SQLITE_OK;
138495138874
pParse->zTail = zSql;
138496138875
i = 0;
138497138876
assert( pzErrMsg!=0 );
138498138877
/* sqlite3ParserTrace(stdout, "parser: "); */
138878
+#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
138879
+ pEngine = zSpace;
138880
+ sqlite3ParserInit(pEngine);
138881
+#else
138499138882
pEngine = sqlite3ParserAlloc(sqlite3Malloc);
138500138883
if( pEngine==0 ){
138501138884
sqlite3OomFault(db);
138502138885
return SQLITE_NOMEM_BKPT;
138503138886
}
138887
+#endif
138504138888
assert( pParse->pNewTable==0 );
138505138889
assert( pParse->pNewTrigger==0 );
138506138890
assert( pParse->nVar==0 );
138507138891
assert( pParse->pVList==0 );
138508138892
while( 1 ){
@@ -138550,11 +138934,15 @@
138550138934
sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
138551138935
sqlite3ParserStackPeak(pEngine)
138552138936
);
138553138937
sqlite3_mutex_leave(sqlite3MallocMutex());
138554138938
#endif /* YYDEBUG */
138939
+#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
138940
+ sqlite3ParserFinalize(pEngine);
138941
+#else
138555138942
sqlite3ParserFree(pEngine, sqlite3_free);
138943
+#endif
138556138944
if( db->mallocFailed ){
138557138945
pParse->rc = SQLITE_NOMEM_BKPT;
138558138946
}
138559138947
if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
138560138948
pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
@@ -144213,10 +144601,11 @@
144213144601
144214144602
/* Precompiled statements used by the implementation. Each of these
144215144603
** statements is run and reset within a single virtual table API call.
144216144604
*/
144217144605
sqlite3_stmt *aStmt[40];
144606
+ sqlite3_stmt *pSeekStmt; /* Cache for fts3CursorSeekStmt() */
144218144607
144219144608
char *zReadExprlist;
144220144609
char *zWriteExprlist;
144221144610
144222144611
int nNodeSize; /* Soft limit for node size */
@@ -144282,10 +144671,11 @@
144282144671
struct Fts3Cursor {
144283144672
sqlite3_vtab_cursor base; /* Base class used by SQLite core */
144284144673
i16 eSearch; /* Search strategy (see below) */
144285144674
u8 isEof; /* True if at End Of Results */
144286144675
u8 isRequireSeek; /* True if must seek pStmt to %_content row */
144676
+ u8 bSeekStmt; /* True if pStmt is a seek */
144287144677
sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */
144288144678
Fts3Expr *pExpr; /* Parsed MATCH query string */
144289144679
int iLangid; /* Language being queried for */
144290144680
int nPhrase; /* Number of matchable phrases in query */
144291144681
Fts3DeferredToken *pDeferred; /* Deferred search tokens, if any */
@@ -144804,10 +145194,11 @@
144804145194
144805145195
assert( p->nPendingData==0 );
144806145196
assert( p->pSegments==0 );
144807145197
144808145198
/* Free any prepared statements held */
145199
+ sqlite3_finalize(p->pSeekStmt);
144809145200
for(i=0; i<SizeofArray(p->aStmt); i++){
144810145201
sqlite3_finalize(p->aStmt[i]);
144811145202
}
144812145203
sqlite3_free(p->zSegmentsTbl);
144813145204
sqlite3_free(p->zReadExprlist);
@@ -145675,13 +146066,13 @@
145675146066
p->nPendingData = 0;
145676146067
p->azColumn = (char **)&p[1];
145677146068
p->pTokenizer = pTokenizer;
145678146069
p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
145679146070
p->bHasDocsize = (isFts4 && bNoDocsize==0);
145680
- p->bHasStat = isFts4;
145681
- p->bFts4 = isFts4;
145682
- p->bDescIdx = bDescIdx;
146071
+ p->bHasStat = (u8)isFts4;
146072
+ p->bFts4 = (u8)isFts4;
146073
+ p->bDescIdx = (u8)bDescIdx;
145683146074
p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */
145684146075
p->zContentTbl = zContent;
145685146076
p->zLanguageid = zLanguageid;
145686146077
zContent = 0;
145687146078
zLanguageid = 0;
@@ -145991,19 +146382,39 @@
145991146382
return SQLITE_NOMEM;
145992146383
}
145993146384
memset(pCsr, 0, sizeof(Fts3Cursor));
145994146385
return SQLITE_OK;
145995146386
}
146387
+
146388
+/*
146389
+** Finalize the statement handle at pCsr->pStmt.
146390
+**
146391
+** Or, if that statement handle is one created by fts3CursorSeekStmt(),
146392
+** and the Fts3Table.pSeekStmt slot is currently NULL, save the statement
146393
+** pointer there instead of finalizing it.
146394
+*/
146395
+static void fts3CursorFinalizeStmt(Fts3Cursor *pCsr){
146396
+ if( pCsr->bSeekStmt ){
146397
+ Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
146398
+ if( p->pSeekStmt==0 ){
146399
+ p->pSeekStmt = pCsr->pStmt;
146400
+ sqlite3_reset(pCsr->pStmt);
146401
+ pCsr->pStmt = 0;
146402
+ }
146403
+ pCsr->bSeekStmt = 0;
146404
+ }
146405
+ sqlite3_finalize(pCsr->pStmt);
146406
+}
145996146407
145997146408
/*
145998146409
** Close the cursor. For additional information see the documentation
145999146410
** on the xClose method of the virtual table interface.
146000146411
*/
146001146412
static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
146002146413
Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
146003146414
assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
146004
- sqlite3_finalize(pCsr->pStmt);
146415
+ fts3CursorFinalizeStmt(pCsr);
146005146416
sqlite3Fts3ExprFree(pCsr->pExpr);
146006146417
sqlite3Fts3FreeDeferredTokens(pCsr);
146007146418
sqlite3_free(pCsr->aDoclist);
146008146419
sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
146009146420
assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
@@ -146017,24 +146428,27 @@
146017146428
**
146018146429
** "SELECT <columns> FROM %_content WHERE rowid = ?"
146019146430
**
146020146431
** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
146021146432
** it. If an error occurs, return an SQLite error code.
146022
-**
146023
-** Otherwise, set *ppStmt to point to pCsr->pStmt and return SQLITE_OK.
146024146433
*/
146025
-static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){
146434
+static int fts3CursorSeekStmt(Fts3Cursor *pCsr){
146026146435
int rc = SQLITE_OK;
146027146436
if( pCsr->pStmt==0 ){
146028146437
Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
146029146438
char *zSql;
146030
- zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
146031
- if( !zSql ) return SQLITE_NOMEM;
146032
- rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
146033
- sqlite3_free(zSql);
146439
+ if( p->pSeekStmt ){
146440
+ pCsr->pStmt = p->pSeekStmt;
146441
+ p->pSeekStmt = 0;
146442
+ }else{
146443
+ zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
146444
+ if( !zSql ) return SQLITE_NOMEM;
146445
+ rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
146446
+ sqlite3_free(zSql);
146447
+ }
146448
+ if( rc==SQLITE_OK ) pCsr->bSeekStmt = 1;
146034146449
}
146035
- *ppStmt = pCsr->pStmt;
146036146450
return rc;
146037146451
}
146038146452
146039146453
/*
146040146454
** Position the pCsr->pStmt statement so that it is on the row
@@ -146042,13 +146456,11 @@
146042146456
** SQLITE_OK on success.
146043146457
*/
146044146458
static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
146045146459
int rc = SQLITE_OK;
146046146460
if( pCsr->isRequireSeek ){
146047
- sqlite3_stmt *pStmt = 0;
146048
-
146049
- rc = fts3CursorSeekStmt(pCsr, &pStmt);
146461
+ rc = fts3CursorSeekStmt(pCsr);
146050146462
if( rc==SQLITE_OK ){
146051146463
sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
146052146464
pCsr->isRequireSeek = 0;
146053146465
if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
146054146466
return SQLITE_OK;
@@ -147502,11 +147914,11 @@
147502147914
if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++];
147503147915
if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++];
147504147916
assert( iIdx==nVal );
147505147917
147506147918
/* In case the cursor has been used before, clear it now. */
147507
- sqlite3_finalize(pCsr->pStmt);
147919
+ fts3CursorFinalizeStmt(pCsr);
147508147920
sqlite3_free(pCsr->aDoclist);
147509147921
sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
147510147922
sqlite3Fts3ExprFree(pCsr->pExpr);
147511147923
memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
147512147924
@@ -147570,11 +147982,11 @@
147570147982
sqlite3_free(zSql);
147571147983
}else{
147572147984
rc = SQLITE_NOMEM;
147573147985
}
147574147986
}else if( eSearch==FTS3_DOCID_SEARCH ){
147575
- rc = fts3CursorSeekStmt(pCsr, &pCsr->pStmt);
147987
+ rc = fts3CursorSeekStmt(pCsr);
147576147988
if( rc==SQLITE_OK ){
147577147989
rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
147578147990
}
147579147991
}
147580147992
if( rc!=SQLITE_OK ) return rc;
@@ -147734,11 +148146,11 @@
147734148146
sqlite3_stmt *pStmt = 0;
147735148147
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
147736148148
if( rc==SQLITE_OK ){
147737148149
int bHasStat = (sqlite3_step(pStmt)==SQLITE_ROW);
147738148150
rc = sqlite3_finalize(pStmt);
147739
- if( rc==SQLITE_OK ) p->bHasStat = bHasStat;
148151
+ if( rc==SQLITE_OK ) p->bHasStat = (u8)bHasStat;
147740148152
}
147741148153
sqlite3_free(zSql);
147742148154
}else{
147743148155
rc = SQLITE_NOMEM;
147744148156
}
@@ -162590,10 +163002,11 @@
162590163002
/* #include <stdio.h> */
162591163003
162592163004
#ifndef SQLITE_AMALGAMATION
162593163005
#include "sqlite3rtree.h"
162594163006
typedef sqlite3_int64 i64;
163007
+typedef sqlite3_uint64 u64;
162595163008
typedef unsigned char u8;
162596163009
typedef unsigned short u16;
162597163010
typedef unsigned int u32;
162598163011
#endif
162599163012
@@ -162638,28 +163051,33 @@
162638163051
struct Rtree {
162639163052
sqlite3_vtab base; /* Base class. Must be first */
162640163053
sqlite3 *db; /* Host database connection */
162641163054
int iNodeSize; /* Size in bytes of each node in the node table */
162642163055
u8 nDim; /* Number of dimensions */
163056
+ u8 nDim2; /* Twice the number of dimensions */
162643163057
u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
162644163058
u8 nBytesPerCell; /* Bytes consumed per cell */
163059
+ u8 inWrTrans; /* True if inside write transaction */
162645163060
int iDepth; /* Current depth of the r-tree structure */
162646163061
char *zDb; /* Name of database containing r-tree table */
162647163062
char *zName; /* Name of r-tree table */
162648
- int nBusy; /* Current number of users of this structure */
163063
+ u32 nBusy; /* Current number of users of this structure */
162649163064
i64 nRowEst; /* Estimated number of rows in this table */
163065
+ u32 nCursor; /* Number of open cursors */
162650163066
162651163067
/* List of nodes removed during a CondenseTree operation. List is
162652163068
** linked together via the pointer normally used for hash chains -
162653163069
** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
162654163070
** headed by the node (leaf nodes have RtreeNode.iNode==0).
162655163071
*/
162656163072
RtreeNode *pDeleted;
162657163073
int iReinsertHeight; /* Height of sub-trees Reinsert() has run on */
163074
+
163075
+ /* Blob I/O on xxx_node */
163076
+ sqlite3_blob *pNodeBlob;
162658163077
162659163078
/* Statements to read/write/delete a record from xxx_node */
162660
- sqlite3_stmt *pReadNode;
162661163079
sqlite3_stmt *pWriteNode;
162662163080
sqlite3_stmt *pDeleteNode;
162663163081
162664163082
/* Statements to read/write/delete a record from xxx_rowid */
162665163083
sqlite3_stmt *pReadRowid;
@@ -162884,26 +163302,106 @@
162884163302
#endif
162885163303
#ifndef MIN
162886163304
# define MIN(x,y) ((x) > (y) ? (y) : (x))
162887163305
#endif
162888163306
163307
+/* What version of GCC is being used. 0 means GCC is not being used */
163308
+#ifndef GCC_VERSION
163309
+#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
163310
+# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
163311
+#else
163312
+# define GCC_VERSION 0
163313
+#endif
163314
+#endif
163315
+
163316
+/* What version of CLANG is being used. 0 means CLANG is not being used */
163317
+#ifndef CLANG_VERSION
163318
+#if defined(__clang__) && !defined(_WIN32) && !defined(SQLITE_DISABLE_INTRINSIC)
163319
+# define CLANG_VERSION \
163320
+ (__clang_major__*1000000+__clang_minor__*1000+__clang_patchlevel__)
163321
+#else
163322
+# define CLANG_VERSION 0
163323
+#endif
163324
+#endif
163325
+
163326
+/* The testcase() macro should already be defined in the amalgamation. If
163327
+** it is not, make it a no-op.
163328
+*/
163329
+#ifndef SQLITE_AMALGAMATION
163330
+# define testcase(X)
163331
+#endif
163332
+
163333
+/*
163334
+** Macros to determine whether the machine is big or little endian,
163335
+** and whether or not that determination is run-time or compile-time.
163336
+**
163337
+** For best performance, an attempt is made to guess at the byte-order
163338
+** using C-preprocessor macros. If that is unsuccessful, or if
163339
+** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
163340
+** at run-time.
163341
+*/
163342
+#ifndef SQLITE_BYTEORDER
163343
+#if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
163344
+ defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
163345
+ defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
163346
+ defined(__arm__)
163347
+# define SQLITE_BYTEORDER 1234
163348
+#elif defined(sparc) || defined(__ppc__)
163349
+# define SQLITE_BYTEORDER 4321
163350
+#else
163351
+# define SQLITE_BYTEORDER 0 /* 0 means "unknown at compile-time" */
163352
+#endif
163353
+#endif
163354
+
163355
+
163356
+/* What version of MSVC is being used. 0 means MSVC is not being used */
163357
+#ifndef MSVC_VERSION
163358
+#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
163359
+# define MSVC_VERSION _MSC_VER
163360
+#else
163361
+# define MSVC_VERSION 0
163362
+#endif
163363
+#endif
163364
+
162889163365
/*
162890163366
** Functions to deserialize a 16 bit integer, 32 bit real number and
162891163367
** 64 bit integer. The deserialized value is returned.
162892163368
*/
162893163369
static int readInt16(u8 *p){
162894163370
return (p[0]<<8) + p[1];
162895163371
}
162896163372
static void readCoord(u8 *p, RtreeCoord *pCoord){
163373
+ assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */
163374
+#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163375
+ pCoord->u = _byteswap_ulong(*(u32*)p);
163376
+#elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
163377
+ pCoord->u = __builtin_bswap32(*(u32*)p);
163378
+#elif SQLITE_BYTEORDER==4321
163379
+ pCoord->u = *(u32*)p;
163380
+#else
162897163381
pCoord->u = (
162898163382
(((u32)p[0]) << 24) +
162899163383
(((u32)p[1]) << 16) +
162900163384
(((u32)p[2]) << 8) +
162901163385
(((u32)p[3]) << 0)
162902163386
);
163387
+#endif
162903163388
}
162904163389
static i64 readInt64(u8 *p){
163390
+#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163391
+ u64 x;
163392
+ memcpy(&x, p, 8);
163393
+ return (i64)_byteswap_uint64(x);
163394
+#elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
163395
+ u64 x;
163396
+ memcpy(&x, p, 8);
163397
+ return (i64)__builtin_bswap64(x);
163398
+#elif SQLITE_BYTEORDER==4321
163399
+ i64 x;
163400
+ memcpy(&x, p, 8);
163401
+ return x;
163402
+#else
162905163403
return (
162906163404
(((i64)p[0]) << 56) +
162907163405
(((i64)p[1]) << 48) +
162908163406
(((i64)p[2]) << 40) +
162909163407
(((i64)p[3]) << 32) +
@@ -162910,42 +163408,64 @@
162910163408
(((i64)p[4]) << 24) +
162911163409
(((i64)p[5]) << 16) +
162912163410
(((i64)p[6]) << 8) +
162913163411
(((i64)p[7]) << 0)
162914163412
);
163413
+#endif
162915163414
}
162916163415
162917163416
/*
162918163417
** Functions to serialize a 16 bit integer, 32 bit real number and
162919163418
** 64 bit integer. The value returned is the number of bytes written
162920163419
** to the argument buffer (always 2, 4 and 8 respectively).
162921163420
*/
162922
-static int writeInt16(u8 *p, int i){
163421
+static void writeInt16(u8 *p, int i){
162923163422
p[0] = (i>> 8)&0xFF;
162924163423
p[1] = (i>> 0)&0xFF;
162925
- return 2;
162926163424
}
162927163425
static int writeCoord(u8 *p, RtreeCoord *pCoord){
162928163426
u32 i;
163427
+ assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */
162929163428
assert( sizeof(RtreeCoord)==4 );
162930163429
assert( sizeof(u32)==4 );
163430
+#if SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
163431
+ i = __builtin_bswap32(pCoord->u);
163432
+ memcpy(p, &i, 4);
163433
+#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163434
+ i = _byteswap_ulong(pCoord->u);
163435
+ memcpy(p, &i, 4);
163436
+#elif SQLITE_BYTEORDER==4321
163437
+ i = pCoord->u;
163438
+ memcpy(p, &i, 4);
163439
+#else
162931163440
i = pCoord->u;
162932163441
p[0] = (i>>24)&0xFF;
162933163442
p[1] = (i>>16)&0xFF;
162934163443
p[2] = (i>> 8)&0xFF;
162935163444
p[3] = (i>> 0)&0xFF;
163445
+#endif
162936163446
return 4;
162937163447
}
162938163448
static int writeInt64(u8 *p, i64 i){
163449
+#if SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
163450
+ i = (i64)__builtin_bswap64((u64)i);
163451
+ memcpy(p, &i, 8);
163452
+#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163453
+ i = (i64)_byteswap_uint64((u64)i);
163454
+ memcpy(p, &i, 8);
163455
+#elif SQLITE_BYTEORDER==4321
163456
+ memcpy(p, &i, 8);
163457
+#else
162939163458
p[0] = (i>>56)&0xFF;
162940163459
p[1] = (i>>48)&0xFF;
162941163460
p[2] = (i>>40)&0xFF;
162942163461
p[3] = (i>>32)&0xFF;
162943163462
p[4] = (i>>24)&0xFF;
162944163463
p[5] = (i>>16)&0xFF;
162945163464
p[6] = (i>> 8)&0xFF;
162946163465
p[7] = (i>> 0)&0xFF;
163466
+#endif
162947163467
return 8;
162948163468
}
162949163469
162950163470
/*
162951163471
** Increment the reference count of node p.
@@ -163023,10 +163543,21 @@
163023163543
pNode->isDirty = 1;
163024163544
nodeReference(pParent);
163025163545
}
163026163546
return pNode;
163027163547
}
163548
+
163549
+/*
163550
+** Clear the Rtree.pNodeBlob object
163551
+*/
163552
+static void nodeBlobReset(Rtree *pRtree){
163553
+ if( pRtree->pNodeBlob && pRtree->inWrTrans==0 && pRtree->nCursor==0 ){
163554
+ sqlite3_blob *pBlob = pRtree->pNodeBlob;
163555
+ pRtree->pNodeBlob = 0;
163556
+ sqlite3_blob_close(pBlob);
163557
+ }
163558
+}
163028163559
163029163560
/*
163030163561
** Obtain a reference to an r-tree node.
163031163562
*/
163032163563
static int nodeAcquire(
@@ -163033,13 +163564,12 @@
163033163564
Rtree *pRtree, /* R-tree structure */
163034163565
i64 iNode, /* Node number to load */
163035163566
RtreeNode *pParent, /* Either the parent node or NULL */
163036163567
RtreeNode **ppNode /* OUT: Acquired node */
163037163568
){
163038
- int rc;
163039
- int rc2 = SQLITE_OK;
163040
- RtreeNode *pNode;
163569
+ int rc = SQLITE_OK;
163570
+ RtreeNode *pNode = 0;
163041163571
163042163572
/* Check if the requested node is already in the hash table. If so,
163043163573
** increase its reference count and return it.
163044163574
*/
163045163575
if( (pNode = nodeHashLookup(pRtree, iNode)) ){
@@ -163051,32 +163581,49 @@
163051163581
pNode->nRef++;
163052163582
*ppNode = pNode;
163053163583
return SQLITE_OK;
163054163584
}
163055163585
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;
163586
+ if( pRtree->pNodeBlob ){
163587
+ sqlite3_blob *pBlob = pRtree->pNodeBlob;
163588
+ pRtree->pNodeBlob = 0;
163589
+ rc = sqlite3_blob_reopen(pBlob, iNode);
163590
+ pRtree->pNodeBlob = pBlob;
163591
+ if( rc ){
163592
+ nodeBlobReset(pRtree);
163593
+ if( rc==SQLITE_NOMEM ) return SQLITE_NOMEM;
163594
+ }
163595
+ }
163596
+ if( pRtree->pNodeBlob==0 ){
163597
+ char *zTab = sqlite3_mprintf("%s_node", pRtree->zName);
163598
+ if( zTab==0 ) return SQLITE_NOMEM;
163599
+ rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, zTab, "data", iNode, 0,
163600
+ &pRtree->pNodeBlob);
163601
+ sqlite3_free(zTab);
163602
+ }
163603
+ if( rc ){
163604
+ nodeBlobReset(pRtree);
163605
+ *ppNode = 0;
163606
+ /* If unable to open an sqlite3_blob on the desired row, that can only
163607
+ ** be because the shadow tables hold erroneous data. */
163608
+ if( rc==SQLITE_ERROR ) rc = SQLITE_CORRUPT_VTAB;
163609
+ }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){
163610
+ pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
163611
+ if( !pNode ){
163612
+ rc = SQLITE_NOMEM;
163613
+ }else{
163614
+ pNode->pParent = pParent;
163615
+ pNode->zData = (u8 *)&pNode[1];
163616
+ pNode->nRef = 1;
163617
+ pNode->iNode = iNode;
163618
+ pNode->isDirty = 0;
163619
+ pNode->pNext = 0;
163620
+ rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
163621
+ pRtree->iNodeSize, 0);
163622
+ nodeReference(pParent);
163623
+ }
163624
+ }
163078163625
163079163626
/* If the root node was just loaded, set pRtree->iDepth to the height
163080163627
** of the r-tree structure. A height of zero means all data is stored on
163081163628
** the root node. A height of one means the children of the root node
163082163629
** are the leaves, and so on. If the depth as specified on the root node
@@ -163124,11 +163671,11 @@
163124163671
int iCell /* Index into pNode into which pCell is written */
163125163672
){
163126163673
int ii;
163127163674
u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
163128163675
p += writeInt64(p, pCell->iRowid);
163129
- for(ii=0; ii<(pRtree->nDim*2); ii++){
163676
+ for(ii=0; ii<pRtree->nDim2; ii++){
163130163677
p += writeCoord(p, &pCell->aCoord[ii]);
163131163678
}
163132163679
pNode->isDirty = 1;
163133163680
}
163134163681
@@ -163258,17 +163805,20 @@
163258163805
int iCell, /* Index of the cell within the node */
163259163806
RtreeCell *pCell /* OUT: Write the cell contents here */
163260163807
){
163261163808
u8 *pData;
163262163809
RtreeCoord *pCoord;
163263
- int ii;
163810
+ int ii = 0;
163264163811
pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
163265163812
pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
163266163813
pCoord = pCell->aCoord;
163267
- for(ii=0; ii<pRtree->nDim*2; ii++){
163268
- readCoord(&pData[ii*4], &pCoord[ii]);
163269
- }
163814
+ do{
163815
+ readCoord(pData, &pCoord[ii]);
163816
+ readCoord(pData+4, &pCoord[ii+1]);
163817
+ pData += 8;
163818
+ ii += 2;
163819
+ }while( ii<pRtree->nDim2 );
163270163820
}
163271163821
163272163822
163273163823
/* Forward declaration for the function that does the work of
163274163824
** the virtual table module xCreate() and xConnect() methods.
@@ -163315,11 +163865,13 @@
163315163865
** zero the structure is deleted.
163316163866
*/
163317163867
static void rtreeRelease(Rtree *pRtree){
163318163868
pRtree->nBusy--;
163319163869
if( pRtree->nBusy==0 ){
163320
- sqlite3_finalize(pRtree->pReadNode);
163870
+ pRtree->inWrTrans = 0;
163871
+ pRtree->nCursor = 0;
163872
+ nodeBlobReset(pRtree);
163321163873
sqlite3_finalize(pRtree->pWriteNode);
163322163874
sqlite3_finalize(pRtree->pDeleteNode);
163323163875
sqlite3_finalize(pRtree->pReadRowid);
163324163876
sqlite3_finalize(pRtree->pWriteRowid);
163325163877
sqlite3_finalize(pRtree->pDeleteRowid);
@@ -163353,10 +163905,11 @@
163353163905
pRtree->zDb, pRtree->zName
163354163906
);
163355163907
if( !zCreate ){
163356163908
rc = SQLITE_NOMEM;
163357163909
}else{
163910
+ nodeBlobReset(pRtree);
163358163911
rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
163359163912
sqlite3_free(zCreate);
163360163913
}
163361163914
if( rc==SQLITE_OK ){
163362163915
rtreeRelease(pRtree);
@@ -163368,17 +163921,19 @@
163368163921
/*
163369163922
** Rtree virtual table module xOpen method.
163370163923
*/
163371163924
static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
163372163925
int rc = SQLITE_NOMEM;
163926
+ Rtree *pRtree = (Rtree *)pVTab;
163373163927
RtreeCursor *pCsr;
163374163928
163375163929
pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
163376163930
if( pCsr ){
163377163931
memset(pCsr, 0, sizeof(RtreeCursor));
163378163932
pCsr->base.pVtab = pVTab;
163379163933
rc = SQLITE_OK;
163934
+ pRtree->nCursor++;
163380163935
}
163381163936
*ppCursor = (sqlite3_vtab_cursor *)pCsr;
163382163937
163383163938
return rc;
163384163939
}
@@ -163407,14 +163962,17 @@
163407163962
*/
163408163963
static int rtreeClose(sqlite3_vtab_cursor *cur){
163409163964
Rtree *pRtree = (Rtree *)(cur->pVtab);
163410163965
int ii;
163411163966
RtreeCursor *pCsr = (RtreeCursor *)cur;
163967
+ assert( pRtree->nCursor>0 );
163412163968
freeCursorConstraints(pCsr);
163413163969
sqlite3_free(pCsr->aPoint);
163414163970
for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
163415163971
sqlite3_free(pCsr);
163972
+ pRtree->nCursor--;
163973
+ nodeBlobReset(pRtree);
163416163974
return SQLITE_OK;
163417163975
}
163418163976
163419163977
/*
163420163978
** Rtree virtual table module xEof method.
@@ -163433,27 +163991,34 @@
163433163991
** endian platforms. The on-disk record stores integer coordinates if
163434163992
** eInt is true and it stores 32-bit floating point records if eInt is
163435163993
** false. a[] is the four bytes of the on-disk record to be decoded.
163436163994
** Store the results in "r".
163437163995
**
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.
163996
+** There are five versions of this macro. The last one is generic. The
163997
+** other four are various architectures-specific optimizations.
163445163998
*/
163446
-#if defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==1234
163999
+#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
164000
+#define RTREE_DECODE_COORD(eInt, a, r) { \
164001
+ RtreeCoord c; /* Coordinate decoded */ \
164002
+ c.u = _byteswap_ulong(*(u32*)a); \
164003
+ r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
164004
+}
164005
+#elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
164006
+#define RTREE_DECODE_COORD(eInt, a, r) { \
164007
+ RtreeCoord c; /* Coordinate decoded */ \
164008
+ c.u = __builtin_bswap32(*(u32*)a); \
164009
+ r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
164010
+}
164011
+#elif SQLITE_BYTEORDER==1234
163447164012
#define RTREE_DECODE_COORD(eInt, a, r) { \
163448164013
RtreeCoord c; /* Coordinate decoded */ \
163449164014
memcpy(&c.u,a,4); \
163450164015
c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)| \
163451164016
((c.u&0xff)<<24)|((c.u&0xff00)<<8); \
163452164017
r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
163453164018
}
163454
-#elif defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==4321
164019
+#elif SQLITE_BYTEORDER==4321
163455164020
#define RTREE_DECODE_COORD(eInt, a, r) { \
163456164021
RtreeCoord c; /* Coordinate decoded */ \
163457164022
memcpy(&c.u,a,4); \
163458164023
r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
163459164024
}
@@ -163476,30 +164041,58 @@
163476164041
u8 *pCellData, /* Raw cell content */
163477164042
RtreeSearchPoint *pSearch, /* Container of this cell */
163478164043
sqlite3_rtree_dbl *prScore, /* OUT: score for the cell */
163479164044
int *peWithin /* OUT: visibility of the cell */
163480164045
){
163481
- int i; /* Loop counter */
163482164046
sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
163483164047
int nCoord = pInfo->nCoord; /* No. of coordinates */
163484164048
int rc; /* Callback return code */
164049
+ RtreeCoord c; /* Translator union */
163485164050
sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2]; /* Decoded coordinates */
163486164051
163487164052
assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
163488164053
assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 );
163489164054
163490164055
if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
163491164056
pInfo->iRowid = readInt64(pCellData);
163492164057
}
163493164058
pCellData += 8;
163494
- for(i=0; i<nCoord; i++, pCellData += 4){
163495
- RTREE_DECODE_COORD(eInt, pCellData, aCoord[i]);
164059
+#ifndef SQLITE_RTREE_INT_ONLY
164060
+ if( eInt==0 ){
164061
+ switch( nCoord ){
164062
+ case 10: readCoord(pCellData+36, &c); aCoord[9] = c.f;
164063
+ readCoord(pCellData+32, &c); aCoord[8] = c.f;
164064
+ case 8: readCoord(pCellData+28, &c); aCoord[7] = c.f;
164065
+ readCoord(pCellData+24, &c); aCoord[6] = c.f;
164066
+ case 6: readCoord(pCellData+20, &c); aCoord[5] = c.f;
164067
+ readCoord(pCellData+16, &c); aCoord[4] = c.f;
164068
+ case 4: readCoord(pCellData+12, &c); aCoord[3] = c.f;
164069
+ readCoord(pCellData+8, &c); aCoord[2] = c.f;
164070
+ default: readCoord(pCellData+4, &c); aCoord[1] = c.f;
164071
+ readCoord(pCellData, &c); aCoord[0] = c.f;
164072
+ }
164073
+ }else
164074
+#endif
164075
+ {
164076
+ switch( nCoord ){
164077
+ case 10: readCoord(pCellData+36, &c); aCoord[9] = c.i;
164078
+ readCoord(pCellData+32, &c); aCoord[8] = c.i;
164079
+ case 8: readCoord(pCellData+28, &c); aCoord[7] = c.i;
164080
+ readCoord(pCellData+24, &c); aCoord[6] = c.i;
164081
+ case 6: readCoord(pCellData+20, &c); aCoord[5] = c.i;
164082
+ readCoord(pCellData+16, &c); aCoord[4] = c.i;
164083
+ case 4: readCoord(pCellData+12, &c); aCoord[3] = c.i;
164084
+ readCoord(pCellData+8, &c); aCoord[2] = c.i;
164085
+ default: readCoord(pCellData+4, &c); aCoord[1] = c.i;
164086
+ readCoord(pCellData, &c); aCoord[0] = c.i;
164087
+ }
163496164088
}
163497164089
if( pConstraint->op==RTREE_MATCH ){
164090
+ int eWithin = 0;
163498164091
rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
163499
- nCoord, aCoord, &i);
163500
- if( i==0 ) *peWithin = NOT_WITHIN;
164092
+ nCoord, aCoord, &eWithin);
164093
+ if( eWithin==0 ) *peWithin = NOT_WITHIN;
163501164094
*prScore = RTREE_ZERO;
163502164095
}else{
163503164096
pInfo->aCoord = aCoord;
163504164097
pInfo->iLevel = pSearch->iLevel - 1;
163505164098
pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
@@ -163531,10 +164124,11 @@
163531164124
*/
163532164125
pCellData += 8 + 4*(p->iCoord&0xfe);
163533164126
163534164127
assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
163535164128
|| p->op==RTREE_GT || p->op==RTREE_EQ );
164129
+ assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */
163536164130
switch( p->op ){
163537164131
case RTREE_LE:
163538164132
case RTREE_LT:
163539164133
case RTREE_EQ:
163540164134
RTREE_DECODE_COORD(eInt, pCellData, val);
@@ -163571,10 +164165,11 @@
163571164165
RtreeDValue xN; /* Coordinate value converted to a double */
163572164166
163573164167
assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
163574164168
|| p->op==RTREE_GT || p->op==RTREE_EQ );
163575164169
pCellData += 8 + p->iCoord*4;
164170
+ assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */
163576164171
RTREE_DECODE_COORD(eInt, pCellData, xN);
163577164172
switch( p->op ){
163578164173
case RTREE_LE: if( xN <= p->u.rValue ) return; break;
163579164174
case RTREE_LT: if( xN < p->u.rValue ) return; break;
163580164175
case RTREE_GE: if( xN >= p->u.rValue ) return; break;
@@ -163639,11 +164234,11 @@
163639164234
if( pA->iLevel>pB->iLevel ) return +1;
163640164235
return 0;
163641164236
}
163642164237
163643164238
/*
163644
-** Interchange to search points in a cursor.
164239
+** Interchange two search points in a cursor.
163645164240
*/
163646164241
static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
163647164242
RtreeSearchPoint t = p->aPoint[i];
163648164243
assert( i<j );
163649164244
p->aPoint[i] = p->aPoint[j];
@@ -163887,11 +164482,11 @@
163887164482
rtreeSearchPointPop(pCur);
163888164483
}
163889164484
if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO;
163890164485
p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
163891164486
if( p==0 ) return SQLITE_NOMEM;
163892
- p->eWithin = eWithin;
164487
+ p->eWithin = (u8)eWithin;
163893164488
p->id = x.id;
163894164489
p->iCell = x.iCell;
163895164490
RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
163896164491
break;
163897164492
}
@@ -163946,11 +164541,10 @@
163946164541
if( rc ) return rc;
163947164542
if( p==0 ) return SQLITE_OK;
163948164543
if( i==0 ){
163949164544
sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
163950164545
}else{
163951
- if( rc ) return rc;
163952164546
nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
163953164547
#ifndef SQLITE_RTREE_INT_ONLY
163954164548
if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
163955164549
sqlite3_result_double(ctx, c.f);
163956164550
}else
@@ -164075,11 +164669,11 @@
164075164669
assert( p!=0 ); /* Always returns pCsr->sPoint */
164076164670
pCsr->aNode[0] = pLeaf;
164077164671
p->id = iNode;
164078164672
p->eWithin = PARTLY_WITHIN;
164079164673
rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
164080
- p->iCell = iCell;
164674
+ p->iCell = (u8)iCell;
164081164675
RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
164082164676
}else{
164083164677
pCsr->atEOF = 1;
164084164678
}
164085164679
}else{
@@ -164108,11 +164702,11 @@
164108164702
*/
164109164703
rc = deserializeGeometry(argv[ii], p);
164110164704
if( rc!=SQLITE_OK ){
164111164705
break;
164112164706
}
164113
- p->pInfo->nCoord = pRtree->nDim*2;
164707
+ p->pInfo->nCoord = pRtree->nDim2;
164114164708
p->pInfo->anQueue = pCsr->anQueue;
164115164709
p->pInfo->mxLevel = pRtree->iDepth + 1;
164116164710
}else{
164117164711
#ifdef SQLITE_RTREE_INT_ONLY
164118164712
p->u.rValue = sqlite3_value_int64(argv[ii]);
@@ -164123,11 +164717,11 @@
164123164717
}
164124164718
}
164125164719
}
164126164720
if( rc==SQLITE_OK ){
164127164721
RtreeSearchPoint *pNew;
164128
- pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, pRtree->iDepth+1);
164722
+ pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
164129164723
if( pNew==0 ) return SQLITE_NOMEM;
164130164724
pNew->id = 1;
164131164725
pNew->iCell = 0;
164132164726
pNew->eWithin = PARTLY_WITHIN;
164133164727
assert( pCsr->bPoint==1 );
@@ -164141,23 +164735,10 @@
164141164735
nodeRelease(pRtree, pRoot);
164142164736
rtreeRelease(pRtree);
164143164737
return rc;
164144164738
}
164145164739
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
-
164159164740
/*
164160164741
** Rtree virtual table module xBestIndex method. There are three
164161164742
** table scan strategies to choose from (in order from most to
164162164743
** least desirable):
164163164744
**
@@ -164233,11 +164814,11 @@
164233164814
** considered almost as quick as a direct rowid lookup (for which
164234164815
** sqlite uses an internal cost of 0.0). It is expected to return
164235164816
** a single row.
164236164817
*/
164237164818
pIdxInfo->estimatedCost = 30.0;
164238
- setEstimatedRows(pIdxInfo, 1);
164819
+ pIdxInfo->estimatedRows = 1;
164239164820
return SQLITE_OK;
164240164821
}
164241164822
164242164823
if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
164243164824
u8 op;
@@ -164251,11 +164832,11 @@
164251164832
assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
164252164833
op = RTREE_MATCH;
164253164834
break;
164254164835
}
164255164836
zIdxStr[iIdx++] = op;
164256
- zIdxStr[iIdx++] = p->iColumn - 1 + '0';
164837
+ zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0');
164257164838
pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
164258164839
pIdxInfo->aConstraintUsage[ii].omit = 1;
164259164840
}
164260164841
}
164261164842
@@ -164265,55 +164846,75 @@
164265164846
return SQLITE_NOMEM;
164266164847
}
164267164848
164268164849
nRow = pRtree->nRowEst >> (iIdx/2);
164269164850
pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
164270
- setEstimatedRows(pIdxInfo, nRow);
164851
+ pIdxInfo->estimatedRows = nRow;
164271164852
164272164853
return rc;
164273164854
}
164274164855
164275164856
/*
164276164857
** Return the N-dimensional volumn of the cell stored in *p.
164277164858
*/
164278164859
static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
164279164860
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])));
164861
+ assert( pRtree->nDim>=1 && pRtree->nDim<=5 );
164862
+#ifndef SQLITE_RTREE_INT_ONLY
164863
+ if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
164864
+ switch( pRtree->nDim ){
164865
+ case 5: area = p->aCoord[9].f - p->aCoord[8].f;
164866
+ case 4: area *= p->aCoord[7].f - p->aCoord[6].f;
164867
+ case 3: area *= p->aCoord[5].f - p->aCoord[4].f;
164868
+ case 2: area *= p->aCoord[3].f - p->aCoord[2].f;
164869
+ default: area *= p->aCoord[1].f - p->aCoord[0].f;
164870
+ }
164871
+ }else
164872
+#endif
164873
+ {
164874
+ switch( pRtree->nDim ){
164875
+ case 5: area = p->aCoord[9].i - p->aCoord[8].i;
164876
+ case 4: area *= p->aCoord[7].i - p->aCoord[6].i;
164877
+ case 3: area *= p->aCoord[5].i - p->aCoord[4].i;
164878
+ case 2: area *= p->aCoord[3].i - p->aCoord[2].i;
164879
+ default: area *= p->aCoord[1].i - p->aCoord[0].i;
164880
+ }
164283164881
}
164284164882
return area;
164285164883
}
164286164884
164287164885
/*
164288164886
** Return the margin length of cell p. The margin length is the sum
164289164887
** of the objects size in each dimension.
164290164888
*/
164291164889
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){
164890
+ RtreeDValue margin = 0;
164891
+ int ii = pRtree->nDim2 - 2;
164892
+ do{
164295164893
margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
164296
- }
164894
+ ii -= 2;
164895
+ }while( ii>=0 );
164297164896
return margin;
164298164897
}
164299164898
164300164899
/*
164301164900
** Store the union of cells p1 and p2 in p1.
164302164901
*/
164303164902
static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
164304
- int ii;
164903
+ int ii = 0;
164305164904
if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
164306
- for(ii=0; ii<(pRtree->nDim*2); ii+=2){
164905
+ do{
164307164906
p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
164308164907
p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
164309
- }
164908
+ ii += 2;
164909
+ }while( ii<pRtree->nDim2 );
164310164910
}else{
164311
- for(ii=0; ii<(pRtree->nDim*2); ii+=2){
164911
+ do{
164312164912
p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
164313164913
p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
164314
- }
164914
+ ii += 2;
164915
+ }while( ii<pRtree->nDim2 );
164315164916
}
164316164917
}
164317164918
164318164919
/*
164319164920
** Return true if the area covered by p2 is a subset of the area covered
@@ -164320,11 +164921,11 @@
164320164921
** by p1. False otherwise.
164321164922
*/
164322164923
static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
164323164924
int ii;
164324164925
int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
164325
- for(ii=0; ii<(pRtree->nDim*2); ii+=2){
164926
+ for(ii=0; ii<pRtree->nDim2; ii+=2){
164326164927
RtreeCoord *a1 = &p1->aCoord[ii];
164327164928
RtreeCoord *a2 = &p2->aCoord[ii];
164328164929
if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f))
164329164930
|| ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i))
164330164931
){
@@ -164355,11 +164956,11 @@
164355164956
int ii;
164356164957
RtreeDValue overlap = RTREE_ZERO;
164357164958
for(ii=0; ii<nCell; ii++){
164358164959
int jj;
164359164960
RtreeDValue o = (RtreeDValue)1;
164360
- for(jj=0; jj<(pRtree->nDim*2); jj+=2){
164961
+ for(jj=0; jj<pRtree->nDim2; jj+=2){
164361164962
RtreeDValue x1, x2;
164362164963
x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
164363164964
x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
164364164965
if( x2<x1 ){
164365164966
o = (RtreeDValue)0;
@@ -165411,11 +166012,11 @@
165411166012
** with "column" that are interpreted as table constraints.
165412166013
** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
165413166014
** This problem was discovered after years of use, so we silently ignore
165414166015
** these kinds of misdeclared tables to avoid breaking any legacy.
165415166016
*/
165416
- assert( nData<=(pRtree->nDim*2 + 3) );
166017
+ assert( nData<=(pRtree->nDim2 + 3) );
165417166018
165418166019
#ifndef SQLITE_RTREE_INT_ONLY
165419166020
if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
165420166021
for(ii=0; ii<nData-4; ii+=2){
165421166022
cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]);
@@ -165500,10 +166101,31 @@
165500166101
165501166102
constraint:
165502166103
rtreeRelease(pRtree);
165503166104
return rc;
165504166105
}
166106
+
166107
+/*
166108
+** Called when a transaction starts.
166109
+*/
166110
+static int rtreeBeginTransaction(sqlite3_vtab *pVtab){
166111
+ Rtree *pRtree = (Rtree *)pVtab;
166112
+ assert( pRtree->inWrTrans==0 );
166113
+ pRtree->inWrTrans++;
166114
+ return SQLITE_OK;
166115
+}
166116
+
166117
+/*
166118
+** Called when a transaction completes (either by COMMIT or ROLLBACK).
166119
+** The sqlite3_blob object should be released at this point.
166120
+*/
166121
+static int rtreeEndTransaction(sqlite3_vtab *pVtab){
166122
+ Rtree *pRtree = (Rtree *)pVtab;
166123
+ pRtree->inWrTrans = 0;
166124
+ nodeBlobReset(pRtree);
166125
+ return SQLITE_OK;
166126
+}
165505166127
165506166128
/*
165507166129
** The xRename method for rtree module virtual tables.
165508166130
*/
165509166131
static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
@@ -165521,10 +166143,11 @@
165521166143
rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
165522166144
sqlite3_free(zSql);
165523166145
}
165524166146
return rc;
165525166147
}
166148
+
165526166149
165527166150
/*
165528166151
** This function populates the pRtree->nRowEst variable with an estimate
165529166152
** of the number of rows in the virtual table. If possible, this is based
165530166153
** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
@@ -165581,19 +166204,19 @@
165581166204
rtreeNext, /* xNext - advance a cursor */
165582166205
rtreeEof, /* xEof */
165583166206
rtreeColumn, /* xColumn - read data */
165584166207
rtreeRowid, /* xRowid - read data */
165585166208
rtreeUpdate, /* xUpdate - write data */
165586
- 0, /* xBegin - begin transaction */
165587
- 0, /* xSync - sync transaction */
165588
- 0, /* xCommit - commit transaction */
165589
- 0, /* xRollback - rollback transaction */
166209
+ rtreeBeginTransaction, /* xBegin - begin transaction */
166210
+ rtreeEndTransaction, /* xSync - sync transaction */
166211
+ rtreeEndTransaction, /* xCommit - commit transaction */
166212
+ rtreeEndTransaction, /* xRollback - rollback transaction */
165590166213
0, /* xFindFunction - function overloading */
165591166214
rtreeRename, /* xRename - rename the table */
165592166215
0, /* xSavepoint */
165593166216
0, /* xRelease */
165594
- 0 /* xRollbackTo */
166217
+ 0, /* xRollbackTo */
165595166218
};
165596166219
165597166220
static int rtreeSqlInit(
165598166221
Rtree *pRtree,
165599166222
sqlite3 *db,
@@ -165601,14 +166224,13 @@
165601166224
const char *zPrefix,
165602166225
int isCreate
165603166226
){
165604166227
int rc = SQLITE_OK;
165605166228
165606
- #define N_STATEMENT 9
166229
+ #define N_STATEMENT 8
165607166230
static const char *azSql[N_STATEMENT] = {
165608
- /* Read and write the xxx_node table */
165609
- "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1",
166231
+ /* Write the xxx_node table */
165610166232
"INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
165611166233
"DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
165612166234
165613166235
/* Read and write the xxx_rowid table */
165614166236
"SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
@@ -165642,19 +166264,18 @@
165642166264
if( rc!=SQLITE_OK ){
165643166265
return rc;
165644166266
}
165645166267
}
165646166268
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;
166269
+ appStmt[0] = &pRtree->pWriteNode;
166270
+ appStmt[1] = &pRtree->pDeleteNode;
166271
+ appStmt[2] = &pRtree->pReadRowid;
166272
+ appStmt[3] = &pRtree->pWriteRowid;
166273
+ appStmt[4] = &pRtree->pDeleteRowid;
166274
+ appStmt[5] = &pRtree->pReadParent;
166275
+ appStmt[6] = &pRtree->pWriteParent;
166276
+ appStmt[7] = &pRtree->pDeleteParent;
165656166277
165657166278
rc = rtreeQueryStat1(db, pRtree);
165658166279
for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
165659166280
char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
165660166281
if( zSql ){
@@ -165788,13 +166409,14 @@
165788166409
memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
165789166410
pRtree->nBusy = 1;
165790166411
pRtree->base.pModule = &rtreeModule;
165791166412
pRtree->zDb = (char *)&pRtree[1];
165792166413
pRtree->zName = &pRtree->zDb[nDb+1];
165793
- pRtree->nDim = (argc-4)/2;
165794
- pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2;
165795
- pRtree->eCoordType = eCoordType;
166414
+ pRtree->nDim = (u8)((argc-4)/2);
166415
+ pRtree->nDim2 = pRtree->nDim*2;
166416
+ pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
166417
+ pRtree->eCoordType = (u8)eCoordType;
165796166418
memcpy(pRtree->zDb, argv[1], nDb);
165797166419
memcpy(pRtree->zName, argv[2], nName);
165798166420
165799166421
/* Figure out the node size to use. */
165800166422
rc = getNodeSize(db, pRtree, isCreate, pzErr);
@@ -165863,11 +166485,12 @@
165863166485
int ii;
165864166486
165865166487
UNUSED_PARAMETER(nArg);
165866166488
memset(&node, 0, sizeof(RtreeNode));
165867166489
memset(&tree, 0, sizeof(Rtree));
165868
- tree.nDim = sqlite3_value_int(apArg[0]);
166490
+ tree.nDim = (u8)sqlite3_value_int(apArg[0]);
166491
+ tree.nDim2 = tree.nDim*2;
165869166492
tree.nBytesPerCell = 8 + 8 * tree.nDim;
165870166493
node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
165871166494
165872166495
for(ii=0; ii<NCELL(&node); ii++){
165873166496
char zCell[512];
@@ -165876,11 +166499,11 @@
165876166499
int jj;
165877166500
165878166501
nodeGetCell(&tree, &node, ii, &cell);
165879166502
sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
165880166503
nCell = (int)strlen(zCell);
165881
- for(jj=0; jj<tree.nDim*2; jj++){
166504
+ for(jj=0; jj<tree.nDim2; jj++){
165882166505
#ifndef SQLITE_RTREE_INT_ONLY
165883166506
sqlite3_snprintf(512-nCell,&zCell[nCell], " %g",
165884166507
(double)cell.aCoord[jj].f);
165885166508
#else
165886166509
sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
@@ -166584,42 +167207,40 @@
166584167207
166585167208
/*
166586167209
** Register the ICU extension functions with database db.
166587167210
*/
166588167211
SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
166589
- struct IcuScalar {
167212
+ static const struct IcuScalar {
166590167213
const char *zName; /* Function name */
166591
- int nArg; /* Number of arguments */
166592
- int enc; /* Optimal text encoding */
166593
- void *pContext; /* sqlite3_user_data() context */
167214
+ unsigned char nArg; /* Number of arguments */
167215
+ unsigned short enc; /* Optimal text encoding */
167216
+ unsigned char iContext; /* sqlite3_user_data() context */
166594167217
void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
166595167218
} 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},
167219
+ {"icu_load_collation", 2, SQLITE_UTF8, 1, icuLoadCollation},
167220
+ {"regexp", 2, SQLITE_ANY|SQLITE_DETERMINISTIC, 0, icuRegexpFunc},
167221
+ {"lower", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
167222
+ {"lower", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
167223
+ {"upper", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
167224
+ {"upper", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
167225
+ {"lower", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
167226
+ {"lower", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
167227
+ {"upper", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
167228
+ {"upper", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
167229
+ {"like", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc},
167230
+ {"like", 3, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc},
166612167231
};
166613
-
166614167232
int rc = SQLITE_OK;
166615167233
int i;
166616167234
167235
+
166617167236
for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
166618
- struct IcuScalar *p = &scalars[i];
167237
+ const struct IcuScalar *p = &scalars[i];
166619167238
rc = sqlite3_create_function(
166620
- db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0
167239
+ db, p->zName, p->nArg, p->enc,
167240
+ p->iContext ? (void*)db : (void*)0,
167241
+ p->xFunc, 0, 0
166621167242
);
166622167243
}
166623167244
166624167245
return rc;
166625167246
}
@@ -169823,11 +170444,11 @@
169823170444
169824170445
/*
169825170446
** Open the database handle and attach the RBU database as "rbu". If an
169826170447
** error occurs, leave an error code and message in the RBU handle.
169827170448
*/
169828
-static void rbuOpenDatabase(sqlite3rbu *p){
170449
+static void rbuOpenDatabase(sqlite3rbu *p, int *pbRetry){
169829170450
assert( p->rc || (p->dbMain==0 && p->dbRbu==0) );
169830170451
assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 );
169831170452
169832170453
/* Open the RBU database */
169833170454
p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
@@ -169898,11 +170519,11 @@
169898170519
if( p->eStage>=RBU_STAGE_MOVE ){
169899170520
bOpen = 1;
169900170521
}else{
169901170522
RbuState *pState = rbuLoadState(p);
169902170523
if( pState ){
169903
- bOpen = (pState->eStage>RBU_STAGE_MOVE);
170524
+ bOpen = (pState->eStage>=RBU_STAGE_MOVE);
169904170525
rbuFreeState(pState);
169905170526
}
169906170527
}
169907170528
if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
169908170529
}
@@ -169910,10 +170531,19 @@
169910170531
p->eStage = 0;
169911170532
if( p->rc==SQLITE_OK && p->dbMain==0 ){
169912170533
if( !rbuIsVacuum(p) ){
169913170534
p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
169914170535
}else if( p->pRbuFd->pWalFd ){
170536
+ if( pbRetry ){
170537
+ p->pRbuFd->bNolock = 0;
170538
+ sqlite3_close(p->dbRbu);
170539
+ sqlite3_close(p->dbMain);
170540
+ p->dbMain = 0;
170541
+ p->dbRbu = 0;
170542
+ *pbRetry = 1;
170543
+ return;
170544
+ }
169915170545
p->rc = SQLITE_ERROR;
169916170546
p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
169917170547
}else{
169918170548
char *zTarget;
169919170549
char *zExtra = 0;
@@ -170090,20 +170720,22 @@
170090170720
p->eStage = RBU_STAGE_CAPTURE;
170091170721
rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
170092170722
if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
170093170723
}
170094170724
170095
- if( p->rc==SQLITE_OK ){
170725
+ if( p->rc==SQLITE_OK && p->nFrame>0 ){
170096170726
p->eStage = RBU_STAGE_CKPT;
170097170727
p->nStep = (pState ? pState->nRow : 0);
170098170728
p->aBuf = rbuMalloc(p, p->pgsz);
170099170729
p->iWalCksum = rbuShmChecksum(p);
170100170730
}
170101170731
170102
- if( p->rc==SQLITE_OK && pState && pState->iWalCksum!=p->iWalCksum ){
170103
- p->rc = SQLITE_DONE;
170104
- p->eStage = RBU_STAGE_DONE;
170732
+ if( p->rc==SQLITE_OK ){
170733
+ if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
170734
+ p->rc = SQLITE_DONE;
170735
+ p->eStage = RBU_STAGE_DONE;
170736
+ }
170105170737
}
170106170738
}
170107170739
170108170740
/*
170109170741
** Called when iAmt bytes are read from offset iOff of the wal file while
@@ -170272,11 +170904,11 @@
170272170904
#else
170273170905
p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
170274170906
#endif
170275170907
170276170908
if( p->rc==SQLITE_OK ){
170277
- rbuOpenDatabase(p);
170909
+ rbuOpenDatabase(p, 0);
170278170910
rbuSetupCheckpoint(p, 0);
170279170911
}
170280170912
}
170281170913
}
170282170914
@@ -170983,10 +171615,11 @@
170983171615
rbuCreateVfs(p);
170984171616
170985171617
/* Open the target, RBU and state databases */
170986171618
if( p->rc==SQLITE_OK ){
170987171619
char *pCsr = (char*)&p[1];
171620
+ int bRetry = 0;
170988171621
if( zTarget ){
170989171622
p->zTarget = pCsr;
170990171623
memcpy(p->zTarget, zTarget, nTarget+1);
170991171624
pCsr += nTarget+1;
170992171625
}
@@ -170994,11 +171627,22 @@
170994171627
memcpy(p->zRbu, zRbu, nRbu+1);
170995171628
pCsr += nRbu+1;
170996171629
if( zState ){
170997171630
p->zState = rbuMPrintf(p, "%s", zState);
170998171631
}
170999
- rbuOpenDatabase(p);
171632
+
171633
+ /* If the first attempt to open the database file fails and the bRetry
171634
+ ** flag it set, this means that the db was not opened because it seemed
171635
+ ** to be a wal-mode db. But, this may have happened due to an earlier
171636
+ ** RBU vacuum operation leaving an old wal file in the directory.
171637
+ ** If this is the case, it will have been checkpointed and deleted
171638
+ ** when the handle was closed and a second attempt to open the
171639
+ ** database may succeed. */
171640
+ rbuOpenDatabase(p, &bRetry);
171641
+ if( bRetry ){
171642
+ rbuOpenDatabase(p, 0);
171643
+ }
171000171644
}
171001171645
171002171646
if( p->rc==SQLITE_OK ){
171003171647
pState = rbuLoadState(p);
171004171648
assert( pState || p->rc!=SQLITE_OK );
@@ -175957,11 +176601,11 @@
175957176601
sqlite3_value **ppValue /* OUT: Value from conflicting row */
175958176602
){
175959176603
if( !pIter->pConflict ){
175960176604
return SQLITE_MISUSE;
175961176605
}
175962
- if( iVal<0 || iVal>=sqlite3_column_count(pIter->pConflict) ){
176606
+ if( iVal<0 || iVal>=pIter->nCol ){
175963176607
return SQLITE_RANGE;
175964176608
}
175965176609
*ppValue = sqlite3_column_value(pIter->pConflict, iVal);
175966176610
return SQLITE_OK;
175967176611
}
@@ -176424,11 +177068,17 @@
176424177068
int i;
176425177069
SessionBuffer buf = {0, 0, 0};
176426177070
176427177071
sessionAppendStr(&buf, "INSERT INTO main.", &rc);
176428177072
sessionAppendIdent(&buf, zTab, &rc);
176429
- sessionAppendStr(&buf, " VALUES(?", &rc);
177073
+ sessionAppendStr(&buf, "(", &rc);
177074
+ for(i=0; i<p->nCol; i++){
177075
+ if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
177076
+ sessionAppendIdent(&buf, p->azCol[i], &rc);
177077
+ }
177078
+
177079
+ sessionAppendStr(&buf, ") VALUES(?", &rc);
176430177080
for(i=1; i<p->nCol; i++){
176431177081
sessionAppendStr(&buf, ", ?", &rc);
176432177082
}
176433177083
sessionAppendStr(&buf, ")", &rc);
176434177084
@@ -176970,42 +177620,51 @@
176970177620
break;
176971177621
}
176972177622
nTab = (int)strlen(zTab);
176973177623
sApply.azCol = (const char **)zTab;
176974177624
}else{
177625
+ int nMinCol = 0;
177626
+ int i;
177627
+
176975177628
sqlite3changeset_pk(pIter, &abPK, 0);
176976177629
rc = sessionTableInfo(
176977177630
db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
176978177631
);
176979177632
if( rc!=SQLITE_OK ) break;
177633
+ for(i=0; i<sApply.nCol; i++){
177634
+ if( sApply.abPK[i] ) nMinCol = i+1;
177635
+ }
176980177636
176981177637
if( sApply.nCol==0 ){
176982177638
schemaMismatch = 1;
176983177639
sqlite3_log(SQLITE_SCHEMA,
176984177640
"sqlite3changeset_apply(): no such table: %s", zTab
176985177641
);
176986177642
}
176987
- else if( sApply.nCol!=nCol ){
177643
+ else if( sApply.nCol<nCol ){
176988177644
schemaMismatch = 1;
176989177645
sqlite3_log(SQLITE_SCHEMA,
176990
- "sqlite3changeset_apply(): table %s has %d columns, expected %d",
177646
+ "sqlite3changeset_apply(): table %s has %d columns, "
177647
+ "expected %d or more",
176991177648
zTab, sApply.nCol, nCol
176992177649
);
176993177650
}
176994
- else if( memcmp(sApply.abPK, abPK, nCol)!=0 ){
177651
+ else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
176995177652
schemaMismatch = 1;
176996177653
sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
176997177654
"primary key mismatch for table %s", zTab
176998177655
);
176999177656
}
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;
177657
+ else{
177658
+ sApply.nCol = nCol;
177659
+ if((rc = sessionSelectRow(db, zTab, &sApply))
177660
+ || (rc = sessionUpdateRow(db, zTab, &sApply))
177661
+ || (rc = sessionDeleteRow(db, zTab, &sApply))
177662
+ || (rc = sessionInsertRow(db, zTab, &sApply))
177663
+ ){
177664
+ break;
177665
+ }
177007177666
}
177008177667
nTab = sqlite3Strlen30(zTab);
177009177668
}
177010177669
}
177011177670
@@ -177593,11 +178252,11 @@
177593178252
** a BLOB, but there is no support for JSONB in the current implementation.
177594178253
** This implementation parses JSON text at 250 MB/s, so it is hard to see
177595178254
** how JSONB might improve on that.)
177596178255
*/
177597178256
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
177598
-#if !defined(_SQLITEINT_H_)
178257
+#if !defined(SQLITEINT_H)
177599178258
/* #include "sqlite3ext.h" */
177600178259
#endif
177601178260
SQLITE_EXTENSION_INIT1
177602178261
/* #include <assert.h> */
177603178262
/* #include <string.h> */
@@ -181644,10 +182303,35 @@
181644182303
*/
181645182304
#ifndef fts5YYMALLOCARGTYPE
181646182305
# define fts5YYMALLOCARGTYPE size_t
181647182306
#endif
181648182307
182308
+/* Initialize a new parser that has already been allocated.
182309
+*/
182310
+static void sqlite3Fts5ParserInit(void *fts5yypParser){
182311
+ fts5yyParser *pParser = (fts5yyParser*)fts5yypParser;
182312
+#ifdef fts5YYTRACKMAXSTACKDEPTH
182313
+ pParser->fts5yyhwm = 0;
182314
+#endif
182315
+#if fts5YYSTACKDEPTH<=0
182316
+ pParser->fts5yytos = NULL;
182317
+ pParser->fts5yystack = NULL;
182318
+ pParser->fts5yystksz = 0;
182319
+ if( fts5yyGrowStack(pParser) ){
182320
+ pParser->fts5yystack = &pParser->fts5yystk0;
182321
+ pParser->fts5yystksz = 1;
182322
+ }
182323
+#endif
182324
+#ifndef fts5YYNOERRORRECOVERY
182325
+ pParser->fts5yyerrcnt = -1;
182326
+#endif
182327
+ pParser->fts5yytos = pParser->fts5yystack;
182328
+ pParser->fts5yystack[0].stateno = 0;
182329
+ pParser->fts5yystack[0].major = 0;
182330
+}
182331
+
182332
+#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
181649182333
/*
181650182334
** This function allocates a new parser.
181651182335
** The only argument is a pointer to a function which works like
181652182336
** malloc.
181653182337
**
@@ -181659,32 +182343,15 @@
181659182343
** to sqlite3Fts5Parser and sqlite3Fts5ParserFree.
181660182344
*/
181661182345
static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(fts5YYMALLOCARGTYPE)){
181662182346
fts5yyParser *pParser;
181663182347
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
- }
182348
+ if( pParser ) sqlite3Fts5ParserInit(pParser);
181684182349
return pParser;
181685182350
}
182351
+#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
182352
+
181686182353
181687182354
/* The following function deletes the "minor type" or semantic value
181688182355
** associated with a symbol. The symbol can be either a terminal
181689182356
** or nonterminal. "fts5yymajor" is the symbol code, and "fts5yypminor" is
181690182357
** a pointer to the value to be deleted. The code used to do the
@@ -181762,10 +182429,22 @@
181762182429
}
181763182430
#endif
181764182431
fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor);
181765182432
}
181766182433
182434
+/*
182435
+** Clear all secondary memory allocations from the parser
182436
+*/
182437
+static void sqlite3Fts5ParserFinalize(void *p){
182438
+ fts5yyParser *pParser = (fts5yyParser*)p;
182439
+ while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
182440
+#if fts5YYSTACKDEPTH<=0
182441
+ if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
182442
+#endif
182443
+}
182444
+
182445
+#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
181767182446
/*
181768182447
** Deallocate and destroy a parser. Destructors are called for
181769182448
** all stack elements before shutting the parser down.
181770182449
**
181771182450
** If the fts5YYPARSEFREENEVERNULL macro exists (for example because it
@@ -181774,20 +182453,17 @@
181774182453
*/
181775182454
static void sqlite3Fts5ParserFree(
181776182455
void *p, /* The parser to be deleted */
181777182456
void (*freeProc)(void*) /* Function used to reclaim memory */
181778182457
){
181779
- fts5yyParser *pParser = (fts5yyParser*)p;
181780182458
#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
-}
182459
+ if( p==0 ) return;
182460
+#endif
182461
+ sqlite3Fts5ParserFinalize(p);
182462
+ (*freeProc)(p);
182463
+}
182464
+#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
181789182465
181790182466
/*
181791182467
** Return the peak depth of the stack for a parser.
181792182468
*/
181793182469
#ifdef fts5YYTRACKMAXSTACKDEPTH
@@ -186122,11 +186798,11 @@
186122186798
memset(&sCtx, 0, sizeof(TokenCtx));
186123186799
sCtx.pPhrase = pAppend;
186124186800
186125186801
rc = fts5ParseStringFromToken(pToken, &z);
186126186802
if( rc==SQLITE_OK ){
186127
- int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_QUERY : 0);
186803
+ int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_PREFIX : 0);
186128186804
int n;
186129186805
sqlite3Fts5Dequote(z);
186130186806
n = (int)strlen(z);
186131186807
rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize);
186132186808
}
@@ -196863,11 +197539,11 @@
196863197539
int nArg, /* Number of args */
196864197540
sqlite3_value **apUnused /* Function arguments */
196865197541
){
196866197542
assert( nArg==0 );
196867197543
UNUSED_PARAM2(nArg, apUnused);
196868
- sqlite3_result_text(pCtx, "fts5: 2017-01-06 16:32:41 a65a62893ca8319e89e48b8a38cf8a59c69a8209", -1, SQLITE_TRANSIENT);
197544
+ sqlite3_result_text(pCtx, "fts5: 2017-02-13 16:02:40 ada05cfa86ad7f5645450ac7a2a21c9aa6e57d2c", -1, SQLITE_TRANSIENT);
196869197545
}
196870197546
196871197547
static int fts5Init(sqlite3 *db){
196872197548
static const sqlite3_module fts5Mod = {
196873197549
/* iVersion */ 2,
196874197550
--- 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.
@@ -202,15 +202,31 @@
202 # define _FILE_OFFSET_BITS 64
203 # endif
204 # define _LARGEFILE_SOURCE 1
205 #endif
206
207 /* What version of GCC is being used. 0 means GCC is not being used */
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
@@ -379,13 +395,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 +533,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 +850,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 +1000,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 +5691,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 +6473,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 +6502,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 +6521,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 +8454,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 +8479,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
@@ -8887,11 +8924,11 @@
8887 ** The session object will be used to create changesets for tables in
8888 ** database zDb, where zDb is either "main", or "temp", or the name of an
8889 ** attached database. It is not an error if database zDb is not attached
8890 ** to the database when the session object is created.
8891 */
8892 int sqlite3session_create(
8893 sqlite3 *db, /* Database handle */
8894 const char *zDb, /* Name of db (e.g. "main") */
8895 sqlite3_session **ppSession /* OUT: New session object */
8896 );
8897
@@ -8905,11 +8942,11 @@
8905 **
8906 ** Session objects must be deleted before the database handle to which they
8907 ** are attached is closed. Refer to the documentation for
8908 ** [sqlite3session_create()] for details.
8909 */
8910 void sqlite3session_delete(sqlite3_session *pSession);
8911
8912
8913 /*
8914 ** CAPI3REF: Enable Or Disable A Session Object
8915 **
@@ -8925,11 +8962,11 @@
8925 ** no-op, and may be used to query the current state of the session.
8926 **
8927 ** The return value indicates the final state of the session object: 0 if
8928 ** the session is disabled, or 1 if it is enabled.
8929 */
8930 int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
8931
8932 /*
8933 ** CAPI3REF: Set Or Clear the Indirect Change Flag
8934 **
8935 ** Each change recorded by a session object is marked as either direct or
@@ -8954,11 +8991,11 @@
8954 ** indirect flag for the specified session object.
8955 **
8956 ** The return value indicates the final state of the indirect flag: 0 if
8957 ** it is clear, or 1 if it is set.
8958 */
8959 int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
8960
8961 /*
8962 ** CAPI3REF: Attach A Table To A Session Object
8963 **
8964 ** If argument zTab is not NULL, then it is the name of a table to attach
@@ -8984,11 +9021,11 @@
8984 ** in one or more of their PRIMARY KEY columns.
8985 **
8986 ** SQLITE_OK is returned if the call completes without error. Or, if an error
8987 ** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
8988 */
8989 int sqlite3session_attach(
8990 sqlite3_session *pSession, /* Session object */
8991 const char *zTab /* Table name */
8992 );
8993
8994 /*
@@ -8998,11 +9035,11 @@
8998 ** in tables that are not attached to the Session object, the filter is called
8999 ** to determine whether changes to the table's rows should be tracked or not.
9000 ** If xFilter returns 0, changes is not tracked. Note that once a table is
9001 ** attached, xFilter will not be called again.
9002 */
9003 void sqlite3session_table_filter(
9004 sqlite3_session *pSession, /* Session object */
9005 int(*xFilter)(
9006 void *pCtx, /* Copy of third arg to _filter_table() */
9007 const char *zTab /* Table name */
9008 ),
@@ -9111,11 +9148,11 @@
9111 ** changeset, even though the delete took place while the session was disabled.
9112 ** Or, if one field of a row is updated while a session is disabled, and
9113 ** another field of the same row is updated while the session is enabled, the
9114 ** resulting changeset will contain an UPDATE change that updates both fields.
9115 */
9116 int sqlite3session_changeset(
9117 sqlite3_session *pSession, /* Session object */
9118 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
9119 void **ppChangeset /* OUT: Buffer containing changeset */
9120 );
9121
@@ -9155,11 +9192,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
@@ -9172,11 +9210,11 @@
9172 ** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
9173 ** may be set to point to a buffer containing an English language error
9174 ** message. It is the responsibility of the caller to free this buffer using
9175 ** sqlite3_free().
9176 */
9177 int sqlite3session_diff(
9178 sqlite3_session *pSession,
9179 const char *zFromDb,
9180 const char *zTbl,
9181 char **pzErrMsg
9182 );
@@ -9208,11 +9246,11 @@
9208 ** Changes within a patchset are ordered in the same way as for changesets
9209 ** generated by the sqlite3session_changeset() function (i.e. all changes for
9210 ** a single table are grouped together, tables appear in the order in which
9211 ** they were attached to the session object).
9212 */
9213 int sqlite3session_patchset(
9214 sqlite3_session *pSession, /* Session object */
9215 int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */
9216 void **ppPatchset /* OUT: Buffer containing changeset */
9217 );
9218
@@ -9229,11 +9267,11 @@
9229 ** an attached table is modified and then later on the original values
9230 ** are restored. However, if this function returns non-zero, then it is
9231 ** guaranteed that a call to sqlite3session_changeset() will return a
9232 ** changeset containing zero changes.
9233 */
9234 int sqlite3session_isempty(sqlite3_session *pSession);
9235
9236 /*
9237 ** CAPI3REF: Create An Iterator To Traverse A Changeset
9238 **
9239 ** Create an iterator used to iterate through the contents of a changeset.
@@ -9264,11 +9302,11 @@
9264 ** this function, all changes that relate to a single table are visited
9265 ** consecutively. There is no chance that the iterator will visit a change
9266 ** the applies to table X, then one for table Y, and then later on visit
9267 ** another change for table X.
9268 */
9269 int sqlite3changeset_start(
9270 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
9271 int nChangeset, /* Size of changeset blob in bytes */
9272 void *pChangeset /* Pointer to blob containing changeset */
9273 );
9274
@@ -9293,11 +9331,11 @@
9293 **
9294 ** If an error occurs, an SQLite error code is returned. Possible error
9295 ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or
9296 ** SQLITE_NOMEM.
9297 */
9298 int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
9299
9300 /*
9301 ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
9302 **
9303 ** The pIter argument passed to this function may either be an iterator
@@ -9321,11 +9359,11 @@
9321 **
9322 ** If no error occurs, SQLITE_OK is returned. If an error does occur, an
9323 ** SQLite error code is returned. The values of the output variables may not
9324 ** be trusted in this case.
9325 */
9326 int sqlite3changeset_op(
9327 sqlite3_changeset_iter *pIter, /* Iterator object */
9328 const char **pzTab, /* OUT: Pointer to table name */
9329 int *pnCol, /* OUT: Number of columns in table */
9330 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
9331 int *pbIndirect /* OUT: True for an 'indirect' change */
@@ -9354,11 +9392,11 @@
9354 ** If this function is called when the iterator does not point to a valid
9355 ** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
9356 ** SQLITE_OK is returned and the output variables populated as described
9357 ** above.
9358 */
9359 int sqlite3changeset_pk(
9360 sqlite3_changeset_iter *pIter, /* Iterator object */
9361 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
9362 int *pnCol /* OUT: Number of entries in output array */
9363 );
9364
@@ -9384,11 +9422,11 @@
9384 ** is similar to the "old.*" columns available to update or delete triggers.
9385 **
9386 ** If some other error occurs (e.g. an OOM condition), an SQLite error code
9387 ** is returned and *ppValue is set to NULL.
9388 */
9389 int sqlite3changeset_old(
9390 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9391 int iVal, /* Column number */
9392 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
9393 );
9394
@@ -9417,11 +9455,11 @@
9417 ** triggers.
9418 **
9419 ** If some other error occurs (e.g. an OOM condition), an SQLite error code
9420 ** is returned and *ppValue is set to NULL.
9421 */
9422 int sqlite3changeset_new(
9423 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9424 int iVal, /* Column number */
9425 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
9426 );
9427
@@ -9444,11 +9482,11 @@
9444 ** and returns SQLITE_OK.
9445 **
9446 ** If some other error occurs (e.g. an OOM condition), an SQLite error code
9447 ** is returned and *ppValue is set to NULL.
9448 */
9449 int sqlite3changeset_conflict(
9450 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9451 int iVal, /* Column number */
9452 sqlite3_value **ppValue /* OUT: Value from conflicting row */
9453 );
9454
@@ -9460,11 +9498,11 @@
9460 ** it sets the output variable to the total number of known foreign key
9461 ** violations in the destination database and returns SQLITE_OK.
9462 **
9463 ** In all other cases this function returns SQLITE_MISUSE.
9464 */
9465 int sqlite3changeset_fk_conflicts(
9466 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9467 int *pnOut /* OUT: Number of FK violations */
9468 );
9469
9470
@@ -9493,11 +9531,11 @@
9493 ** rc = sqlite3changeset_finalize();
9494 ** if( rc!=SQLITE_OK ){
9495 ** // An error has occurred
9496 ** }
9497 */
9498 int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
9499
9500 /*
9501 ** CAPI3REF: Invert A Changeset
9502 **
9503 ** This function is used to "invert" a changeset object. Applying an inverted
@@ -9523,11 +9561,11 @@
9523 ** call to this function.
9524 **
9525 ** WARNING/TODO: This function currently assumes that the input is a valid
9526 ** changeset. If it is not, the results are undefined.
9527 */
9528 int sqlite3changeset_invert(
9529 int nIn, const void *pIn, /* Input changeset */
9530 int *pnOut, void **ppOut /* OUT: Inverse of input */
9531 );
9532
9533 /*
@@ -9552,11 +9590,11 @@
9552 ** *pnOut = 0;
9553 ** }
9554 **
9555 ** Refer to the sqlite3_changegroup documentation below for details.
9556 */
9557 int sqlite3changeset_concat(
9558 int nA, /* Number of bytes in buffer pA */
9559 void *pA, /* Pointer to buffer containing changeset A */
9560 int nB, /* Number of bytes in buffer pB */
9561 void *pB, /* Pointer to buffer containing changeset B */
9562 int *pnOut, /* OUT: Number of bytes in output changeset */
@@ -9740,11 +9778,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 +9823,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 +9842,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 +9862,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,
@@ -9852,11 +9896,11 @@
9852 ** If any other error (aside from a constraint failure when attempting to
9853 ** write to the target database) occurs, then the savepoint transaction is
9854 ** rolled back, restoring the target database to its original state, and an
9855 ** SQLite error code returned.
9856 */
9857 int sqlite3changeset_apply(
9858 sqlite3 *db, /* Apply change to "main" db of this handle */
9859 int nChangeset, /* Size of changeset in bytes */
9860 void *pChangeset, /* Changeset blob */
9861 int(*xFilter)(
9862 void *pCtx, /* Copy of sixth arg to _apply() */
@@ -10053,11 +10097,11 @@
10053 **
10054 ** The sessions module never invokes an xOutput callback with the third
10055 ** parameter set to a value less than or equal to zero. Other than this,
10056 ** no guarantees are made as to the size of the chunks of data returned.
10057 */
10058 int sqlite3changeset_apply_strm(
10059 sqlite3 *db, /* Apply change to "main" db of this handle */
10060 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
10061 void *pIn, /* First arg for xInput */
10062 int(*xFilter)(
10063 void *pCtx, /* Copy of sixth arg to _apply() */
@@ -10068,35 +10112,35 @@
10068 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
10069 sqlite3_changeset_iter *p /* Handle describing change and conflict */
10070 ),
10071 void *pCtx /* First argument passed to xConflict */
10072 );
10073 int sqlite3changeset_concat_strm(
10074 int (*xInputA)(void *pIn, void *pData, int *pnData),
10075 void *pInA,
10076 int (*xInputB)(void *pIn, void *pData, int *pnData),
10077 void *pInB,
10078 int (*xOutput)(void *pOut, const void *pData, int nData),
10079 void *pOut
10080 );
10081 int sqlite3changeset_invert_strm(
10082 int (*xInput)(void *pIn, void *pData, int *pnData),
10083 void *pIn,
10084 int (*xOutput)(void *pOut, const void *pData, int nData),
10085 void *pOut
10086 );
10087 int sqlite3changeset_start_strm(
10088 sqlite3_changeset_iter **pp,
10089 int (*xInput)(void *pIn, void *pData, int *pnData),
10090 void *pIn
10091 );
10092 int sqlite3session_changeset_strm(
10093 sqlite3_session *pSession,
10094 int (*xOutput)(void *pOut, const void *pData, int nData),
10095 void *pOut
10096 );
10097 int sqlite3session_patchset_strm(
10098 sqlite3_session *pSession,
10099 int (*xOutput)(void *pOut, const void *pData, int nData),
10100 void *pOut
10101 );
10102 int sqlite3changegroup_add_strm(sqlite3_changegroup*,
@@ -10999,10 +11043,11 @@
10999 # if defined(_MSC_VER) && _MSC_VER>=1400
11000 # if !defined(_WIN32_WCE)
11001 # include <intrin.h>
11002 # pragma intrinsic(_byteswap_ushort)
11003 # pragma intrinsic(_byteswap_ulong)
 
11004 # pragma intrinsic(_ReadWriteBarrier)
11005 # else
11006 # include <cmnintrin.h>
11007 # endif
11008 # endif
@@ -11537,10 +11582,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 +11678,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.
@@ -11798,36 +11858,39 @@
11798 ** Macros to determine whether the machine is big or little endian,
11799 ** and whether or not that determination is run-time or compile-time.
11800 **
11801 ** For best performance, an attempt is made to guess at the byte-order
11802 ** using C-preprocessor macros. If that is unsuccessful, or if
11803 ** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
11804 ** at run-time.
11805 */
11806 #if (defined(i386) || defined(__i386__) || defined(_M_IX86) || \
 
11807 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
11808 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
11809 defined(__arm__)) && !defined(SQLITE_RUNTIME_BYTEORDER)
11810 # define SQLITE_BYTEORDER 1234
11811 # define SQLITE_BIGENDIAN 0
11812 # define SQLITE_LITTLEENDIAN 1
11813 # define SQLITE_UTF16NATIVE SQLITE_UTF16LE
 
 
11814 #endif
11815 #if (defined(sparc) || defined(__ppc__)) \
11816 && !defined(SQLITE_RUNTIME_BYTEORDER)
11817 # define SQLITE_BYTEORDER 4321
11818 # define SQLITE_BIGENDIAN 1
11819 # define SQLITE_LITTLEENDIAN 0
11820 # define SQLITE_UTF16NATIVE SQLITE_UTF16BE
11821 #endif
11822 #if !defined(SQLITE_BYTEORDER)
 
 
 
11823 # ifdef SQLITE_AMALGAMATION
11824 const int sqlite3one = 1;
11825 # else
11826 extern const int sqlite3one;
11827 # endif
11828 # define SQLITE_BYTEORDER 0 /* 0 means "unknown at compile-time" */
11829 # define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0)
11830 # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
11831 # define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
11832 #endif
11833
@@ -12346,13 +12409,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 +12443,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 +12576,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 +12937,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 +13239,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 +14072,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 +14338,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 +15345,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 +15806,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 +16479,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 +16541,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 +16824,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 +16937,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 +17266,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 +17292,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 +18304,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 */
@@ -20619,20 +20705,22 @@
20619 ** cases of nByte<=0 will be intercepted and dealt with by higher level
20620 ** routines.
20621 */
20622 static void *sqlite3MemMalloc(int nByte){
20623 #ifdef SQLITE_MALLOCSIZE
20624 void *p = SQLITE_MALLOC( nByte );
 
 
20625 if( p==0 ){
20626 testcase( sqlite3GlobalConfig.xLog!=0 );
20627 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
20628 }
20629 return p;
20630 #else
20631 sqlite3_int64 *p;
20632 assert( nByte>0 );
20633 nByte = ROUND8(nByte);
20634 p = SQLITE_MALLOC( nByte+8 );
20635 if( p ){
20636 p[0] = nByte;
20637 p++;
20638 }else{
@@ -23750,12 +23838,11 @@
23750 SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
23751 #if defined(SQLITE_MEMORY_BARRIER)
23752 SQLITE_MEMORY_BARRIER;
23753 #elif defined(__GNUC__)
23754 __sync_synchronize();
23755 #elif !defined(SQLITE_DISABLE_INTRINSIC) && \
23756 defined(_MSC_VER) && _MSC_VER>=1300
23757 _ReadWriteBarrier();
23758 #elif defined(MemoryBarrier)
23759 MemoryBarrier();
23760 #endif
23761 }
@@ -24283,15 +24370,23 @@
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;
@@ -24311,11 +24406,10 @@
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 +25045,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 +25078,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 +25175,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 +25193,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 +25309,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 +25678,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 +25717,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 }
@@ -28600,17 +28695,15 @@
28600 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
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
28612 u32 x;
28613 memcpy(&x,p,4);
28614 return _byteswap_ulong(x);
28615 #else
28616 testcase( p[0]&0x80 );
@@ -28618,16 +28711,14 @@
28618 #endif
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);
28630 memcpy(p,&x,4);
28631 #else
28632 p[0] = (u8)(v>>24);
28633 p[1] = (u8)(v>>16);
@@ -28739,10 +28830,13 @@
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 +28847,31 @@
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 +28883,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 +47588,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
@@ -58445,15 +58554,13 @@
58445 ** two-byte aligned address. get2bytea() is only used for accessing the
58446 ** cell addresses in a btree header.
58447 */
58448 #if SQLITE_BYTEORDER==4321
58449 # define get2byteAligned(x) (*(u16*)(x))
58450 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
58451 && GCC_VERSION>=4008000
58452 # define get2byteAligned(x) __builtin_bswap16(*(u16*)(x))
58453 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
58454 && defined(_MSC_VER) && _MSC_VER>=1300
58455 # define get2byteAligned(x) _byteswap_ushort(*(u16*)(x))
58456 #else
58457 # define get2byteAligned(x) ((x)[0]<<8 | (x)[1])
58458 #endif
58459
@@ -58624,27 +58731,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 +62215,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 +62897,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 +63288,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 +63323,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 +63352,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 +63368,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 +63429,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 +63441,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 +63470,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 +63519,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 +63952,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 +64107,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 +66151,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 +66829,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 +66841,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 +66884,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 +66912,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 +67002,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 +71926,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 +73640,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 +75727,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 +75763,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 +75794,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 +77470,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 +77507,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 +77567,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 +77590,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 +78571,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 +78585,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 +78945,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 +79179,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 +79666,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 +80122,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 +80135,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 +80169,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 +80212,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 +80767,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 +82371,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 +82387,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 +82405,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 +82426,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 +82518,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 +82640,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 +82654,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 +82704,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 +83094,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 +83216,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 +83857,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 +84798,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 +85034,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 +85097,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 +85130,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 +85195,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 +85217,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 +85277,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 +85339,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 +85366,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 +85387,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 +88938,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
@@ -90501,11 +90696,11 @@
90501 aff = sqlite3ExprAffinity(pExpr->pLeft);
90502 if( pExpr->pRight ){
90503 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
90504 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
90505 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
90506 }else if( NEVER(aff==0) ){
90507 aff = SQLITE_AFF_BLOB;
90508 }
90509 return aff;
90510 }
90511
@@ -91237,21 +91432,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 +91883,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 +91982,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 +94091,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 +94143,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 +94476,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 +94532,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 +94587,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 +94659,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 +97203,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 +98028,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 +98442,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 +104607,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 +104688,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 +104769,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 +104803,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 +104963,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 +106763,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 +109921,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 +109937,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 +109973,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 +109984,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 +110000,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 +110441,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 +110473,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 +111258,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 +112921,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 +116435,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 +119964,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 +120016,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 +120071,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 +120359,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 +121438,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 +121527,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 +122884,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 +122918,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 +122930,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 +122945,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 +123108,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 +123177,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 +123418,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 +123453,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 +123477,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 +123503,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 +127427,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 +128794,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 +128824,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 +129070,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);
@@ -129293,10 +129637,11 @@
129293 if( pIdx ){
129294 int j = iColumn;
129295 iColumn = pIdx->aiColumn[j];
129296 if( iColumn==XN_EXPR ){
129297 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
 
129298 }else if( iColumn==pIdx->pTable->iPKey ){
129299 iColumn = XN_ROWID;
129300 }else if( iColumn>=0 ){
129301 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
129302 pScan->zCollName = pIdx->azColl[j];
@@ -133934,11 +134279,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 +134344,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 +135805,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 +135845,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 +135975,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 +135999,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 +138859,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 +138873,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 +138934,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));
@@ -144213,10 +144601,11 @@
144213
144214 /* Precompiled statements used by the implementation. Each of these
144215 ** statements is run and reset within a single virtual table API call.
144216 */
144217 sqlite3_stmt *aStmt[40];
 
144218
144219 char *zReadExprlist;
144220 char *zWriteExprlist;
144221
144222 int nNodeSize; /* Soft limit for node size */
@@ -144282,10 +144671,11 @@
144282 struct Fts3Cursor {
144283 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
144284 i16 eSearch; /* Search strategy (see below) */
144285 u8 isEof; /* True if at End Of Results */
144286 u8 isRequireSeek; /* True if must seek pStmt to %_content row */
 
144287 sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */
144288 Fts3Expr *pExpr; /* Parsed MATCH query string */
144289 int iLangid; /* Language being queried for */
144290 int nPhrase; /* Number of matchable phrases in query */
144291 Fts3DeferredToken *pDeferred; /* Deferred search tokens, if any */
@@ -144804,10 +145194,11 @@
144804
144805 assert( p->nPendingData==0 );
144806 assert( p->pSegments==0 );
144807
144808 /* Free any prepared statements held */
 
144809 for(i=0; i<SizeofArray(p->aStmt); i++){
144810 sqlite3_finalize(p->aStmt[i]);
144811 }
144812 sqlite3_free(p->zSegmentsTbl);
144813 sqlite3_free(p->zReadExprlist);
@@ -145675,13 +146066,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;
@@ -145991,19 +146382,39 @@
145991 return SQLITE_NOMEM;
145992 }
145993 memset(pCsr, 0, sizeof(Fts3Cursor));
145994 return SQLITE_OK;
145995 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145996
145997 /*
145998 ** Close the cursor. For additional information see the documentation
145999 ** on the xClose method of the virtual table interface.
146000 */
146001 static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
146002 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
146003 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
146004 sqlite3_finalize(pCsr->pStmt);
146005 sqlite3Fts3ExprFree(pCsr->pExpr);
146006 sqlite3Fts3FreeDeferredTokens(pCsr);
146007 sqlite3_free(pCsr->aDoclist);
146008 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
146009 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
@@ -146017,24 +146428,27 @@
146017 **
146018 ** "SELECT <columns> FROM %_content WHERE rowid = ?"
146019 **
146020 ** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
146021 ** it. If an error occurs, return an SQLite error code.
146022 **
146023 ** Otherwise, set *ppStmt to point to pCsr->pStmt and return SQLITE_OK.
146024 */
146025 static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){
146026 int rc = SQLITE_OK;
146027 if( pCsr->pStmt==0 ){
146028 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
146029 char *zSql;
146030 zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
146031 if( !zSql ) return SQLITE_NOMEM;
146032 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
146033 sqlite3_free(zSql);
 
 
 
 
 
 
146034 }
146035 *ppStmt = pCsr->pStmt;
146036 return rc;
146037 }
146038
146039 /*
146040 ** Position the pCsr->pStmt statement so that it is on the row
@@ -146042,13 +146456,11 @@
146042 ** SQLITE_OK on success.
146043 */
146044 static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
146045 int rc = SQLITE_OK;
146046 if( pCsr->isRequireSeek ){
146047 sqlite3_stmt *pStmt = 0;
146048
146049 rc = fts3CursorSeekStmt(pCsr, &pStmt);
146050 if( rc==SQLITE_OK ){
146051 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
146052 pCsr->isRequireSeek = 0;
146053 if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
146054 return SQLITE_OK;
@@ -147502,11 +147914,11 @@
147502 if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++];
147503 if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++];
147504 assert( iIdx==nVal );
147505
147506 /* In case the cursor has been used before, clear it now. */
147507 sqlite3_finalize(pCsr->pStmt);
147508 sqlite3_free(pCsr->aDoclist);
147509 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
147510 sqlite3Fts3ExprFree(pCsr->pExpr);
147511 memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
147512
@@ -147570,11 +147982,11 @@
147570 sqlite3_free(zSql);
147571 }else{
147572 rc = SQLITE_NOMEM;
147573 }
147574 }else if( eSearch==FTS3_DOCID_SEARCH ){
147575 rc = fts3CursorSeekStmt(pCsr, &pCsr->pStmt);
147576 if( rc==SQLITE_OK ){
147577 rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
147578 }
147579 }
147580 if( rc!=SQLITE_OK ) return rc;
@@ -147734,11 +148146,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 }
@@ -162590,10 +163002,11 @@
162590 /* #include <stdio.h> */
162591
162592 #ifndef SQLITE_AMALGAMATION
162593 #include "sqlite3rtree.h"
162594 typedef sqlite3_int64 i64;
 
162595 typedef unsigned char u8;
162596 typedef unsigned short u16;
162597 typedef unsigned int u32;
162598 #endif
162599
@@ -162638,28 +163051,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 +163302,106 @@
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,42 +163408,64 @@
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
162920 ** to the argument buffer (always 2, 4 and 8 respectively).
162921 */
162922 static int writeInt16(u8 *p, int i){
162923 p[0] = (i>> 8)&0xFF;
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 +163543,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 +163564,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 +163581,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 +163671,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 +163805,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 +163865,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 +163905,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 +163921,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 +163962,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 +163991,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 +164041,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 +164124,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 +164165,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 +164234,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 +164482,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 +164541,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 +164669,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 +164702,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 +164717,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 +164735,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 +164814,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 +164832,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 +164846,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 +164921,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 +164956,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 +166012,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 +166101,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 +166143,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 +166204,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 +166224,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 +166264,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 +166409,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 +166485,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 +166499,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 +167207,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 +170444,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 +170519,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 +170531,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 +170720,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 +170904,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 +171615,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 +171627,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 +176601,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 +177068,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 +177620,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 +178252,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 +182303,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 +182343,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 +182429,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 +182453,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 +186798,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 +197539,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.
@@ -202,15 +202,31 @@
202 # define _FILE_OFFSET_BITS 64
203 # endif
204 # define _LARGEFILE_SOURCE 1
205 #endif
206
207 /* The GCC_VERSION, CLANG_VERSION, and MSVC_VERSION macros are used to
208 ** conditionally include optimizations for each of these compilers. A
209 ** value of 0 means that compiler is not being used. The
210 ** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
211 ** optimizations, and hence set all compiler macros to 0
212 */
213 #if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
214 # define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
215 #else
216 # define GCC_VERSION 0
217 #endif
218 #if defined(__clang__) && !defined(_WIN32) && !defined(SQLITE_DISABLE_INTRINSIC)
219 # define CLANG_VERSION \
220 (__clang_major__*1000000+__clang_minor__*1000+__clang_patchlevel__)
221 #else
222 # define CLANG_VERSION 0
223 #endif
224 #if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
225 # define MSVC_VERSION _MSC_VER
226 #else
227 # define MSVC_VERSION 0
228 #endif
229
230 /* Needed for various definitions... */
231 #if defined(__GNUC__) && !defined(_GNU_SOURCE)
232 # define _GNU_SOURCE
@@ -379,13 +395,13 @@
395 **
396 ** See also: [sqlite3_libversion()],
397 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
398 ** [sqlite_version()] and [sqlite_source_id()].
399 */
400 #define SQLITE_VERSION "3.17.0"
401 #define SQLITE_VERSION_NUMBER 3017000
402 #define SQLITE_SOURCE_ID "2017-02-13 16:02:40 ada05cfa86ad7f5645450ac7a2a21c9aa6e57d2c"
403
404 /*
405 ** CAPI3REF: Run-Time Library Version Numbers
406 ** KEYWORDS: sqlite3_version sqlite3_sourceid
407 **
@@ -517,11 +533,15 @@
533 ** sqlite3_uint64 and sqlite_uint64 types can store integer values
534 ** between 0 and +18446744073709551615 inclusive.
535 */
536 #ifdef SQLITE_INT64_TYPE
537 typedef SQLITE_INT64_TYPE sqlite_int64;
538 # ifdef SQLITE_UINT64_TYPE
539 typedef SQLITE_UINT64_TYPE sqlite_uint64;
540 # else
541 typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
542 # endif
543 #elif defined(_MSC_VER) || defined(__BORLANDC__)
544 typedef __int64 sqlite_int64;
545 typedef unsigned __int64 sqlite_uint64;
546 #else
547 typedef long long int sqlite_int64;
@@ -830,11 +850,11 @@
850 ** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
851 ** after reboot following a crash or power loss, the only bytes in a
852 ** file that were written at the application level might have changed
853 ** and that adjacent bytes, even bytes within the same sector are
854 ** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
855 ** flag indicates that a file cannot be deleted when open. The
856 ** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
857 ** read-only media and cannot be changed even by processes with
858 ** elevated privileges.
859 */
860 #define SQLITE_IOCAP_ATOMIC 0x00000001
@@ -980,10 +1000,13 @@
1000 ** <li> [SQLITE_IOCAP_ATOMIC16K]
1001 ** <li> [SQLITE_IOCAP_ATOMIC32K]
1002 ** <li> [SQLITE_IOCAP_ATOMIC64K]
1003 ** <li> [SQLITE_IOCAP_SAFE_APPEND]
1004 ** <li> [SQLITE_IOCAP_SEQUENTIAL]
1005 ** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
1006 ** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
1007 ** <li> [SQLITE_IOCAP_IMMUTABLE]
1008 ** </ul>
1009 **
1010 ** The SQLITE_IOCAP_ATOMIC property means that all writes of
1011 ** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
1012 ** mean that writes of blocks that are nnn bytes in size and
@@ -5668,11 +5691,11 @@
5691 ** ^(The update hook is not invoked when internal system tables are
5692 ** modified (i.e. sqlite_master and sqlite_sequence).)^
5693 ** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
5694 **
5695 ** ^In the current implementation, the update hook
5696 ** is not invoked when conflicting rows are deleted because of an
5697 ** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook
5698 ** invoked when rows are deleted using the [truncate optimization].
5699 ** The exceptions defined in this paragraph might change in a future
5700 ** release of SQLite.
5701 **
@@ -6450,10 +6473,16 @@
6473 **
6474 ** ^Unless it returns SQLITE_MISUSE, this function sets the
6475 ** [database connection] error code and message accessible via
6476 ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
6477 **
6478 ** A BLOB referenced by sqlite3_blob_open() may be read using the
6479 ** [sqlite3_blob_read()] interface and modified by using
6480 ** [sqlite3_blob_write()]. The [BLOB handle] can be moved to a
6481 ** different row of the same table using the [sqlite3_blob_reopen()]
6482 ** interface. However, the column, table, or database of a [BLOB handle]
6483 ** cannot be changed after the [BLOB handle] is opened.
6484 **
6485 ** ^(If the row that a BLOB handle points to is modified by an
6486 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
6487 ** then the BLOB handle is marked as "expired".
6488 ** This is true if any column of the row is changed, even a column
@@ -6473,10 +6502,14 @@
6502 ** and the built-in [zeroblob] SQL function may be used to create a
6503 ** zero-filled blob to read or write using the incremental-blob interface.
6504 **
6505 ** To avoid a resource leak, every open [BLOB handle] should eventually
6506 ** be released by a call to [sqlite3_blob_close()].
6507 **
6508 ** See also: [sqlite3_blob_close()],
6509 ** [sqlite3_blob_reopen()], [sqlite3_blob_read()],
6510 ** [sqlite3_blob_bytes()], [sqlite3_blob_write()].
6511 */
6512 SQLITE_API int sqlite3_blob_open(
6513 sqlite3*,
6514 const char *zDb,
6515 const char *zTable,
@@ -6488,15 +6521,15 @@
6521
6522 /*
6523 ** CAPI3REF: Move a BLOB Handle to a New Row
6524 ** METHOD: sqlite3_blob
6525 **
6526 ** ^This function is used to move an existing [BLOB handle] so that it points
6527 ** to a different row of the same database table. ^The new row is identified
6528 ** by the rowid value passed as the second argument. Only the row can be
6529 ** changed. ^The database, table and column on which the blob handle is open
6530 ** remain the same. Moving an existing [BLOB handle] to a new row is
6531 ** faster than closing the existing handle and opening a new one.
6532 **
6533 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
6534 ** it must exist and there must be either a blob or text value stored in
6535 ** the nominated column.)^ ^If the new row is not present in the table, or if
@@ -8421,22 +8454,22 @@
8454 ** ^These interfaces are only available if SQLite is compiled using the
8455 ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
8456 **
8457 ** ^The [sqlite3_preupdate_hook()] interface registers a callback function
8458 ** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
8459 ** on a database table.
8460 ** ^At most one preupdate hook may be registered at a time on a single
8461 ** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
8462 ** the previous setting.
8463 ** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
8464 ** with a NULL pointer as the second parameter.
8465 ** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
8466 ** the first parameter to callbacks.
8467 **
8468 ** ^The preupdate hook only fires for changes to real database tables; the
8469 ** preupdate hook is not invoked for changes to [virtual tables] or to
8470 ** system tables like sqlite_master or sqlite_stat1.
8471 **
8472 ** ^The second parameter to the preupdate callback is a pointer to
8473 ** the [database connection] that registered the preupdate hook.
8474 ** ^The third parameter to the preupdate callback is one of the constants
8475 ** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
@@ -8446,16 +8479,20 @@
8479 ** will be "main" for the main database or "temp" for TEMP tables or
8480 ** the name given after the AS keyword in the [ATTACH] statement for attached
8481 ** databases.)^
8482 ** ^The fifth parameter to the preupdate callback is the name of the
8483 ** table that is being modified.
8484 **
8485 ** For an UPDATE or DELETE operation on a [rowid table], the sixth
8486 ** parameter passed to the preupdate callback is the initial [rowid] of the
8487 ** row being modified or deleted. For an INSERT operation on a rowid table,
8488 ** or any operation on a WITHOUT ROWID table, the value of the sixth
8489 ** parameter is undefined. For an INSERT or UPDATE on a rowid table the
8490 ** seventh parameter is the final rowid value of the row being inserted
8491 ** or updated. The value of the seventh parameter passed to the callback
8492 ** function is not defined for operations on WITHOUT ROWID tables, or for
8493 ** INSERT operations on rowid tables.
8494 **
8495 ** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
8496 ** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
8497 ** provide additional information about a preupdate event. These routines
8498 ** may only be called from within a preupdate callback. Invoking any of
@@ -8887,11 +8924,11 @@
8924 ** The session object will be used to create changesets for tables in
8925 ** database zDb, where zDb is either "main", or "temp", or the name of an
8926 ** attached database. It is not an error if database zDb is not attached
8927 ** to the database when the session object is created.
8928 */
8929 SQLITE_API int sqlite3session_create(
8930 sqlite3 *db, /* Database handle */
8931 const char *zDb, /* Name of db (e.g. "main") */
8932 sqlite3_session **ppSession /* OUT: New session object */
8933 );
8934
@@ -8905,11 +8942,11 @@
8942 **
8943 ** Session objects must be deleted before the database handle to which they
8944 ** are attached is closed. Refer to the documentation for
8945 ** [sqlite3session_create()] for details.
8946 */
8947 SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
8948
8949
8950 /*
8951 ** CAPI3REF: Enable Or Disable A Session Object
8952 **
@@ -8925,11 +8962,11 @@
8962 ** no-op, and may be used to query the current state of the session.
8963 **
8964 ** The return value indicates the final state of the session object: 0 if
8965 ** the session is disabled, or 1 if it is enabled.
8966 */
8967 SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
8968
8969 /*
8970 ** CAPI3REF: Set Or Clear the Indirect Change Flag
8971 **
8972 ** Each change recorded by a session object is marked as either direct or
@@ -8954,11 +8991,11 @@
8991 ** indirect flag for the specified session object.
8992 **
8993 ** The return value indicates the final state of the indirect flag: 0 if
8994 ** it is clear, or 1 if it is set.
8995 */
8996 SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
8997
8998 /*
8999 ** CAPI3REF: Attach A Table To A Session Object
9000 **
9001 ** If argument zTab is not NULL, then it is the name of a table to attach
@@ -8984,11 +9021,11 @@
9021 ** in one or more of their PRIMARY KEY columns.
9022 **
9023 ** SQLITE_OK is returned if the call completes without error. Or, if an error
9024 ** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
9025 */
9026 SQLITE_API int sqlite3session_attach(
9027 sqlite3_session *pSession, /* Session object */
9028 const char *zTab /* Table name */
9029 );
9030
9031 /*
@@ -8998,11 +9035,11 @@
9035 ** in tables that are not attached to the Session object, the filter is called
9036 ** to determine whether changes to the table's rows should be tracked or not.
9037 ** If xFilter returns 0, changes is not tracked. Note that once a table is
9038 ** attached, xFilter will not be called again.
9039 */
9040 SQLITE_API void sqlite3session_table_filter(
9041 sqlite3_session *pSession, /* Session object */
9042 int(*xFilter)(
9043 void *pCtx, /* Copy of third arg to _filter_table() */
9044 const char *zTab /* Table name */
9045 ),
@@ -9111,11 +9148,11 @@
9148 ** changeset, even though the delete took place while the session was disabled.
9149 ** Or, if one field of a row is updated while a session is disabled, and
9150 ** another field of the same row is updated while the session is enabled, the
9151 ** resulting changeset will contain an UPDATE change that updates both fields.
9152 */
9153 SQLITE_API int sqlite3session_changeset(
9154 sqlite3_session *pSession, /* Session object */
9155 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
9156 void **ppChangeset /* OUT: Buffer containing changeset */
9157 );
9158
@@ -9155,11 +9192,12 @@
9192 **
9193 ** <li> For each row (primary key) that exists in the to-table but not in
9194 ** the from-table, a DELETE record is added to the session object.
9195 **
9196 ** <li> For each row (primary key) that exists in both tables, but features
9197 ** different non-PK values in each, an UPDATE record is added to the
9198 ** session.
9199 ** </ul>
9200 **
9201 ** To clarify, if this function is called and then a changeset constructed
9202 ** using [sqlite3session_changeset()], then after applying that changeset to
9203 ** database zFrom the contents of the two compatible tables would be
@@ -9172,11 +9210,11 @@
9210 ** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
9211 ** may be set to point to a buffer containing an English language error
9212 ** message. It is the responsibility of the caller to free this buffer using
9213 ** sqlite3_free().
9214 */
9215 SQLITE_API int sqlite3session_diff(
9216 sqlite3_session *pSession,
9217 const char *zFromDb,
9218 const char *zTbl,
9219 char **pzErrMsg
9220 );
@@ -9208,11 +9246,11 @@
9246 ** Changes within a patchset are ordered in the same way as for changesets
9247 ** generated by the sqlite3session_changeset() function (i.e. all changes for
9248 ** a single table are grouped together, tables appear in the order in which
9249 ** they were attached to the session object).
9250 */
9251 SQLITE_API int sqlite3session_patchset(
9252 sqlite3_session *pSession, /* Session object */
9253 int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */
9254 void **ppPatchset /* OUT: Buffer containing changeset */
9255 );
9256
@@ -9229,11 +9267,11 @@
9267 ** an attached table is modified and then later on the original values
9268 ** are restored. However, if this function returns non-zero, then it is
9269 ** guaranteed that a call to sqlite3session_changeset() will return a
9270 ** changeset containing zero changes.
9271 */
9272 SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
9273
9274 /*
9275 ** CAPI3REF: Create An Iterator To Traverse A Changeset
9276 **
9277 ** Create an iterator used to iterate through the contents of a changeset.
@@ -9264,11 +9302,11 @@
9302 ** this function, all changes that relate to a single table are visited
9303 ** consecutively. There is no chance that the iterator will visit a change
9304 ** the applies to table X, then one for table Y, and then later on visit
9305 ** another change for table X.
9306 */
9307 SQLITE_API int sqlite3changeset_start(
9308 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
9309 int nChangeset, /* Size of changeset blob in bytes */
9310 void *pChangeset /* Pointer to blob containing changeset */
9311 );
9312
@@ -9293,11 +9331,11 @@
9331 **
9332 ** If an error occurs, an SQLite error code is returned. Possible error
9333 ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or
9334 ** SQLITE_NOMEM.
9335 */
9336 SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
9337
9338 /*
9339 ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
9340 **
9341 ** The pIter argument passed to this function may either be an iterator
@@ -9321,11 +9359,11 @@
9359 **
9360 ** If no error occurs, SQLITE_OK is returned. If an error does occur, an
9361 ** SQLite error code is returned. The values of the output variables may not
9362 ** be trusted in this case.
9363 */
9364 SQLITE_API int sqlite3changeset_op(
9365 sqlite3_changeset_iter *pIter, /* Iterator object */
9366 const char **pzTab, /* OUT: Pointer to table name */
9367 int *pnCol, /* OUT: Number of columns in table */
9368 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
9369 int *pbIndirect /* OUT: True for an 'indirect' change */
@@ -9354,11 +9392,11 @@
9392 ** If this function is called when the iterator does not point to a valid
9393 ** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
9394 ** SQLITE_OK is returned and the output variables populated as described
9395 ** above.
9396 */
9397 SQLITE_API int sqlite3changeset_pk(
9398 sqlite3_changeset_iter *pIter, /* Iterator object */
9399 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
9400 int *pnCol /* OUT: Number of entries in output array */
9401 );
9402
@@ -9384,11 +9422,11 @@
9422 ** is similar to the "old.*" columns available to update or delete triggers.
9423 **
9424 ** If some other error occurs (e.g. an OOM condition), an SQLite error code
9425 ** is returned and *ppValue is set to NULL.
9426 */
9427 SQLITE_API int sqlite3changeset_old(
9428 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9429 int iVal, /* Column number */
9430 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
9431 );
9432
@@ -9417,11 +9455,11 @@
9455 ** triggers.
9456 **
9457 ** If some other error occurs (e.g. an OOM condition), an SQLite error code
9458 ** is returned and *ppValue is set to NULL.
9459 */
9460 SQLITE_API int sqlite3changeset_new(
9461 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9462 int iVal, /* Column number */
9463 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
9464 );
9465
@@ -9444,11 +9482,11 @@
9482 ** and returns SQLITE_OK.
9483 **
9484 ** If some other error occurs (e.g. an OOM condition), an SQLite error code
9485 ** is returned and *ppValue is set to NULL.
9486 */
9487 SQLITE_API int sqlite3changeset_conflict(
9488 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9489 int iVal, /* Column number */
9490 sqlite3_value **ppValue /* OUT: Value from conflicting row */
9491 );
9492
@@ -9460,11 +9498,11 @@
9498 ** it sets the output variable to the total number of known foreign key
9499 ** violations in the destination database and returns SQLITE_OK.
9500 **
9501 ** In all other cases this function returns SQLITE_MISUSE.
9502 */
9503 SQLITE_API int sqlite3changeset_fk_conflicts(
9504 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9505 int *pnOut /* OUT: Number of FK violations */
9506 );
9507
9508
@@ -9493,11 +9531,11 @@
9531 ** rc = sqlite3changeset_finalize();
9532 ** if( rc!=SQLITE_OK ){
9533 ** // An error has occurred
9534 ** }
9535 */
9536 SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
9537
9538 /*
9539 ** CAPI3REF: Invert A Changeset
9540 **
9541 ** This function is used to "invert" a changeset object. Applying an inverted
@@ -9523,11 +9561,11 @@
9561 ** call to this function.
9562 **
9563 ** WARNING/TODO: This function currently assumes that the input is a valid
9564 ** changeset. If it is not, the results are undefined.
9565 */
9566 SQLITE_API int sqlite3changeset_invert(
9567 int nIn, const void *pIn, /* Input changeset */
9568 int *pnOut, void **ppOut /* OUT: Inverse of input */
9569 );
9570
9571 /*
@@ -9552,11 +9590,11 @@
9590 ** *pnOut = 0;
9591 ** }
9592 **
9593 ** Refer to the sqlite3_changegroup documentation below for details.
9594 */
9595 SQLITE_API int sqlite3changeset_concat(
9596 int nA, /* Number of bytes in buffer pA */
9597 void *pA, /* Pointer to buffer containing changeset A */
9598 int nB, /* Number of bytes in buffer pB */
9599 void *pB, /* Pointer to buffer containing changeset B */
9600 int *pnOut, /* OUT: Number of bytes in output changeset */
@@ -9740,11 +9778,11 @@
9778 ** considered compatible if all of the following are true:
9779 **
9780 ** <ul>
9781 ** <li> The table has the same name as the name recorded in the
9782 ** changeset, and
9783 ** <li> The table has at least as many columns as recorded in the
9784 ** changeset, and
9785 ** <li> The table has primary key columns in the same position as
9786 ** recorded in the changeset.
9787 ** </ul>
9788 **
@@ -9785,11 +9823,15 @@
9823 ** the changeset the row is deleted from the target database.
9824 **
9825 ** If a row with matching primary key values is found, but one or more of
9826 ** the non-primary key fields contains a value different from the original
9827 ** row value stored in the changeset, the conflict-handler function is
9828 ** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
9829 ** database table has more columns than are recorded in the changeset,
9830 ** only the values of those non-primary key fields are compared against
9831 ** the current database contents - any trailing database table columns
9832 ** are ignored.
9833 **
9834 ** If no row with matching primary key values is found in the database,
9835 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
9836 ** passed as the second argument.
9837 **
@@ -9800,11 +9842,13 @@
9842 ** operation is attempted because an earlier call to the conflict handler
9843 ** function returned [SQLITE_CHANGESET_REPLACE].
9844 **
9845 ** <dt>INSERT Changes<dd>
9846 ** For each INSERT change, an attempt is made to insert the new row into
9847 ** the database. If the changeset row contains fewer fields than the
9848 ** database table, the trailing fields are populated with their default
9849 ** values.
9850 **
9851 ** If the attempt to insert the row fails because the database already
9852 ** contains a row with the same primary key values, the conflict handler
9853 ** function is invoked with the second argument set to
9854 ** [SQLITE_CHANGESET_CONFLICT].
@@ -9818,17 +9862,17 @@
9862 **
9863 ** <dt>UPDATE Changes<dd>
9864 ** For each UPDATE change, this function checks if the target database
9865 ** contains a row with the same primary key value (or values) as the
9866 ** original row values stored in the changeset. If it does, and the values
9867 ** stored in all modified non-primary key columns also match the values
9868 ** stored in the changeset the row is updated within the target database.
9869 **
9870 ** If a row with matching primary key values is found, but one or more of
9871 ** the modified non-primary key fields contains a value different from an
9872 ** original row value stored in the changeset, the conflict-handler function
9873 ** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
9874 ** UPDATE changes only contain values for non-primary key fields that are
9875 ** to be modified, only those fields need to match the original values to
9876 ** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
9877 **
9878 ** If no row with matching primary key values is found in the database,
@@ -9852,11 +9896,11 @@
9896 ** If any other error (aside from a constraint failure when attempting to
9897 ** write to the target database) occurs, then the savepoint transaction is
9898 ** rolled back, restoring the target database to its original state, and an
9899 ** SQLite error code returned.
9900 */
9901 SQLITE_API int sqlite3changeset_apply(
9902 sqlite3 *db, /* Apply change to "main" db of this handle */
9903 int nChangeset, /* Size of changeset in bytes */
9904 void *pChangeset, /* Changeset blob */
9905 int(*xFilter)(
9906 void *pCtx, /* Copy of sixth arg to _apply() */
@@ -10053,11 +10097,11 @@
10097 **
10098 ** The sessions module never invokes an xOutput callback with the third
10099 ** parameter set to a value less than or equal to zero. Other than this,
10100 ** no guarantees are made as to the size of the chunks of data returned.
10101 */
10102 SQLITE_API int sqlite3changeset_apply_strm(
10103 sqlite3 *db, /* Apply change to "main" db of this handle */
10104 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
10105 void *pIn, /* First arg for xInput */
10106 int(*xFilter)(
10107 void *pCtx, /* Copy of sixth arg to _apply() */
@@ -10068,35 +10112,35 @@
10112 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
10113 sqlite3_changeset_iter *p /* Handle describing change and conflict */
10114 ),
10115 void *pCtx /* First argument passed to xConflict */
10116 );
10117 SQLITE_API int sqlite3changeset_concat_strm(
10118 int (*xInputA)(void *pIn, void *pData, int *pnData),
10119 void *pInA,
10120 int (*xInputB)(void *pIn, void *pData, int *pnData),
10121 void *pInB,
10122 int (*xOutput)(void *pOut, const void *pData, int nData),
10123 void *pOut
10124 );
10125 SQLITE_API int sqlite3changeset_invert_strm(
10126 int (*xInput)(void *pIn, void *pData, int *pnData),
10127 void *pIn,
10128 int (*xOutput)(void *pOut, const void *pData, int nData),
10129 void *pOut
10130 );
10131 SQLITE_API int sqlite3changeset_start_strm(
10132 sqlite3_changeset_iter **pp,
10133 int (*xInput)(void *pIn, void *pData, int *pnData),
10134 void *pIn
10135 );
10136 SQLITE_API int sqlite3session_changeset_strm(
10137 sqlite3_session *pSession,
10138 int (*xOutput)(void *pOut, const void *pData, int nData),
10139 void *pOut
10140 );
10141 SQLITE_API int sqlite3session_patchset_strm(
10142 sqlite3_session *pSession,
10143 int (*xOutput)(void *pOut, const void *pData, int nData),
10144 void *pOut
10145 );
10146 int sqlite3changegroup_add_strm(sqlite3_changegroup*,
@@ -10999,10 +11043,11 @@
11043 # if defined(_MSC_VER) && _MSC_VER>=1400
11044 # if !defined(_WIN32_WCE)
11045 # include <intrin.h>
11046 # pragma intrinsic(_byteswap_ushort)
11047 # pragma intrinsic(_byteswap_ulong)
11048 # pragma intrinsic(_byteswap_uint64)
11049 # pragma intrinsic(_ReadWriteBarrier)
11050 # else
11051 # include <cmnintrin.h>
11052 # endif
11053 # endif
@@ -11537,10 +11582,22 @@
11582 #include <stdlib.h>
11583 #include <string.h>
11584 #include <assert.h>
11585 #include <stddef.h>
11586
11587 /*
11588 ** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY.
11589 ** This allows better measurements of where memcpy() is used when running
11590 ** cachegrind. But this macro version of memcpy() is very slow so it
11591 ** should not be used in production. This is a performance measurement
11592 ** hack only.
11593 */
11594 #ifdef SQLITE_INLINE_MEMCPY
11595 # define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\
11596 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
11597 #endif
11598
11599 /*
11600 ** If compiling for a processor that lacks floating point support,
11601 ** substitute integer for floating-point
11602 */
11603 #ifdef SQLITE_OMIT_FLOATING_POINT
@@ -11621,13 +11678,16 @@
11678 /*
11679 ** The default initial allocation for the pagecache when using separate
11680 ** pagecaches for each database connection. A positive number is the
11681 ** number of pages. A negative number N translations means that a buffer
11682 ** of -1024*N bytes is allocated and used for as many pages as it will hold.
11683 **
11684 ** The default value of "20" was choosen to minimize the run-time of the
11685 ** speedtest1 test program with options: --shrink-memory --reprepare
11686 */
11687 #ifndef SQLITE_DEFAULT_PCACHE_INITSZ
11688 # define SQLITE_DEFAULT_PCACHE_INITSZ 20
11689 #endif
11690
11691 /*
11692 ** GCC does not define the offsetof() macro so we'll have to do it
11693 ** ourselves.
@@ -11798,36 +11858,39 @@
11858 ** Macros to determine whether the machine is big or little endian,
11859 ** and whether or not that determination is run-time or compile-time.
11860 **
11861 ** For best performance, an attempt is made to guess at the byte-order
11862 ** using C-preprocessor macros. If that is unsuccessful, or if
11863 ** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
11864 ** at run-time.
11865 */
11866 #ifndef SQLITE_BYTEORDER
11867 # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
11868 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
11869 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
11870 defined(__arm__)
11871 # define SQLITE_BYTEORDER 1234
11872 # elif defined(sparc) || defined(__ppc__)
11873 # define SQLITE_BYTEORDER 4321
11874 # else
11875 # define SQLITE_BYTEORDER 0
11876 # endif
11877 #endif
11878 #if SQLITE_BYTEORDER==4321
 
 
11879 # define SQLITE_BIGENDIAN 1
11880 # define SQLITE_LITTLEENDIAN 0
11881 # define SQLITE_UTF16NATIVE SQLITE_UTF16BE
11882 #elif SQLITE_BYTEORDER==1234
11883 # define SQLITE_BIGENDIAN 0
11884 # define SQLITE_LITTLEENDIAN 1
11885 # define SQLITE_UTF16NATIVE SQLITE_UTF16LE
11886 #else
11887 # ifdef SQLITE_AMALGAMATION
11888 const int sqlite3one = 1;
11889 # else
11890 extern const int sqlite3one;
11891 # endif
 
11892 # define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0)
11893 # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
11894 # define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
11895 #endif
11896
@@ -12346,13 +12409,14 @@
12409 );
12410 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*);
12411 SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*);
12412 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, u8 flags);
12413
12414 /* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */
12415 #define BTREE_SAVEPOSITION 0x02 /* Leave cursor pointing at NEXT or PREV */
12416 #define BTREE_AUXDELETE 0x04 /* not the primary delete operation */
12417 #define BTREE_APPEND 0x08 /* Insert is likely an append */
12418
12419 /* An instance of the BtreePayload object describes the content of a single
12420 ** entry in either an index or table btree.
12421 **
12422 ** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
@@ -12379,11 +12443,11 @@
12443 int nData; /* Size of pData. 0 if none. */
12444 int nZero; /* Extra zero data appended after pData,nData */
12445 };
12446
12447 SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
12448 int flags, int seekResult);
12449 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
12450 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
12451 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
12452 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
12453 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
@@ -12512,12 +12576,11 @@
12576 ** as an instance of the following structure:
12577 */
12578 struct VdbeOp {
12579 u8 opcode; /* What operation to perform */
12580 signed char p4type; /* One of the P4_xxx constants for p4 */
12581 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
 
12582 int p1; /* First operand */
12583 int p2; /* Second parameter (often the jump destination) */
12584 int p3; /* The third parameter */
12585 union p4union { /* fourth parameter */
12586 int i; /* Integer value if p4type==P4_INT32 */
@@ -12874,11 +12937,11 @@
12937 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
12938 SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8);
12939 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
12940 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
12941 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
12942 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
12943 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
12944 SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
12945 SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
12946 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
12947 SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
@@ -13176,18 +13239,20 @@
13239 SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
13240 SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
13241 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
13242 SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
13243 SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
13244 # ifdef SQLITE_DIRECT_OVERFLOW_READ
13245 SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager, Pgno);
13246 # endif
13247 # ifdef SQLITE_ENABLE_SNAPSHOT
13248 SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot);
13249 SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot);
13250 SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager);
13251 # endif
13252 #else
13253 # define sqlite3PagerUseWal(x,y) 0
13254 #endif
13255
13256 #ifdef SQLITE_ENABLE_ZIPVFS
13257 SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager);
13258 #endif
@@ -14007,10 +14072,11 @@
14072 signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
14073 u8 suppressErr; /* Do not issue error messages if true */
14074 u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
14075 u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
14076 u8 mTrace; /* zero or more SQLITE_TRACE flags */
14077 u8 skipBtreeMutex; /* True if no shared-cache backends */
14078 int nextPagesize; /* Pagesize after VACUUM if >0 */
14079 u32 magic; /* Magic number for detect library misuse */
14080 int nChange; /* Value returned by sqlite3_changes() */
14081 int nTotalChange; /* Value returned by sqlite3_total_changes() */
14082 int aLimit[SQLITE_N_LIMIT]; /* Limits */
@@ -14272,10 +14338,11 @@
14338 #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
14339 #define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
14340 #define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */
14341 #define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a
14342 ** single query - might change over time */
14343 #define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
14344
14345 /*
14346 ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
14347 ** used to create the initializers for the FuncDef structures.
14348 **
@@ -15278,11 +15345,11 @@
15345 #define WHERE_DISTINCTBY 0x0080 /* pOrderby is really a DISTINCT clause */
15346 #define WHERE_WANT_DISTINCT 0x0100 /* All output needs to be distinct */
15347 #define WHERE_SORTBYGROUP 0x0200 /* Support sqlite3WhereIsSorted() */
15348 #define WHERE_SEEK_TABLE 0x0400 /* Do not defer seeks on main table */
15349 #define WHERE_ORDERBY_LIMIT 0x0800 /* ORDERBY+LIMIT on the inner loop */
15350 #define WHERE_SEEK_UNIQ_TABLE 0x1000 /* Do not defer seeks if unique */
15351 /* 0x2000 not currently used */
15352 #define WHERE_USE_LIMIT 0x4000 /* Use the LIMIT in cost estimates */
15353 /* 0x8000 not currently used */
15354
15355 /* Allowed return values from sqlite3WhereIsDistinct()
@@ -15739,25 +15806,23 @@
15806 ** OPFLAG_AUXDELETE == BTREE_AUXDELETE
15807 */
15808 #define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
15809 /* Also used in P2 (not P5) of OP_Delete */
15810 #define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */
15811 #define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
15812 #define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */
15813 #define OPFLAG_APPEND 0x08 /* This is likely to be an append */
15814 #define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
 
15815 #define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
 
15816 #define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */
15817 #define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */
15818 #define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */
15819 #define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */
15820 #define OPFLAG_FORDELETE 0x08 /* OP_Open should use BTREE_FORDELETE */
15821 #define OPFLAG_P2ISREG 0x10 /* P2 to OP_Open** is a register number */
15822 #define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */
15823 #define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete/Insert: save cursor pos */
15824 #define OPFLAG_AUXDELETE 0x04 /* OP_Delete: index in a DELETE op */
15825
15826 /*
15827 * Each trigger present in the database schema is stored as an instance of
15828 * struct Trigger.
@@ -16414,11 +16479,11 @@
16479 SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
16480 SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
16481 SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int);
16482 SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, Expr*, int);
16483 SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
16484 SQLITE_PRIVATE int sqlite3ExprCodeAtInit(Parse*, Expr*, int);
16485 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
16486 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
16487 SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
16488 SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
16489 #define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
@@ -16476,10 +16541,15 @@
16541 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
16542 SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
16543 SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
16544 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
16545 u8,u8,int,int*,int*);
16546 #ifdef SQLITE_ENABLE_NULL_TRIM
16547 SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe*,Table*);
16548 #else
16549 # define sqlite3SetMakeRecordP5(A,B)
16550 #endif
16551 SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
16552 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
16553 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
16554 SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
16555 SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
@@ -16754,12 +16824,14 @@
16824 #endif
16825
16826 /*
16827 ** The interface to the LEMON-generated parser
16828 */
16829 #ifndef SQLITE_AMALGAMATION
16830 SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64));
16831 SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
16832 #endif
16833 SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
16834 #ifdef YYTRACKMAXSTACKDEPTH
16835 SQLITE_PRIVATE int sqlite3ParserStackPeak(void*);
16836 #endif
16837
@@ -16865,10 +16937,11 @@
16937 #define sqlite3FkActions(a,b,c,d,e,f)
16938 #define sqlite3FkCheck(a,b,c,d,e,f)
16939 #define sqlite3FkDropTable(a,b,c)
16940 #define sqlite3FkOldmask(a,b) 0
16941 #define sqlite3FkRequired(a,b,c,d) 0
16942 #define sqlite3FkReferences(a) 0
16943 #endif
16944 #ifndef SQLITE_OMIT_FOREIGN_KEY
16945 SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *, Table*);
16946 SQLITE_PRIVATE int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
16947 #else
@@ -17193,10 +17266,23 @@
17266 ** setting.)
17267 */
17268 #ifndef SQLITE_STMTJRNL_SPILL
17269 # define SQLITE_STMTJRNL_SPILL (64*1024)
17270 #endif
17271
17272 /*
17273 ** The default lookaside-configuration, the format "SZ,N". SZ is the
17274 ** number of bytes in each lookaside slot (should be a multiple of 8)
17275 ** and N is the number of slots. The lookaside-configuration can be
17276 ** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
17277 ** or at run-time for an individual database connection using
17278 ** sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE);
17279 */
17280 #ifndef SQLITE_DEFAULT_LOOKASIDE
17281 # define SQLITE_DEFAULT_LOOKASIDE 1200,100
17282 #endif
17283
17284
17285 /*
17286 ** The following singleton contains the global configuration for
17287 ** the SQLite library.
17288 */
@@ -17206,12 +17292,11 @@
17292 SQLITE_THREADSAFE==1, /* bFullMutex */
17293 SQLITE_USE_URI, /* bOpenUri */
17294 SQLITE_ALLOW_COVERING_INDEX_SCAN, /* bUseCis */
17295 0x7ffffffe, /* mxStrlen */
17296 0, /* neverCorrupt */
17297 SQLITE_DEFAULT_LOOKASIDE, /* szLookaside, nLookaside */
 
17298 SQLITE_STMTJRNL_SPILL, /* nStmtSpill */
17299 {0,0,0,0,0,0,0,0}, /* m */
17300 {0,0,0,0,0,0,0,0,0}, /* mutex */
17301 {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
17302 (void*)0, /* pHeap */
@@ -18219,10 +18304,11 @@
18304 int iNewReg; /* Register for new.* values */
18305 i64 iKey1; /* First key value passed to hook */
18306 i64 iKey2; /* Second key value passed to hook */
18307 Mem *aNew; /* Array of new.* values */
18308 Table *pTab; /* Schema object being upated */
18309 Index *pPk; /* PK index if pTab is WITHOUT ROWID */
18310 };
18311
18312 /*
18313 ** Function prototypes
18314 */
@@ -20619,20 +20705,22 @@
20705 ** cases of nByte<=0 will be intercepted and dealt with by higher level
20706 ** routines.
20707 */
20708 static void *sqlite3MemMalloc(int nByte){
20709 #ifdef SQLITE_MALLOCSIZE
20710 void *p;
20711 testcase( ROUND8(nByte)==nByte );
20712 p = SQLITE_MALLOC( nByte );
20713 if( p==0 ){
20714 testcase( sqlite3GlobalConfig.xLog!=0 );
20715 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
20716 }
20717 return p;
20718 #else
20719 sqlite3_int64 *p;
20720 assert( nByte>0 );
20721 testcase( ROUND8(nByte)!=nByte );
20722 p = SQLITE_MALLOC( nByte+8 );
20723 if( p ){
20724 p[0] = nByte;
20725 p++;
20726 }else{
@@ -23750,12 +23838,11 @@
23838 SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
23839 #if defined(SQLITE_MEMORY_BARRIER)
23840 SQLITE_MEMORY_BARRIER;
23841 #elif defined(__GNUC__)
23842 __sync_synchronize();
23843 #elif MSVC_VERSION>=1300
 
23844 _ReadWriteBarrier();
23845 #elif defined(MemoryBarrier)
23846 MemoryBarrier();
23847 #endif
23848 }
@@ -24283,15 +24370,23 @@
24370
24371 /*
24372 ** Do a memory allocation with statistics and alarms. Assume the
24373 ** lock is already held.
24374 */
24375 static void mallocWithAlarm(int n, void **pp){
 
24376 void *p;
24377 int nFull;
24378 assert( sqlite3_mutex_held(mem0.mutex) );
24379 assert( n>0 );
24380
24381 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
24382 ** implementation of malloc_good_size(), which must be called in debug
24383 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
24384 ** or else a crash results. Hence, do not attempt to optimize out the
24385 ** following xRoundup() call. */
24386 nFull = sqlite3GlobalConfig.m.xRoundup(n);
24387
24388 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
24389 if( mem0.alarmThreshold>0 ){
24390 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
24391 if( nUsed >= mem0.alarmThreshold - nFull ){
24392 mem0.nearlyFull = 1;
@@ -24311,11 +24406,10 @@
24406 nFull = sqlite3MallocSize(p);
24407 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
24408 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
24409 }
24410 *pp = p;
 
24411 }
24412
24413 /*
24414 ** Allocate memory. This routine is like sqlite3_malloc() except that it
24415 ** assumes the memory subsystem has already been initialized.
@@ -24951,11 +25045,10 @@
25045
25046 /*
25047 ** Allowed values for et_info.flags
25048 */
25049 #define FLAG_SIGNED 1 /* True if the value to convert is signed */
 
25050 #define FLAG_STRING 4 /* Allow infinity precision */
25051
25052
25053 /*
25054 ** The following table is searched linearly, so it is good to put the
@@ -24985,15 +25078,14 @@
25078 { 'i', 10, 1, etRADIX, 0, 0 },
25079 { 'n', 0, 0, etSIZE, 0, 0 },
25080 { '%', 0, 0, etPERCENT, 0, 0 },
25081 { 'p', 16, 0, etPOINTER, 0, 1 },
25082
25083 /* All the rest are undocumented and are for internal use only */
25084 { 'T', 0, 0, etTOKEN, 0, 0 },
25085 { 'S', 0, 0, etSRCLIST, 0, 0 },
25086 { 'r', 10, 1, etORDINAL, 0, 0 },
 
25087 };
25088
25089 /*
25090 ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
25091 ** conversions will work.
@@ -25083,11 +25175,10 @@
25175 etByte flag_long; /* True if "l" flag is present */
25176 etByte flag_longlong; /* True if the "ll" flag is present */
25177 etByte done; /* Loop termination flag */
25178 etByte xtype = etINVALID; /* Conversion paradigm */
25179 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
 
25180 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
25181 sqlite_uint64 longvalue; /* Value for integer types */
25182 LONGDOUBLE_TYPE realvalue; /* Value for real types */
25183 const et_info *infop; /* Pointer to the appropriate info structure */
25184 char *zOut; /* Rendering buffer */
@@ -25102,17 +25193,15 @@
25193 #endif
25194 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
25195 char buf[etBUFSIZE]; /* Conversion buffer */
25196
25197 bufpt = 0;
25198 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
25199 pArgList = va_arg(ap, PrintfArguments*);
25200 bArgList = 1;
 
 
25201 }else{
25202 bArgList = 0;
25203 }
25204 for(; (c=(*fmt))!=0; ++fmt){
25205 if( c!='%' ){
25206 bufpt = (char *)fmt;
25207 #if HAVE_STRCHRNUL
@@ -25220,15 +25309,11 @@
25309 infop = &fmtinfo[0];
25310 xtype = etINVALID;
25311 for(idx=0; idx<ArraySize(fmtinfo); idx++){
25312 if( c==fmtinfo[idx].fmttype ){
25313 infop = &fmtinfo[idx];
25314 xtype = infop->type;
 
 
 
 
25315 break;
25316 }
25317 }
25318
25319 /*
@@ -25593,22 +25678,28 @@
25678 ** consume, not the length of the output...
25679 ** if( precision>=0 && precision<length ) length = precision; */
25680 break;
25681 }
25682 case etTOKEN: {
25683 Token *pToken;
25684 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
25685 pToken = va_arg(ap, Token*);
25686 assert( bArgList==0 );
25687 if( pToken && pToken->n ){
25688 sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
25689 }
25690 length = width = 0;
25691 break;
25692 }
25693 case etSRCLIST: {
25694 SrcList *pSrc;
25695 int k;
25696 struct SrcList_item *pItem;
25697 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
25698 pSrc = va_arg(ap, SrcList*);
25699 k = va_arg(ap, int);
25700 pItem = &pSrc->a[k];
25701 assert( bArgList==0 );
25702 assert( k>=0 && k<pSrc->nSrc );
25703 if( pItem->zDatabase ){
25704 sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
25705 sqlite3StrAccumAppend(pAccum, ".", 1);
@@ -25626,13 +25717,17 @@
25717 ** The text of the conversion is pointed to by "bufpt" and is
25718 ** "length" characters long. The field width is "width". Do
25719 ** the output.
25720 */
25721 width -= length;
25722 if( width>0 ){
25723 if( !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
25724 sqlite3StrAccumAppend(pAccum, bufpt, length);
25725 if( flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
25726 }else{
25727 sqlite3StrAccumAppend(pAccum, bufpt, length);
25728 }
25729
25730 if( zExtra ){
25731 sqlite3DbFree(pAccum->db, zExtra);
25732 zExtra = 0;
25733 }
@@ -28600,17 +28695,15 @@
28695 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
28696 #if SQLITE_BYTEORDER==4321
28697 u32 x;
28698 memcpy(&x,p,4);
28699 return x;
28700 #elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
 
28701 u32 x;
28702 memcpy(&x,p,4);
28703 return __builtin_bswap32(x);
28704 #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
 
28705 u32 x;
28706 memcpy(&x,p,4);
28707 return _byteswap_ulong(x);
28708 #else
28709 testcase( p[0]&0x80 );
@@ -28618,16 +28711,14 @@
28711 #endif
28712 }
28713 SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
28714 #if SQLITE_BYTEORDER==4321
28715 memcpy(p,&v,4);
28716 #elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
 
28717 u32 x = __builtin_bswap32(v);
28718 memcpy(p,&x,4);
28719 #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
 
28720 u32 x = _byteswap_ulong(v);
28721 memcpy(p,&x,4);
28722 #else
28723 p[0] = (u8)(v>>24);
28724 p[1] = (u8)(v>>16);
@@ -28739,10 +28830,13 @@
28830 ** the other 64-bit signed integer at *pA and store the result in *pA.
28831 ** Return 0 on success. Or if the operation would have resulted in an
28832 ** overflow, leave *pA unchanged and return 1.
28833 */
28834 SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
28835 #if GCC_VERSION>=5004000 || CLANG_VERSION>=4000000
28836 return __builtin_add_overflow(*pA, iB, pA);
28837 #else
28838 i64 iA = *pA;
28839 testcase( iA==0 ); testcase( iA==1 );
28840 testcase( iB==-1 ); testcase( iB==0 );
28841 if( iB>=0 ){
28842 testcase( iA>0 && LARGEST_INT64 - iA == iB );
@@ -28753,23 +28847,31 @@
28847 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
28848 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
28849 }
28850 *pA += iB;
28851 return 0;
28852 #endif
28853 }
28854 SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
28855 #if GCC_VERSION>=5004000 || CLANG_VERSION>=4000000
28856 return __builtin_sub_overflow(*pA, iB, pA);
28857 #else
28858 testcase( iB==SMALLEST_INT64+1 );
28859 if( iB==SMALLEST_INT64 ){
28860 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
28861 if( (*pA)>=0 ) return 1;
28862 *pA -= iB;
28863 return 0;
28864 }else{
28865 return sqlite3AddInt64(pA, -iB);
28866 }
28867 #endif
28868 }
28869 SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
28870 #if GCC_VERSION>=5004000 || CLANG_VERSION>=4000000
28871 return __builtin_mul_overflow(*pA, iB, pA);
28872 #else
28873 i64 iA = *pA;
28874 if( iB>0 ){
28875 if( iA>LARGEST_INT64/iB ) return 1;
28876 if( iA<SMALLEST_INT64/iB ) return 1;
28877 }else if( iB<0 ){
@@ -28781,10 +28883,11 @@
28883 if( -iA>LARGEST_INT64/-iB ) return 1;
28884 }
28885 }
28886 *pA = iA*iB;
28887 return 0;
28888 #endif
28889 }
28890
28891 /*
28892 ** Compute the absolute value of a 32-bit signed integer, of possible. Or
28893 ** if the integer has a value of -2147483648, return +2147483647
@@ -47485,18 +47588,24 @@
47588 ** if( pPager->jfd->pMethods ){ ...
47589 */
47590 #define isOpen(pFd) ((pFd)->pMethods!=0)
47591
47592 /*
47593 ** Return true if this pager uses a write-ahead log to read page pgno.
47594 ** Return false if the pager reads pgno directly from the database.
47595 */
47596 #if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_DIRECT_OVERFLOW_READ)
47597 SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager, Pgno pgno){
47598 u32 iRead = 0;
47599 int rc;
47600 if( pPager->pWal==0 ) return 0;
47601 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
47602 return rc || iRead;
47603 }
47604 #endif
47605 #ifndef SQLITE_OMIT_WAL
47606 # define pagerUseWal(x) ((x)->pWal!=0)
 
 
 
47607 #else
47608 # define pagerUseWal(x) 0
47609 # define pagerRollbackWal(x) 0
47610 # define pagerWalFrames(v,w,x,y) 0
47611 # define pagerOpenWalIfPresent(z) SQLITE_OK
@@ -58445,15 +58554,13 @@
58554 ** two-byte aligned address. get2bytea() is only used for accessing the
58555 ** cell addresses in a btree header.
58556 */
58557 #if SQLITE_BYTEORDER==4321
58558 # define get2byteAligned(x) (*(u16*)(x))
58559 #elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000
 
58560 # define get2byteAligned(x) __builtin_bswap16(*(u16*)(x))
58561 #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
 
58562 # define get2byteAligned(x) _byteswap_ushort(*(u16*)(x))
58563 #else
58564 # define get2byteAligned(x) ((x)[0]<<8 | (x)[1])
58565 #endif
58566
@@ -58624,27 +58731,38 @@
58731 ** Enter the mutexes in accending order by BtShared pointer address
58732 ** to avoid the possibility of deadlock when two threads with
58733 ** two or more btrees in common both try to lock all their btrees
58734 ** at the same instant.
58735 */
58736 static void SQLITE_NOINLINE btreeEnterAll(sqlite3 *db){
58737 int i;
58738 int skipOk = 1;
58739 Btree *p;
58740 assert( sqlite3_mutex_held(db->mutex) );
58741 for(i=0; i<db->nDb; i++){
58742 p = db->aDb[i].pBt;
58743 if( p && p->sharable ){
58744 sqlite3BtreeEnter(p);
58745 skipOk = 0;
58746 }
58747 }
58748 db->skipBtreeMutex = skipOk;
58749 }
58750 SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
58751 if( db->skipBtreeMutex==0 ) btreeEnterAll(db);
58752 }
58753 static void SQLITE_NOINLINE btreeLeaveAll(sqlite3 *db){
58754 int i;
58755 Btree *p;
58756 assert( sqlite3_mutex_held(db->mutex) );
58757 for(i=0; i<db->nDb; i++){
58758 p = db->aDb[i].pBt;
58759 if( p ) sqlite3BtreeLeave(p);
58760 }
58761 }
58762 SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
58763 if( db->skipBtreeMutex==0 ) btreeLeaveAll(db);
58764 }
58765
58766 #ifndef NDEBUG
58767 /*
58768 ** Return true if the current thread holds the database connection
@@ -62097,16 +62215,18 @@
62215 for(i=0; i<nCell; i++){
62216 u8 *pCell = findCell(pPage, i);
62217 if( eType==PTRMAP_OVERFLOW1 ){
62218 CellInfo info;
62219 pPage->xParseCell(pPage, pCell, &info);
62220 if( info.nLocal<info.nPayload ){
62221 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
62222 return SQLITE_CORRUPT_BKPT;
62223 }
62224 if( iFrom==get4byte(pCell+info.nSize-4) ){
62225 put4byte(pCell+info.nSize-4, iTo);
62226 break;
62227 }
62228 }
62229 }else{
62230 if( get4byte(pCell)==iFrom ){
62231 put4byte(pCell, iTo);
62232 break;
@@ -62777,11 +62897,16 @@
62897 if( p && p->inTrans==TRANS_WRITE ){
62898 BtShared *pBt = p->pBt;
62899 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
62900 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
62901 sqlite3BtreeEnter(p);
62902 if( op==SAVEPOINT_ROLLBACK ){
62903 rc = saveAllCursors(pBt, 0, 0);
62904 }
62905 if( rc==SQLITE_OK ){
62906 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
62907 }
62908 if( rc==SQLITE_OK ){
62909 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
62910 pBt->nPage = 0;
62911 }
62912 rc = newDatabase(pBt);
@@ -63163,25 +63288,24 @@
63288 ** for the entry that the pCur cursor is pointing to. The eOp
63289 ** argument is interpreted as follows:
63290 **
63291 ** 0: The operation is a read. Populate the overflow cache.
63292 ** 1: The operation is a write. Populate the overflow cache.
 
63293 **
63294 ** A total of "amt" bytes are read or written beginning at "offset".
63295 ** Data is read to or from the buffer pBuf.
63296 **
63297 ** The content being read or written might appear on the main page
63298 ** or be scattered out on multiple overflow pages.
63299 **
63300 ** If the current cursor entry uses one or more overflow pages
63301 ** this function may allocate space for and lazily populate
63302 ** the overflow page-list cache array (BtCursor.aOverflow).
63303 ** Subsequent calls use this cache to make seeking to the supplied offset
63304 ** more efficient.
63305 **
63306 ** Once an overflow page-list cache has been allocated, it must be
63307 ** invalidated if some other cursor writes to the same table, or if
63308 ** the cursor is moved to a different row. Additionally, in auto-vacuum
63309 ** mode, the following events may invalidate an overflow page-list cache.
63310 **
63311 ** * An incremental vacuum,
@@ -63199,25 +63323,21 @@
63323 int rc = SQLITE_OK;
63324 int iIdx = 0;
63325 MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
63326 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
63327 #ifdef SQLITE_DIRECT_OVERFLOW_READ
63328 unsigned char * const pBufStart = pBuf; /* Start of original out buffer */
 
63329 #endif
63330
63331 assert( pPage );
63332 assert( eOp==0 || eOp==1 );
63333 assert( pCur->eState==CURSOR_VALID );
63334 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
63335 assert( cursorHoldsMutex(pCur) );
 
63336
63337 getCellInfo(pCur);
63338 aPayload = pCur->info.pPayload;
 
 
 
63339 assert( offset+amt <= pCur->info.nPayload );
63340
63341 assert( aPayload > pPage->aData );
63342 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
63343 /* Trying to read or write past the end of the data is an error. The
@@ -63232,11 +63352,11 @@
63352 if( offset<pCur->info.nLocal ){
63353 int a = amt;
63354 if( a+offset>pCur->info.nLocal ){
63355 a = pCur->info.nLocal - offset;
63356 }
63357 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
63358 offset = 0;
63359 pBuf += a;
63360 amt -= a;
63361 }else{
63362 offset -= pCur->info.nLocal;
@@ -63248,69 +63368,58 @@
63368 Pgno nextPage;
63369
63370 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
63371
63372 /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
 
63373 **
63374 ** The aOverflow[] array is sized at one entry for each overflow page
63375 ** in the overflow chain. The page number of the first overflow page is
63376 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
63377 ** means "not yet known" (the cache is lazily populated).
63378 */
63379 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
63380 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
63381 if( nOvfl>pCur->nOvflAlloc ){
63382 Pgno *aNew = (Pgno*)sqlite3Realloc(
63383 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
63384 );
63385 if( aNew==0 ){
63386 return SQLITE_NOMEM_BKPT;
63387 }else{
63388 pCur->nOvflAlloc = nOvfl*2;
63389 pCur->aOverflow = aNew;
63390 }
63391 }
63392 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
63393 pCur->curFlags |= BTCF_ValidOvfl;
63394 }else{
63395 /* If the overflow page-list cache has been allocated and the
63396 ** entry for the first required overflow page is valid, skip
63397 ** directly to it.
63398 */
63399 if( pCur->aOverflow[offset/ovflSize] ){
63400 iIdx = (offset/ovflSize);
63401 nextPage = pCur->aOverflow[iIdx];
63402 offset = (offset%ovflSize);
63403 }
63404 }
63405
63406 assert( rc==SQLITE_OK && amt>0 );
63407 while( nextPage ){
 
 
 
 
 
 
 
 
 
 
 
 
63408 /* If required, populate the overflow page-list cache. */
63409 assert( pCur->aOverflow[iIdx]==0
63410 || pCur->aOverflow[iIdx]==nextPage
63411 || CORRUPT_DB );
63412 pCur->aOverflow[iIdx] = nextPage;
 
 
63413
63414 if( offset>=ovflSize ){
63415 /* The only reason to read this page is to obtain the page
63416 ** number for the next page in the overflow chain. The page
63417 ** data is not required. So first try to lookup the overflow
63418 ** page-list cache, if any, then fall back to the getOverflowPage()
63419 ** function.
 
 
 
63420 */
 
63421 assert( pCur->curFlags & BTCF_ValidOvfl );
63422 assert( pCur->pBtree->db==pBt->db );
63423 if( pCur->aOverflow[iIdx+1] ){
63424 nextPage = pCur->aOverflow[iIdx+1];
63425 }else{
@@ -63320,11 +63429,11 @@
63429 }else{
63430 /* Need to read this page properly. It contains some of the
63431 ** range of data that is being read (eOp==0) or written (eOp!=0).
63432 */
63433 #ifdef SQLITE_DIRECT_OVERFLOW_READ
63434 sqlite3_file *fd; /* File from which to do direct overflow read */
63435 #endif
63436 int a = amt;
63437 if( a + offset > ovflSize ){
63438 a = ovflSize - offset;
63439 }
@@ -63332,31 +63441,29 @@
63441 #ifdef SQLITE_DIRECT_OVERFLOW_READ
63442 /* If all the following are true:
63443 **
63444 ** 1) this is a read operation, and
63445 ** 2) data is required from the start of this overflow page, and
63446 ** 3) there is no open write-transaction, and
63447 ** 4) the database is file-backed, and
63448 ** 5) the page is not in the WAL file
63449 ** 6) at least 4 bytes have already been read into the output buffer
 
63450 **
63451 ** then data can be read directly from the database file into the
63452 ** output buffer, bypassing the page-cache altogether. This speeds
63453 ** up loading large records that span many overflow pages.
63454 */
63455 if( eOp==0 /* (1) */
63456 && offset==0 /* (2) */
63457 && pBt->inTransaction==TRANS_READ /* (3) */
63458 && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (4) */
63459 && 0==sqlite3PagerUseWal(pBt->pPager, nextPage) /* (5) */
63460 && &pBuf[-4]>=pBufStart /* (6) */
 
63461 ){
63462 u8 aSave[4];
63463 u8 *aWrite = &pBuf[-4];
63464 assert( aWrite>=pBufStart ); /* due to (6) */
63465 memcpy(aSave, aWrite, 4);
63466 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
63467 nextPage = get4byte(aWrite);
63468 memcpy(aWrite, aSave, 4);
63469 }else
@@ -63363,28 +63470,31 @@
63470 #endif
63471
63472 {
63473 DbPage *pDbPage;
63474 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
63475 (eOp==0 ? PAGER_GET_READONLY : 0)
63476 );
63477 if( rc==SQLITE_OK ){
63478 aPayload = sqlite3PagerGetData(pDbPage);
63479 nextPage = get4byte(aPayload);
63480 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
63481 sqlite3PagerUnref(pDbPage);
63482 offset = 0;
63483 }
63484 }
63485 amt -= a;
63486 if( amt==0 ) return rc;
63487 pBuf += a;
63488 }
63489 if( rc ) break;
63490 iIdx++;
63491 }
63492 }
63493
63494 if( rc==SQLITE_OK && amt>0 ){
63495 return SQLITE_CORRUPT_BKPT; /* Overflow chain ends prematurely */
63496 }
63497 return rc;
63498 }
63499
63500 /*
@@ -63409,25 +63519,38 @@
63519 assert( pCur->eState==CURSOR_VALID );
63520 assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
63521 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
63522 return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
63523 }
63524
63525 /*
63526 ** This variant of sqlite3BtreePayload() works even if the cursor has not
63527 ** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read()
63528 ** interface.
63529 */
63530 #ifndef SQLITE_OMIT_INCRBLOB
63531 static SQLITE_NOINLINE int accessPayloadChecked(
63532 BtCursor *pCur,
63533 u32 offset,
63534 u32 amt,
63535 void *pBuf
63536 ){
63537 int rc;
63538 if ( pCur->eState==CURSOR_INVALID ){
63539 return SQLITE_ABORT;
63540 }
63541 assert( cursorOwnsBtShared(pCur) );
63542 rc = btreeRestoreCursorPosition(pCur);
63543 return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
63544 }
63545 SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
63546 if( pCur->eState==CURSOR_VALID ){
63547 assert( cursorOwnsBtShared(pCur) );
63548 return accessPayload(pCur, offset, amt, pBuf, 0);
63549 }else{
63550 return accessPayloadChecked(pCur, offset, amt, pBuf);
63551 }
63552 }
63553 #endif /* SQLITE_OMIT_INCRBLOB */
63554
63555 /*
63556 ** Return a pointer to payload information from the entry that the
@@ -63829,13 +63952,30 @@
63952 ){
63953 if( pCur->info.nKey==intKey ){
63954 *pRes = 0;
63955 return SQLITE_OK;
63956 }
63957 if( pCur->info.nKey<intKey ){
63958 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
63959 *pRes = -1;
63960 return SQLITE_OK;
63961 }
63962 /* If the requested key is one more than the previous key, then
63963 ** try to get there using sqlite3BtreeNext() rather than a full
63964 ** binary search. This is an optimization only. The correct answer
63965 ** is still obtained without this ase, only a little more slowely */
63966 if( pCur->info.nKey+1==intKey && !pCur->skipNext ){
63967 *pRes = 0;
63968 rc = sqlite3BtreeNext(pCur, pRes);
63969 if( rc ) return rc;
63970 if( *pRes==0 ){
63971 getCellInfo(pCur);
63972 if( pCur->info.nKey==intKey ){
63973 return SQLITE_OK;
63974 }
63975 }
63976 }
63977 }
63978 }
63979
63980 if( pIdxKey ){
63981 xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
@@ -63967,11 +64107,12 @@
64107 if( pCellKey==0 ){
64108 rc = SQLITE_NOMEM_BKPT;
64109 goto moveto_finish;
64110 }
64111 pCur->aiIdx[pCur->iPage] = (u16)idx;
64112 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
64113 pCur->curFlags &= ~BTCF_ValidOvfl;
64114 if( rc ){
64115 sqlite3_free(pCellKey);
64116 goto moveto_finish;
64117 }
64118 c = xRecordCompare(nCell, pCellKey, pIdxKey);
@@ -66010,11 +66151,10 @@
66151 */
66152 usableSpace = pBt->usableSize - 12 + leafCorrection;
66153 for(i=0; i<nOld; i++){
66154 MemPage *p = apOld[i];
66155 szNew[i] = usableSpace - p->nFree;
 
66156 for(j=0; j<p->nOverflow; j++){
66157 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
66158 }
66159 cntNew[i] = cntOld[i];
66160 }
@@ -66689,11 +66829,11 @@
66829 ** to decode the key.
66830 */
66831 SQLITE_PRIVATE int sqlite3BtreeInsert(
66832 BtCursor *pCur, /* Insert data into the table of this cursor */
66833 const BtreePayload *pX, /* Content of the row to be inserted */
66834 int flags, /* True if this is likely an append */
66835 int seekResult /* Result of prior MovetoUnpacked() call */
66836 ){
66837 int rc;
66838 int loc = seekResult; /* -1: before desired location +1: after */
66839 int szNew = 0;
@@ -66701,10 +66841,12 @@
66841 MemPage *pPage;
66842 Btree *p = pCur->pBtree;
66843 BtShared *pBt = p->pBt;
66844 unsigned char *oldCell;
66845 unsigned char *newCell = 0;
66846
66847 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND))==flags );
66848
66849 if( pCur->eState==CURSOR_FAULT ){
66850 assert( pCur->skipNext!=SQLITE_OK );
66851 return pCur->skipNext;
66852 }
@@ -66742,23 +66884,28 @@
66884 assert( pX->pKey==0 );
66885 /* If this is an insert into a table b-tree, invalidate any incrblob
66886 ** cursors open on the row being replaced */
66887 invalidateIncrblobCursors(p, pX->nKey, 0);
66888
66889 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
66890 ** to a row with the same key as the new entry being inserted. */
66891 assert( (flags & BTREE_SAVEPOSITION)==0 ||
66892 ((pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey) );
66893
66894 /* If the cursor is currently on the last row and we are appending a
66895 ** new row onto the end, set the "loc" to avoid an unnecessary
66896 ** btreeMoveto() call */
66897 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
66898 loc = 0;
66899 }else if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey>0
66900 && pCur->info.nKey==pX->nKey-1 ){
66901 loc = -1;
66902 }else if( loc==0 ){
66903 rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
66904 if( rc ) return rc;
66905 }
66906 }else if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
66907 if( pX->nMem ){
66908 UnpackedRecord r;
66909 r.pKeyInfo = pCur->pKeyInfo;
66910 r.aMem = pX->aMem;
66911 r.nField = pX->nMem;
@@ -66765,13 +66912,13 @@
66912 r.default_rc = 0;
66913 r.errCode = 0;
66914 r.r1 = 0;
66915 r.r2 = 0;
66916 r.eqSeen = 0;
66917 rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc);
66918 }else{
66919 rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
66920 }
66921 if( rc ) return rc;
66922 }
66923 assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
66924
@@ -66855,10 +67002,24 @@
67002 ** fails. Internal data structure corruption will result otherwise.
67003 ** Also, set the cursor state to invalid. This stops saveCursorPosition()
67004 ** from trying to save the current position of the cursor. */
67005 pCur->apPage[pCur->iPage]->nOverflow = 0;
67006 pCur->eState = CURSOR_INVALID;
67007 if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){
67008 rc = moveToRoot(pCur);
67009 if( pCur->pKeyInfo ){
67010 assert( pCur->pKey==0 );
67011 pCur->pKey = sqlite3Malloc( pX->nKey );
67012 if( pCur->pKey==0 ){
67013 rc = SQLITE_NOMEM;
67014 }else{
67015 memcpy(pCur->pKey, pX->pKey, pX->nKey);
67016 }
67017 }
67018 pCur->eState = CURSOR_REQUIRESEEK;
67019 pCur->nKey = pX->nKey;
67020 }
67021 }
67022 assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
67023
67024 end_insert:
67025 return rc;
@@ -71765,11 +71926,11 @@
71926 sqlite3VdbeGetOp(p,addr)->p2 = val;
71927 }
71928 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
71929 sqlite3VdbeGetOp(p,addr)->p3 = val;
71930 }
71931 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
71932 assert( p->nOp>0 || p->db->mallocFailed );
71933 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
71934 }
71935
71936 /*
@@ -73479,64 +73640,63 @@
73640 ** statement transaction is committed.
73641 **
73642 ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
73643 ** Otherwise SQLITE_OK.
73644 */
73645 static SQLITE_NOINLINE int vdbeCloseStatement(Vdbe *p, int eOp){
73646 sqlite3 *const db = p->db;
73647 int rc = SQLITE_OK;
73648 int i;
73649 const int iSavepoint = p->iStatement-1;
73650
73651 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
73652 assert( db->nStatement>0 );
73653 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
73654
73655 for(i=0; i<db->nDb; i++){
73656 int rc2 = SQLITE_OK;
73657 Btree *pBt = db->aDb[i].pBt;
73658 if( pBt ){
73659 if( eOp==SAVEPOINT_ROLLBACK ){
73660 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
73661 }
73662 if( rc2==SQLITE_OK ){
73663 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
73664 }
73665 if( rc==SQLITE_OK ){
73666 rc = rc2;
73667 }
73668 }
73669 }
73670 db->nStatement--;
73671 p->iStatement = 0;
73672
73673 if( rc==SQLITE_OK ){
73674 if( eOp==SAVEPOINT_ROLLBACK ){
73675 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
73676 }
73677 if( rc==SQLITE_OK ){
73678 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
73679 }
73680 }
73681
73682 /* If the statement transaction is being rolled back, also restore the
73683 ** database handles deferred constraint counter to the value it had when
73684 ** the statement transaction was opened. */
73685 if( eOp==SAVEPOINT_ROLLBACK ){
73686 db->nDeferredCons = p->nStmtDefCons;
73687 db->nDeferredImmCons = p->nStmtDefImmCons;
73688 }
73689 return rc;
73690 }
73691 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
73692 if( p->db->nStatement && p->iStatement ){
73693 return vdbeCloseStatement(p, eOp);
73694 }
73695 return SQLITE_OK;
73696 }
73697
 
73698
73699 /*
73700 ** This function is called when a transaction opened by the database
73701 ** handle associated with the VM passed as an argument is about to be
73702 ** committed. If there are outstanding deferred foreign key constraint
@@ -75567,14 +75727,14 @@
75727 ** structure itself, using sqlite3DbFree().
75728 **
75729 ** This function is used to free UnpackedRecord structures allocated by
75730 ** the vdbeUnpackRecord() function found in vdbeapi.c.
75731 */
75732 static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
75733 if( p ){
75734 int i;
75735 for(i=0; i<nField; i++){
75736 Mem *pMem = &p->aMem[i];
75737 if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
75738 }
75739 sqlite3DbFree(db, p);
75740 }
@@ -75603,14 +75763,19 @@
75763 const char *zTbl = pTab->zName;
75764 static const u8 fakeSortOrder = 0;
75765
75766 assert( db->pPreUpdate==0 );
75767 memset(&preupdate, 0, sizeof(PreUpdate));
75768 if( HasRowid(pTab)==0 ){
75769 iKey1 = iKey2 = 0;
75770 preupdate.pPk = sqlite3PrimaryKeyIndex(pTab);
75771 }else{
75772 if( op==SQLITE_UPDATE ){
75773 iKey2 = v->aMem[iReg].u.i;
75774 }else{
75775 iKey2 = iKey1;
75776 }
75777 }
75778
75779 assert( pCsr->nField==pTab->nCol
75780 || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
75781 );
@@ -75629,12 +75794,12 @@
75794
75795 db->pPreUpdate = &preupdate;
75796 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
75797 db->pPreUpdate = 0;
75798 sqlite3DbFree(db, preupdate.aRecord);
75799 vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pUnpacked);
75800 vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pNewUnpacked);
75801 if( preupdate.aNew ){
75802 int i;
75803 for(i=0; i<pCsr->nField; i++){
75804 sqlite3VdbeMemRelease(&preupdate.aNew[i]);
75805 }
@@ -77305,18 +77470,22 @@
77470 ** This function is called from within a pre-update callback to retrieve
77471 ** a field of the row currently being updated or deleted.
77472 */
77473 SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
77474 PreUpdate *p = db->pPreUpdate;
77475 Mem *pMem;
77476 int rc = SQLITE_OK;
77477
77478 /* Test that this call is being made from within an SQLITE_DELETE or
77479 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
77480 if( !p || p->op==SQLITE_INSERT ){
77481 rc = SQLITE_MISUSE_BKPT;
77482 goto preupdate_old_out;
77483 }
77484 if( p->pPk ){
77485 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
77486 }
77487 if( iIdx>=p->pCsr->nField || iIdx<0 ){
77488 rc = SQLITE_RANGE;
77489 goto preupdate_old_out;
77490 }
77491
@@ -77338,21 +77507,18 @@
77507 goto preupdate_old_out;
77508 }
77509 p->aRecord = aRec;
77510 }
77511
77512 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
77513 if( iIdx==p->pTab->iPKey ){
77514 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
77515 }else if( iIdx>=p->pUnpacked->nField ){
77516 *ppValue = (sqlite3_value *)columnNullValue();
77517 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
77518 if( pMem->flags & MEM_Int ){
77519 sqlite3VdbeMemRealify(pMem);
 
 
 
 
 
 
77520 }
77521 }
77522
77523 preupdate_old_out:
77524 sqlite3Error(db, rc);
@@ -77401,10 +77567,13 @@
77567
77568 if( !p || p->op==SQLITE_DELETE ){
77569 rc = SQLITE_MISUSE_BKPT;
77570 goto preupdate_new_out;
77571 }
77572 if( p->pPk && p->op!=SQLITE_UPDATE ){
77573 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
77574 }
77575 if( iIdx>=p->pCsr->nField || iIdx<0 ){
77576 rc = SQLITE_RANGE;
77577 goto preupdate_new_out;
77578 }
77579
@@ -77421,17 +77590,15 @@
77590 rc = SQLITE_NOMEM;
77591 goto preupdate_new_out;
77592 }
77593 p->pNewUnpacked = pUnpack;
77594 }
77595 pMem = &pUnpack->aMem[iIdx];
77596 if( iIdx==p->pTab->iPKey ){
77597 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
77598 }else if( iIdx>=pUnpack->nField ){
77599 pMem = (sqlite3_value *)columnNullValue();
 
 
 
 
 
77600 }
77601 }else{
77602 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
77603 ** value. Make a copy of the cell contents and return a pointer to it.
77604 ** It is not safe to return a pointer to the memory cell itself as the
@@ -78404,12 +78571,10 @@
78571 Mem *aMem = p->aMem; /* Copy of p->aMem */
78572 Mem *pIn1 = 0; /* 1st input operand */
78573 Mem *pIn2 = 0; /* 2nd input operand */
78574 Mem *pIn3 = 0; /* 3rd input operand */
78575 Mem *pOut = 0; /* Output operand */
 
 
78576 #ifdef VDBE_PROFILE
78577 u64 start; /* CPU clock count at start of opcode */
78578 #endif
78579 /*** INSERT STACK UNION HERE ***/
78580
@@ -78420,11 +78585,10 @@
78585 ** sqlite3_column_text16() failed. */
78586 goto no_mem;
78587 }
78588 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
78589 assert( p->bIsReader || p->readOnly!=0 );
 
78590 p->iCurrentTime = 0;
78591 assert( p->explain==0 );
78592 p->pResultSet = 0;
78593 db->busyHandler.nBusy = 0;
78594 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
@@ -78781,11 +78945,10 @@
78945 pFrame = p->pFrame;
78946 p->pFrame = pFrame->pParent;
78947 p->nFrame--;
78948 sqlite3VdbeSetChanges(db, p->nChange);
78949 pcx = sqlite3VdbeFrameRestore(pFrame);
 
78950 if( pOp->p2==OE_Ignore ){
78951 /* Instruction pcx is the OP_Program that invoked the sub-program
78952 ** currently being halted. If the p2 instruction of this OP_Halt
78953 ** instruction is set to OE_Ignore, then the sub-program is throwing
78954 ** an IGNORE exception. In this case jump to the address specified
@@ -79016,11 +79179,11 @@
79179 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
79180 pVar = &p->aVar[pOp->p1 - 1];
79181 if( sqlite3VdbeMemTooBig(pVar) ){
79182 goto too_big;
79183 }
79184 pOut = &aMem[pOp->p2];
79185 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
79186 UPDATE_MAX_BLOBSIZE(pOut);
79187 break;
79188 }
79189
@@ -79503,13 +79666,11 @@
79666 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
79667 }
79668 #endif
79669 MemSetTypeFlag(pCtx->pOut, MEM_Null);
79670 pCtx->fErrorOrAux = 0;
 
79671 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
 
79672
79673 /* If the function returned an error, throw an exception */
79674 if( pCtx->fErrorOrAux ){
79675 if( pCtx->isError ){
79676 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
@@ -79961,12 +80122,12 @@
80122 }
80123
80124
80125 /* Opcode: Permutation * * * P4 *
80126 **
80127 ** Set the permutation used by the OP_Compare operator in the next
80128 ** instruction. The permutation is stored in the P4 operand.
80129 **
80130 ** The permutation is only valid until the next OP_Compare that has
80131 ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
80132 ** occur immediately prior to the OP_Compare.
80133 **
@@ -79974,11 +80135,12 @@
80135 ** and does not become part of the permutation.
80136 */
80137 case OP_Permutation: {
80138 assert( pOp->p4type==P4_INTARRAY );
80139 assert( pOp->p4.ai );
80140 assert( pOp[1].opcode==OP_Compare );
80141 assert( pOp[1].p5 & OPFLAG_PERMUTE );
80142 break;
80143 }
80144
80145 /* Opcode: Compare P1 P2 P3 P4 P5
80146 ** Synopsis: r[P1@P3] <-> r[P2@P3]
@@ -80007,12 +80169,21 @@
80169 int p2;
80170 const KeyInfo *pKeyInfo;
80171 int idx;
80172 CollSeq *pColl; /* Collating sequence to use on this term */
80173 int bRev; /* True for DESCENDING sort order */
80174 int *aPermute; /* The permutation */
80175
80176 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
80177 aPermute = 0;
80178 }else{
80179 assert( pOp>aOp );
80180 assert( pOp[-1].opcode==OP_Permutation );
80181 assert( pOp[-1].p4type==P4_INTARRAY );
80182 aPermute = pOp[-1].p4.ai + 1;
80183 assert( aPermute!=0 );
80184 }
80185 n = pOp->p3;
80186 pKeyInfo = pOp->p4.pKeyInfo;
80187 assert( n>0 );
80188 assert( pKeyInfo!=0 );
80189 p1 = pOp->p1;
@@ -80041,11 +80212,10 @@
80212 if( iCompare ){
80213 if( bRev ) iCompare = -iCompare;
80214 break;
80215 }
80216 }
 
80217 break;
80218 }
80219
80220 /* Opcode: Jump P1 P2 P3 * *
80221 **
@@ -80597,10 +80767,24 @@
80767 do{
80768 applyAffinity(pRec++, *(zAffinity++), encoding);
80769 assert( zAffinity[0]==0 || pRec<=pLast );
80770 }while( zAffinity[0] );
80771 }
80772
80773 #ifdef SQLITE_ENABLE_NULL_TRIM
80774 /* NULLs can be safely trimmed from the end of the record, as long as
80775 ** as the schema format is 2 or more and none of the omitted columns
80776 ** have a non-NULL default value. Also, the record must be left with
80777 ** at least one field. If P5>0 then it will be one more than the
80778 ** index of the right-most column with a non-NULL default value */
80779 if( pOp->p5 ){
80780 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
80781 pLast--;
80782 nField--;
80783 }
80784 }
80785 #endif
80786
80787 /* Loop through the elements that will make up the record to figure
80788 ** out how much space is required for the new record.
80789 */
80790 pRec = pLast;
@@ -82187,11 +82371,11 @@
82371 assert( memIsValid(pData) );
82372 pC = p->apCsr[pOp->p1];
82373 assert( pC!=0 );
82374 assert( pC->eCurType==CURTYPE_BTREE );
82375 assert( pC->uc.pCursor!=0 );
82376 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
82377 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
82378 REGISTER_TRACE(pOp->p2, pData);
82379
82380 if( pOp->opcode==OP_Insert ){
82381 pKey = &aMem[pOp->p3];
@@ -82203,18 +82387,17 @@
82387 assert( pOp->opcode==OP_InsertInt );
82388 x.nKey = pOp->p3;
82389 }
82390
82391 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
 
82392 assert( pC->iDb>=0 );
82393 zDb = db->aDb[pC->iDb].zDbSName;
82394 pTab = pOp->p4.pTab;
82395 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
82396 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
82397 }else{
82398 pTab = 0; /* Not needed. Silence a compiler warning. */
82399 zDb = 0; /* Not needed. Silence a compiler warning. */
82400 }
82401
82402 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
82403 /* Invoke the pre-update hook, if any */
@@ -82222,14 +82405,15 @@
82405 && pOp->p4type==P4_TABLE
82406 && !(pOp->p5 & OPFLAG_ISUPDATE)
82407 ){
82408 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey, pOp->p2);
82409 }
82410 if( pOp->p5 & OPFLAG_ISNOOP ) break;
82411 #endif
82412
82413 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
82414 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
82415 if( pData->flags & MEM_Null ){
82416 x.pData = 0;
82417 x.nData = 0;
82418 }else{
82419 assert( pData->flags & (MEM_Blob|MEM_Str) );
@@ -82242,11 +82426,11 @@
82426 }else{
82427 x.nZero = 0;
82428 }
82429 x.pKey = 0;
82430 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
82431 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
82432 );
82433 pC->deferredMoveto = 0;
82434 pC->cacheStatus = CACHE_STALE;
82435
82436 /* Invoke the update-hook if required. */
@@ -82334,12 +82518,15 @@
82518 pTab = 0; /* Not needed. Silence a compiler warning. */
82519 }
82520
82521 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
82522 /* Invoke the pre-update-hook if required. */
82523 if( db->xPreUpdateCallback && pOp->p4.pTab ){
82524 assert( !(opflags & OPFLAG_ISUPDATE)
82525 || HasRowid(pTab)==0
82526 || (aMem[pOp->p3].flags & MEM_Int)
82527 );
82528 sqlite3VdbePreUpdateHook(p, pC,
82529 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
82530 zDb, pTab, pC->movetoTarget,
82531 pOp->p3
82532 );
@@ -82453,11 +82640,11 @@
82640 if( rc ) goto abort_due_to_error;
82641 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
82642 break;
82643 }
82644
82645 /* Opcode: RowData P1 P2 P3 * *
82646 ** Synopsis: r[P2]=data
82647 **
82648 ** Write into register P2 the complete row content for the row at
82649 ** which cursor P1 is currently pointing.
82650 ** There is no interpretation of the data.
@@ -82467,18 +82654,30 @@
82654 ** If cursor P1 is an index, then the content is the key of the row.
82655 ** If cursor P2 is a table, then the content extracted is the data.
82656 **
82657 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
82658 ** of a real table, not a pseudo-table.
82659 **
82660 ** If P3!=0 then this opcode is allowed to make an ephermeral pointer
82661 ** into the database page. That means that the content of the output
82662 ** register will be invalidated as soon as the cursor moves - including
82663 ** moves caused by other cursors that "save" the the current cursors
82664 ** position in order that they can write to the same table. If P3==0
82665 ** then a copy of the data is made into memory. P3!=0 is faster, but
82666 ** P3==0 is safer.
82667 **
82668 ** If P3!=0 then the content of the P2 register is unsuitable for use
82669 ** in OP_Result and any OP_Result will invalidate the P2 register content.
82670 ** The P2 register content is invalidated by opcodes like OP_Function or
82671 ** by any use of another cursor pointing to the same table.
82672 */
82673 case OP_RowData: {
82674 VdbeCursor *pC;
82675 BtCursor *pCrsr;
82676 u32 n;
82677
82678 pOut = out2Prerelease(p, pOp);
 
82679
82680 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
82681 pC = p->apCsr[pOp->p1];
82682 assert( pC!=0 );
82683 assert( pC->eCurType==CURTYPE_BTREE );
@@ -82505,18 +82704,13 @@
82704 n = sqlite3BtreePayloadSize(pCrsr);
82705 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
82706 goto too_big;
82707 }
82708 testcase( n==0 );
82709 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut);
 
 
 
 
 
82710 if( rc ) goto abort_due_to_error;
82711 if( !pOp->p3 ) Deephemeralize(pOut);
82712 UPDATE_MAX_BLOBSIZE(pOut);
82713 REGISTER_TRACE(pOp->p2, pOut);
82714 break;
82715 }
82716
@@ -82900,11 +83094,11 @@
83094 x.nKey = pIn2->n;
83095 x.pKey = pIn2->z;
83096 x.aMem = aMem + pOp->p3;
83097 x.nMem = (u16)pOp->p4.i;
83098 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
83099 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)),
83100 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
83101 );
83102 assert( pC->deferredMoveto==0 );
83103 pC->cacheStatus = CACHE_STALE;
83104 }
@@ -83022,11 +83216,10 @@
83216 pTabCur->aAltMap = pOp->p4.ai;
83217 pTabCur->pAltCursor = pC;
83218 }else{
83219 pOut = out2Prerelease(p, pOp);
83220 pOut->u.i = rowid;
 
83221 }
83222 }else{
83223 assert( pOp->opcode==OP_IdxRowid );
83224 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
83225 }
@@ -83664,11 +83857,11 @@
83857 assert( (int)(pOp - aOp)==pFrame->pc );
83858 }
83859
83860 p->nFrame++;
83861 pFrame->pParent = p->pFrame;
83862 pFrame->lastRowid = db->lastRowid;
83863 pFrame->nChange = p->nChange;
83864 pFrame->nDbChange = p->db->nChange;
83865 assert( pFrame->pAuxData==0 );
83866 pFrame->pAuxData = p->pAuxData;
83867 p->pAuxData = 0;
@@ -84605,11 +84798,11 @@
84798 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
84799 db->vtabOnConflict = vtabOnConflict;
84800 sqlite3VtabImportErrmsg(p, pVtab);
84801 if( rc==SQLITE_OK && pOp->p1 ){
84802 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
84803 db->lastRowid = rowid;
84804 }
84805 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
84806 if( pOp->p5==OE_Ignore ){
84807 rc = SQLITE_OK;
84808 }else{
@@ -84841,11 +85034,10 @@
85034
85035 /* This is the only way out of this procedure. We have to
85036 ** release the mutexes on btrees that were acquired at the
85037 ** top. */
85038 vdbe_return:
 
85039 testcase( nVmStep>0 );
85040 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
85041 sqlite3VdbeLeave(p);
85042 assert( rc!=SQLITE_OK || nExtraDelete==0
85043 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
@@ -84905,14 +85097,13 @@
85097 /*
85098 ** Valid sqlite3_blob* handles point to Incrblob structures.
85099 */
85100 typedef struct Incrblob Incrblob;
85101 struct Incrblob {
 
85102 int nByte; /* Size of open blob, in bytes */
85103 int iOffset; /* Byte offset of blob in cursor data */
85104 u16 iCol; /* Table column this handle is open on */
85105 BtCursor *pCsr; /* Cursor pointing at blob row */
85106 sqlite3_stmt *pStmt; /* Statement holding cursor open */
85107 sqlite3 *db; /* The associated database */
85108 char *zDb; /* Database name */
85109 Table *pTab; /* Table object */
@@ -84939,21 +85130,31 @@
85130 static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
85131 int rc; /* Error code */
85132 char *zErr = 0; /* Error message */
85133 Vdbe *v = (Vdbe *)p->pStmt;
85134
85135 /* Set the value of register r[1] in the SQL statement to integer iRow.
85136 ** This is done directly as a performance optimization
 
85137 */
85138 v->aMem[1].flags = MEM_Int;
85139 v->aMem[1].u.i = iRow;
85140
85141 /* If the statement has been run before (and is paused at the OP_ResultRow)
85142 ** then back it up to the point where it does the OP_SeekRowid. This could
85143 ** have been down with an extra OP_Goto, but simply setting the program
85144 ** counter is faster. */
85145 if( v->pc>3 ){
85146 v->pc = 3;
85147 rc = sqlite3VdbeExec(v);
85148 }else{
85149 rc = sqlite3_step(p->pStmt);
85150 }
85151 if( rc==SQLITE_ROW ){
85152 VdbeCursor *pC = v->apCsr[0];
85153 u32 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
85154 testcase( pC->nHdrParsed==p->iCol );
85155 testcase( pC->nHdrParsed==p->iCol+1 );
85156 if( type<12 ){
85157 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
85158 type==0?"null": type==7?"real": "integer"
85159 );
85160 rc = SQLITE_ERROR;
@@ -84994,11 +85195,11 @@
85195 sqlite3* db, /* The database connection */
85196 const char *zDb, /* The attached database containing the blob */
85197 const char *zTable, /* The table containing the blob */
85198 const char *zColumn, /* The column containing the blob */
85199 sqlite_int64 iRow, /* The row containing the glob */
85200 int wrFlag, /* True -> read/write access, false -> read-only */
85201 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
85202 ){
85203 int nAttempt = 0;
85204 int iCol; /* Index of zColumn in row-record */
85205 int rc = SQLITE_OK;
@@ -85016,11 +85217,11 @@
85217 #ifdef SQLITE_ENABLE_API_ARMOR
85218 if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
85219 return SQLITE_MISUSE_BKPT;
85220 }
85221 #endif
85222 wrFlag = !!wrFlag; /* wrFlag = (wrFlag ? 1 : 0); */
85223
85224 sqlite3_mutex_enter(db->mutex);
85225
85226 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
85227 if( !pBlob ) goto blob_open_out;
@@ -85076,13 +85277,12 @@
85277 goto blob_open_out;
85278 }
85279
85280 /* If the value is being opened for writing, check that the
85281 ** column is not indexed, and that it is not part of a foreign key.
85282 */
85283 if( wrFlag ){
 
85284 const char *zFault = 0;
85285 Index *pIdx;
85286 #ifndef SQLITE_OMIT_FOREIGN_KEY
85287 if( db->flags&SQLITE_ForeignKeys ){
85288 /* Check that the column is not part of an FK child key definition. It
@@ -85139,22 +85339,21 @@
85339 */
85340 static const int iLn = VDBE_OFFSET_LINENO(2);
85341 static const VdbeOpList openBlob[] = {
85342 {OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */
85343 {OP_OpenRead, 0, 0, 0}, /* 1: Open a cursor */
85344 /* blobSeekToRow() will initialize r[1] to the desired rowid */
85345 {OP_NotExists, 0, 5, 1}, /* 2: Seek the cursor to rowid=r[1] */
85346 {OP_Column, 0, 0, 1}, /* 3 */
85347 {OP_ResultRow, 1, 0, 0}, /* 4 */
85348 {OP_Halt, 0, 0, 0}, /* 5 */
 
85349 };
85350 Vdbe *v = (Vdbe *)pBlob->pStmt;
85351 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
85352 VdbeOp *aOp;
85353
85354 sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag,
85355 pTab->pSchema->schema_cookie,
85356 pTab->pSchema->iGeneration);
85357 sqlite3VdbeChangeP5(v, 1);
85358 aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
85359
@@ -85167,19 +85366,19 @@
85366 #ifdef SQLITE_OMIT_SHARED_CACHE
85367 aOp[0].opcode = OP_Noop;
85368 #else
85369 aOp[0].p1 = iDb;
85370 aOp[0].p2 = pTab->tnum;
85371 aOp[0].p3 = wrFlag;
85372 sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
85373 }
85374 if( db->mallocFailed==0 ){
85375 #endif
85376
85377 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
85378 ** parameter of the other to pTab->tnum. */
85379 if( wrFlag ) aOp[1].opcode = OP_OpenWrite;
85380 aOp[1].p2 = pTab->tnum;
85381 aOp[1].p3 = iDb;
85382
85383 /* Configure the number of columns. Configure the cursor to
85384 ** think that the table has one more column than it really
@@ -85188,27 +85387,25 @@
85387 ** we can invoke OP_Column to fill in the vdbe cursors type
85388 ** and offset cache without causing any IO.
85389 */
85390 aOp[1].p4type = P4_INT32;
85391 aOp[1].p4.i = pTab->nCol+1;
85392 aOp[3].p2 = pTab->nCol;
85393
85394 pParse->nVar = 0;
85395 pParse->nMem = 1;
85396 pParse->nTab = 1;
85397 sqlite3VdbeMakeReady(v, pParse);
85398 }
85399 }
85400
 
85401 pBlob->iCol = iCol;
85402 pBlob->db = db;
85403 sqlite3BtreeLeaveAll(db);
85404 if( db->mallocFailed ){
85405 goto blob_open_out;
85406 }
 
85407 rc = blobSeekToRow(pBlob, iRow, &zErr);
85408 } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
85409
85410 blob_open_out:
85411 if( rc==SQLITE_OK && db->mallocFailed==0 ){
@@ -88741,12 +88938,10 @@
88938 ** This file contains routines used for walking the parser tree and
88939 ** resolve all identifiers by associating them with a particular
88940 ** table and column.
88941 */
88942 /* #include "sqliteInt.h" */
 
 
88943
88944 /*
88945 ** Walk the expression tree pExpr and increase the aggregate function
88946 ** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
88947 ** This needs to occur when copying a TK_AGG_FUNCTION node from an
@@ -90501,11 +90696,11 @@
90696 aff = sqlite3ExprAffinity(pExpr->pLeft);
90697 if( pExpr->pRight ){
90698 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
90699 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
90700 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
90701 }else if( aff==0 ){
90702 aff = SQLITE_AFF_BLOB;
90703 }
90704 return aff;
90705 }
90706
@@ -91237,21 +91432,27 @@
91432 int doAdd = 0;
91433 if( z[0]=='?' ){
91434 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
91435 ** use it as the variable number */
91436 i64 i;
91437 int bOk;
91438 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
91439 i = z[1]-'0'; /* The common case of ?N for a single digit N */
91440 bOk = 1;
91441 }else{
91442 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
91443 }
91444 testcase( i==0 );
91445 testcase( i==1 );
91446 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
91447 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
91448 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
91449 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
91450 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
91451 return;
91452 }
91453 x = (ynVar)i;
91454 if( x>pParse->nVar ){
91455 pParse->nVar = (int)x;
91456 doAdd = 1;
91457 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
91458 doAdd = 1;
@@ -91682,37 +91883,45 @@
91883 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
91884 pNewItem->idx = pOldItem->idx;
91885 }
91886 return pNew;
91887 }
91888 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *pDup, int flags){
91889 Select *pRet = 0;
91890 Select *pNext = 0;
91891 Select **pp = &pRet;
91892 Select *p;
91893
91894 assert( db!=0 );
91895 for(p=pDup; p; p=p->pPrior){
91896 Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
91897 if( pNew==0 ) break;
91898 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
91899 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
91900 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
91901 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
91902 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
91903 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
91904 pNew->op = p->op;
91905 pNew->pNext = pNext;
91906 pNew->pPrior = 0;
91907 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
91908 pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
91909 pNew->iLimit = 0;
91910 pNew->iOffset = 0;
91911 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
91912 pNew->addrOpenEphm[0] = -1;
91913 pNew->addrOpenEphm[1] = -1;
91914 pNew->nSelectRow = p->nSelectRow;
91915 pNew->pWith = withDup(db, p->pWith);
91916 sqlite3SelectSetName(pNew, p->zSelName);
91917 *pp = pNew;
91918 pp = &pNew->pPrior;
91919 pNext = pNew;
91920 }
91921
91922 return pRet;
91923 }
91924 #else
91925 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
91926 assert( p==0 );
91927 return 0;
@@ -91773,11 +91982,11 @@
91982 **
91983 ** (a,b,c) = (expr1,expr2,expr3)
91984 ** Or: (a,b,c) = (SELECT x,y,z FROM ....)
91985 **
91986 ** For each term of the vector assignment, append new entries to the
91987 ** expression list pList. In the case of a subquery on the RHS, append
91988 ** TK_SELECT_COLUMN expressions.
91989 */
91990 SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(
91991 Parse *pParse, /* Parsing context */
91992 ExprList *pList, /* List to which to append. Might be NULL */
@@ -93882,10 +94091,15 @@
94091 int i; /* Loop counter */
94092 sqlite3 *db = pParse->db; /* The database connection */
94093 u8 enc = ENC(db); /* The text encoding used by this database */
94094 CollSeq *pColl = 0; /* A collating sequence */
94095
94096 if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
94097 /* SQL functions can be expensive. So try to move constant functions
94098 ** out of the inner loop, even if that means an extra OP_Copy. */
94099 return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
94100 }
94101 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
94102 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
94103 pFarg = 0;
94104 }else{
94105 pFarg = pExpr->x.pList;
@@ -93929,10 +94143,26 @@
94143 */
94144 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
94145 assert( nFarg>=1 );
94146 return sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
94147 }
94148
94149 #ifdef SQLITE_DEBUG
94150 /* The AFFINITY() function evaluates to a string that describes
94151 ** the type affinity of the argument. This is used for testing of
94152 ** the SQLite type logic.
94153 */
94154 if( pDef->funcFlags & SQLITE_FUNC_AFFINITY ){
94155 const char *azAff[] = { "blob", "text", "numeric", "integer", "real" };
94156 char aff;
94157 assert( nFarg==1 );
94158 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
94159 sqlite3VdbeLoadString(v, target,
94160 aff ? azAff[aff-SQLITE_AFF_BLOB] : "none");
94161 return target;
94162 }
94163 #endif
94164
94165 for(i=0; i<nFarg; i++){
94166 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
94167 testcase( i==31 );
94168 constMask |= MASKBIT32(i);
@@ -94246,28 +94476,44 @@
94476 return inReg;
94477 }
94478
94479 /*
94480 ** Factor out the code of the given expression to initialization time.
94481 **
94482 ** If regDest>=0 then the result is always stored in that register and the
94483 ** result is not reusable. If regDest<0 then this routine is free to
94484 ** store the value whereever it wants. The register where the expression
94485 ** is stored is returned. When regDest<0, two identical expressions will
94486 ** code to the same register.
94487 */
94488 SQLITE_PRIVATE int sqlite3ExprCodeAtInit(
94489 Parse *pParse, /* Parsing context */
94490 Expr *pExpr, /* The expression to code when the VDBE initializes */
94491 int regDest /* Store the value in this register */
 
94492 ){
94493 ExprList *p;
94494 assert( ConstFactorOk(pParse) );
94495 p = pParse->pConstExpr;
94496 if( regDest<0 && p ){
94497 struct ExprList_item *pItem;
94498 int i;
94499 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
94500 if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){
94501 return pItem->u.iConstExprReg;
94502 }
94503 }
94504 }
94505 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
94506 p = sqlite3ExprListAppend(pParse, p, pExpr);
94507 if( p ){
94508 struct ExprList_item *pItem = &p->a[p->nExpr-1];
94509 pItem->reusable = regDest<0;
94510 if( regDest<0 ) regDest = ++pParse->nMem;
94511 pItem->u.iConstExprReg = regDest;
 
94512 }
94513 pParse->pConstExpr = p;
94514 return regDest;
94515 }
94516
94517 /*
94518 ** Generate code to evaluate an expression and store the results
94519 ** into a register. Return the register number where the results
@@ -94286,23 +94532,12 @@
94532 pExpr = sqlite3ExprSkipCollate(pExpr);
94533 if( ConstFactorOk(pParse)
94534 && pExpr->op!=TK_REGISTER
94535 && sqlite3ExprIsConstantNotJoin(pExpr)
94536 ){
 
 
94537 *pReg = 0;
94538 r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1);
 
 
 
 
 
 
 
 
 
94539 }else{
94540 int r1 = sqlite3GetTempReg(pParse);
94541 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
94542 if( r2==r1 ){
94543 *pReg = r1;
@@ -94352,11 +94587,11 @@
94587 ** in register target. If the expression is constant, then this routine
94588 ** might choose to code the expression at initialization time.
94589 */
94590 SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
94591 if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
94592 sqlite3ExprCodeAtInit(pParse, pExpr, target);
94593 }else{
94594 sqlite3ExprCode(pParse, pExpr, target);
94595 }
94596 }
94597
@@ -94424,11 +94659,11 @@
94659 n--;
94660 }else{
94661 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
94662 }
94663 }else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){
94664 sqlite3ExprCodeAtInit(pParse, pExpr, target+i);
94665 }else{
94666 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
94667 if( inReg!=target+i ){
94668 VdbeOp *pOp;
94669 if( copyOp==OP_Copy
@@ -96968,10 +97203,16 @@
97203 ** used to query statistical information that has been gathered into
97204 ** the Stat4Accum object by prior calls to stat_push(). The P parameter
97205 ** has type BLOB but it is really just a pointer to the Stat4Accum object.
97206 ** The content to returned is determined by the parameter J
97207 ** which is one of the STAT_GET_xxxx values defined above.
97208 **
97209 ** The stat_get(P,J) function is not available to generic SQL. It is
97210 ** inserted as part of a manually constructed bytecode program. (See
97211 ** the callStatGet() routine below.) It is guaranteed that the P
97212 ** parameter will always be a poiner to a Stat4Accum object, never a
97213 ** NULL.
97214 **
97215 ** If neither STAT3 nor STAT4 are enabled, then J is always
97216 ** STAT_GET_STAT1 and is hence omitted and this routine becomes
97217 ** a one-parameter function, stat_get(P), that always returns the
97218 ** stat1 table entry information.
@@ -97787,11 +98028,11 @@
98028 sumEq += aSample[i].anEq[iCol];
98029 nSum100 += 100;
98030 }
98031 }
98032
98033 if( nDist100>nSum100 && sumEq<nRow ){
98034 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
98035 }
98036 if( avgEq==0 ) avgEq = 1;
98037 pIdx->aAvgEq[iCol] = avgEq;
98038 }
@@ -98201,10 +98442,11 @@
98442 assert( pVfs );
98443 flags |= SQLITE_OPEN_MAIN_DB;
98444 rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags);
98445 sqlite3_free( zPath );
98446 db->nDb++;
98447 db->skipBtreeMutex = 0;
98448 if( rc==SQLITE_CONSTRAINT ){
98449 rc = SQLITE_ERROR;
98450 zErrDyn = sqlite3MPrintf(db, "database is already attached");
98451 }else if( rc==SQLITE_OK ){
98452 Pager *pPager;
@@ -104365,16 +104607,12 @@
104607 }
104608 }else
104609 #endif
104610 {
104611 int count = (pParse->nested==0); /* True to count changes */
 
 
 
 
104612 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
104613 iKey, nKey, count, OE_Default, eOnePass, aiCurOnePass[1]);
104614 }
104615
104616 /* End of the loop over all rowids/primary-keys. */
104617 if( eOnePass!=ONEPASS_OFF ){
104618 sqlite3VdbeResolveLabel(v, addrBypass);
@@ -104450,19 +104688,21 @@
104688 ** then this function must seek iDataCur to the entry identified by iPk
104689 ** and nPk before reading from it.
104690 **
104691 ** If eMode is ONEPASS_MULTI, then this call is being made as part
104692 ** of a ONEPASS delete that affects multiple rows. In this case, if
104693 ** iIdxNoSeek is a valid cursor number (>=0) and is not the same as
104694 ** iDataCur, then its position should be preserved following the delete
104695 ** operation. Or, if iIdxNoSeek is not a valid cursor number, the
104696 ** position of iDataCur should be preserved instead.
104697 **
104698 ** iIdxNoSeek:
104699 ** If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
104700 ** then it identifies an index cursor (from within array of cursors
104701 ** starting at iIdxCur) that already points to the index entry to be deleted.
104702 ** Except, this optimization is disabled if there are BEFORE triggers since
104703 ** the trigger body might have moved the cursor.
104704 */
104705 SQLITE_PRIVATE void sqlite3GenerateRowDelete(
104706 Parse *pParse, /* Parsing context */
104707 Table *pTab, /* Table containing the row to be deleted */
104708 Trigger *pTrigger, /* List of triggers to (potentially) fire */
@@ -104529,17 +104769,22 @@
104769 TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
104770 );
104771
104772 /* If any BEFORE triggers were coded, then seek the cursor to the
104773 ** row to be deleted again. It may be that the BEFORE triggers moved
104774 ** the cursor or already deleted the row that the cursor was
104775 ** pointing to.
104776 **
104777 ** Also disable the iIdxNoSeek optimization since the BEFORE trigger
104778 ** may have moved that cursor.
104779 */
104780 if( addrStart<sqlite3VdbeCurrentAddr(v) ){
104781 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
104782 VdbeCoverageIf(v, opSeek==OP_NotExists);
104783 VdbeCoverageIf(v, opSeek==OP_NotFound);
104784 testcase( iIdxNoSeek>=0 );
104785 iIdxNoSeek = -1;
104786 }
104787
104788 /* Do FK processing. This call checks that any FK constraints that
104789 ** refer to this table (i.e. constraints attached to other tables)
104790 ** are not violated by deleting this row. */
@@ -104558,15 +104803,17 @@
104803 */
104804 if( pTab->pSelect==0 ){
104805 u8 p5 = 0;
104806 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
104807 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
104808 if( pParse->nested==0 ){
104809 sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
104810 }
104811 if( eMode!=ONEPASS_OFF ){
104812 sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE);
104813 }
104814 if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
104815 sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
104816 }
104817 if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION;
104818 sqlite3VdbeChangeP5(v, p5);
104819 }
@@ -104716,10 +104963,14 @@
104963 ** opcode if it is present */
104964 sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
104965 }
104966 if( regOut ){
104967 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
104968 if( pIdx->pTable->pSelect ){
104969 const char *zAff = sqlite3IndexAffinityStr(pParse->db, pIdx);
104970 sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
104971 }
104972 }
104973 sqlite3ReleaseTempRange(pParse, regBase, nCol);
104974 return regBase;
104975 }
104976
@@ -106512,10 +106763,13 @@
106763 DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ),
106764 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
106765 FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
106766 FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
106767 FUNCTION2(likely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
106768 #ifdef SQLITE_DEBUG
106769 FUNCTION2(affinity, 1, 0, 0, noopFunc, SQLITE_FUNC_AFFINITY),
106770 #endif
106771 FUNCTION(ltrim, 1, 1, 0, trimFunc ),
106772 FUNCTION(ltrim, 2, 1, 0, trimFunc ),
106773 FUNCTION(rtrim, 1, 2, 0, trimFunc ),
106774 FUNCTION(rtrim, 2, 2, 0, trimFunc ),
106775 FUNCTION(trim, 1, 3, 0, trimFunc ),
@@ -109667,11 +109921,11 @@
109921 if( db->flags&SQLITE_RecTriggers ){
109922 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
109923 }
109924 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
109925 regR, nPkField, 0, OE_Replace,
109926 (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
109927 seenReplace = 1;
109928 break;
109929 }
109930 }
109931 sqlite3VdbeResolveLabel(v, addrUniqueOk);
@@ -109683,10 +109937,29 @@
109937 }
109938
109939 *pbMayReplace = seenReplace;
109940 VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
109941 }
109942
109943 #ifdef SQLITE_ENABLE_NULL_TRIM
109944 /*
109945 ** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
109946 ** to be the number of columns in table pTab that must not be NULL-trimmed.
109947 **
109948 ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
109949 */
109950 SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
109951 u16 i;
109952
109953 /* Records with omitted columns are only allowed for schema format
109954 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
109955 if( pTab->pSchema->file_format<2 ) return;
109956
109957 for(i=pTab->nCol; i>1 && pTab->aCol[i-1].pDflt==0; i--){}
109958 sqlite3VdbeChangeP5(v, i);
109959 }
109960 #endif
109961
109962 /*
109963 ** This routine generates code to finish the INSERT or UPDATE operation
109964 ** that was started by a prior call to sqlite3GenerateConstraintChecks.
109965 ** A consecutive range of registers starting at regNewData contains the
@@ -109700,11 +109973,11 @@
109973 Table *pTab, /* the table into which we are inserting */
109974 int iDataCur, /* Cursor of the canonical data source */
109975 int iIdxCur, /* First index cursor */
109976 int regNewData, /* Range of content */
109977 int *aRegIdx, /* Register used by each index. 0 for unused indices */
109978 int update_flags, /* True for UPDATE, False for INSERT */
109979 int appendBias, /* True if this is likely to be an append */
109980 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
109981 ){
109982 Vdbe *v; /* Prepared statements under construction */
109983 Index *pIdx; /* An index being inserted or updated */
@@ -109711,10 +109984,15 @@
109984 u8 pik_flags; /* flag values passed to the btree insert */
109985 int regData; /* Content registers (after the rowid) */
109986 int regRec; /* Register holding assembled record for the table */
109987 int i; /* Loop counter */
109988 u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */
109989
109990 assert( update_flags==0
109991 || update_flags==OPFLAG_ISUPDATE
109992 || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)
109993 );
109994
109995 v = sqlite3GetVdbe(pParse);
109996 assert( v!=0 );
109997 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
109998 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
@@ -109722,34 +110000,43 @@
110000 bAffinityDone = 1;
110001 if( pIdx->pPartIdxWhere ){
110002 sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
110003 VdbeCoverage(v);
110004 }
110005 pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
 
 
 
 
110006 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
110007 assert( pParse->nested==0 );
110008 pik_flags |= OPFLAG_NCHANGE;
110009 pik_flags |= (update_flags & OPFLAG_SAVEPOSITION);
110010 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
110011 if( update_flags==0 ){
110012 sqlite3VdbeAddOp4(v, OP_InsertInt,
110013 iIdxCur+i, aRegIdx[i], 0, (char*)pTab, P4_TABLE
110014 );
110015 sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP);
110016 }
110017 #endif
110018 }
110019 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
110020 aRegIdx[i]+1,
110021 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
110022 sqlite3VdbeChangeP5(v, pik_flags);
110023 }
110024 if( !HasRowid(pTab) ) return;
110025 regData = regNewData + 1;
110026 regRec = sqlite3GetTempReg(pParse);
110027 sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
110028 sqlite3SetMakeRecordP5(v, pTab);
110029 if( !bAffinityDone ){
110030 sqlite3TableAffinity(v, pTab, 0);
110031 sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
110032 }
110033 if( pParse->nested ){
110034 pik_flags = 0;
110035 }else{
110036 pik_flags = OPFLAG_NCHANGE;
110037 pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID);
110038 }
110039 if( appendBias ){
110040 pik_flags |= OPFLAG_APPEND;
110041 }
110042 if( useSeekResult ){
@@ -110154,11 +110441,11 @@
110441 addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
110442 }else{
110443 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
110444 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
110445 }
110446 sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
110447 if( db->flags & SQLITE_Vacuum ){
110448 sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
110449 insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|
110450 OPFLAG_APPEND|OPFLAG_USESEEKRESULT;
110451 }else{
@@ -110186,11 +110473,11 @@
110473 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
110474 sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
110475 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
110476 VdbeComment((v, "%s", pDestIdx->zName));
110477 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
110478 sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
110479 if( db->flags & SQLITE_Vacuum ){
110480 /* This INSERT command is part of a VACUUM operation, which guarantees
110481 ** that the destination table is empty. If all indexed columns use
110482 ** collation sequence BINARY, then it can also be assumed that the
110483 ** index will be populated by inserting keys in strictly sorted
@@ -110971,11 +111258,10 @@
111258 #endif /* SQLITE3EXT_H */
111259
111260 /************** End of sqlite3ext.h ******************************************/
111261 /************** Continuing where we left off in loadext.c ********************/
111262 /* #include "sqliteInt.h" */
 
111263
111264 #ifndef SQLITE_OMIT_LOAD_EXTENSION
111265 /*
111266 ** Some API routines are omitted when various features are
111267 ** excluded from a build of SQLite. Substitute a NULL pointer
@@ -112635,11 +112921,11 @@
112921
112922 /*
112923 ** Locate a pragma in the aPragmaName[] array.
112924 */
112925 static const PragmaName *pragmaLocate(const char *zName){
112926 int upr, lwr, mid = 0, rc;
112927 lwr = 0;
112928 upr = ArraySize(aPragmaName)-1;
112929 while( lwr<=upr ){
112930 mid = (lwr+upr)/2;
112931 rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
@@ -116149,10 +116435,11 @@
116435 v = pParse->pVdbe;
116436 r1 = sqlite3GetTempReg(pParse);
116437 sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
116438 sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
116439 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, iMem, N);
116440 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
116441 sqlite3ReleaseTempReg(pParse, r1);
116442 }
116443
116444 /*
116445 ** This routine generates the code for the inside of the inner loop
@@ -119677,11 +119964,19 @@
119964 assert( pTab->nTabRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nTabRef==2 ));
119965
119966 pCte->zCteErr = "circular reference: %s";
119967 pSavedWith = pParse->pWith;
119968 pParse->pWith = pWith;
119969 if( bMayRecursive ){
119970 Select *pPrior = pSel->pPrior;
119971 assert( pPrior->pWith==0 );
119972 pPrior->pWith = pSel->pWith;
119973 sqlite3WalkSelect(pWalker, pPrior);
119974 pPrior->pWith = 0;
119975 }else{
119976 sqlite3WalkSelect(pWalker, pSel);
119977 }
119978 pParse->pWith = pWith;
119979
119980 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
119981 pEList = pLeft->pEList;
119982 if( pCte->pCols ){
@@ -119721,14 +120016,16 @@
120016 ** sqlite3SelectExpand() when walking a SELECT tree to resolve table
120017 ** names and other FROM clause elements.
120018 */
120019 static void selectPopWith(Walker *pWalker, Select *p){
120020 Parse *pParse = pWalker->pParse;
120021 if( pParse->pWith && p->pPrior==0 ){
120022 With *pWith = findRightmost(p)->pWith;
120023 if( pWith!=0 ){
120024 assert( pParse->pWith==pWith );
120025 pParse->pWith = pWith->pOuter;
120026 }
120027 }
120028 }
120029 #else
120030 #define selectPopWith 0
120031 #endif
@@ -119774,12 +120071,12 @@
120071 if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
120072 return WRC_Prune;
120073 }
120074 pTabList = p->pSrc;
120075 pEList = p->pEList;
120076 if( p->pWith ){
120077 sqlite3WithPush(pParse, p->pWith, 0);
120078 }
120079
120080 /* Make sure cursor numbers have been assigned to all entries in
120081 ** the FROM clause of the SELECT statement.
120082 */
@@ -120062,13 +120359,11 @@
120359 if( pParse->hasCompound ){
120360 w.xSelectCallback = convertCompoundSelectToSubquery;
120361 sqlite3WalkSelect(&w, pSelect);
120362 }
120363 w.xSelectCallback = selectExpander;
120364 w.xSelectCallback2 = selectPopWith;
 
 
120365 sqlite3WalkSelect(&w, pSelect);
120366 }
120367
120368
120369 #ifndef SQLITE_OMIT_SUBQUERY
@@ -121143,11 +121438,11 @@
121438 /* This case runs if the aggregate has no GROUP BY clause. The
121439 ** processing is much simpler since there is only a single row
121440 ** of output.
121441 */
121442 resetAccumulator(pParse, &sAggInfo);
121443 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax, 0,flag,0);
121444 if( pWInfo==0 ){
121445 sqlite3ExprListDelete(db, pDel);
121446 goto select_end;
121447 }
121448 updateAccumulator(pParse, &sAggInfo);
@@ -121232,12 +121527,10 @@
121527 **
121528 ** These routines are in a separate files so that they will not be linked
121529 ** if they are not used.
121530 */
121531 /* #include "sqliteInt.h" */
 
 
121532
121533 #ifndef SQLITE_OMIT_GET_TABLE
121534
121535 /*
121536 ** This structure is used to pass data from sqlite3_get_table() through
@@ -122591,16 +122884,16 @@
122884 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
122885 pCol->affinity, &pValue);
122886 if( pValue ){
122887 sqlite3VdbeAppendP4(v, pValue, P4_MEM);
122888 }
122889 }
122890 #ifndef SQLITE_OMIT_FLOATING_POINT
122891 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
122892 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
122893 }
122894 #endif
 
122895 }
122896
122897 /*
122898 ** Process an UPDATE statement.
122899 **
@@ -122625,11 +122918,11 @@
122918 int nIdx; /* Number of indices that need updating */
122919 int iBaseCur; /* Base cursor number */
122920 int iDataCur; /* Cursor for the canonical data btree */
122921 int iIdxCur; /* Cursor for the first index */
122922 sqlite3 *db; /* The database structure */
122923 int *aRegIdx = 0; /* First register in array assigned to each index */
122924 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
122925 ** an expression for the i-th column of the table.
122926 ** aXRef[i]==-1 if the i-th column is not changed. */
122927 u8 *aToOpen; /* 1 for tables and indices to be opened */
122928 u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */
@@ -122637,14 +122930,15 @@
122930 u8 chngKey; /* Either chngPk or chngRowid */
122931 Expr *pRowidExpr = 0; /* Expression defining the new record number */
122932 AuthContext sContext; /* The authorization context */
122933 NameContext sNC; /* The name-context to resolve expressions in */
122934 int iDb; /* Database containing the table being updated */
122935 int eOnePass; /* ONEPASS_XXX value from where.c */
122936 int hasFK; /* True if foreign key processing is required */
122937 int labelBreak; /* Jump here to break out of UPDATE loop */
122938 int labelContinue; /* Jump here to continue next step of UPDATE loop */
122939 int flags; /* Flags for sqlite3WhereBegin() */
122940
122941 #ifndef SQLITE_OMIT_TRIGGER
122942 int isView; /* True when updating a view (INSTEAD OF trigger) */
122943 Trigger *pTrigger; /* List of triggers on pTab, if required */
122944 int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
@@ -122651,10 +122945,14 @@
122945 #endif
122946 int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */
122947 int iEph = 0; /* Ephemeral table holding all primary key values */
122948 int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */
122949 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
122950 int addrOpen = 0; /* Address of OP_OpenEphemeral */
122951 int iPk = 0; /* First of nPk cells holding PRIMARY KEY value */
122952 i16 nPk = 0; /* Number of components of the PRIMARY KEY */
122953 int bReplace = 0; /* True if REPLACE conflict resolution might happen */
122954
122955 /* Register Allocations */
122956 int regRowCount = 0; /* A count of rows changed */
122957 int regOldRowid = 0; /* The old rowid */
122958 int regNewRowid = 0; /* The new rowid */
@@ -122810,17 +123108,27 @@
123108 for(i=0; i<pIdx->nKeyCol; i++){
123109 i16 iIdxCol = pIdx->aiColumn[i];
123110 if( iIdxCol<0 || aXRef[iIdxCol]>=0 ){
123111 reg = ++pParse->nMem;
123112 pParse->nMem += pIdx->nColumn;
123113 if( (onError==OE_Replace)
123114 || (onError==OE_Default && pIdx->onError==OE_Replace)
123115 ){
123116 bReplace = 1;
123117 }
123118 break;
123119 }
123120 }
123121 }
123122 if( reg==0 ) aToOpen[j+1] = 0;
123123 aRegIdx[j] = reg;
123124 }
123125 if( bReplace ){
123126 /* If REPLACE conflict resolution might be invoked, open cursors on all
123127 ** indexes in case they are needed to delete records. */
123128 memset(aToOpen, 1, nIdx+1);
123129 }
123130
123131 /* Begin generating code. */
123132 v = sqlite3GetVdbe(pParse);
123133 if( v==0 ) goto update_cleanup;
123134 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
@@ -122869,107 +123177,127 @@
123177 pWhere, onError);
123178 goto update_cleanup;
123179 }
123180 #endif
123181
123182 /* Initialize the count of updated rows */
123183 if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
123184 regRowCount = ++pParse->nMem;
123185 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
123186 }
123187
123188 if( HasRowid(pTab) ){
123189 sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123190 }else{
 
 
 
 
123191 assert( pPk!=0 );
123192 nPk = pPk->nKeyCol;
123193 iPk = pParse->nMem+1;
123194 pParse->nMem += nPk;
123195 regKey = ++pParse->nMem;
123196 iEph = pParse->nTab++;
123197
123198 sqlite3VdbeAddOp2(v, OP_Null, 0, iPk);
123199 addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
123200 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
123201 }
123202
123203 /* Begin the database scan.
123204 **
123205 ** Do not consider a single-pass strategy for a multi-row update if
123206 ** there are any triggers or foreign keys to process, or rows may
123207 ** be deleted as a result of REPLACE conflict handling. Any of these
123208 ** things might disturb a cursor being used to scan through the table
123209 ** or index, causing a single-pass approach to malfunction. */
123210 flags = WHERE_ONEPASS_DESIRED|WHERE_SEEK_UNIQ_TABLE;
123211 if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
123212 flags |= WHERE_ONEPASS_MULTIROW;
123213 }
123214 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags, iIdxCur);
123215 if( pWInfo==0 ) goto update_cleanup;
123216
123217 /* A one-pass strategy that might update more than one row may not
123218 ** be used if any column of the index used for the scan is being
123219 ** updated. Otherwise, if there is an index on "b", statements like
123220 ** the following could create an infinite loop:
123221 **
123222 ** UPDATE t1 SET b=b+1 WHERE b>?
123223 **
123224 ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
123225 ** strategy that uses an index for which one or more columns are being
123226 ** updated. */
123227 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
123228 if( eOnePass==ONEPASS_MULTI ){
123229 int iCur = aiCurOnePass[1];
123230 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
123231 eOnePass = ONEPASS_OFF;
123232 }
123233 assert( iCur!=iDataCur || !HasRowid(pTab) );
123234 }
123235
123236 if( HasRowid(pTab) ){
123237 /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
123238 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
123239 ** leave it in register regOldRowid. */
123240 sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
123241 if( eOnePass==ONEPASS_OFF ){
123242 sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
123243 }
123244 }else{
123245 /* Read the PK of the current row into an array of registers. In
123246 ** ONEPASS_OFF mode, serialize the array into a record and store it in
123247 ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change
123248 ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table
123249 ** is not required) and leave the PK fields in the array of registers. */
123250 for(i=0; i<nPk; i++){
123251 assert( pPk->aiColumn[i]>=0 );
123252 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur,pPk->aiColumn[i],iPk+i);
 
123253 }
123254 if( eOnePass ){
123255 sqlite3VdbeChangeToNoop(v, addrOpen);
123256 nKey = nPk;
123257 regKey = iPk;
123258 }else{
123259 sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,
123260 sqlite3IndexAffinityStr(db, pPk), nPk);
123261 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEph, regKey, iPk, nPk);
123262 }
 
123263 }
123264
123265 if( eOnePass!=ONEPASS_MULTI ){
123266 sqlite3WhereEnd(pWInfo);
 
 
 
123267 }
123268
123269 labelBreak = sqlite3VdbeMakeLabel(v);
123270 if( !isView ){
123271 int addrOnce = 0;
123272
123273 /* Open every index that needs updating. */
123274 if( eOnePass!=ONEPASS_OFF ){
 
 
 
 
 
 
 
 
 
 
 
 
 
123275 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
123276 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
123277 }
123278
123279 if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
123280 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
123281 }
123282 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur, aToOpen,
123283 0, 0);
123284 if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
123285 }
123286
123287 /* Top of the update loop */
123288 if( eOnePass!=ONEPASS_OFF ){
123289 if( !isView && aiCurOnePass[0]!=iDataCur && aiCurOnePass[1]!=iDataCur ){
123290 assert( pPk );
123291 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
123292 VdbeCoverageNeverTaken(v);
123293 }
123294 if( eOnePass==ONEPASS_SINGLE ){
123295 labelContinue = labelBreak;
123296 }else{
123297 labelContinue = sqlite3VdbeMakeLabel(v);
123298 }
123299 sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
123300 VdbeCoverageIf(v, pPk==0);
123301 VdbeCoverageIf(v, pPk!=0);
123302 }else if( pPk ){
123303 labelContinue = sqlite3VdbeMakeLabel(v);
@@ -123090,11 +123418,10 @@
123418 }
123419 }
123420
123421 if( !isView ){
123422 int addr1 = 0; /* Address of jump instruction */
 
123423
123424 /* Do constraint checks. */
123425 assert( regOldRowid>0 );
123426 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
123427 regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace,
@@ -123126,18 +123453,22 @@
123453 ** is the column index supplied by the user.
123454 */
123455 assert( regNew==regNewRowid+1 );
123456 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
123457 sqlite3VdbeAddOp3(v, OP_Delete, iDataCur,
123458 OPFLAG_ISUPDATE | ((hasFK || chngKey) ? 0 : OPFLAG_ISNOOP),
123459 regNewRowid
123460 );
123461 if( eOnePass==ONEPASS_MULTI ){
123462 assert( hasFK==0 && chngKey==0 );
123463 sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION);
123464 }
123465 if( !pParse->nested ){
123466 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
123467 }
123468 #else
123469 if( hasFK || chngKey ){
123470 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0);
123471 }
123472 #endif
123473 if( bReplace || chngKey ){
123474 sqlite3VdbeJumpHere(v, addr1);
@@ -123146,12 +123477,15 @@
123477 if( hasFK ){
123478 sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);
123479 }
123480
123481 /* Insert the new index entries and the new record. */
123482 sqlite3CompleteInsertion(
123483 pParse, pTab, iDataCur, iIdxCur, regNewRowid, aRegIdx,
123484 OPFLAG_ISUPDATE | (eOnePass==ONEPASS_MULTI ? OPFLAG_SAVEPOSITION : 0),
123485 0, 0
123486 );
123487
123488 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
123489 ** handle rows (possibly in other tables) that refer via a foreign key
123490 ** to the row just updated. */
123491 if( hasFK ){
@@ -123169,12 +123503,15 @@
123503 TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue);
123504
123505 /* Repeat the above with the next record to be updated, until
123506 ** all record selected by the WHERE clause have been updated.
123507 */
123508 if( eOnePass==ONEPASS_SINGLE ){
123509 /* Nothing to do at end-of-loop for a single-pass */
123510 }else if( eOnePass==ONEPASS_MULTI ){
123511 sqlite3VdbeResolveLabel(v, labelContinue);
123512 sqlite3WhereEnd(pWInfo);
123513 }else if( pPk ){
123514 sqlite3VdbeResolveLabel(v, labelContinue);
123515 sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v);
123516 }else{
123517 sqlite3VdbeGoto(v, labelContinue);
@@ -127090,11 +127427,14 @@
127427
127428 /* Seek the table cursor, if required */
127429 if( omitTable ){
127430 /* pIdx is a covering index. No need to access the main table. */
127431 }else if( HasRowid(pIdx->pTable) ){
127432 if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE) || (
127433 (pWInfo->wctrlFlags & WHERE_SEEK_UNIQ_TABLE)
127434 && (pWInfo->eOnePass==ONEPASS_SINGLE)
127435 )){
127436 iRowidReg = ++pParse->nMem;
127437 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
127438 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
127439 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg);
127440 VdbeCoverage(v);
@@ -128454,10 +128794,11 @@
128794 int noCase = 0; /* uppercase equivalent to lowercase */
128795 int op; /* Top-level operator. pExpr->op */
128796 Parse *pParse = pWInfo->pParse; /* Parsing context */
128797 sqlite3 *db = pParse->db; /* Database connection */
128798 unsigned char eOp2; /* op2 value for LIKE/REGEXP/GLOB */
128799 int nLeft; /* Number of elements on left side vector */
128800
128801 if( db->mallocFailed ){
128802 return;
128803 }
128804 pTerm = &pWC->a[idxTerm];
@@ -128483,10 +128824,14 @@
128824 if( ExprHasProperty(pExpr, EP_FromJoin) ){
128825 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
128826 prereqAll |= x;
128827 extraRight = x-1; /* ON clause terms may not be used with an index
128828 ** on left table of a LEFT JOIN. Ticket #3015 */
128829 if( (prereqAll>>1)>=x ){
128830 sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
128831 return;
128832 }
128833 }
128834 pTerm->prereqAll = prereqAll;
128835 pTerm->leftCursor = -1;
128836 pTerm->iParent = -1;
128837 pTerm->eOperator = 0;
@@ -128725,17 +129070,16 @@
129070 **
129071 ** This is only required if at least one side of the comparison operation
129072 ** is not a sub-select. */
129073 if( pWC->op==TK_AND
129074 && (pExpr->op==TK_EQ || pExpr->op==TK_IS)
129075 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
129076 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
129077 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
129078 || (pExpr->pRight->flags & EP_xIsSelect)==0)
129079 ){
 
129080 int i;
 
129081 for(i=0; i<nLeft; i++){
129082 int idxNew;
129083 Expr *pNew;
129084 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i);
129085 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i);
@@ -129293,10 +129637,11 @@
129637 if( pIdx ){
129638 int j = iColumn;
129639 iColumn = pIdx->aiColumn[j];
129640 if( iColumn==XN_EXPR ){
129641 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
129642 pScan->zCollName = pIdx->azColl[j];
129643 }else if( iColumn==pIdx->pTable->iPKey ){
129644 iColumn = XN_ROWID;
129645 }else if( iColumn>=0 ){
129646 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
129647 pScan->zCollName = pIdx->azColl[j];
@@ -133934,11 +134279,12 @@
134279 x = sqlite3ColumnOfIndex(pIdx, x);
134280 if( x>=0 ){
134281 pOp->p2 = x;
134282 pOp->p1 = pLevel->iIdxCur;
134283 }
134284 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0
134285 || pWInfo->eOnePass );
134286 }else if( pOp->opcode==OP_Rowid ){
134287 pOp->p1 = pLevel->iIdxCur;
134288 pOp->opcode = OP_IdxRowid;
134289 }
134290 }
@@ -133998,10 +134344,23 @@
134344 ** Indicate that sqlite3ParserFree() will never be called with a null
134345 ** pointer.
134346 */
134347 #define YYPARSEFREENEVERNULL 1
134348
134349 /*
134350 ** In the amalgamation, the parse.c file generated by lemon and the
134351 ** tokenize.c file are concatenated. In that case, sqlite3RunParser()
134352 ** has access to the the size of the yyParser object and so the parser
134353 ** engine can be allocated from stack. In that case, only the
134354 ** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
134355 ** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
134356 ** omitted.
134357 */
134358 #ifdef SQLITE_AMALGAMATION
134359 # define sqlite3Parser_ENGINEALWAYSONSTACK 1
134360 #endif
134361
134362 /*
134363 ** Alternative datatype for the argument to the malloc() routine passed
134364 ** into sqlite3ParserAlloc(). The default is size_t.
134365 */
134366 #define YYMALLOCARGTYPE u64
@@ -135446,10 +135805,35 @@
135805 */
135806 #ifndef YYMALLOCARGTYPE
135807 # define YYMALLOCARGTYPE size_t
135808 #endif
135809
135810 /* Initialize a new parser that has already been allocated.
135811 */
135812 SQLITE_PRIVATE void sqlite3ParserInit(void *yypParser){
135813 yyParser *pParser = (yyParser*)yypParser;
135814 #ifdef YYTRACKMAXSTACKDEPTH
135815 pParser->yyhwm = 0;
135816 #endif
135817 #if YYSTACKDEPTH<=0
135818 pParser->yytos = NULL;
135819 pParser->yystack = NULL;
135820 pParser->yystksz = 0;
135821 if( yyGrowStack(pParser) ){
135822 pParser->yystack = &pParser->yystk0;
135823 pParser->yystksz = 1;
135824 }
135825 #endif
135826 #ifndef YYNOERRORRECOVERY
135827 pParser->yyerrcnt = -1;
135828 #endif
135829 pParser->yytos = pParser->yystack;
135830 pParser->yystack[0].stateno = 0;
135831 pParser->yystack[0].major = 0;
135832 }
135833
135834 #ifndef sqlite3Parser_ENGINEALWAYSONSTACK
135835 /*
135836 ** This function allocates a new parser.
135837 ** The only argument is a pointer to a function which works like
135838 ** malloc.
135839 **
@@ -135461,32 +135845,15 @@
135845 ** to sqlite3Parser and sqlite3ParserFree.
135846 */
135847 SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPE)){
135848 yyParser *pParser;
135849 pParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) );
135850 if( pParser ) sqlite3ParserInit(pParser);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135851 return pParser;
135852 }
135853 #endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
135854
135855
135856 /* The following function deletes the "minor type" or semantic value
135857 ** associated with a symbol. The symbol can be either a terminal
135858 ** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
135859 ** a pointer to the value to be deleted. The code used to do the
@@ -135608,10 +135975,22 @@
135975 }
135976 #endif
135977 yy_destructor(pParser, yytos->major, &yytos->minor);
135978 }
135979
135980 /*
135981 ** Clear all secondary memory allocations from the parser
135982 */
135983 SQLITE_PRIVATE void sqlite3ParserFinalize(void *p){
135984 yyParser *pParser = (yyParser*)p;
135985 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
135986 #if YYSTACKDEPTH<=0
135987 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
135988 #endif
135989 }
135990
135991 #ifndef sqlite3Parser_ENGINEALWAYSONSTACK
135992 /*
135993 ** Deallocate and destroy a parser. Destructors are called for
135994 ** all stack elements before shutting the parser down.
135995 **
135996 ** If the YYPARSEFREENEVERNULL macro exists (for example because it
@@ -135620,20 +135999,17 @@
135999 */
136000 SQLITE_PRIVATE void sqlite3ParserFree(
136001 void *p, /* The parser to be deleted */
136002 void (*freeProc)(void*) /* Function used to reclaim memory */
136003 ){
 
136004 #ifndef YYPARSEFREENEVERNULL
136005 if( p==0 ) return;
136006 #endif
136007 sqlite3ParserFinalize(p);
136008 (*freeProc)(p);
136009 }
136010 #endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
 
 
136011
136012 /*
136013 ** Return the peak depth of the stack for a parser.
136014 */
136015 #ifdef YYTRACKMAXSTACKDEPTH
@@ -138483,10 +138859,13 @@
138859 void *pEngine; /* The LEMON-generated LALR(1) parser */
138860 int tokenType; /* type of the next token */
138861 int lastTokenParsed = -1; /* type of the previous token */
138862 sqlite3 *db = pParse->db; /* The database connection */
138863 int mxSqlLen; /* Max length of an SQL string */
138864 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
138865 unsigned char zSpace[sizeof(yyParser)]; /* Space for parser engine object */
138866 #endif
138867
138868 assert( zSql!=0 );
138869 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
138870 if( db->nVdbeActive==0 ){
138871 db->u1.isInterrupted = 0;
@@ -138494,15 +138873,20 @@
138873 pParse->rc = SQLITE_OK;
138874 pParse->zTail = zSql;
138875 i = 0;
138876 assert( pzErrMsg!=0 );
138877 /* sqlite3ParserTrace(stdout, "parser: "); */
138878 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
138879 pEngine = zSpace;
138880 sqlite3ParserInit(pEngine);
138881 #else
138882 pEngine = sqlite3ParserAlloc(sqlite3Malloc);
138883 if( pEngine==0 ){
138884 sqlite3OomFault(db);
138885 return SQLITE_NOMEM_BKPT;
138886 }
138887 #endif
138888 assert( pParse->pNewTable==0 );
138889 assert( pParse->pNewTrigger==0 );
138890 assert( pParse->nVar==0 );
138891 assert( pParse->pVList==0 );
138892 while( 1 ){
@@ -138550,11 +138934,15 @@
138934 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
138935 sqlite3ParserStackPeak(pEngine)
138936 );
138937 sqlite3_mutex_leave(sqlite3MallocMutex());
138938 #endif /* YYDEBUG */
138939 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
138940 sqlite3ParserFinalize(pEngine);
138941 #else
138942 sqlite3ParserFree(pEngine, sqlite3_free);
138943 #endif
138944 if( db->mallocFailed ){
138945 pParse->rc = SQLITE_NOMEM_BKPT;
138946 }
138947 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
138948 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
@@ -144213,10 +144601,11 @@
144601
144602 /* Precompiled statements used by the implementation. Each of these
144603 ** statements is run and reset within a single virtual table API call.
144604 */
144605 sqlite3_stmt *aStmt[40];
144606 sqlite3_stmt *pSeekStmt; /* Cache for fts3CursorSeekStmt() */
144607
144608 char *zReadExprlist;
144609 char *zWriteExprlist;
144610
144611 int nNodeSize; /* Soft limit for node size */
@@ -144282,10 +144671,11 @@
144671 struct Fts3Cursor {
144672 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
144673 i16 eSearch; /* Search strategy (see below) */
144674 u8 isEof; /* True if at End Of Results */
144675 u8 isRequireSeek; /* True if must seek pStmt to %_content row */
144676 u8 bSeekStmt; /* True if pStmt is a seek */
144677 sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */
144678 Fts3Expr *pExpr; /* Parsed MATCH query string */
144679 int iLangid; /* Language being queried for */
144680 int nPhrase; /* Number of matchable phrases in query */
144681 Fts3DeferredToken *pDeferred; /* Deferred search tokens, if any */
@@ -144804,10 +145194,11 @@
145194
145195 assert( p->nPendingData==0 );
145196 assert( p->pSegments==0 );
145197
145198 /* Free any prepared statements held */
145199 sqlite3_finalize(p->pSeekStmt);
145200 for(i=0; i<SizeofArray(p->aStmt); i++){
145201 sqlite3_finalize(p->aStmt[i]);
145202 }
145203 sqlite3_free(p->zSegmentsTbl);
145204 sqlite3_free(p->zReadExprlist);
@@ -145675,13 +146066,13 @@
146066 p->nPendingData = 0;
146067 p->azColumn = (char **)&p[1];
146068 p->pTokenizer = pTokenizer;
146069 p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
146070 p->bHasDocsize = (isFts4 && bNoDocsize==0);
146071 p->bHasStat = (u8)isFts4;
146072 p->bFts4 = (u8)isFts4;
146073 p->bDescIdx = (u8)bDescIdx;
146074 p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */
146075 p->zContentTbl = zContent;
146076 p->zLanguageid = zLanguageid;
146077 zContent = 0;
146078 zLanguageid = 0;
@@ -145991,19 +146382,39 @@
146382 return SQLITE_NOMEM;
146383 }
146384 memset(pCsr, 0, sizeof(Fts3Cursor));
146385 return SQLITE_OK;
146386 }
146387
146388 /*
146389 ** Finalize the statement handle at pCsr->pStmt.
146390 **
146391 ** Or, if that statement handle is one created by fts3CursorSeekStmt(),
146392 ** and the Fts3Table.pSeekStmt slot is currently NULL, save the statement
146393 ** pointer there instead of finalizing it.
146394 */
146395 static void fts3CursorFinalizeStmt(Fts3Cursor *pCsr){
146396 if( pCsr->bSeekStmt ){
146397 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
146398 if( p->pSeekStmt==0 ){
146399 p->pSeekStmt = pCsr->pStmt;
146400 sqlite3_reset(pCsr->pStmt);
146401 pCsr->pStmt = 0;
146402 }
146403 pCsr->bSeekStmt = 0;
146404 }
146405 sqlite3_finalize(pCsr->pStmt);
146406 }
146407
146408 /*
146409 ** Close the cursor. For additional information see the documentation
146410 ** on the xClose method of the virtual table interface.
146411 */
146412 static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
146413 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
146414 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
146415 fts3CursorFinalizeStmt(pCsr);
146416 sqlite3Fts3ExprFree(pCsr->pExpr);
146417 sqlite3Fts3FreeDeferredTokens(pCsr);
146418 sqlite3_free(pCsr->aDoclist);
146419 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
146420 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
@@ -146017,24 +146428,27 @@
146428 **
146429 ** "SELECT <columns> FROM %_content WHERE rowid = ?"
146430 **
146431 ** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
146432 ** it. If an error occurs, return an SQLite error code.
 
 
146433 */
146434 static int fts3CursorSeekStmt(Fts3Cursor *pCsr){
146435 int rc = SQLITE_OK;
146436 if( pCsr->pStmt==0 ){
146437 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
146438 char *zSql;
146439 if( p->pSeekStmt ){
146440 pCsr->pStmt = p->pSeekStmt;
146441 p->pSeekStmt = 0;
146442 }else{
146443 zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
146444 if( !zSql ) return SQLITE_NOMEM;
146445 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
146446 sqlite3_free(zSql);
146447 }
146448 if( rc==SQLITE_OK ) pCsr->bSeekStmt = 1;
146449 }
 
146450 return rc;
146451 }
146452
146453 /*
146454 ** Position the pCsr->pStmt statement so that it is on the row
@@ -146042,13 +146456,11 @@
146456 ** SQLITE_OK on success.
146457 */
146458 static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
146459 int rc = SQLITE_OK;
146460 if( pCsr->isRequireSeek ){
146461 rc = fts3CursorSeekStmt(pCsr);
 
 
146462 if( rc==SQLITE_OK ){
146463 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
146464 pCsr->isRequireSeek = 0;
146465 if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
146466 return SQLITE_OK;
@@ -147502,11 +147914,11 @@
147914 if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++];
147915 if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++];
147916 assert( iIdx==nVal );
147917
147918 /* In case the cursor has been used before, clear it now. */
147919 fts3CursorFinalizeStmt(pCsr);
147920 sqlite3_free(pCsr->aDoclist);
147921 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
147922 sqlite3Fts3ExprFree(pCsr->pExpr);
147923 memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
147924
@@ -147570,11 +147982,11 @@
147982 sqlite3_free(zSql);
147983 }else{
147984 rc = SQLITE_NOMEM;
147985 }
147986 }else if( eSearch==FTS3_DOCID_SEARCH ){
147987 rc = fts3CursorSeekStmt(pCsr);
147988 if( rc==SQLITE_OK ){
147989 rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
147990 }
147991 }
147992 if( rc!=SQLITE_OK ) return rc;
@@ -147734,11 +148146,11 @@
148146 sqlite3_stmt *pStmt = 0;
148147 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
148148 if( rc==SQLITE_OK ){
148149 int bHasStat = (sqlite3_step(pStmt)==SQLITE_ROW);
148150 rc = sqlite3_finalize(pStmt);
148151 if( rc==SQLITE_OK ) p->bHasStat = (u8)bHasStat;
148152 }
148153 sqlite3_free(zSql);
148154 }else{
148155 rc = SQLITE_NOMEM;
148156 }
@@ -162590,10 +163002,11 @@
163002 /* #include <stdio.h> */
163003
163004 #ifndef SQLITE_AMALGAMATION
163005 #include "sqlite3rtree.h"
163006 typedef sqlite3_int64 i64;
163007 typedef sqlite3_uint64 u64;
163008 typedef unsigned char u8;
163009 typedef unsigned short u16;
163010 typedef unsigned int u32;
163011 #endif
163012
@@ -162638,28 +163051,33 @@
163051 struct Rtree {
163052 sqlite3_vtab base; /* Base class. Must be first */
163053 sqlite3 *db; /* Host database connection */
163054 int iNodeSize; /* Size in bytes of each node in the node table */
163055 u8 nDim; /* Number of dimensions */
163056 u8 nDim2; /* Twice the number of dimensions */
163057 u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
163058 u8 nBytesPerCell; /* Bytes consumed per cell */
163059 u8 inWrTrans; /* True if inside write transaction */
163060 int iDepth; /* Current depth of the r-tree structure */
163061 char *zDb; /* Name of database containing r-tree table */
163062 char *zName; /* Name of r-tree table */
163063 u32 nBusy; /* Current number of users of this structure */
163064 i64 nRowEst; /* Estimated number of rows in this table */
163065 u32 nCursor; /* Number of open cursors */
163066
163067 /* List of nodes removed during a CondenseTree operation. List is
163068 ** linked together via the pointer normally used for hash chains -
163069 ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
163070 ** headed by the node (leaf nodes have RtreeNode.iNode==0).
163071 */
163072 RtreeNode *pDeleted;
163073 int iReinsertHeight; /* Height of sub-trees Reinsert() has run on */
163074
163075 /* Blob I/O on xxx_node */
163076 sqlite3_blob *pNodeBlob;
163077
163078 /* Statements to read/write/delete a record from xxx_node */
 
163079 sqlite3_stmt *pWriteNode;
163080 sqlite3_stmt *pDeleteNode;
163081
163082 /* Statements to read/write/delete a record from xxx_rowid */
163083 sqlite3_stmt *pReadRowid;
@@ -162884,26 +163302,106 @@
163302 #endif
163303 #ifndef MIN
163304 # define MIN(x,y) ((x) > (y) ? (y) : (x))
163305 #endif
163306
163307 /* What version of GCC is being used. 0 means GCC is not being used */
163308 #ifndef GCC_VERSION
163309 #if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
163310 # define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
163311 #else
163312 # define GCC_VERSION 0
163313 #endif
163314 #endif
163315
163316 /* What version of CLANG is being used. 0 means CLANG is not being used */
163317 #ifndef CLANG_VERSION
163318 #if defined(__clang__) && !defined(_WIN32) && !defined(SQLITE_DISABLE_INTRINSIC)
163319 # define CLANG_VERSION \
163320 (__clang_major__*1000000+__clang_minor__*1000+__clang_patchlevel__)
163321 #else
163322 # define CLANG_VERSION 0
163323 #endif
163324 #endif
163325
163326 /* The testcase() macro should already be defined in the amalgamation. If
163327 ** it is not, make it a no-op.
163328 */
163329 #ifndef SQLITE_AMALGAMATION
163330 # define testcase(X)
163331 #endif
163332
163333 /*
163334 ** Macros to determine whether the machine is big or little endian,
163335 ** and whether or not that determination is run-time or compile-time.
163336 **
163337 ** For best performance, an attempt is made to guess at the byte-order
163338 ** using C-preprocessor macros. If that is unsuccessful, or if
163339 ** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
163340 ** at run-time.
163341 */
163342 #ifndef SQLITE_BYTEORDER
163343 #if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
163344 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
163345 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
163346 defined(__arm__)
163347 # define SQLITE_BYTEORDER 1234
163348 #elif defined(sparc) || defined(__ppc__)
163349 # define SQLITE_BYTEORDER 4321
163350 #else
163351 # define SQLITE_BYTEORDER 0 /* 0 means "unknown at compile-time" */
163352 #endif
163353 #endif
163354
163355
163356 /* What version of MSVC is being used. 0 means MSVC is not being used */
163357 #ifndef MSVC_VERSION
163358 #if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
163359 # define MSVC_VERSION _MSC_VER
163360 #else
163361 # define MSVC_VERSION 0
163362 #endif
163363 #endif
163364
163365 /*
163366 ** Functions to deserialize a 16 bit integer, 32 bit real number and
163367 ** 64 bit integer. The deserialized value is returned.
163368 */
163369 static int readInt16(u8 *p){
163370 return (p[0]<<8) + p[1];
163371 }
163372 static void readCoord(u8 *p, RtreeCoord *pCoord){
163373 assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */
163374 #if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163375 pCoord->u = _byteswap_ulong(*(u32*)p);
163376 #elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
163377 pCoord->u = __builtin_bswap32(*(u32*)p);
163378 #elif SQLITE_BYTEORDER==4321
163379 pCoord->u = *(u32*)p;
163380 #else
163381 pCoord->u = (
163382 (((u32)p[0]) << 24) +
163383 (((u32)p[1]) << 16) +
163384 (((u32)p[2]) << 8) +
163385 (((u32)p[3]) << 0)
163386 );
163387 #endif
163388 }
163389 static i64 readInt64(u8 *p){
163390 #if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163391 u64 x;
163392 memcpy(&x, p, 8);
163393 return (i64)_byteswap_uint64(x);
163394 #elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
163395 u64 x;
163396 memcpy(&x, p, 8);
163397 return (i64)__builtin_bswap64(x);
163398 #elif SQLITE_BYTEORDER==4321
163399 i64 x;
163400 memcpy(&x, p, 8);
163401 return x;
163402 #else
163403 return (
163404 (((i64)p[0]) << 56) +
163405 (((i64)p[1]) << 48) +
163406 (((i64)p[2]) << 40) +
163407 (((i64)p[3]) << 32) +
@@ -162910,42 +163408,64 @@
163408 (((i64)p[4]) << 24) +
163409 (((i64)p[5]) << 16) +
163410 (((i64)p[6]) << 8) +
163411 (((i64)p[7]) << 0)
163412 );
163413 #endif
163414 }
163415
163416 /*
163417 ** Functions to serialize a 16 bit integer, 32 bit real number and
163418 ** 64 bit integer. The value returned is the number of bytes written
163419 ** to the argument buffer (always 2, 4 and 8 respectively).
163420 */
163421 static void writeInt16(u8 *p, int i){
163422 p[0] = (i>> 8)&0xFF;
163423 p[1] = (i>> 0)&0xFF;
 
163424 }
163425 static int writeCoord(u8 *p, RtreeCoord *pCoord){
163426 u32 i;
163427 assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */
163428 assert( sizeof(RtreeCoord)==4 );
163429 assert( sizeof(u32)==4 );
163430 #if SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
163431 i = __builtin_bswap32(pCoord->u);
163432 memcpy(p, &i, 4);
163433 #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163434 i = _byteswap_ulong(pCoord->u);
163435 memcpy(p, &i, 4);
163436 #elif SQLITE_BYTEORDER==4321
163437 i = pCoord->u;
163438 memcpy(p, &i, 4);
163439 #else
163440 i = pCoord->u;
163441 p[0] = (i>>24)&0xFF;
163442 p[1] = (i>>16)&0xFF;
163443 p[2] = (i>> 8)&0xFF;
163444 p[3] = (i>> 0)&0xFF;
163445 #endif
163446 return 4;
163447 }
163448 static int writeInt64(u8 *p, i64 i){
163449 #if SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
163450 i = (i64)__builtin_bswap64((u64)i);
163451 memcpy(p, &i, 8);
163452 #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
163453 i = (i64)_byteswap_uint64((u64)i);
163454 memcpy(p, &i, 8);
163455 #elif SQLITE_BYTEORDER==4321
163456 memcpy(p, &i, 8);
163457 #else
163458 p[0] = (i>>56)&0xFF;
163459 p[1] = (i>>48)&0xFF;
163460 p[2] = (i>>40)&0xFF;
163461 p[3] = (i>>32)&0xFF;
163462 p[4] = (i>>24)&0xFF;
163463 p[5] = (i>>16)&0xFF;
163464 p[6] = (i>> 8)&0xFF;
163465 p[7] = (i>> 0)&0xFF;
163466 #endif
163467 return 8;
163468 }
163469
163470 /*
163471 ** Increment the reference count of node p.
@@ -163023,10 +163543,21 @@
163543 pNode->isDirty = 1;
163544 nodeReference(pParent);
163545 }
163546 return pNode;
163547 }
163548
163549 /*
163550 ** Clear the Rtree.pNodeBlob object
163551 */
163552 static void nodeBlobReset(Rtree *pRtree){
163553 if( pRtree->pNodeBlob && pRtree->inWrTrans==0 && pRtree->nCursor==0 ){
163554 sqlite3_blob *pBlob = pRtree->pNodeBlob;
163555 pRtree->pNodeBlob = 0;
163556 sqlite3_blob_close(pBlob);
163557 }
163558 }
163559
163560 /*
163561 ** Obtain a reference to an r-tree node.
163562 */
163563 static int nodeAcquire(
@@ -163033,13 +163564,12 @@
163564 Rtree *pRtree, /* R-tree structure */
163565 i64 iNode, /* Node number to load */
163566 RtreeNode *pParent, /* Either the parent node or NULL */
163567 RtreeNode **ppNode /* OUT: Acquired node */
163568 ){
163569 int rc = SQLITE_OK;
163570 RtreeNode *pNode = 0;
 
163571
163572 /* Check if the requested node is already in the hash table. If so,
163573 ** increase its reference count and return it.
163574 */
163575 if( (pNode = nodeHashLookup(pRtree, iNode)) ){
@@ -163051,32 +163581,49 @@
163581 pNode->nRef++;
163582 *ppNode = pNode;
163583 return SQLITE_OK;
163584 }
163585
163586 if( pRtree->pNodeBlob ){
163587 sqlite3_blob *pBlob = pRtree->pNodeBlob;
163588 pRtree->pNodeBlob = 0;
163589 rc = sqlite3_blob_reopen(pBlob, iNode);
163590 pRtree->pNodeBlob = pBlob;
163591 if( rc ){
163592 nodeBlobReset(pRtree);
163593 if( rc==SQLITE_NOMEM ) return SQLITE_NOMEM;
163594 }
163595 }
163596 if( pRtree->pNodeBlob==0 ){
163597 char *zTab = sqlite3_mprintf("%s_node", pRtree->zName);
163598 if( zTab==0 ) return SQLITE_NOMEM;
163599 rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, zTab, "data", iNode, 0,
163600 &pRtree->pNodeBlob);
163601 sqlite3_free(zTab);
163602 }
163603 if( rc ){
163604 nodeBlobReset(pRtree);
163605 *ppNode = 0;
163606 /* If unable to open an sqlite3_blob on the desired row, that can only
163607 ** be because the shadow tables hold erroneous data. */
163608 if( rc==SQLITE_ERROR ) rc = SQLITE_CORRUPT_VTAB;
163609 }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){
163610 pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
163611 if( !pNode ){
163612 rc = SQLITE_NOMEM;
163613 }else{
163614 pNode->pParent = pParent;
163615 pNode->zData = (u8 *)&pNode[1];
163616 pNode->nRef = 1;
163617 pNode->iNode = iNode;
163618 pNode->isDirty = 0;
163619 pNode->pNext = 0;
163620 rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
163621 pRtree->iNodeSize, 0);
163622 nodeReference(pParent);
163623 }
163624 }
163625
163626 /* If the root node was just loaded, set pRtree->iDepth to the height
163627 ** of the r-tree structure. A height of zero means all data is stored on
163628 ** the root node. A height of one means the children of the root node
163629 ** are the leaves, and so on. If the depth as specified on the root node
@@ -163124,11 +163671,11 @@
163671 int iCell /* Index into pNode into which pCell is written */
163672 ){
163673 int ii;
163674 u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
163675 p += writeInt64(p, pCell->iRowid);
163676 for(ii=0; ii<pRtree->nDim2; ii++){
163677 p += writeCoord(p, &pCell->aCoord[ii]);
163678 }
163679 pNode->isDirty = 1;
163680 }
163681
@@ -163258,17 +163805,20 @@
163805 int iCell, /* Index of the cell within the node */
163806 RtreeCell *pCell /* OUT: Write the cell contents here */
163807 ){
163808 u8 *pData;
163809 RtreeCoord *pCoord;
163810 int ii = 0;
163811 pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
163812 pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
163813 pCoord = pCell->aCoord;
163814 do{
163815 readCoord(pData, &pCoord[ii]);
163816 readCoord(pData+4, &pCoord[ii+1]);
163817 pData += 8;
163818 ii += 2;
163819 }while( ii<pRtree->nDim2 );
163820 }
163821
163822
163823 /* Forward declaration for the function that does the work of
163824 ** the virtual table module xCreate() and xConnect() methods.
@@ -163315,11 +163865,13 @@
163865 ** zero the structure is deleted.
163866 */
163867 static void rtreeRelease(Rtree *pRtree){
163868 pRtree->nBusy--;
163869 if( pRtree->nBusy==0 ){
163870 pRtree->inWrTrans = 0;
163871 pRtree->nCursor = 0;
163872 nodeBlobReset(pRtree);
163873 sqlite3_finalize(pRtree->pWriteNode);
163874 sqlite3_finalize(pRtree->pDeleteNode);
163875 sqlite3_finalize(pRtree->pReadRowid);
163876 sqlite3_finalize(pRtree->pWriteRowid);
163877 sqlite3_finalize(pRtree->pDeleteRowid);
@@ -163353,10 +163905,11 @@
163905 pRtree->zDb, pRtree->zName
163906 );
163907 if( !zCreate ){
163908 rc = SQLITE_NOMEM;
163909 }else{
163910 nodeBlobReset(pRtree);
163911 rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
163912 sqlite3_free(zCreate);
163913 }
163914 if( rc==SQLITE_OK ){
163915 rtreeRelease(pRtree);
@@ -163368,17 +163921,19 @@
163921 /*
163922 ** Rtree virtual table module xOpen method.
163923 */
163924 static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
163925 int rc = SQLITE_NOMEM;
163926 Rtree *pRtree = (Rtree *)pVTab;
163927 RtreeCursor *pCsr;
163928
163929 pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
163930 if( pCsr ){
163931 memset(pCsr, 0, sizeof(RtreeCursor));
163932 pCsr->base.pVtab = pVTab;
163933 rc = SQLITE_OK;
163934 pRtree->nCursor++;
163935 }
163936 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
163937
163938 return rc;
163939 }
@@ -163407,14 +163962,17 @@
163962 */
163963 static int rtreeClose(sqlite3_vtab_cursor *cur){
163964 Rtree *pRtree = (Rtree *)(cur->pVtab);
163965 int ii;
163966 RtreeCursor *pCsr = (RtreeCursor *)cur;
163967 assert( pRtree->nCursor>0 );
163968 freeCursorConstraints(pCsr);
163969 sqlite3_free(pCsr->aPoint);
163970 for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
163971 sqlite3_free(pCsr);
163972 pRtree->nCursor--;
163973 nodeBlobReset(pRtree);
163974 return SQLITE_OK;
163975 }
163976
163977 /*
163978 ** Rtree virtual table module xEof method.
@@ -163433,27 +163991,34 @@
163991 ** endian platforms. The on-disk record stores integer coordinates if
163992 ** eInt is true and it stores 32-bit floating point records if eInt is
163993 ** false. a[] is the four bytes of the on-disk record to be decoded.
163994 ** Store the results in "r".
163995 **
163996 ** There are five versions of this macro. The last one is generic. The
163997 ** other four are various architectures-specific optimizations.
 
 
 
 
 
163998 */
163999 #if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
164000 #define RTREE_DECODE_COORD(eInt, a, r) { \
164001 RtreeCoord c; /* Coordinate decoded */ \
164002 c.u = _byteswap_ulong(*(u32*)a); \
164003 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
164004 }
164005 #elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
164006 #define RTREE_DECODE_COORD(eInt, a, r) { \
164007 RtreeCoord c; /* Coordinate decoded */ \
164008 c.u = __builtin_bswap32(*(u32*)a); \
164009 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
164010 }
164011 #elif SQLITE_BYTEORDER==1234
164012 #define RTREE_DECODE_COORD(eInt, a, r) { \
164013 RtreeCoord c; /* Coordinate decoded */ \
164014 memcpy(&c.u,a,4); \
164015 c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)| \
164016 ((c.u&0xff)<<24)|((c.u&0xff00)<<8); \
164017 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
164018 }
164019 #elif SQLITE_BYTEORDER==4321
164020 #define RTREE_DECODE_COORD(eInt, a, r) { \
164021 RtreeCoord c; /* Coordinate decoded */ \
164022 memcpy(&c.u,a,4); \
164023 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
164024 }
@@ -163476,30 +164041,58 @@
164041 u8 *pCellData, /* Raw cell content */
164042 RtreeSearchPoint *pSearch, /* Container of this cell */
164043 sqlite3_rtree_dbl *prScore, /* OUT: score for the cell */
164044 int *peWithin /* OUT: visibility of the cell */
164045 ){
 
164046 sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
164047 int nCoord = pInfo->nCoord; /* No. of coordinates */
164048 int rc; /* Callback return code */
164049 RtreeCoord c; /* Translator union */
164050 sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2]; /* Decoded coordinates */
164051
164052 assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
164053 assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 );
164054
164055 if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
164056 pInfo->iRowid = readInt64(pCellData);
164057 }
164058 pCellData += 8;
164059 #ifndef SQLITE_RTREE_INT_ONLY
164060 if( eInt==0 ){
164061 switch( nCoord ){
164062 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.f;
164063 readCoord(pCellData+32, &c); aCoord[8] = c.f;
164064 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.f;
164065 readCoord(pCellData+24, &c); aCoord[6] = c.f;
164066 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.f;
164067 readCoord(pCellData+16, &c); aCoord[4] = c.f;
164068 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.f;
164069 readCoord(pCellData+8, &c); aCoord[2] = c.f;
164070 default: readCoord(pCellData+4, &c); aCoord[1] = c.f;
164071 readCoord(pCellData, &c); aCoord[0] = c.f;
164072 }
164073 }else
164074 #endif
164075 {
164076 switch( nCoord ){
164077 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.i;
164078 readCoord(pCellData+32, &c); aCoord[8] = c.i;
164079 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.i;
164080 readCoord(pCellData+24, &c); aCoord[6] = c.i;
164081 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.i;
164082 readCoord(pCellData+16, &c); aCoord[4] = c.i;
164083 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.i;
164084 readCoord(pCellData+8, &c); aCoord[2] = c.i;
164085 default: readCoord(pCellData+4, &c); aCoord[1] = c.i;
164086 readCoord(pCellData, &c); aCoord[0] = c.i;
164087 }
164088 }
164089 if( pConstraint->op==RTREE_MATCH ){
164090 int eWithin = 0;
164091 rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
164092 nCoord, aCoord, &eWithin);
164093 if( eWithin==0 ) *peWithin = NOT_WITHIN;
164094 *prScore = RTREE_ZERO;
164095 }else{
164096 pInfo->aCoord = aCoord;
164097 pInfo->iLevel = pSearch->iLevel - 1;
164098 pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
@@ -163531,10 +164124,11 @@
164124 */
164125 pCellData += 8 + 4*(p->iCoord&0xfe);
164126
164127 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
164128 || p->op==RTREE_GT || p->op==RTREE_EQ );
164129 assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */
164130 switch( p->op ){
164131 case RTREE_LE:
164132 case RTREE_LT:
164133 case RTREE_EQ:
164134 RTREE_DECODE_COORD(eInt, pCellData, val);
@@ -163571,10 +164165,11 @@
164165 RtreeDValue xN; /* Coordinate value converted to a double */
164166
164167 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
164168 || p->op==RTREE_GT || p->op==RTREE_EQ );
164169 pCellData += 8 + p->iCoord*4;
164170 assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */
164171 RTREE_DECODE_COORD(eInt, pCellData, xN);
164172 switch( p->op ){
164173 case RTREE_LE: if( xN <= p->u.rValue ) return; break;
164174 case RTREE_LT: if( xN < p->u.rValue ) return; break;
164175 case RTREE_GE: if( xN >= p->u.rValue ) return; break;
@@ -163639,11 +164234,11 @@
164234 if( pA->iLevel>pB->iLevel ) return +1;
164235 return 0;
164236 }
164237
164238 /*
164239 ** Interchange two search points in a cursor.
164240 */
164241 static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
164242 RtreeSearchPoint t = p->aPoint[i];
164243 assert( i<j );
164244 p->aPoint[i] = p->aPoint[j];
@@ -163887,11 +164482,11 @@
164482 rtreeSearchPointPop(pCur);
164483 }
164484 if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO;
164485 p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
164486 if( p==0 ) return SQLITE_NOMEM;
164487 p->eWithin = (u8)eWithin;
164488 p->id = x.id;
164489 p->iCell = x.iCell;
164490 RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
164491 break;
164492 }
@@ -163946,11 +164541,10 @@
164541 if( rc ) return rc;
164542 if( p==0 ) return SQLITE_OK;
164543 if( i==0 ){
164544 sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
164545 }else{
 
164546 nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
164547 #ifndef SQLITE_RTREE_INT_ONLY
164548 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
164549 sqlite3_result_double(ctx, c.f);
164550 }else
@@ -164075,11 +164669,11 @@
164669 assert( p!=0 ); /* Always returns pCsr->sPoint */
164670 pCsr->aNode[0] = pLeaf;
164671 p->id = iNode;
164672 p->eWithin = PARTLY_WITHIN;
164673 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
164674 p->iCell = (u8)iCell;
164675 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
164676 }else{
164677 pCsr->atEOF = 1;
164678 }
164679 }else{
@@ -164108,11 +164702,11 @@
164702 */
164703 rc = deserializeGeometry(argv[ii], p);
164704 if( rc!=SQLITE_OK ){
164705 break;
164706 }
164707 p->pInfo->nCoord = pRtree->nDim2;
164708 p->pInfo->anQueue = pCsr->anQueue;
164709 p->pInfo->mxLevel = pRtree->iDepth + 1;
164710 }else{
164711 #ifdef SQLITE_RTREE_INT_ONLY
164712 p->u.rValue = sqlite3_value_int64(argv[ii]);
@@ -164123,11 +164717,11 @@
164717 }
164718 }
164719 }
164720 if( rc==SQLITE_OK ){
164721 RtreeSearchPoint *pNew;
164722 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
164723 if( pNew==0 ) return SQLITE_NOMEM;
164724 pNew->id = 1;
164725 pNew->iCell = 0;
164726 pNew->eWithin = PARTLY_WITHIN;
164727 assert( pCsr->bPoint==1 );
@@ -164141,23 +164735,10 @@
164735 nodeRelease(pRtree, pRoot);
164736 rtreeRelease(pRtree);
164737 return rc;
164738 }
164739
 
 
 
 
 
 
 
 
 
 
 
 
 
164740 /*
164741 ** Rtree virtual table module xBestIndex method. There are three
164742 ** table scan strategies to choose from (in order from most to
164743 ** least desirable):
164744 **
@@ -164233,11 +164814,11 @@
164814 ** considered almost as quick as a direct rowid lookup (for which
164815 ** sqlite uses an internal cost of 0.0). It is expected to return
164816 ** a single row.
164817 */
164818 pIdxInfo->estimatedCost = 30.0;
164819 pIdxInfo->estimatedRows = 1;
164820 return SQLITE_OK;
164821 }
164822
164823 if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
164824 u8 op;
@@ -164251,11 +164832,11 @@
164832 assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
164833 op = RTREE_MATCH;
164834 break;
164835 }
164836 zIdxStr[iIdx++] = op;
164837 zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0');
164838 pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
164839 pIdxInfo->aConstraintUsage[ii].omit = 1;
164840 }
164841 }
164842
@@ -164265,55 +164846,75 @@
164846 return SQLITE_NOMEM;
164847 }
164848
164849 nRow = pRtree->nRowEst >> (iIdx/2);
164850 pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
164851 pIdxInfo->estimatedRows = nRow;
164852
164853 return rc;
164854 }
164855
164856 /*
164857 ** Return the N-dimensional volumn of the cell stored in *p.
164858 */
164859 static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
164860 RtreeDValue area = (RtreeDValue)1;
164861 assert( pRtree->nDim>=1 && pRtree->nDim<=5 );
164862 #ifndef SQLITE_RTREE_INT_ONLY
164863 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
164864 switch( pRtree->nDim ){
164865 case 5: area = p->aCoord[9].f - p->aCoord[8].f;
164866 case 4: area *= p->aCoord[7].f - p->aCoord[6].f;
164867 case 3: area *= p->aCoord[5].f - p->aCoord[4].f;
164868 case 2: area *= p->aCoord[3].f - p->aCoord[2].f;
164869 default: area *= p->aCoord[1].f - p->aCoord[0].f;
164870 }
164871 }else
164872 #endif
164873 {
164874 switch( pRtree->nDim ){
164875 case 5: area = p->aCoord[9].i - p->aCoord[8].i;
164876 case 4: area *= p->aCoord[7].i - p->aCoord[6].i;
164877 case 3: area *= p->aCoord[5].i - p->aCoord[4].i;
164878 case 2: area *= p->aCoord[3].i - p->aCoord[2].i;
164879 default: area *= p->aCoord[1].i - p->aCoord[0].i;
164880 }
164881 }
164882 return area;
164883 }
164884
164885 /*
164886 ** Return the margin length of cell p. The margin length is the sum
164887 ** of the objects size in each dimension.
164888 */
164889 static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
164890 RtreeDValue margin = 0;
164891 int ii = pRtree->nDim2 - 2;
164892 do{
164893 margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
164894 ii -= 2;
164895 }while( ii>=0 );
164896 return margin;
164897 }
164898
164899 /*
164900 ** Store the union of cells p1 and p2 in p1.
164901 */
164902 static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
164903 int ii = 0;
164904 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
164905 do{
164906 p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
164907 p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
164908 ii += 2;
164909 }while( ii<pRtree->nDim2 );
164910 }else{
164911 do{
164912 p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
164913 p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
164914 ii += 2;
164915 }while( ii<pRtree->nDim2 );
164916 }
164917 }
164918
164919 /*
164920 ** Return true if the area covered by p2 is a subset of the area covered
@@ -164320,11 +164921,11 @@
164921 ** by p1. False otherwise.
164922 */
164923 static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
164924 int ii;
164925 int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
164926 for(ii=0; ii<pRtree->nDim2; ii+=2){
164927 RtreeCoord *a1 = &p1->aCoord[ii];
164928 RtreeCoord *a2 = &p2->aCoord[ii];
164929 if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f))
164930 || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i))
164931 ){
@@ -164355,11 +164956,11 @@
164956 int ii;
164957 RtreeDValue overlap = RTREE_ZERO;
164958 for(ii=0; ii<nCell; ii++){
164959 int jj;
164960 RtreeDValue o = (RtreeDValue)1;
164961 for(jj=0; jj<pRtree->nDim2; jj+=2){
164962 RtreeDValue x1, x2;
164963 x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
164964 x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
164965 if( x2<x1 ){
164966 o = (RtreeDValue)0;
@@ -165411,11 +166012,11 @@
166012 ** with "column" that are interpreted as table constraints.
166013 ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
166014 ** This problem was discovered after years of use, so we silently ignore
166015 ** these kinds of misdeclared tables to avoid breaking any legacy.
166016 */
166017 assert( nData<=(pRtree->nDim2 + 3) );
166018
166019 #ifndef SQLITE_RTREE_INT_ONLY
166020 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
166021 for(ii=0; ii<nData-4; ii+=2){
166022 cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]);
@@ -165500,10 +166101,31 @@
166101
166102 constraint:
166103 rtreeRelease(pRtree);
166104 return rc;
166105 }
166106
166107 /*
166108 ** Called when a transaction starts.
166109 */
166110 static int rtreeBeginTransaction(sqlite3_vtab *pVtab){
166111 Rtree *pRtree = (Rtree *)pVtab;
166112 assert( pRtree->inWrTrans==0 );
166113 pRtree->inWrTrans++;
166114 return SQLITE_OK;
166115 }
166116
166117 /*
166118 ** Called when a transaction completes (either by COMMIT or ROLLBACK).
166119 ** The sqlite3_blob object should be released at this point.
166120 */
166121 static int rtreeEndTransaction(sqlite3_vtab *pVtab){
166122 Rtree *pRtree = (Rtree *)pVtab;
166123 pRtree->inWrTrans = 0;
166124 nodeBlobReset(pRtree);
166125 return SQLITE_OK;
166126 }
166127
166128 /*
166129 ** The xRename method for rtree module virtual tables.
166130 */
166131 static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
@@ -165521,10 +166143,11 @@
166143 rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
166144 sqlite3_free(zSql);
166145 }
166146 return rc;
166147 }
166148
166149
166150 /*
166151 ** This function populates the pRtree->nRowEst variable with an estimate
166152 ** of the number of rows in the virtual table. If possible, this is based
166153 ** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
@@ -165581,19 +166204,19 @@
166204 rtreeNext, /* xNext - advance a cursor */
166205 rtreeEof, /* xEof */
166206 rtreeColumn, /* xColumn - read data */
166207 rtreeRowid, /* xRowid - read data */
166208 rtreeUpdate, /* xUpdate - write data */
166209 rtreeBeginTransaction, /* xBegin - begin transaction */
166210 rtreeEndTransaction, /* xSync - sync transaction */
166211 rtreeEndTransaction, /* xCommit - commit transaction */
166212 rtreeEndTransaction, /* xRollback - rollback transaction */
166213 0, /* xFindFunction - function overloading */
166214 rtreeRename, /* xRename - rename the table */
166215 0, /* xSavepoint */
166216 0, /* xRelease */
166217 0, /* xRollbackTo */
166218 };
166219
166220 static int rtreeSqlInit(
166221 Rtree *pRtree,
166222 sqlite3 *db,
@@ -165601,14 +166224,13 @@
166224 const char *zPrefix,
166225 int isCreate
166226 ){
166227 int rc = SQLITE_OK;
166228
166229 #define N_STATEMENT 8
166230 static const char *azSql[N_STATEMENT] = {
166231 /* Write the xxx_node table */
 
166232 "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
166233 "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
166234
166235 /* Read and write the xxx_rowid table */
166236 "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
@@ -165642,19 +166264,18 @@
166264 if( rc!=SQLITE_OK ){
166265 return rc;
166266 }
166267 }
166268
166269 appStmt[0] = &pRtree->pWriteNode;
166270 appStmt[1] = &pRtree->pDeleteNode;
166271 appStmt[2] = &pRtree->pReadRowid;
166272 appStmt[3] = &pRtree->pWriteRowid;
166273 appStmt[4] = &pRtree->pDeleteRowid;
166274 appStmt[5] = &pRtree->pReadParent;
166275 appStmt[6] = &pRtree->pWriteParent;
166276 appStmt[7] = &pRtree->pDeleteParent;
 
166277
166278 rc = rtreeQueryStat1(db, pRtree);
166279 for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
166280 char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
166281 if( zSql ){
@@ -165788,13 +166409,14 @@
166409 memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
166410 pRtree->nBusy = 1;
166411 pRtree->base.pModule = &rtreeModule;
166412 pRtree->zDb = (char *)&pRtree[1];
166413 pRtree->zName = &pRtree->zDb[nDb+1];
166414 pRtree->nDim = (u8)((argc-4)/2);
166415 pRtree->nDim2 = pRtree->nDim*2;
166416 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
166417 pRtree->eCoordType = (u8)eCoordType;
166418 memcpy(pRtree->zDb, argv[1], nDb);
166419 memcpy(pRtree->zName, argv[2], nName);
166420
166421 /* Figure out the node size to use. */
166422 rc = getNodeSize(db, pRtree, isCreate, pzErr);
@@ -165863,11 +166485,12 @@
166485 int ii;
166486
166487 UNUSED_PARAMETER(nArg);
166488 memset(&node, 0, sizeof(RtreeNode));
166489 memset(&tree, 0, sizeof(Rtree));
166490 tree.nDim = (u8)sqlite3_value_int(apArg[0]);
166491 tree.nDim2 = tree.nDim*2;
166492 tree.nBytesPerCell = 8 + 8 * tree.nDim;
166493 node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
166494
166495 for(ii=0; ii<NCELL(&node); ii++){
166496 char zCell[512];
@@ -165876,11 +166499,11 @@
166499 int jj;
166500
166501 nodeGetCell(&tree, &node, ii, &cell);
166502 sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
166503 nCell = (int)strlen(zCell);
166504 for(jj=0; jj<tree.nDim2; jj++){
166505 #ifndef SQLITE_RTREE_INT_ONLY
166506 sqlite3_snprintf(512-nCell,&zCell[nCell], " %g",
166507 (double)cell.aCoord[jj].f);
166508 #else
166509 sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
@@ -166584,42 +167207,40 @@
167207
167208 /*
167209 ** Register the ICU extension functions with database db.
167210 */
167211 SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
167212 static const struct IcuScalar {
167213 const char *zName; /* Function name */
167214 unsigned char nArg; /* Number of arguments */
167215 unsigned short enc; /* Optimal text encoding */
167216 unsigned char iContext; /* sqlite3_user_data() context */
167217 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
167218 } scalars[] = {
167219 {"icu_load_collation", 2, SQLITE_UTF8, 1, icuLoadCollation},
167220 {"regexp", 2, SQLITE_ANY|SQLITE_DETERMINISTIC, 0, icuRegexpFunc},
167221 {"lower", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
167222 {"lower", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
167223 {"upper", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
167224 {"upper", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
167225 {"lower", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
167226 {"lower", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
167227 {"upper", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
167228 {"upper", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
167229 {"like", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc},
167230 {"like", 3, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc},
 
 
 
 
167231 };
 
167232 int rc = SQLITE_OK;
167233 int i;
167234
167235
167236 for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
167237 const struct IcuScalar *p = &scalars[i];
167238 rc = sqlite3_create_function(
167239 db, p->zName, p->nArg, p->enc,
167240 p->iContext ? (void*)db : (void*)0,
167241 p->xFunc, 0, 0
167242 );
167243 }
167244
167245 return rc;
167246 }
@@ -169823,11 +170444,11 @@
170444
170445 /*
170446 ** Open the database handle and attach the RBU database as "rbu". If an
170447 ** error occurs, leave an error code and message in the RBU handle.
170448 */
170449 static void rbuOpenDatabase(sqlite3rbu *p, int *pbRetry){
170450 assert( p->rc || (p->dbMain==0 && p->dbRbu==0) );
170451 assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 );
170452
170453 /* Open the RBU database */
170454 p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
@@ -169898,11 +170519,11 @@
170519 if( p->eStage>=RBU_STAGE_MOVE ){
170520 bOpen = 1;
170521 }else{
170522 RbuState *pState = rbuLoadState(p);
170523 if( pState ){
170524 bOpen = (pState->eStage>=RBU_STAGE_MOVE);
170525 rbuFreeState(pState);
170526 }
170527 }
170528 if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
170529 }
@@ -169910,10 +170531,19 @@
170531 p->eStage = 0;
170532 if( p->rc==SQLITE_OK && p->dbMain==0 ){
170533 if( !rbuIsVacuum(p) ){
170534 p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
170535 }else if( p->pRbuFd->pWalFd ){
170536 if( pbRetry ){
170537 p->pRbuFd->bNolock = 0;
170538 sqlite3_close(p->dbRbu);
170539 sqlite3_close(p->dbMain);
170540 p->dbMain = 0;
170541 p->dbRbu = 0;
170542 *pbRetry = 1;
170543 return;
170544 }
170545 p->rc = SQLITE_ERROR;
170546 p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
170547 }else{
170548 char *zTarget;
170549 char *zExtra = 0;
@@ -170090,20 +170720,22 @@
170720 p->eStage = RBU_STAGE_CAPTURE;
170721 rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
170722 if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
170723 }
170724
170725 if( p->rc==SQLITE_OK && p->nFrame>0 ){
170726 p->eStage = RBU_STAGE_CKPT;
170727 p->nStep = (pState ? pState->nRow : 0);
170728 p->aBuf = rbuMalloc(p, p->pgsz);
170729 p->iWalCksum = rbuShmChecksum(p);
170730 }
170731
170732 if( p->rc==SQLITE_OK ){
170733 if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
170734 p->rc = SQLITE_DONE;
170735 p->eStage = RBU_STAGE_DONE;
170736 }
170737 }
170738 }
170739
170740 /*
170741 ** Called when iAmt bytes are read from offset iOff of the wal file while
@@ -170272,11 +170904,11 @@
170904 #else
170905 p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
170906 #endif
170907
170908 if( p->rc==SQLITE_OK ){
170909 rbuOpenDatabase(p, 0);
170910 rbuSetupCheckpoint(p, 0);
170911 }
170912 }
170913 }
170914
@@ -170983,10 +171615,11 @@
171615 rbuCreateVfs(p);
171616
171617 /* Open the target, RBU and state databases */
171618 if( p->rc==SQLITE_OK ){
171619 char *pCsr = (char*)&p[1];
171620 int bRetry = 0;
171621 if( zTarget ){
171622 p->zTarget = pCsr;
171623 memcpy(p->zTarget, zTarget, nTarget+1);
171624 pCsr += nTarget+1;
171625 }
@@ -170994,11 +171627,22 @@
171627 memcpy(p->zRbu, zRbu, nRbu+1);
171628 pCsr += nRbu+1;
171629 if( zState ){
171630 p->zState = rbuMPrintf(p, "%s", zState);
171631 }
171632
171633 /* If the first attempt to open the database file fails and the bRetry
171634 ** flag it set, this means that the db was not opened because it seemed
171635 ** to be a wal-mode db. But, this may have happened due to an earlier
171636 ** RBU vacuum operation leaving an old wal file in the directory.
171637 ** If this is the case, it will have been checkpointed and deleted
171638 ** when the handle was closed and a second attempt to open the
171639 ** database may succeed. */
171640 rbuOpenDatabase(p, &bRetry);
171641 if( bRetry ){
171642 rbuOpenDatabase(p, 0);
171643 }
171644 }
171645
171646 if( p->rc==SQLITE_OK ){
171647 pState = rbuLoadState(p);
171648 assert( pState || p->rc!=SQLITE_OK );
@@ -175957,11 +176601,11 @@
176601 sqlite3_value **ppValue /* OUT: Value from conflicting row */
176602 ){
176603 if( !pIter->pConflict ){
176604 return SQLITE_MISUSE;
176605 }
176606 if( iVal<0 || iVal>=pIter->nCol ){
176607 return SQLITE_RANGE;
176608 }
176609 *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
176610 return SQLITE_OK;
176611 }
@@ -176424,11 +177068,17 @@
177068 int i;
177069 SessionBuffer buf = {0, 0, 0};
177070
177071 sessionAppendStr(&buf, "INSERT INTO main.", &rc);
177072 sessionAppendIdent(&buf, zTab, &rc);
177073 sessionAppendStr(&buf, "(", &rc);
177074 for(i=0; i<p->nCol; i++){
177075 if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
177076 sessionAppendIdent(&buf, p->azCol[i], &rc);
177077 }
177078
177079 sessionAppendStr(&buf, ") VALUES(?", &rc);
177080 for(i=1; i<p->nCol; i++){
177081 sessionAppendStr(&buf, ", ?", &rc);
177082 }
177083 sessionAppendStr(&buf, ")", &rc);
177084
@@ -176970,42 +177620,51 @@
177620 break;
177621 }
177622 nTab = (int)strlen(zTab);
177623 sApply.azCol = (const char **)zTab;
177624 }else{
177625 int nMinCol = 0;
177626 int i;
177627
177628 sqlite3changeset_pk(pIter, &abPK, 0);
177629 rc = sessionTableInfo(
177630 db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
177631 );
177632 if( rc!=SQLITE_OK ) break;
177633 for(i=0; i<sApply.nCol; i++){
177634 if( sApply.abPK[i] ) nMinCol = i+1;
177635 }
177636
177637 if( sApply.nCol==0 ){
177638 schemaMismatch = 1;
177639 sqlite3_log(SQLITE_SCHEMA,
177640 "sqlite3changeset_apply(): no such table: %s", zTab
177641 );
177642 }
177643 else if( sApply.nCol<nCol ){
177644 schemaMismatch = 1;
177645 sqlite3_log(SQLITE_SCHEMA,
177646 "sqlite3changeset_apply(): table %s has %d columns, "
177647 "expected %d or more",
177648 zTab, sApply.nCol, nCol
177649 );
177650 }
177651 else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
177652 schemaMismatch = 1;
177653 sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
177654 "primary key mismatch for table %s", zTab
177655 );
177656 }
177657 else{
177658 sApply.nCol = nCol;
177659 if((rc = sessionSelectRow(db, zTab, &sApply))
177660 || (rc = sessionUpdateRow(db, zTab, &sApply))
177661 || (rc = sessionDeleteRow(db, zTab, &sApply))
177662 || (rc = sessionInsertRow(db, zTab, &sApply))
177663 ){
177664 break;
177665 }
177666 }
177667 nTab = sqlite3Strlen30(zTab);
177668 }
177669 }
177670
@@ -177593,11 +178252,11 @@
178252 ** a BLOB, but there is no support for JSONB in the current implementation.
178253 ** This implementation parses JSON text at 250 MB/s, so it is hard to see
178254 ** how JSONB might improve on that.)
178255 */
178256 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
178257 #if !defined(SQLITEINT_H)
178258 /* #include "sqlite3ext.h" */
178259 #endif
178260 SQLITE_EXTENSION_INIT1
178261 /* #include <assert.h> */
178262 /* #include <string.h> */
@@ -181644,10 +182303,35 @@
182303 */
182304 #ifndef fts5YYMALLOCARGTYPE
182305 # define fts5YYMALLOCARGTYPE size_t
182306 #endif
182307
182308 /* Initialize a new parser that has already been allocated.
182309 */
182310 static void sqlite3Fts5ParserInit(void *fts5yypParser){
182311 fts5yyParser *pParser = (fts5yyParser*)fts5yypParser;
182312 #ifdef fts5YYTRACKMAXSTACKDEPTH
182313 pParser->fts5yyhwm = 0;
182314 #endif
182315 #if fts5YYSTACKDEPTH<=0
182316 pParser->fts5yytos = NULL;
182317 pParser->fts5yystack = NULL;
182318 pParser->fts5yystksz = 0;
182319 if( fts5yyGrowStack(pParser) ){
182320 pParser->fts5yystack = &pParser->fts5yystk0;
182321 pParser->fts5yystksz = 1;
182322 }
182323 #endif
182324 #ifndef fts5YYNOERRORRECOVERY
182325 pParser->fts5yyerrcnt = -1;
182326 #endif
182327 pParser->fts5yytos = pParser->fts5yystack;
182328 pParser->fts5yystack[0].stateno = 0;
182329 pParser->fts5yystack[0].major = 0;
182330 }
182331
182332 #ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
182333 /*
182334 ** This function allocates a new parser.
182335 ** The only argument is a pointer to a function which works like
182336 ** malloc.
182337 **
@@ -181659,32 +182343,15 @@
182343 ** to sqlite3Fts5Parser and sqlite3Fts5ParserFree.
182344 */
182345 static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(fts5YYMALLOCARGTYPE)){
182346 fts5yyParser *pParser;
182347 pParser = (fts5yyParser*)(*mallocProc)( (fts5YYMALLOCARGTYPE)sizeof(fts5yyParser) );
182348 if( pParser ) sqlite3Fts5ParserInit(pParser);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182349 return pParser;
182350 }
182351 #endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
182352
182353
182354 /* The following function deletes the "minor type" or semantic value
182355 ** associated with a symbol. The symbol can be either a terminal
182356 ** or nonterminal. "fts5yymajor" is the symbol code, and "fts5yypminor" is
182357 ** a pointer to the value to be deleted. The code used to do the
@@ -181762,10 +182429,22 @@
182429 }
182430 #endif
182431 fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor);
182432 }
182433
182434 /*
182435 ** Clear all secondary memory allocations from the parser
182436 */
182437 static void sqlite3Fts5ParserFinalize(void *p){
182438 fts5yyParser *pParser = (fts5yyParser*)p;
182439 while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
182440 #if fts5YYSTACKDEPTH<=0
182441 if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
182442 #endif
182443 }
182444
182445 #ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
182446 /*
182447 ** Deallocate and destroy a parser. Destructors are called for
182448 ** all stack elements before shutting the parser down.
182449 **
182450 ** If the fts5YYPARSEFREENEVERNULL macro exists (for example because it
@@ -181774,20 +182453,17 @@
182453 */
182454 static void sqlite3Fts5ParserFree(
182455 void *p, /* The parser to be deleted */
182456 void (*freeProc)(void*) /* Function used to reclaim memory */
182457 ){
 
182458 #ifndef fts5YYPARSEFREENEVERNULL
182459 if( p==0 ) return;
182460 #endif
182461 sqlite3Fts5ParserFinalize(p);
182462 (*freeProc)(p);
182463 }
182464 #endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
 
 
182465
182466 /*
182467 ** Return the peak depth of the stack for a parser.
182468 */
182469 #ifdef fts5YYTRACKMAXSTACKDEPTH
@@ -186122,11 +186798,11 @@
186798 memset(&sCtx, 0, sizeof(TokenCtx));
186799 sCtx.pPhrase = pAppend;
186800
186801 rc = fts5ParseStringFromToken(pToken, &z);
186802 if( rc==SQLITE_OK ){
186803 int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_PREFIX : 0);
186804 int n;
186805 sqlite3Fts5Dequote(z);
186806 n = (int)strlen(z);
186807 rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize);
186808 }
@@ -196863,11 +197539,11 @@
197539 int nArg, /* Number of args */
197540 sqlite3_value **apUnused /* Function arguments */
197541 ){
197542 assert( nArg==0 );
197543 UNUSED_PARAM2(nArg, apUnused);
197544 sqlite3_result_text(pCtx, "fts5: 2017-02-13 16:02:40 ada05cfa86ad7f5645450ac7a2a21c9aa6e57d2c", -1, SQLITE_TRANSIENT);
197545 }
197546
197547 static int fts5Init(sqlite3 *db){
197548 static const sqlite3_module fts5Mod = {
197549 /* iVersion */ 2,
197550
+83 -55
--- 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-13 16:02:40 ada05cfa86ad7f5645450ac7a2a21c9aa6e57d2c"
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
@@ -8627,11 +8648,11 @@
86278648
** The session object will be used to create changesets for tables in
86288649
** database zDb, where zDb is either "main", or "temp", or the name of an
86298650
** attached database. It is not an error if database zDb is not attached
86308651
** to the database when the session object is created.
86318652
*/
8632
-int sqlite3session_create(
8653
+SQLITE_API int sqlite3session_create(
86338654
sqlite3 *db, /* Database handle */
86348655
const char *zDb, /* Name of db (e.g. "main") */
86358656
sqlite3_session **ppSession /* OUT: New session object */
86368657
);
86378658
@@ -8645,11 +8666,11 @@
86458666
**
86468667
** Session objects must be deleted before the database handle to which they
86478668
** are attached is closed. Refer to the documentation for
86488669
** [sqlite3session_create()] for details.
86498670
*/
8650
-void sqlite3session_delete(sqlite3_session *pSession);
8671
+SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
86518672
86528673
86538674
/*
86548675
** CAPI3REF: Enable Or Disable A Session Object
86558676
**
@@ -8665,11 +8686,11 @@
86658686
** no-op, and may be used to query the current state of the session.
86668687
**
86678688
** The return value indicates the final state of the session object: 0 if
86688689
** the session is disabled, or 1 if it is enabled.
86698690
*/
8670
-int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
8691
+SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
86718692
86728693
/*
86738694
** CAPI3REF: Set Or Clear the Indirect Change Flag
86748695
**
86758696
** Each change recorded by a session object is marked as either direct or
@@ -8694,11 +8715,11 @@
86948715
** indirect flag for the specified session object.
86958716
**
86968717
** The return value indicates the final state of the indirect flag: 0 if
86978718
** it is clear, or 1 if it is set.
86988719
*/
8699
-int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
8720
+SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
87008721
87018722
/*
87028723
** CAPI3REF: Attach A Table To A Session Object
87038724
**
87048725
** If argument zTab is not NULL, then it is the name of a table to attach
@@ -8724,11 +8745,11 @@
87248745
** in one or more of their PRIMARY KEY columns.
87258746
**
87268747
** SQLITE_OK is returned if the call completes without error. Or, if an error
87278748
** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
87288749
*/
8729
-int sqlite3session_attach(
8750
+SQLITE_API int sqlite3session_attach(
87308751
sqlite3_session *pSession, /* Session object */
87318752
const char *zTab /* Table name */
87328753
);
87338754
87348755
/*
@@ -8738,11 +8759,11 @@
87388759
** in tables that are not attached to the Session object, the filter is called
87398760
** to determine whether changes to the table's rows should be tracked or not.
87408761
** If xFilter returns 0, changes is not tracked. Note that once a table is
87418762
** attached, xFilter will not be called again.
87428763
*/
8743
-void sqlite3session_table_filter(
8764
+SQLITE_API void sqlite3session_table_filter(
87448765
sqlite3_session *pSession, /* Session object */
87458766
int(*xFilter)(
87468767
void *pCtx, /* Copy of third arg to _filter_table() */
87478768
const char *zTab /* Table name */
87488769
),
@@ -8851,11 +8872,11 @@
88518872
** changeset, even though the delete took place while the session was disabled.
88528873
** Or, if one field of a row is updated while a session is disabled, and
88538874
** another field of the same row is updated while the session is enabled, the
88548875
** resulting changeset will contain an UPDATE change that updates both fields.
88558876
*/
8856
-int sqlite3session_changeset(
8877
+SQLITE_API int sqlite3session_changeset(
88578878
sqlite3_session *pSession, /* Session object */
88588879
int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
88598880
void **ppChangeset /* OUT: Buffer containing changeset */
88608881
);
88618882
@@ -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
@@ -8912,11 +8934,11 @@
89128934
** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
89138935
** may be set to point to a buffer containing an English language error
89148936
** message. It is the responsibility of the caller to free this buffer using
89158937
** sqlite3_free().
89168938
*/
8917
-int sqlite3session_diff(
8939
+SQLITE_API int sqlite3session_diff(
89188940
sqlite3_session *pSession,
89198941
const char *zFromDb,
89208942
const char *zTbl,
89218943
char **pzErrMsg
89228944
);
@@ -8948,11 +8970,11 @@
89488970
** Changes within a patchset are ordered in the same way as for changesets
89498971
** generated by the sqlite3session_changeset() function (i.e. all changes for
89508972
** a single table are grouped together, tables appear in the order in which
89518973
** they were attached to the session object).
89528974
*/
8953
-int sqlite3session_patchset(
8975
+SQLITE_API int sqlite3session_patchset(
89548976
sqlite3_session *pSession, /* Session object */
89558977
int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */
89568978
void **ppPatchset /* OUT: Buffer containing changeset */
89578979
);
89588980
@@ -8969,11 +8991,11 @@
89698991
** an attached table is modified and then later on the original values
89708992
** are restored. However, if this function returns non-zero, then it is
89718993
** guaranteed that a call to sqlite3session_changeset() will return a
89728994
** changeset containing zero changes.
89738995
*/
8974
-int sqlite3session_isempty(sqlite3_session *pSession);
8996
+SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
89758997
89768998
/*
89778999
** CAPI3REF: Create An Iterator To Traverse A Changeset
89789000
**
89799001
** Create an iterator used to iterate through the contents of a changeset.
@@ -9004,11 +9026,11 @@
90049026
** this function, all changes that relate to a single table are visited
90059027
** consecutively. There is no chance that the iterator will visit a change
90069028
** the applies to table X, then one for table Y, and then later on visit
90079029
** another change for table X.
90089030
*/
9009
-int sqlite3changeset_start(
9031
+SQLITE_API int sqlite3changeset_start(
90109032
sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
90119033
int nChangeset, /* Size of changeset blob in bytes */
90129034
void *pChangeset /* Pointer to blob containing changeset */
90139035
);
90149036
@@ -9033,11 +9055,11 @@
90339055
**
90349056
** If an error occurs, an SQLite error code is returned. Possible error
90359057
** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or
90369058
** SQLITE_NOMEM.
90379059
*/
9038
-int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
9060
+SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
90399061
90409062
/*
90419063
** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
90429064
**
90439065
** The pIter argument passed to this function may either be an iterator
@@ -9061,11 +9083,11 @@
90619083
**
90629084
** If no error occurs, SQLITE_OK is returned. If an error does occur, an
90639085
** SQLite error code is returned. The values of the output variables may not
90649086
** be trusted in this case.
90659087
*/
9066
-int sqlite3changeset_op(
9088
+SQLITE_API int sqlite3changeset_op(
90679089
sqlite3_changeset_iter *pIter, /* Iterator object */
90689090
const char **pzTab, /* OUT: Pointer to table name */
90699091
int *pnCol, /* OUT: Number of columns in table */
90709092
int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
90719093
int *pbIndirect /* OUT: True for an 'indirect' change */
@@ -9094,11 +9116,11 @@
90949116
** If this function is called when the iterator does not point to a valid
90959117
** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
90969118
** SQLITE_OK is returned and the output variables populated as described
90979119
** above.
90989120
*/
9099
-int sqlite3changeset_pk(
9121
+SQLITE_API int sqlite3changeset_pk(
91009122
sqlite3_changeset_iter *pIter, /* Iterator object */
91019123
unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
91029124
int *pnCol /* OUT: Number of entries in output array */
91039125
);
91049126
@@ -9124,11 +9146,11 @@
91249146
** is similar to the "old.*" columns available to update or delete triggers.
91259147
**
91269148
** If some other error occurs (e.g. an OOM condition), an SQLite error code
91279149
** is returned and *ppValue is set to NULL.
91289150
*/
9129
-int sqlite3changeset_old(
9151
+SQLITE_API int sqlite3changeset_old(
91309152
sqlite3_changeset_iter *pIter, /* Changeset iterator */
91319153
int iVal, /* Column number */
91329154
sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
91339155
);
91349156
@@ -9157,11 +9179,11 @@
91579179
** triggers.
91589180
**
91599181
** If some other error occurs (e.g. an OOM condition), an SQLite error code
91609182
** is returned and *ppValue is set to NULL.
91619183
*/
9162
-int sqlite3changeset_new(
9184
+SQLITE_API int sqlite3changeset_new(
91639185
sqlite3_changeset_iter *pIter, /* Changeset iterator */
91649186
int iVal, /* Column number */
91659187
sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
91669188
);
91679189
@@ -9184,11 +9206,11 @@
91849206
** and returns SQLITE_OK.
91859207
**
91869208
** If some other error occurs (e.g. an OOM condition), an SQLite error code
91879209
** is returned and *ppValue is set to NULL.
91889210
*/
9189
-int sqlite3changeset_conflict(
9211
+SQLITE_API int sqlite3changeset_conflict(
91909212
sqlite3_changeset_iter *pIter, /* Changeset iterator */
91919213
int iVal, /* Column number */
91929214
sqlite3_value **ppValue /* OUT: Value from conflicting row */
91939215
);
91949216
@@ -9200,11 +9222,11 @@
92009222
** it sets the output variable to the total number of known foreign key
92019223
** violations in the destination database and returns SQLITE_OK.
92029224
**
92039225
** In all other cases this function returns SQLITE_MISUSE.
92049226
*/
9205
-int sqlite3changeset_fk_conflicts(
9227
+SQLITE_API int sqlite3changeset_fk_conflicts(
92069228
sqlite3_changeset_iter *pIter, /* Changeset iterator */
92079229
int *pnOut /* OUT: Number of FK violations */
92089230
);
92099231
92109232
@@ -9233,11 +9255,11 @@
92339255
** rc = sqlite3changeset_finalize();
92349256
** if( rc!=SQLITE_OK ){
92359257
** // An error has occurred
92369258
** }
92379259
*/
9238
-int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
9260
+SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
92399261
92409262
/*
92419263
** CAPI3REF: Invert A Changeset
92429264
**
92439265
** This function is used to "invert" a changeset object. Applying an inverted
@@ -9263,11 +9285,11 @@
92639285
** call to this function.
92649286
**
92659287
** WARNING/TODO: This function currently assumes that the input is a valid
92669288
** changeset. If it is not, the results are undefined.
92679289
*/
9268
-int sqlite3changeset_invert(
9290
+SQLITE_API int sqlite3changeset_invert(
92699291
int nIn, const void *pIn, /* Input changeset */
92709292
int *pnOut, void **ppOut /* OUT: Inverse of input */
92719293
);
92729294
92739295
/*
@@ -9292,11 +9314,11 @@
92929314
** *pnOut = 0;
92939315
** }
92949316
**
92959317
** Refer to the sqlite3_changegroup documentation below for details.
92969318
*/
9297
-int sqlite3changeset_concat(
9319
+SQLITE_API int sqlite3changeset_concat(
92989320
int nA, /* Number of bytes in buffer pA */
92999321
void *pA, /* Pointer to buffer containing changeset A */
93009322
int nB, /* Number of bytes in buffer pB */
93019323
void *pB, /* Pointer to buffer containing changeset B */
93029324
int *pnOut, /* OUT: Number of bytes in output changeset */
@@ -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,
@@ -9592,11 +9620,11 @@
95929620
** If any other error (aside from a constraint failure when attempting to
95939621
** write to the target database) occurs, then the savepoint transaction is
95949622
** rolled back, restoring the target database to its original state, and an
95959623
** SQLite error code returned.
95969624
*/
9597
-int sqlite3changeset_apply(
9625
+SQLITE_API int sqlite3changeset_apply(
95989626
sqlite3 *db, /* Apply change to "main" db of this handle */
95999627
int nChangeset, /* Size of changeset in bytes */
96009628
void *pChangeset, /* Changeset blob */
96019629
int(*xFilter)(
96029630
void *pCtx, /* Copy of sixth arg to _apply() */
@@ -9793,11 +9821,11 @@
97939821
**
97949822
** The sessions module never invokes an xOutput callback with the third
97959823
** parameter set to a value less than or equal to zero. Other than this,
97969824
** no guarantees are made as to the size of the chunks of data returned.
97979825
*/
9798
-int sqlite3changeset_apply_strm(
9826
+SQLITE_API int sqlite3changeset_apply_strm(
97999827
sqlite3 *db, /* Apply change to "main" db of this handle */
98009828
int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
98019829
void *pIn, /* First arg for xInput */
98029830
int(*xFilter)(
98039831
void *pCtx, /* Copy of sixth arg to _apply() */
@@ -9808,35 +9836,35 @@
98089836
int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
98099837
sqlite3_changeset_iter *p /* Handle describing change and conflict */
98109838
),
98119839
void *pCtx /* First argument passed to xConflict */
98129840
);
9813
-int sqlite3changeset_concat_strm(
9841
+SQLITE_API int sqlite3changeset_concat_strm(
98149842
int (*xInputA)(void *pIn, void *pData, int *pnData),
98159843
void *pInA,
98169844
int (*xInputB)(void *pIn, void *pData, int *pnData),
98179845
void *pInB,
98189846
int (*xOutput)(void *pOut, const void *pData, int nData),
98199847
void *pOut
98209848
);
9821
-int sqlite3changeset_invert_strm(
9849
+SQLITE_API int sqlite3changeset_invert_strm(
98229850
int (*xInput)(void *pIn, void *pData, int *pnData),
98239851
void *pIn,
98249852
int (*xOutput)(void *pOut, const void *pData, int nData),
98259853
void *pOut
98269854
);
9827
-int sqlite3changeset_start_strm(
9855
+SQLITE_API int sqlite3changeset_start_strm(
98289856
sqlite3_changeset_iter **pp,
98299857
int (*xInput)(void *pIn, void *pData, int *pnData),
98309858
void *pIn
98319859
);
9832
-int sqlite3session_changeset_strm(
9860
+SQLITE_API int sqlite3session_changeset_strm(
98339861
sqlite3_session *pSession,
98349862
int (*xOutput)(void *pOut, const void *pData, int nData),
98359863
void *pOut
98369864
);
9837
-int sqlite3session_patchset_strm(
9865
+SQLITE_API int sqlite3session_patchset_strm(
98389866
sqlite3_session *pSession,
98399867
int (*xOutput)(void *pOut, const void *pData, int nData),
98409868
void *pOut
98419869
);
98429870
int sqlite3changegroup_add_strm(sqlite3_changegroup*,
98439871
--- 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
@@ -8627,11 +8648,11 @@
8627 ** The session object will be used to create changesets for tables in
8628 ** database zDb, where zDb is either "main", or "temp", or the name of an
8629 ** attached database. It is not an error if database zDb is not attached
8630 ** to the database when the session object is created.
8631 */
8632 int sqlite3session_create(
8633 sqlite3 *db, /* Database handle */
8634 const char *zDb, /* Name of db (e.g. "main") */
8635 sqlite3_session **ppSession /* OUT: New session object */
8636 );
8637
@@ -8645,11 +8666,11 @@
8645 **
8646 ** Session objects must be deleted before the database handle to which they
8647 ** are attached is closed. Refer to the documentation for
8648 ** [sqlite3session_create()] for details.
8649 */
8650 void sqlite3session_delete(sqlite3_session *pSession);
8651
8652
8653 /*
8654 ** CAPI3REF: Enable Or Disable A Session Object
8655 **
@@ -8665,11 +8686,11 @@
8665 ** no-op, and may be used to query the current state of the session.
8666 **
8667 ** The return value indicates the final state of the session object: 0 if
8668 ** the session is disabled, or 1 if it is enabled.
8669 */
8670 int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
8671
8672 /*
8673 ** CAPI3REF: Set Or Clear the Indirect Change Flag
8674 **
8675 ** Each change recorded by a session object is marked as either direct or
@@ -8694,11 +8715,11 @@
8694 ** indirect flag for the specified session object.
8695 **
8696 ** The return value indicates the final state of the indirect flag: 0 if
8697 ** it is clear, or 1 if it is set.
8698 */
8699 int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
8700
8701 /*
8702 ** CAPI3REF: Attach A Table To A Session Object
8703 **
8704 ** If argument zTab is not NULL, then it is the name of a table to attach
@@ -8724,11 +8745,11 @@
8724 ** in one or more of their PRIMARY KEY columns.
8725 **
8726 ** SQLITE_OK is returned if the call completes without error. Or, if an error
8727 ** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
8728 */
8729 int sqlite3session_attach(
8730 sqlite3_session *pSession, /* Session object */
8731 const char *zTab /* Table name */
8732 );
8733
8734 /*
@@ -8738,11 +8759,11 @@
8738 ** in tables that are not attached to the Session object, the filter is called
8739 ** to determine whether changes to the table's rows should be tracked or not.
8740 ** If xFilter returns 0, changes is not tracked. Note that once a table is
8741 ** attached, xFilter will not be called again.
8742 */
8743 void sqlite3session_table_filter(
8744 sqlite3_session *pSession, /* Session object */
8745 int(*xFilter)(
8746 void *pCtx, /* Copy of third arg to _filter_table() */
8747 const char *zTab /* Table name */
8748 ),
@@ -8851,11 +8872,11 @@
8851 ** changeset, even though the delete took place while the session was disabled.
8852 ** Or, if one field of a row is updated while a session is disabled, and
8853 ** another field of the same row is updated while the session is enabled, the
8854 ** resulting changeset will contain an UPDATE change that updates both fields.
8855 */
8856 int sqlite3session_changeset(
8857 sqlite3_session *pSession, /* Session object */
8858 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
8859 void **ppChangeset /* OUT: Buffer containing changeset */
8860 );
8861
@@ -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
@@ -8912,11 +8934,11 @@
8912 ** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
8913 ** may be set to point to a buffer containing an English language error
8914 ** message. It is the responsibility of the caller to free this buffer using
8915 ** sqlite3_free().
8916 */
8917 int sqlite3session_diff(
8918 sqlite3_session *pSession,
8919 const char *zFromDb,
8920 const char *zTbl,
8921 char **pzErrMsg
8922 );
@@ -8948,11 +8970,11 @@
8948 ** Changes within a patchset are ordered in the same way as for changesets
8949 ** generated by the sqlite3session_changeset() function (i.e. all changes for
8950 ** a single table are grouped together, tables appear in the order in which
8951 ** they were attached to the session object).
8952 */
8953 int sqlite3session_patchset(
8954 sqlite3_session *pSession, /* Session object */
8955 int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */
8956 void **ppPatchset /* OUT: Buffer containing changeset */
8957 );
8958
@@ -8969,11 +8991,11 @@
8969 ** an attached table is modified and then later on the original values
8970 ** are restored. However, if this function returns non-zero, then it is
8971 ** guaranteed that a call to sqlite3session_changeset() will return a
8972 ** changeset containing zero changes.
8973 */
8974 int sqlite3session_isempty(sqlite3_session *pSession);
8975
8976 /*
8977 ** CAPI3REF: Create An Iterator To Traverse A Changeset
8978 **
8979 ** Create an iterator used to iterate through the contents of a changeset.
@@ -9004,11 +9026,11 @@
9004 ** this function, all changes that relate to a single table are visited
9005 ** consecutively. There is no chance that the iterator will visit a change
9006 ** the applies to table X, then one for table Y, and then later on visit
9007 ** another change for table X.
9008 */
9009 int sqlite3changeset_start(
9010 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
9011 int nChangeset, /* Size of changeset blob in bytes */
9012 void *pChangeset /* Pointer to blob containing changeset */
9013 );
9014
@@ -9033,11 +9055,11 @@
9033 **
9034 ** If an error occurs, an SQLite error code is returned. Possible error
9035 ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or
9036 ** SQLITE_NOMEM.
9037 */
9038 int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
9039
9040 /*
9041 ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
9042 **
9043 ** The pIter argument passed to this function may either be an iterator
@@ -9061,11 +9083,11 @@
9061 **
9062 ** If no error occurs, SQLITE_OK is returned. If an error does occur, an
9063 ** SQLite error code is returned. The values of the output variables may not
9064 ** be trusted in this case.
9065 */
9066 int sqlite3changeset_op(
9067 sqlite3_changeset_iter *pIter, /* Iterator object */
9068 const char **pzTab, /* OUT: Pointer to table name */
9069 int *pnCol, /* OUT: Number of columns in table */
9070 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
9071 int *pbIndirect /* OUT: True for an 'indirect' change */
@@ -9094,11 +9116,11 @@
9094 ** If this function is called when the iterator does not point to a valid
9095 ** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
9096 ** SQLITE_OK is returned and the output variables populated as described
9097 ** above.
9098 */
9099 int sqlite3changeset_pk(
9100 sqlite3_changeset_iter *pIter, /* Iterator object */
9101 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
9102 int *pnCol /* OUT: Number of entries in output array */
9103 );
9104
@@ -9124,11 +9146,11 @@
9124 ** is similar to the "old.*" columns available to update or delete triggers.
9125 **
9126 ** If some other error occurs (e.g. an OOM condition), an SQLite error code
9127 ** is returned and *ppValue is set to NULL.
9128 */
9129 int sqlite3changeset_old(
9130 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9131 int iVal, /* Column number */
9132 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
9133 );
9134
@@ -9157,11 +9179,11 @@
9157 ** triggers.
9158 **
9159 ** If some other error occurs (e.g. an OOM condition), an SQLite error code
9160 ** is returned and *ppValue is set to NULL.
9161 */
9162 int sqlite3changeset_new(
9163 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9164 int iVal, /* Column number */
9165 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
9166 );
9167
@@ -9184,11 +9206,11 @@
9184 ** and returns SQLITE_OK.
9185 **
9186 ** If some other error occurs (e.g. an OOM condition), an SQLite error code
9187 ** is returned and *ppValue is set to NULL.
9188 */
9189 int sqlite3changeset_conflict(
9190 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9191 int iVal, /* Column number */
9192 sqlite3_value **ppValue /* OUT: Value from conflicting row */
9193 );
9194
@@ -9200,11 +9222,11 @@
9200 ** it sets the output variable to the total number of known foreign key
9201 ** violations in the destination database and returns SQLITE_OK.
9202 **
9203 ** In all other cases this function returns SQLITE_MISUSE.
9204 */
9205 int sqlite3changeset_fk_conflicts(
9206 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9207 int *pnOut /* OUT: Number of FK violations */
9208 );
9209
9210
@@ -9233,11 +9255,11 @@
9233 ** rc = sqlite3changeset_finalize();
9234 ** if( rc!=SQLITE_OK ){
9235 ** // An error has occurred
9236 ** }
9237 */
9238 int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
9239
9240 /*
9241 ** CAPI3REF: Invert A Changeset
9242 **
9243 ** This function is used to "invert" a changeset object. Applying an inverted
@@ -9263,11 +9285,11 @@
9263 ** call to this function.
9264 **
9265 ** WARNING/TODO: This function currently assumes that the input is a valid
9266 ** changeset. If it is not, the results are undefined.
9267 */
9268 int sqlite3changeset_invert(
9269 int nIn, const void *pIn, /* Input changeset */
9270 int *pnOut, void **ppOut /* OUT: Inverse of input */
9271 );
9272
9273 /*
@@ -9292,11 +9314,11 @@
9292 ** *pnOut = 0;
9293 ** }
9294 **
9295 ** Refer to the sqlite3_changegroup documentation below for details.
9296 */
9297 int sqlite3changeset_concat(
9298 int nA, /* Number of bytes in buffer pA */
9299 void *pA, /* Pointer to buffer containing changeset A */
9300 int nB, /* Number of bytes in buffer pB */
9301 void *pB, /* Pointer to buffer containing changeset B */
9302 int *pnOut, /* OUT: Number of bytes in output changeset */
@@ -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,
@@ -9592,11 +9620,11 @@
9592 ** If any other error (aside from a constraint failure when attempting to
9593 ** write to the target database) occurs, then the savepoint transaction is
9594 ** rolled back, restoring the target database to its original state, and an
9595 ** SQLite error code returned.
9596 */
9597 int sqlite3changeset_apply(
9598 sqlite3 *db, /* Apply change to "main" db of this handle */
9599 int nChangeset, /* Size of changeset in bytes */
9600 void *pChangeset, /* Changeset blob */
9601 int(*xFilter)(
9602 void *pCtx, /* Copy of sixth arg to _apply() */
@@ -9793,11 +9821,11 @@
9793 **
9794 ** The sessions module never invokes an xOutput callback with the third
9795 ** parameter set to a value less than or equal to zero. Other than this,
9796 ** no guarantees are made as to the size of the chunks of data returned.
9797 */
9798 int sqlite3changeset_apply_strm(
9799 sqlite3 *db, /* Apply change to "main" db of this handle */
9800 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
9801 void *pIn, /* First arg for xInput */
9802 int(*xFilter)(
9803 void *pCtx, /* Copy of sixth arg to _apply() */
@@ -9808,35 +9836,35 @@
9808 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
9809 sqlite3_changeset_iter *p /* Handle describing change and conflict */
9810 ),
9811 void *pCtx /* First argument passed to xConflict */
9812 );
9813 int sqlite3changeset_concat_strm(
9814 int (*xInputA)(void *pIn, void *pData, int *pnData),
9815 void *pInA,
9816 int (*xInputB)(void *pIn, void *pData, int *pnData),
9817 void *pInB,
9818 int (*xOutput)(void *pOut, const void *pData, int nData),
9819 void *pOut
9820 );
9821 int sqlite3changeset_invert_strm(
9822 int (*xInput)(void *pIn, void *pData, int *pnData),
9823 void *pIn,
9824 int (*xOutput)(void *pOut, const void *pData, int nData),
9825 void *pOut
9826 );
9827 int sqlite3changeset_start_strm(
9828 sqlite3_changeset_iter **pp,
9829 int (*xInput)(void *pIn, void *pData, int *pnData),
9830 void *pIn
9831 );
9832 int sqlite3session_changeset_strm(
9833 sqlite3_session *pSession,
9834 int (*xOutput)(void *pOut, const void *pData, int nData),
9835 void *pOut
9836 );
9837 int sqlite3session_patchset_strm(
9838 sqlite3_session *pSession,
9839 int (*xOutput)(void *pOut, const void *pData, int nData),
9840 void *pOut
9841 );
9842 int sqlite3changegroup_add_strm(sqlite3_changegroup*,
9843
--- 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-13 16:02:40 ada05cfa86ad7f5645450ac7a2a21c9aa6e57d2c"
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
@@ -8627,11 +8648,11 @@
8648 ** The session object will be used to create changesets for tables in
8649 ** database zDb, where zDb is either "main", or "temp", or the name of an
8650 ** attached database. It is not an error if database zDb is not attached
8651 ** to the database when the session object is created.
8652 */
8653 SQLITE_API int sqlite3session_create(
8654 sqlite3 *db, /* Database handle */
8655 const char *zDb, /* Name of db (e.g. "main") */
8656 sqlite3_session **ppSession /* OUT: New session object */
8657 );
8658
@@ -8645,11 +8666,11 @@
8666 **
8667 ** Session objects must be deleted before the database handle to which they
8668 ** are attached is closed. Refer to the documentation for
8669 ** [sqlite3session_create()] for details.
8670 */
8671 SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
8672
8673
8674 /*
8675 ** CAPI3REF: Enable Or Disable A Session Object
8676 **
@@ -8665,11 +8686,11 @@
8686 ** no-op, and may be used to query the current state of the session.
8687 **
8688 ** The return value indicates the final state of the session object: 0 if
8689 ** the session is disabled, or 1 if it is enabled.
8690 */
8691 SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
8692
8693 /*
8694 ** CAPI3REF: Set Or Clear the Indirect Change Flag
8695 **
8696 ** Each change recorded by a session object is marked as either direct or
@@ -8694,11 +8715,11 @@
8715 ** indirect flag for the specified session object.
8716 **
8717 ** The return value indicates the final state of the indirect flag: 0 if
8718 ** it is clear, or 1 if it is set.
8719 */
8720 SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
8721
8722 /*
8723 ** CAPI3REF: Attach A Table To A Session Object
8724 **
8725 ** If argument zTab is not NULL, then it is the name of a table to attach
@@ -8724,11 +8745,11 @@
8745 ** in one or more of their PRIMARY KEY columns.
8746 **
8747 ** SQLITE_OK is returned if the call completes without error. Or, if an error
8748 ** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
8749 */
8750 SQLITE_API int sqlite3session_attach(
8751 sqlite3_session *pSession, /* Session object */
8752 const char *zTab /* Table name */
8753 );
8754
8755 /*
@@ -8738,11 +8759,11 @@
8759 ** in tables that are not attached to the Session object, the filter is called
8760 ** to determine whether changes to the table's rows should be tracked or not.
8761 ** If xFilter returns 0, changes is not tracked. Note that once a table is
8762 ** attached, xFilter will not be called again.
8763 */
8764 SQLITE_API void sqlite3session_table_filter(
8765 sqlite3_session *pSession, /* Session object */
8766 int(*xFilter)(
8767 void *pCtx, /* Copy of third arg to _filter_table() */
8768 const char *zTab /* Table name */
8769 ),
@@ -8851,11 +8872,11 @@
8872 ** changeset, even though the delete took place while the session was disabled.
8873 ** Or, if one field of a row is updated while a session is disabled, and
8874 ** another field of the same row is updated while the session is enabled, the
8875 ** resulting changeset will contain an UPDATE change that updates both fields.
8876 */
8877 SQLITE_API int sqlite3session_changeset(
8878 sqlite3_session *pSession, /* Session object */
8879 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
8880 void **ppChangeset /* OUT: Buffer containing changeset */
8881 );
8882
@@ -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
@@ -8912,11 +8934,11 @@
8934 ** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
8935 ** may be set to point to a buffer containing an English language error
8936 ** message. It is the responsibility of the caller to free this buffer using
8937 ** sqlite3_free().
8938 */
8939 SQLITE_API int sqlite3session_diff(
8940 sqlite3_session *pSession,
8941 const char *zFromDb,
8942 const char *zTbl,
8943 char **pzErrMsg
8944 );
@@ -8948,11 +8970,11 @@
8970 ** Changes within a patchset are ordered in the same way as for changesets
8971 ** generated by the sqlite3session_changeset() function (i.e. all changes for
8972 ** a single table are grouped together, tables appear in the order in which
8973 ** they were attached to the session object).
8974 */
8975 SQLITE_API int sqlite3session_patchset(
8976 sqlite3_session *pSession, /* Session object */
8977 int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */
8978 void **ppPatchset /* OUT: Buffer containing changeset */
8979 );
8980
@@ -8969,11 +8991,11 @@
8991 ** an attached table is modified and then later on the original values
8992 ** are restored. However, if this function returns non-zero, then it is
8993 ** guaranteed that a call to sqlite3session_changeset() will return a
8994 ** changeset containing zero changes.
8995 */
8996 SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
8997
8998 /*
8999 ** CAPI3REF: Create An Iterator To Traverse A Changeset
9000 **
9001 ** Create an iterator used to iterate through the contents of a changeset.
@@ -9004,11 +9026,11 @@
9026 ** this function, all changes that relate to a single table are visited
9027 ** consecutively. There is no chance that the iterator will visit a change
9028 ** the applies to table X, then one for table Y, and then later on visit
9029 ** another change for table X.
9030 */
9031 SQLITE_API int sqlite3changeset_start(
9032 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
9033 int nChangeset, /* Size of changeset blob in bytes */
9034 void *pChangeset /* Pointer to blob containing changeset */
9035 );
9036
@@ -9033,11 +9055,11 @@
9055 **
9056 ** If an error occurs, an SQLite error code is returned. Possible error
9057 ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or
9058 ** SQLITE_NOMEM.
9059 */
9060 SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
9061
9062 /*
9063 ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
9064 **
9065 ** The pIter argument passed to this function may either be an iterator
@@ -9061,11 +9083,11 @@
9083 **
9084 ** If no error occurs, SQLITE_OK is returned. If an error does occur, an
9085 ** SQLite error code is returned. The values of the output variables may not
9086 ** be trusted in this case.
9087 */
9088 SQLITE_API int sqlite3changeset_op(
9089 sqlite3_changeset_iter *pIter, /* Iterator object */
9090 const char **pzTab, /* OUT: Pointer to table name */
9091 int *pnCol, /* OUT: Number of columns in table */
9092 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
9093 int *pbIndirect /* OUT: True for an 'indirect' change */
@@ -9094,11 +9116,11 @@
9116 ** If this function is called when the iterator does not point to a valid
9117 ** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
9118 ** SQLITE_OK is returned and the output variables populated as described
9119 ** above.
9120 */
9121 SQLITE_API int sqlite3changeset_pk(
9122 sqlite3_changeset_iter *pIter, /* Iterator object */
9123 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
9124 int *pnCol /* OUT: Number of entries in output array */
9125 );
9126
@@ -9124,11 +9146,11 @@
9146 ** is similar to the "old.*" columns available to update or delete triggers.
9147 **
9148 ** If some other error occurs (e.g. an OOM condition), an SQLite error code
9149 ** is returned and *ppValue is set to NULL.
9150 */
9151 SQLITE_API int sqlite3changeset_old(
9152 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9153 int iVal, /* Column number */
9154 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
9155 );
9156
@@ -9157,11 +9179,11 @@
9179 ** triggers.
9180 **
9181 ** If some other error occurs (e.g. an OOM condition), an SQLite error code
9182 ** is returned and *ppValue is set to NULL.
9183 */
9184 SQLITE_API int sqlite3changeset_new(
9185 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9186 int iVal, /* Column number */
9187 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
9188 );
9189
@@ -9184,11 +9206,11 @@
9206 ** and returns SQLITE_OK.
9207 **
9208 ** If some other error occurs (e.g. an OOM condition), an SQLite error code
9209 ** is returned and *ppValue is set to NULL.
9210 */
9211 SQLITE_API int sqlite3changeset_conflict(
9212 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9213 int iVal, /* Column number */
9214 sqlite3_value **ppValue /* OUT: Value from conflicting row */
9215 );
9216
@@ -9200,11 +9222,11 @@
9222 ** it sets the output variable to the total number of known foreign key
9223 ** violations in the destination database and returns SQLITE_OK.
9224 **
9225 ** In all other cases this function returns SQLITE_MISUSE.
9226 */
9227 SQLITE_API int sqlite3changeset_fk_conflicts(
9228 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9229 int *pnOut /* OUT: Number of FK violations */
9230 );
9231
9232
@@ -9233,11 +9255,11 @@
9255 ** rc = sqlite3changeset_finalize();
9256 ** if( rc!=SQLITE_OK ){
9257 ** // An error has occurred
9258 ** }
9259 */
9260 SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
9261
9262 /*
9263 ** CAPI3REF: Invert A Changeset
9264 **
9265 ** This function is used to "invert" a changeset object. Applying an inverted
@@ -9263,11 +9285,11 @@
9285 ** call to this function.
9286 **
9287 ** WARNING/TODO: This function currently assumes that the input is a valid
9288 ** changeset. If it is not, the results are undefined.
9289 */
9290 SQLITE_API int sqlite3changeset_invert(
9291 int nIn, const void *pIn, /* Input changeset */
9292 int *pnOut, void **ppOut /* OUT: Inverse of input */
9293 );
9294
9295 /*
@@ -9292,11 +9314,11 @@
9314 ** *pnOut = 0;
9315 ** }
9316 **
9317 ** Refer to the sqlite3_changegroup documentation below for details.
9318 */
9319 SQLITE_API int sqlite3changeset_concat(
9320 int nA, /* Number of bytes in buffer pA */
9321 void *pA, /* Pointer to buffer containing changeset A */
9322 int nB, /* Number of bytes in buffer pB */
9323 void *pB, /* Pointer to buffer containing changeset B */
9324 int *pnOut, /* OUT: Number of bytes in output changeset */
@@ -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,
@@ -9592,11 +9620,11 @@
9620 ** If any other error (aside from a constraint failure when attempting to
9621 ** write to the target database) occurs, then the savepoint transaction is
9622 ** rolled back, restoring the target database to its original state, and an
9623 ** SQLite error code returned.
9624 */
9625 SQLITE_API int sqlite3changeset_apply(
9626 sqlite3 *db, /* Apply change to "main" db of this handle */
9627 int nChangeset, /* Size of changeset in bytes */
9628 void *pChangeset, /* Changeset blob */
9629 int(*xFilter)(
9630 void *pCtx, /* Copy of sixth arg to _apply() */
@@ -9793,11 +9821,11 @@
9821 **
9822 ** The sessions module never invokes an xOutput callback with the third
9823 ** parameter set to a value less than or equal to zero. Other than this,
9824 ** no guarantees are made as to the size of the chunks of data returned.
9825 */
9826 SQLITE_API int sqlite3changeset_apply_strm(
9827 sqlite3 *db, /* Apply change to "main" db of this handle */
9828 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
9829 void *pIn, /* First arg for xInput */
9830 int(*xFilter)(
9831 void *pCtx, /* Copy of sixth arg to _apply() */
@@ -9808,35 +9836,35 @@
9836 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
9837 sqlite3_changeset_iter *p /* Handle describing change and conflict */
9838 ),
9839 void *pCtx /* First argument passed to xConflict */
9840 );
9841 SQLITE_API int sqlite3changeset_concat_strm(
9842 int (*xInputA)(void *pIn, void *pData, int *pnData),
9843 void *pInA,
9844 int (*xInputB)(void *pIn, void *pData, int *pnData),
9845 void *pInB,
9846 int (*xOutput)(void *pOut, const void *pData, int nData),
9847 void *pOut
9848 );
9849 SQLITE_API int sqlite3changeset_invert_strm(
9850 int (*xInput)(void *pIn, void *pData, int *pnData),
9851 void *pIn,
9852 int (*xOutput)(void *pOut, const void *pData, int nData),
9853 void *pOut
9854 );
9855 SQLITE_API int sqlite3changeset_start_strm(
9856 sqlite3_changeset_iter **pp,
9857 int (*xInput)(void *pIn, void *pData, int *pnData),
9858 void *pIn
9859 );
9860 SQLITE_API int sqlite3session_changeset_strm(
9861 sqlite3_session *pSession,
9862 int (*xOutput)(void *pOut, const void *pData, int nData),
9863 void *pOut
9864 );
9865 SQLITE_API int sqlite3session_patchset_strm(
9866 sqlite3_session *pSession,
9867 int (*xOutput)(void *pOut, const void *pData, int nData),
9868 void *pOut
9869 );
9870 int sqlite3changegroup_add_strm(sqlite3_changegroup*,
9871

Keyboard Shortcuts

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