Fossil SCM

Update the built-in SQLite to the first 3.23.0 beta.

drh 2018-03-28 18:38 trunk
Commit d470fc70d661aa18bfae448c779ff76e88dfd989e78978d81aebe0d0c493e3e2
3 files changed +25 -10 +989 -122 +224 -14
+25 -10
--- src/shell.c
+++ src/shell.c
@@ -148,10 +148,13 @@
148148
# include <fcntl.h>
149149
# define isatty(h) _isatty(h)
150150
# ifndef access
151151
# define access(f,m) _access((f),(m))
152152
# endif
153
+# ifndef unlink
154
+# define unlink _unlink
155
+# endif
153156
# undef popen
154157
# define popen _popen
155158
# undef pclose
156159
# define pclose _pclose
157160
#else
@@ -2140,10 +2143,13 @@
21402143
# include "windows.h"
21412144
# include <io.h>
21422145
# include <direct.h>
21432146
/* # include "test_windirent.h" */
21442147
# define dirent DIRENT
2148
+# ifndef chmod
2149
+# define chmod _chmod
2150
+# endif
21452151
# ifndef stat
21462152
# define stat _stat
21472153
# endif
21482154
# define mkdir(path,mode) _mkdir(path)
21492155
# define lstat(path,buf) stat(path,buf)
@@ -6125,13 +6131,15 @@
61256131
nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
61266132
if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
61276133
p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
61286134
61296135
/* Append the data to the body of the new archive */
6130
- if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
6131
- memcpy(&p->body.a[p->body.n], aData, nData);
6132
- p->body.n += nData;
6136
+ if( nData>0 ){
6137
+ if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
6138
+ memcpy(&p->body.a[p->body.n], aData, nData);
6139
+ p->body.n += nData;
6140
+ }
61336141
61346142
/* Append the CDS record to the directory of the new archive */
61356143
nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
61366144
if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
61376145
p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
@@ -12853,15 +12861,15 @@
1285312861
if( rc!=SQLITE_OK ) goto end_ar_transaction;
1285412862
}
1285512863
rc = arExecSql(pAr, zCreate);
1285612864
}
1285712865
for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
12858
- char *zSql = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
12866
+ char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
1285912867
pAr->bVerbose ? "shell_putsnl(name)" : "name",
1286012868
pAr->azArg[i], pAr->zDir);
12861
- rc = arExecSql(pAr, zSql);
12862
- sqlite3_free(zSql);
12869
+ rc = arExecSql(pAr, zSql2);
12870
+ sqlite3_free(zSql2);
1286312871
}
1286412872
end_ar_transaction:
1286512873
if( rc!=SQLITE_OK ){
1286612874
arExecSql(pAr, "ROLLBACK TO ar; RELEASE ar;");
1286712875
}else{
@@ -14135,12 +14143,12 @@
1413514143
rc = 1;
1413614144
goto meta_command_exit;
1413714145
}
1413814146
}
1413914147
if( zName!=0 ){
14140
- int isMaster = sqlite3_strlike(zName, "sqlite_master", 0)==0;
14141
- if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master",0)==0 ){
14148
+ int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0;
14149
+ if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){
1414214150
char *new_argv[2], *new_colv[2];
1414314151
new_argv[0] = sqlite3_mprintf(
1414414152
"CREATE TABLE %s (\n"
1414514153
" type text,\n"
1414614154
" name text,\n"
@@ -14196,17 +14204,22 @@
1419614204
}
1419714205
#endif
1419814206
appendText(&sSelect, ") WHERE ", 0);
1419914207
if( zName ){
1420014208
char *zQarg = sqlite3_mprintf("%Q", zName);
14209
+ int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
14210
+ strchr(zName, '[') != 0;
1420114211
if( strchr(zName, '.') ){
1420214212
appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
1420314213
}else{
1420414214
appendText(&sSelect, "lower(tbl_name)", 0);
1420514215
}
14206
- appendText(&sSelect, strchr(zName, '*') ? " GLOB " : " LIKE ", 0);
14216
+ appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
1420714217
appendText(&sSelect, zQarg, 0);
14218
+ if( !bGlob ){
14219
+ appendText(&sSelect, " ESCAPE '\\' ", 0);
14220
+ }
1420814221
appendText(&sSelect, " AND ", 0);
1420914222
sqlite3_free(zQarg);
1421014223
}
1421114224
appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
1421214225
" ORDER BY snum, rowid", 0);
@@ -14617,11 +14630,11 @@
1461714630
rc = 1;
1461814631
goto meta_command_exit;
1461914632
}else{
1462014633
zLike = z;
1462114634
bSeparate = 1;
14622
- if( sqlite3_strlike("sqlite_%", zLike, 0)==0 ) bSchema = 1;
14635
+ if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
1462314636
}
1462414637
}
1462514638
if( bSchema ){
1462614639
zSql = "SELECT lower(name) FROM sqlite_master"
1462714640
" WHERE type='table' AND coalesce(rootpage,0)>1"
@@ -15923,10 +15936,12 @@
1592315936
}else if( strcmp(z,"-zip")==0 ){
1592415937
data.openMode = SHELL_OPEN_ZIPFILE;
1592515938
#endif
1592615939
}else if( strcmp(z,"-append")==0 ){
1592715940
data.openMode = SHELL_OPEN_APPENDVFS;
15941
+ }else if( strcmp(z,"-readonly")==0 ){
15942
+ data.openMode = SHELL_OPEN_READONLY;
1592815943
}else if( strcmp(z,"-ascii")==0 ){
1592915944
data.mode = MODE_Ascii;
1593015945
sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
1593115946
SEP_Unit);
1593215947
sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
1593315948
--- src/shell.c
+++ src/shell.c
@@ -148,10 +148,13 @@
148 # include <fcntl.h>
149 # define isatty(h) _isatty(h)
150 # ifndef access
151 # define access(f,m) _access((f),(m))
152 # endif
 
 
 
153 # undef popen
154 # define popen _popen
155 # undef pclose
156 # define pclose _pclose
157 #else
@@ -2140,10 +2143,13 @@
2140 # include "windows.h"
2141 # include <io.h>
2142 # include <direct.h>
2143 /* # include "test_windirent.h" */
2144 # define dirent DIRENT
 
 
 
2145 # ifndef stat
2146 # define stat _stat
2147 # endif
2148 # define mkdir(path,mode) _mkdir(path)
2149 # define lstat(path,buf) stat(path,buf)
@@ -6125,13 +6131,15 @@
6125 nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
6126 if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
6127 p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
6128
6129 /* Append the data to the body of the new archive */
6130 if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
6131 memcpy(&p->body.a[p->body.n], aData, nData);
6132 p->body.n += nData;
 
 
6133
6134 /* Append the CDS record to the directory of the new archive */
6135 nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
6136 if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
6137 p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
@@ -12853,15 +12861,15 @@
12853 if( rc!=SQLITE_OK ) goto end_ar_transaction;
12854 }
12855 rc = arExecSql(pAr, zCreate);
12856 }
12857 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
12858 char *zSql = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
12859 pAr->bVerbose ? "shell_putsnl(name)" : "name",
12860 pAr->azArg[i], pAr->zDir);
12861 rc = arExecSql(pAr, zSql);
12862 sqlite3_free(zSql);
12863 }
12864 end_ar_transaction:
12865 if( rc!=SQLITE_OK ){
12866 arExecSql(pAr, "ROLLBACK TO ar; RELEASE ar;");
12867 }else{
@@ -14135,12 +14143,12 @@
14135 rc = 1;
14136 goto meta_command_exit;
14137 }
14138 }
14139 if( zName!=0 ){
14140 int isMaster = sqlite3_strlike(zName, "sqlite_master", 0)==0;
14141 if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master",0)==0 ){
14142 char *new_argv[2], *new_colv[2];
14143 new_argv[0] = sqlite3_mprintf(
14144 "CREATE TABLE %s (\n"
14145 " type text,\n"
14146 " name text,\n"
@@ -14196,17 +14204,22 @@
14196 }
14197 #endif
14198 appendText(&sSelect, ") WHERE ", 0);
14199 if( zName ){
14200 char *zQarg = sqlite3_mprintf("%Q", zName);
 
 
14201 if( strchr(zName, '.') ){
14202 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
14203 }else{
14204 appendText(&sSelect, "lower(tbl_name)", 0);
14205 }
14206 appendText(&sSelect, strchr(zName, '*') ? " GLOB " : " LIKE ", 0);
14207 appendText(&sSelect, zQarg, 0);
 
 
 
14208 appendText(&sSelect, " AND ", 0);
14209 sqlite3_free(zQarg);
14210 }
14211 appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
14212 " ORDER BY snum, rowid", 0);
@@ -14617,11 +14630,11 @@
14617 rc = 1;
14618 goto meta_command_exit;
14619 }else{
14620 zLike = z;
14621 bSeparate = 1;
14622 if( sqlite3_strlike("sqlite_%", zLike, 0)==0 ) bSchema = 1;
14623 }
14624 }
14625 if( bSchema ){
14626 zSql = "SELECT lower(name) FROM sqlite_master"
14627 " WHERE type='table' AND coalesce(rootpage,0)>1"
@@ -15923,10 +15936,12 @@
15923 }else if( strcmp(z,"-zip")==0 ){
15924 data.openMode = SHELL_OPEN_ZIPFILE;
15925 #endif
15926 }else if( strcmp(z,"-append")==0 ){
15927 data.openMode = SHELL_OPEN_APPENDVFS;
 
 
15928 }else if( strcmp(z,"-ascii")==0 ){
15929 data.mode = MODE_Ascii;
15930 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
15931 SEP_Unit);
15932 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
15933
--- src/shell.c
+++ src/shell.c
@@ -148,10 +148,13 @@
148 # include <fcntl.h>
149 # define isatty(h) _isatty(h)
150 # ifndef access
151 # define access(f,m) _access((f),(m))
152 # endif
153 # ifndef unlink
154 # define unlink _unlink
155 # endif
156 # undef popen
157 # define popen _popen
158 # undef pclose
159 # define pclose _pclose
160 #else
@@ -2140,10 +2143,13 @@
2143 # include "windows.h"
2144 # include <io.h>
2145 # include <direct.h>
2146 /* # include "test_windirent.h" */
2147 # define dirent DIRENT
2148 # ifndef chmod
2149 # define chmod _chmod
2150 # endif
2151 # ifndef stat
2152 # define stat _stat
2153 # endif
2154 # define mkdir(path,mode) _mkdir(path)
2155 # define lstat(path,buf) stat(path,buf)
@@ -6125,13 +6131,15 @@
6131 nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
6132 if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
6133 p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
6134
6135 /* Append the data to the body of the new archive */
6136 if( nData>0 ){
6137 if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
6138 memcpy(&p->body.a[p->body.n], aData, nData);
6139 p->body.n += nData;
6140 }
6141
6142 /* Append the CDS record to the directory of the new archive */
6143 nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
6144 if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
6145 p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
@@ -12853,15 +12861,15 @@
12861 if( rc!=SQLITE_OK ) goto end_ar_transaction;
12862 }
12863 rc = arExecSql(pAr, zCreate);
12864 }
12865 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
12866 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
12867 pAr->bVerbose ? "shell_putsnl(name)" : "name",
12868 pAr->azArg[i], pAr->zDir);
12869 rc = arExecSql(pAr, zSql2);
12870 sqlite3_free(zSql2);
12871 }
12872 end_ar_transaction:
12873 if( rc!=SQLITE_OK ){
12874 arExecSql(pAr, "ROLLBACK TO ar; RELEASE ar;");
12875 }else{
@@ -14135,12 +14143,12 @@
14143 rc = 1;
14144 goto meta_command_exit;
14145 }
14146 }
14147 if( zName!=0 ){
14148 int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0;
14149 if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){
14150 char *new_argv[2], *new_colv[2];
14151 new_argv[0] = sqlite3_mprintf(
14152 "CREATE TABLE %s (\n"
14153 " type text,\n"
14154 " name text,\n"
@@ -14196,17 +14204,22 @@
14204 }
14205 #endif
14206 appendText(&sSelect, ") WHERE ", 0);
14207 if( zName ){
14208 char *zQarg = sqlite3_mprintf("%Q", zName);
14209 int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
14210 strchr(zName, '[') != 0;
14211 if( strchr(zName, '.') ){
14212 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
14213 }else{
14214 appendText(&sSelect, "lower(tbl_name)", 0);
14215 }
14216 appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
14217 appendText(&sSelect, zQarg, 0);
14218 if( !bGlob ){
14219 appendText(&sSelect, " ESCAPE '\\' ", 0);
14220 }
14221 appendText(&sSelect, " AND ", 0);
14222 sqlite3_free(zQarg);
14223 }
14224 appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
14225 " ORDER BY snum, rowid", 0);
@@ -14617,11 +14630,11 @@
14630 rc = 1;
14631 goto meta_command_exit;
14632 }else{
14633 zLike = z;
14634 bSeparate = 1;
14635 if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
14636 }
14637 }
14638 if( bSchema ){
14639 zSql = "SELECT lower(name) FROM sqlite_master"
14640 " WHERE type='table' AND coalesce(rootpage,0)>1"
@@ -15923,10 +15936,12 @@
15936 }else if( strcmp(z,"-zip")==0 ){
15937 data.openMode = SHELL_OPEN_ZIPFILE;
15938 #endif
15939 }else if( strcmp(z,"-append")==0 ){
15940 data.openMode = SHELL_OPEN_APPENDVFS;
15941 }else if( strcmp(z,"-readonly")==0 ){
15942 data.openMode = SHELL_OPEN_READONLY;
15943 }else if( strcmp(z,"-ascii")==0 ){
15944 data.mode = MODE_Ascii;
15945 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
15946 SEP_Unit);
15947 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
15948
+989 -122
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1147,11 +1147,11 @@
11471147
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
11481148
** [sqlite_version()] and [sqlite_source_id()].
11491149
*/
11501150
#define SQLITE_VERSION "3.23.0"
11511151
#define SQLITE_VERSION_NUMBER 3023000
1152
-#define SQLITE_SOURCE_ID "2018-03-22 17:13:44 eb4f452e354065d610ff57a6a9312ad119b6b0cc467f9dff105f0718bc27ef01"
1152
+#define SQLITE_SOURCE_ID "2018-03-28 15:56:55 eb29b3369e76ec1df25a5484d8ec5fb924e23d5c70aaa4d794b2b17ee187alt1"
11531153
11541154
/*
11551155
** CAPI3REF: Run-Time Library Version Numbers
11561156
** KEYWORDS: sqlite3_version sqlite3_sourceid
11571157
**
@@ -2086,10 +2086,16 @@
20862086
** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
20872087
** ^This file control takes the file descriptor out of batch write mode
20882088
** so that all subsequent write operations are independent.
20892089
** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
20902090
** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
2091
+**
2092
+** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
2093
+** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode causes attempts to obtain
2094
+** a file lock using the xLock or xShmLock methods of the VFS to wait
2095
+** for up to M milliseconds before failing, where M is the single
2096
+** unsigned integer parameter.
20912097
** </ul>
20922098
*/
20932099
#define SQLITE_FCNTL_LOCKSTATE 1
20942100
#define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
20952101
#define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -2120,10 +2126,11 @@
21202126
#define SQLITE_FCNTL_WIN32_GET_HANDLE 29
21212127
#define SQLITE_FCNTL_PDB 30
21222128
#define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE 31
21232129
#define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE 32
21242130
#define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE 33
2131
+#define SQLITE_FCNTL_LOCK_TIMEOUT 34
21252132
21262133
/* deprecated names */
21272134
#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
21282135
#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
21292136
#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -9833,11 +9840,11 @@
98339840
98349841
/*
98359842
** CAPI3REF: Deserialize a database
98369843
**
98379844
** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
9838
-** [database connection] D to disconnection from database S and then
9845
+** [database connection] D to disconnect from database S and then
98399846
** reopen S as an in-memory database based on the serialization contained
98409847
** in P. The serialized database P is N bytes in size. M is the size of
98419848
** the buffer P, which might be larger than N. If M is larger than N, and
98429849
** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
98439850
** permitted to add content to the in-memory database as long as the total
@@ -10974,23 +10981,22 @@
1097410981
SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
1097510982
1097610983
/*
1097710984
** CAPI3REF: Apply A Changeset To A Database
1097810985
**
10979
-** Apply a changeset to a database. This function attempts to update the
10980
-** "main" database attached to handle db with the changes found in the
10981
-** changeset passed via the second and third arguments.
10986
+** Apply a changeset or patchset to a database. These functions attempt to
10987
+** update the "main" database attached to handle db with the changes found in
10988
+** the changeset passed via the second and third arguments.
1098210989
**
10983
-** The fourth argument (xFilter) passed to this function is the "filter
10990
+** The fourth argument (xFilter) passed to these functions is the "filter
1098410991
** callback". If it is not NULL, then for each table affected by at least one
1098510992
** change in the changeset, the filter callback is invoked with
1098610993
** the table name as the second argument, and a copy of the context pointer
10987
-** passed as the sixth argument to this function as the first. If the "filter
10988
-** callback" returns zero, then no attempt is made to apply any changes to
10989
-** the table. Otherwise, if the return value is non-zero or the xFilter
10990
-** argument to this function is NULL, all changes related to the table are
10991
-** attempted.
10994
+** passed as the sixth argument as the first. If the "filter callback"
10995
+** returns zero, then no attempt is made to apply any changes to the table.
10996
+** Otherwise, if the return value is non-zero or the xFilter argument to
10997
+** is NULL, all changes related to the table are attempted.
1099210998
**
1099310999
** For each table that is not excluded by the filter callback, this function
1099411000
** tests that the target database contains a compatible table. A table is
1099511001
** considered compatible if all of the following are true:
1099611002
**
@@ -11031,11 +11037,11 @@
1103111037
** the documentation for the three
1103211038
** [SQLITE_CHANGESET_OMIT|available return values] for details.
1103311039
**
1103411040
** <dl>
1103511041
** <dt>DELETE Changes<dd>
11036
-** For each DELETE change, this function checks if the target database
11042
+** For each DELETE change, the function checks if the target database
1103711043
** contains a row with the same primary key value (or values) as the
1103811044
** original row values stored in the changeset. If it does, and the values
1103911045
** stored in all non-primary key columns also match the values stored in
1104011046
** the changeset the row is deleted from the target database.
1104111047
**
@@ -11076,11 +11082,11 @@
1107611082
** This includes the case where the INSERT operation is re-attempted because
1107711083
** an earlier call to the conflict handler function returned
1107811084
** [SQLITE_CHANGESET_REPLACE].
1107911085
**
1108011086
** <dt>UPDATE Changes<dd>
11081
-** For each UPDATE change, this function checks if the target database
11087
+** For each UPDATE change, the function checks if the target database
1108211088
** contains a row with the same primary key value (or values) as the
1108311089
** original row values stored in the changeset. If it does, and the values
1108411090
** stored in all modified non-primary key columns also match the values
1108511091
** stored in the changeset the row is updated within the target database.
1108611092
**
@@ -11107,15 +11113,25 @@
1110711113
** It is safe to execute SQL statements, including those that write to the
1110811114
** table that the callback related to, from within the xConflict callback.
1110911115
** This can be used to further customize the applications conflict
1111011116
** resolution strategy.
1111111117
**
11112
-** All changes made by this function are enclosed in a savepoint transaction.
11118
+** All changes made by these functions are enclosed in a savepoint transaction.
1111311119
** If any other error (aside from a constraint failure when attempting to
1111411120
** write to the target database) occurs, then the savepoint transaction is
1111511121
** rolled back, restoring the target database to its original state, and an
1111611122
** SQLite error code returned.
11123
+**
11124
+** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
11125
+** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
11126
+** may set (*ppRebase) to point to a "rebase" that may be used with the
11127
+** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
11128
+** is set to the size of the buffer in bytes. It is the responsibility of the
11129
+** caller to eventually free any such buffer using sqlite3_free(). The buffer
11130
+** is only allocated and populated if one or more conflicts were encountered
11131
+** while applying the patchset. See comments surrounding the sqlite3_rebaser
11132
+** APIs for further details.
1111711133
*/
1111811134
SQLITE_API int sqlite3changeset_apply(
1111911135
sqlite3 *db, /* Apply change to "main" db of this handle */
1112011136
int nChangeset, /* Size of changeset in bytes */
1112111137
void *pChangeset, /* Changeset blob */
@@ -11127,10 +11143,26 @@
1112711143
void *pCtx, /* Copy of sixth arg to _apply() */
1112811144
int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
1112911145
sqlite3_changeset_iter *p /* Handle describing change and conflict */
1113011146
),
1113111147
void *pCtx /* First argument passed to xConflict */
11148
+);
11149
+SQLITE_API int sqlite3changeset_apply_v2(
11150
+ sqlite3 *db, /* Apply change to "main" db of this handle */
11151
+ int nChangeset, /* Size of changeset in bytes */
11152
+ void *pChangeset, /* Changeset blob */
11153
+ int(*xFilter)(
11154
+ void *pCtx, /* Copy of sixth arg to _apply() */
11155
+ const char *zTab /* Table name */
11156
+ ),
11157
+ int(*xConflict)(
11158
+ void *pCtx, /* Copy of sixth arg to _apply() */
11159
+ int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
11160
+ sqlite3_changeset_iter *p /* Handle describing change and conflict */
11161
+ ),
11162
+ void *pCtx, /* First argument passed to xConflict */
11163
+ void **ppRebase, int *pnRebase
1113211164
);
1113311165
1113411166
/*
1113511167
** CAPI3REF: Constants Passed To The Conflict Handler
1113611168
**
@@ -11225,10 +11257,165 @@
1122511257
*/
1122611258
#define SQLITE_CHANGESET_OMIT 0
1122711259
#define SQLITE_CHANGESET_REPLACE 1
1122811260
#define SQLITE_CHANGESET_ABORT 2
1122911261
11262
+/*
11263
+** CAPI3REF: Rebasing changesets
11264
+** EXPERIMENTAL
11265
+**
11266
+** Suppose there is a site hosting a database in state S0. And that
11267
+** modifications are made that move that database to state S1 and a
11268
+** changeset recorded (the "local" changeset). Then, a changeset based
11269
+** on S0 is received from another site (the "remote" changeset) and
11270
+** applied to the database. The database is then in state
11271
+** (S1+"remote"), where the exact state depends on any conflict
11272
+** resolution decisions (OMIT or REPLACE) made while applying "remote".
11273
+** Rebasing a changeset is to update it to take those conflict
11274
+** resolution decisions into account, so that the same conflicts
11275
+** do not have to be resolved elsewhere in the network.
11276
+**
11277
+** For example, if both the local and remote changesets contain an
11278
+** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
11279
+**
11280
+** local: INSERT INTO t1 VALUES(1, 'v1');
11281
+** remote: INSERT INTO t1 VALUES(1, 'v2');
11282
+**
11283
+** and the conflict resolution is REPLACE, then the INSERT change is
11284
+** removed from the local changeset (it was overridden). Or, if the
11285
+** conflict resolution was "OMIT", then the local changeset is modified
11286
+** to instead contain:
11287
+**
11288
+** UPDATE t1 SET b = 'v2' WHERE a=1;
11289
+**
11290
+** Changes within the local changeset are rebased as follows:
11291
+**
11292
+** <dl>
11293
+** <dt>Local INSERT<dd>
11294
+** This may only conflict with a remote INSERT. If the conflict
11295
+** resolution was OMIT, then add an UPDATE change to the rebased
11296
+** changeset. Or, if the conflict resolution was REPLACE, add
11297
+** nothing to the rebased changeset.
11298
+**
11299
+** <dt>Local DELETE<dd>
11300
+** This may conflict with a remote UPDATE or DELETE. In both cases the
11301
+** only possible resolution is OMIT. If the remote operation was a
11302
+** DELETE, then add no change to the rebased changeset. If the remote
11303
+** operation was an UPDATE, then the old.* fields of change are updated
11304
+** to reflect the new.* values in the UPDATE.
11305
+**
11306
+** <dt>Local UPDATE<dd>
11307
+** This may conflict with a remote UPDATE or DELETE. If it conflicts
11308
+** with a DELETE, and the conflict resolution was OMIT, then the update
11309
+** is changed into an INSERT. Any undefined values in the new.* record
11310
+** from the update change are filled in using the old.* values from
11311
+** the conflicting DELETE. Or, if the conflict resolution was REPLACE,
11312
+** the UPDATE change is simply omitted from the rebased changeset.
11313
+**
11314
+** If conflict is with a remote UPDATE and the resolution is OMIT, then
11315
+** the old.* values are rebased using the new.* values in the remote
11316
+** change. Or, if the resolution is REPLACE, then the change is copied
11317
+** into the rebased changeset with updates to columns also updated by
11318
+** the conflicting remote UPDATE removed. If this means no columns would
11319
+** be updated, the change is omitted.
11320
+** </dl>
11321
+**
11322
+** A local change may be rebased against multiple remote changes
11323
+** simultaneously. If a single key is modified by multiple remote
11324
+** changesets, they are combined as follows before the local changeset
11325
+** is rebased:
11326
+**
11327
+** <ul>
11328
+** <li> If there has been one or more REPLACE resolutions on a
11329
+** key, it is rebased according to a REPLACE.
11330
+**
11331
+** <li> If there have been no REPLACE resolutions on a key, then
11332
+** the local changeset is rebased according to the most recent
11333
+** of the OMIT resolutions.
11334
+** </ul>
11335
+**
11336
+** Note that conflict resolutions from multiple remote changesets are
11337
+** combined on a per-field basis, not per-row. This means that in the
11338
+** case of multiple remote UPDATE operations, some fields of a single
11339
+** local change may be rebased for REPLACE while others are rebased for
11340
+** OMIT.
11341
+**
11342
+** In order to rebase a local changeset, the remote changeset must first
11343
+** be applied to the local database using sqlite3changeset_apply_v2() and
11344
+** the buffer of rebase information captured. Then:
11345
+**
11346
+** <ol>
11347
+** <li> An sqlite3_rebaser object is created by calling
11348
+** sqlite3rebaser_create().
11349
+** <li> The new object is configured with the rebase buffer obtained from
11350
+** sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
11351
+** If the local changeset is to be rebased against multiple remote
11352
+** changesets, then sqlite3rebaser_configure() should be called
11353
+** multiple times, in the same order that the multiple
11354
+** sqlite3changeset_apply_v2() calls were made.
11355
+** <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
11356
+** <li> The sqlite3_rebaser object is deleted by calling
11357
+** sqlite3rebaser_delete().
11358
+** </ol>
11359
+*/
11360
+typedef struct sqlite3_rebaser sqlite3_rebaser;
11361
+
11362
+/*
11363
+** CAPI3REF: Create a changeset rebaser object.
11364
+** EXPERIMENTAL
11365
+**
11366
+** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
11367
+** point to the new object and return SQLITE_OK. Otherwise, if an error
11368
+** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew)
11369
+** to NULL.
11370
+*/
11371
+SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
11372
+
11373
+/*
11374
+** CAPI3REF: Configure a changeset rebaser object.
11375
+** EXPERIMENTAL
11376
+**
11377
+** Configure the changeset rebaser object to rebase changesets according
11378
+** to the conflict resolutions described by buffer pRebase (size nRebase
11379
+** bytes), which must have been obtained from a previous call to
11380
+** sqlite3changeset_apply_v2().
11381
+*/
11382
+SQLITE_API int sqlite3rebaser_configure(
11383
+ sqlite3_rebaser*,
11384
+ int nRebase, const void *pRebase
11385
+);
11386
+
11387
+/*
11388
+** CAPI3REF: Rebase a changeset
11389
+** EXPERIMENTAL
11390
+**
11391
+** Argument pIn must point to a buffer containing a changeset nIn bytes
11392
+** in size. This function allocates and populates a buffer with a copy
11393
+** of the changeset rebased rebased according to the configuration of the
11394
+** rebaser object passed as the first argument. If successful, (*ppOut)
11395
+** is set to point to the new buffer containing the rebased changset and
11396
+** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
11397
+** responsibility of the caller to eventually free the new buffer using
11398
+** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
11399
+** are set to zero and an SQLite error code returned.
11400
+*/
11401
+SQLITE_API int sqlite3rebaser_rebase(
11402
+ sqlite3_rebaser*,
11403
+ int nIn, const void *pIn,
11404
+ int *pnOut, void **ppOut
11405
+);
11406
+
11407
+/*
11408
+** CAPI3REF: Delete a changeset rebaser object.
11409
+** EXPERIMENTAL
11410
+**
11411
+** Delete the changeset rebaser object and all associated resources. There
11412
+** should be one call to this function for each successful invocation
11413
+** of sqlite3rebaser_create().
11414
+*/
11415
+SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p);
11416
+
1123011417
/*
1123111418
** CAPI3REF: Streaming Versions of API functions.
1123211419
**
1123311420
** The six streaming API xxx_strm() functions serve similar purposes to the
1123411421
** corresponding non-streaming API functions:
@@ -11328,10 +11515,26 @@
1132811515
void *pCtx, /* Copy of sixth arg to _apply() */
1132911516
int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
1133011517
sqlite3_changeset_iter *p /* Handle describing change and conflict */
1133111518
),
1133211519
void *pCtx /* First argument passed to xConflict */
11520
+);
11521
+SQLITE_API int sqlite3changeset_apply_v2_strm(
11522
+ sqlite3 *db, /* Apply change to "main" db of this handle */
11523
+ int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
11524
+ void *pIn, /* First arg for xInput */
11525
+ int(*xFilter)(
11526
+ void *pCtx, /* Copy of sixth arg to _apply() */
11527
+ const char *zTab /* Table name */
11528
+ ),
11529
+ int(*xConflict)(
11530
+ void *pCtx, /* Copy of sixth arg to _apply() */
11531
+ int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
11532
+ sqlite3_changeset_iter *p /* Handle describing change and conflict */
11533
+ ),
11534
+ void *pCtx, /* First argument passed to xConflict */
11535
+ void **ppRebase, int *pnRebase
1133311536
);
1133411537
SQLITE_API int sqlite3changeset_concat_strm(
1133511538
int (*xInputA)(void *pIn, void *pData, int *pnData),
1133611539
void *pInA,
1133711540
int (*xInputB)(void *pIn, void *pData, int *pnData),
@@ -11365,10 +11568,17 @@
1136511568
void *pIn
1136611569
);
1136711570
SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
1136811571
int (*xOutput)(void *pOut, const void *pData, int nData),
1136911572
void *pOut
11573
+);
11574
+SQLITE_API int sqlite3rebaser_rebase_strm(
11575
+ sqlite3_rebaser *pRebaser,
11576
+ int (*xInput)(void *pIn, void *pData, int *pnData),
11577
+ void *pIn,
11578
+ int (*xOutput)(void *pOut, const void *pData, int nData),
11579
+ void *pOut
1137011580
);
1137111581
1137211582
1137311583
/*
1137411584
** Make sure we can call this stuff from C++.
@@ -13245,13 +13455,14 @@
1324513455
** handle is passed a pointer to sqlite.busyHandler. The busy-handler
1324613456
** callback is currently invoked only from within pager.c.
1324713457
*/
1324813458
typedef struct BusyHandler BusyHandler;
1324913459
struct BusyHandler {
13250
- int (*xFunc)(void *,int); /* The busy callback */
13251
- void *pArg; /* First arg to busy callback */
13252
- int nBusy; /* Incremented with each busy call */
13460
+ int (*xBusyHandler)(void *,int); /* The busy callback */
13461
+ void *pBusyArg; /* First arg to busy callback */
13462
+ int nBusy; /* Incremented with each busy call */
13463
+ u8 bExtraFileArg; /* Include sqlite3_file as callback arg */
1325313464
};
1325413465
1325513466
/*
1325613467
** Name of the master database table. The master database table
1325713468
** is a special table that holds the names and attributes of all
@@ -14456,11 +14667,11 @@
1445614667
);
1445714668
SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3*);
1445814669
SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
1445914670
1446014671
/* Functions used to configure a Pager object. */
14461
-SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
14672
+SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *);
1446214673
SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
1446314674
#ifdef SQLITE_HAS_CODEC
1446414675
SQLITE_PRIVATE void sqlite3PagerAlignReserve(Pager*,Pager*);
1446514676
#endif
1446614677
SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
@@ -14542,10 +14753,15 @@
1454214753
SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
1454314754
SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
1454414755
SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
1454514756
SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*);
1454614757
SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
14758
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
14759
+SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager);
14760
+#else
14761
+# define sqlite3PagerResetLockTimeout(X)
14762
+#endif
1454714763
1454814764
/* Functions used to truncate the database file. */
1454914765
SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
1455014766
1455114767
SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16);
@@ -18099,11 +18315,11 @@
1809918315
SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
1810018316
SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
1810118317
SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
1810218318
SQLITE_PRIVATE char sqlite3AffinityType(const char*, u8*);
1810318319
SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
18104
-SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
18320
+SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*, sqlite3_file*);
1810518321
SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
1810618322
SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
1810718323
SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
1810818324
SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
1810918325
SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
@@ -21070,11 +21286,13 @@
2107021286
** is only a hint and can be safely ignored. The sqlite3OsFileControlHint()
2107121287
** routine has no return value since the return value would be meaningless.
2107221288
*/
2107321289
SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
2107421290
#ifdef SQLITE_TEST
21075
- if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){
21291
+ if( op!=SQLITE_FCNTL_COMMIT_PHASETWO
21292
+ && op!=SQLITE_FCNTL_LOCK_TIMEOUT
21293
+ ){
2107621294
/* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
2107721295
** is using a regular VFS, it is called after the corresponding
2107821296
** transaction has been committed. Injecting a fault at this point
2107921297
** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
2108021298
** but the transaction is committed anyway.
@@ -31026,10 +31244,13 @@
3102631244
int openFlags; /* The flags specified at open() */
3102731245
#endif
3102831246
#if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
3102931247
unsigned fsFlags; /* cached details from statfs() */
3103031248
#endif
31249
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
31250
+ unsigned iBusyTimeout; /* Wait this many millisec on locks */
31251
+#endif
3103131252
#if OS_VXWORKS
3103231253
struct vxworksFileId *pId; /* Unique file ID */
3103331254
#endif
3103431255
#ifdef SQLITE_DEBUG
3103531256
/* The next group of variables are used to track whether or not the
@@ -32459,10 +32680,47 @@
3245932680
OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
3246032681
3246132682
*pResOut = reserved;
3246232683
return rc;
3246332684
}
32685
+
32686
+/*
32687
+** Set a posix-advisory-lock.
32688
+**
32689
+** There are two versions of this routine. If compiled with
32690
+** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter
32691
+** which is a pointer to a unixFile. If the unixFile->iBusyTimeout
32692
+** value is set, then it is the number of milliseconds to wait before
32693
+** failing the lock. The iBusyTimeout value is always reset back to
32694
+** zero on each call.
32695
+**
32696
+** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
32697
+** attempt to set the lock.
32698
+*/
32699
+#ifndef SQLITE_ENABLE_SETLK_TIMEOUT
32700
+# define osSetPosixAdvisoryLock(h,x,t) osFcntl(h,F_SETLK,x)
32701
+#else
32702
+static int osSetPosixAdvisoryLock(
32703
+ int h, /* The file descriptor on which to take the lock */
32704
+ struct flock *pLock, /* The description of the lock */
32705
+ unixFile *pFile /* Structure holding timeout value */
32706
+){
32707
+ int rc = osFcntl(h,F_SETLK,pLock);
32708
+ while( rc<0 && pFile->iBusyTimeout>0 ){
32709
+ /* On systems that support some kind of blocking file lock with a timeout,
32710
+ ** make appropriate changes here to invoke that blocking file lock. On
32711
+ ** generic posix, however, there is no such API. So we simply try the
32712
+ ** lock once every millisecond until either the timeout expires, or until
32713
+ ** the lock is obtained. */
32714
+ usleep(1000);
32715
+ rc = osFcntl(h,F_SETLK,pLock);
32716
+ pFile->iBusyTimeout--;
32717
+ }
32718
+ return rc;
32719
+}
32720
+#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
32721
+
3246432722
3246532723
/*
3246632724
** Attempt to set a system-lock on the file pFile. The lock is
3246732725
** described by pLock.
3246832726
**
@@ -32492,19 +32750,19 @@
3249232750
assert( pInode->nLock==0 );
3249332751
lock.l_whence = SEEK_SET;
3249432752
lock.l_start = SHARED_FIRST;
3249532753
lock.l_len = SHARED_SIZE;
3249632754
lock.l_type = F_WRLCK;
32497
- rc = osFcntl(pFile->h, F_SETLK, &lock);
32755
+ rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
3249832756
if( rc<0 ) return rc;
3249932757
pInode->bProcessLock = 1;
3250032758
pInode->nLock++;
3250132759
}else{
3250232760
rc = 0;
3250332761
}
3250432762
}else{
32505
- rc = osFcntl(pFile->h, F_SETLK, pLock);
32763
+ rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
3250632764
}
3250732765
return rc;
3250832766
}
3250932767
3251032768
/*
@@ -34860,10 +35118,16 @@
3486035118
}
3486135119
case SQLITE_FCNTL_HAS_MOVED: {
3486235120
*(int*)pArg = fileHasMoved(pFile);
3486335121
return SQLITE_OK;
3486435122
}
35123
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
35124
+ case SQLITE_FCNTL_LOCK_TIMEOUT: {
35125
+ pFile->iBusyTimeout = *(int*)pArg;
35126
+ return SQLITE_OK;
35127
+ }
35128
+#endif
3486535129
#if SQLITE_MAX_MMAP_SIZE>0
3486635130
case SQLITE_FCNTL_MMAP_SIZE: {
3486735131
i64 newLimit = *(i64*)pArg;
3486835132
int rc = SQLITE_OK;
3486935133
if( newLimit>sqlite3GlobalConfig.mxMmap ){
@@ -35179,11 +35443,11 @@
3517935443
/* Initialize the locking parameters */
3518035444
f.l_type = lockType;
3518135445
f.l_whence = SEEK_SET;
3518235446
f.l_start = ofst;
3518335447
f.l_len = n;
35184
- rc = osFcntl(pShmNode->h, F_SETLK, &f);
35448
+ rc = osSetPosixAdvisoryLock(pShmNode->h, &f, pFile);
3518535449
rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
3518635450
}
3518735451
3518835452
/* Update the global lock state and do debug tracing */
3518935453
#ifdef SQLITE_DEBUG
@@ -45039,11 +45303,11 @@
4503945303
** May you find forgiveness for yourself and forgive others.
4504045304
** May you share freely, never taking more than you give.
4504145305
**
4504245306
******************************************************************************
4504345307
**
45044
-** This file implements in-memory VFS. A database is held as a contiguous
45308
+** This file implements an in-memory VFS. A database is held as a contiguous
4504545309
** block of memory.
4504645310
**
4504745311
** This file also implements interface sqlite3_serialize() and
4504845312
** sqlite3_deserialize().
4504945313
*/
@@ -52527,11 +52791,11 @@
5252752791
**
5252852792
** If the busy-handler callback returns non-zero, the lock is
5252952793
** retried. If it returns zero, then the SQLITE_BUSY error is
5253052794
** returned to the caller of the pager API function.
5253152795
*/
52532
-SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
52796
+SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(
5253352797
Pager *pPager, /* Pager object */
5253452798
int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
5253552799
void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
5253652800
){
5253752801
pPager->xBusyHandler = xBusyHandler;
@@ -54516,10 +54780,11 @@
5451654780
Pager *pPager;
5451754781
assert( pPg!=0 );
5451854782
assert( pPg->pgno==1 );
5451954783
assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
5452054784
pPager = pPg->pPager;
54785
+ sqlite3PagerResetLockTimeout(pPager);
5452154786
sqlite3PcacheRelease(pPg);
5452254787
pagerUnlockIfUnused(pPager);
5452354788
}
5452454789
5452554790
/*
@@ -55793,10 +56058,22 @@
5579356058
*/
5579456059
SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
5579556060
return pPager->fd;
5579656061
}
5579756062
56063
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
56064
+/*
56065
+** Reset the lock timeout for pager.
56066
+*/
56067
+SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager){
56068
+ if( isOpen(pPager->fd) ){
56069
+ int x = 0;
56070
+ sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_LOCK_TIMEOUT, &x);
56071
+ }
56072
+}
56073
+#endif
56074
+
5579856075
/*
5579956076
** Return the file handle for the journal file (if it exists).
5580056077
** This will be either the rollback journal or the WAL file.
5580156078
*/
5580256079
SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){
@@ -56253,10 +56530,11 @@
5625356530
(eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
5625456531
pPager->pBusyHandlerArg,
5625556532
pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
5625656533
pnLog, pnCkpt
5625756534
);
56535
+ sqlite3PagerResetLockTimeout(pPager);
5625856536
}
5625956537
return rc;
5626056538
}
5626156539
5626256540
SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
@@ -63529,11 +63807,12 @@
6352963807
*/
6353063808
static int btreeInvokeBusyHandler(void *pArg){
6353163809
BtShared *pBt = (BtShared*)pArg;
6353263810
assert( pBt->db );
6353363811
assert( sqlite3_mutex_held(pBt->db->mutex) );
63534
- return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
63812
+ return sqlite3InvokeBusyHandler(&pBt->db->busyHandler,
63813
+ sqlite3PagerFile(pBt->pPager));
6353563814
}
6353663815
6353763816
/*
6353863817
** Open a database file.
6353963818
**
@@ -63707,11 +63986,11 @@
6370763986
if( rc!=SQLITE_OK ){
6370863987
goto btree_open_out;
6370963988
}
6371063989
pBt->openFlags = (u8)flags;
6371163990
pBt->db = db;
63712
- sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
63991
+ sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
6371363992
p->pBt = pBt;
6371463993
6371563994
pBt->pCursor = 0;
6371663995
pBt->pPage1 = 0;
6371763996
if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
@@ -64670,10 +64949,11 @@
6467064949
if( rc!=SQLITE_OK ){
6467164950
unlockBtreeIfUnused(pBt);
6467264951
}
6467364952
}while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
6467464953
btreeInvokeBusyHandler(pBt) );
64954
+ sqlite3PagerResetLockTimeout(pBt->pPager);
6467564955
6467664956
if( rc==SQLITE_OK ){
6467764957
if( p->inTrans==TRANS_NONE ){
6467864958
pBt->nTransaction++;
6467964959
#ifndef SQLITE_OMIT_SHARED_CACHE
@@ -98715,20 +98995,35 @@
9871598995
** This is the Expr node callback for sqlite3ExprImpliesNotNullRow().
9871698996
** If the expression node requires that the table at pWalker->iCur
9871798997
** have a non-NULL column, then set pWalker->eCode to 1 and abort.
9871898998
*/
9871998999
static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
99000
+ /* This routine is only called for WHERE clause expressions and so it
99001
+ ** cannot have any TK_AGG_COLUMN entries because those are only found
99002
+ ** in HAVING clauses. We can get a TK_AGG_FUNCTION in a WHERE clause,
99003
+ ** but that is an illegal construct and the query will be rejected at
99004
+ ** a later stage of processing, so the TK_AGG_FUNCTION case does not
99005
+ ** need to be considered here. */
99006
+ assert( pExpr->op!=TK_AGG_COLUMN );
99007
+ testcase( pExpr->op==TK_AGG_FUNCTION );
99008
+
9872099009
if( ExprHasProperty(pExpr, EP_FromJoin) ) return WRC_Prune;
9872199010
switch( pExpr->op ){
9872299011
case TK_ISNULL:
9872399012
case TK_IS:
9872499013
case TK_OR:
99014
+ case TK_CASE:
99015
+ case TK_IN:
9872599016
case TK_FUNCTION:
98726
- case TK_AGG_FUNCTION:
99017
+ testcase( pExpr->op==TK_ISNULL );
99018
+ testcase( pExpr->op==TK_IS );
99019
+ testcase( pExpr->op==TK_OR );
99020
+ testcase( pExpr->op==TK_CASE );
99021
+ testcase( pExpr->op==TK_IN );
99022
+ testcase( pExpr->op==TK_FUNCTION );
9872799023
return WRC_Prune;
9872899024
case TK_COLUMN:
98729
- case TK_AGG_COLUMN:
9873099025
if( pWalker->u.iCur==pExpr->iTable ){
9873199026
pWalker->eCode = 1;
9873299027
return WRC_Abort;
9873399028
}
9873499029
return WRC_Prune;
@@ -98740,10 +99035,17 @@
9874099035
/*
9874199036
** Return true (non-zero) if expression p can only be true if at least
9874299037
** one column of table iTab is non-null. In other words, return true
9874399038
** if expression p will always be NULL or false if every column of iTab
9874499039
** is NULL.
99040
+**
99041
+** False negatives are acceptable. In other words, it is ok to return
99042
+** zero even if expression p will never be true of every column of iTab
99043
+** is NULL. A false negative is merely a missed optimization opportunity.
99044
+**
99045
+** False positives are not allowed, however. A false positive may result
99046
+** in an incorrect answer.
9874599047
**
9874699048
** Terms of p that are marked with EP_FromJoin (and hence that come from
9874799049
** the ON or USING clauses of LEFT JOINS) are excluded from the analysis.
9874899050
**
9874999051
** This routine is used to check if a LEFT JOIN can be converted into
@@ -101005,11 +101307,11 @@
101005101307
}
101006101308
if( pTab->tnum==0 ){
101007101309
/* Do not gather statistics on views or virtual tables */
101008101310
return;
101009101311
}
101010
- if( sqlite3_strlike("sqlite_%", pTab->zName, 0)==0 ){
101312
+ if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
101011101313
/* Do not gather statistics on system tables */
101012101314
return;
101013101315
}
101014101316
assert( sqlite3BtreeHoldsAllMutexes(db) );
101015101317
iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
@@ -104066,11 +104368,11 @@
104066104368
if( !sqlite3ExprIsConstantOrFunction(pExpr, db->init.busy) ){
104067104369
sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
104068104370
pCol->zName);
104069104371
}else{
104070104372
/* A copy of pExpr is used instead of the original, as pExpr contains
104071
- ** tokens that point to volatile memory.
104373
+ ** tokens that point to volatile memory.
104072104374
*/
104073104375
Expr x;
104074104376
sqlite3ExprDelete(db, pCol->pDflt);
104075104377
memset(&x, 0, sizeof(x));
104076104378
x.op = TK_SPAN;
@@ -104988,11 +105290,11 @@
104988105290
Table *pSelTab; /* A fake table from which we get the result set */
104989105291
Select *pSel; /* Copy of the SELECT that implements the view */
104990105292
int nErr = 0; /* Number of errors encountered */
104991105293
int n; /* Temporarily holds the number of cursors assigned */
104992105294
sqlite3 *db = pParse->db; /* Database connection for malloc errors */
104993
-#ifndef SQLITE_OMIT_VIRTUALTABLE
105295
+#ifndef SQLITE_OMIT_VIRTUALTABLE
104994105296
int rc;
104995105297
#endif
104996105298
#ifndef SQLITE_OMIT_AUTHORIZATION
104997105299
sqlite3_xauth xAuth; /* Saved xAuth pointer */
104998105300
#endif
@@ -123778,12 +124080,13 @@
123778124080
** (2) The inner query is the recursive part of a common table expression.
123779124081
**
123780124082
** (3) The inner query has a LIMIT clause (since the changes to the WHERE
123781124083
** close would change the meaning of the LIMIT).
123782124084
**
123783
-** (4) (** This restriction was removed on 2018-03-21. It used to read:
123784
-** The inner query is the right operand of a LEFT JOIN. **)
124085
+** (4) The inner query is the right operand of a LEFT JOIN and the
124086
+** expression to be pushed down does not come from the ON clause
124087
+** on that LEFT JOIN.
123785124088
**
123786124089
** (5) The WHERE clause expression originates in the ON or USING clause
123787124090
** of a LEFT JOIN where iCursor is not the right-hand table of that
123788124091
** left join. An example:
123789124092
**
@@ -123801,11 +124104,12 @@
123801124104
*/
123802124105
static int pushDownWhereTerms(
123803124106
Parse *pParse, /* Parse context (for malloc() and error reporting) */
123804124107
Select *pSubq, /* The subquery whose WHERE clause is to be augmented */
123805124108
Expr *pWhere, /* The WHERE clause of the outer query */
123806
- int iCursor /* Cursor number of the subquery */
124109
+ int iCursor, /* Cursor number of the subquery */
124110
+ int isLeftJoin /* True if pSubq is the right term of a LEFT JOIN */
123807124111
){
123808124112
Expr *pNew;
123809124113
int nChng = 0;
123810124114
if( pWhere==0 ) return 0;
123811124115
if( pSubq->selFlags & SF_Recursive ) return 0; /* restriction (2) */
@@ -123825,13 +124129,20 @@
123825124129
123826124130
if( pSubq->pLimit!=0 ){
123827124131
return 0; /* restriction (3) */
123828124132
}
123829124133
while( pWhere->op==TK_AND ){
123830
- nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, iCursor);
124134
+ nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight,
124135
+ iCursor, isLeftJoin);
123831124136
pWhere = pWhere->pLeft;
123832124137
}
124138
+ if( isLeftJoin
124139
+ && (ExprHasProperty(pWhere,EP_FromJoin)==0
124140
+ || pWhere->iRightJoinTable!=iCursor)
124141
+ ){
124142
+ return 0; /* restriction (4) */
124143
+ }
123833124144
if( ExprHasProperty(pWhere,EP_FromJoin) && pWhere->iRightJoinTable!=iCursor ){
123834124145
return 0; /* restriction (5) */
123835124146
}
123836124147
if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
123837124148
nChng++;
@@ -125295,11 +125606,12 @@
125295125606
125296125607
/* Make copies of constant WHERE-clause terms in the outer query down
125297125608
** inside the subquery. This can help the subquery to run more efficiently.
125298125609
*/
125299125610
if( OptimizationEnabled(db, SQLITE_PushDown)
125300
- && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor)
125611
+ && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor,
125612
+ (pItem->fg.jointype & JT_OUTER)!=0)
125301125613
){
125302125614
#if SELECTTRACE_ENABLED
125303125615
if( sqlite3SelectTrace & 0x100 ){
125304125616
SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n"));
125305125617
sqlite3TreeViewSelect(0, p, 0);
@@ -146146,43 +146458,62 @@
146146146458
/*
146147146459
** This routine implements a busy callback that sleeps and tries
146148146460
** again until a timeout value is reached. The timeout value is
146149146461
** an integer number of milliseconds passed in as the first
146150146462
** argument.
146463
+**
146464
+** Return non-zero to retry the lock. Return zero to stop trying
146465
+** and cause SQLite to return SQLITE_BUSY.
146151146466
*/
146152146467
static int sqliteDefaultBusyCallback(
146153
- void *ptr, /* Database connection */
146154
- int count /* Number of times table has been busy */
146468
+ void *ptr, /* Database connection */
146469
+ int count, /* Number of times table has been busy */
146470
+ sqlite3_file *pFile /* The file on which the lock occurred */
146155146471
){
146156146472
#if SQLITE_OS_WIN || HAVE_USLEEP
146473
+ /* This case is for systems that have support for sleeping for fractions of
146474
+ ** a second. Examples: All windows systems, unix systems with usleep() */
146157146475
static const u8 delays[] =
146158146476
{ 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
146159146477
static const u8 totals[] =
146160146478
{ 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
146161146479
# define NDELAY ArraySize(delays)
146162146480
sqlite3 *db = (sqlite3 *)ptr;
146163
- int timeout = db->busyTimeout;
146481
+ int tmout = db->busyTimeout;
146164146482
int delay, prior;
146165146483
146484
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
146485
+ if( sqlite3OsFileControl(pFile,SQLITE_FCNTL_LOCK_TIMEOUT,&tmout)==SQLITE_OK ){
146486
+ if( count ){
146487
+ tmout = 0;
146488
+ sqlite3OsFileControl(pFile, SQLITE_FCNTL_LOCK_TIMEOUT, &tmout);
146489
+ return 0;
146490
+ }else{
146491
+ return 1;
146492
+ }
146493
+ }
146494
+#endif
146166146495
assert( count>=0 );
146167146496
if( count < NDELAY ){
146168146497
delay = delays[count];
146169146498
prior = totals[count];
146170146499
}else{
146171146500
delay = delays[NDELAY-1];
146172146501
prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
146173146502
}
146174
- if( prior + delay > timeout ){
146175
- delay = timeout - prior;
146503
+ if( prior + delay > tmout ){
146504
+ delay = tmout - prior;
146176146505
if( delay<=0 ) return 0;
146177146506
}
146178146507
sqlite3OsSleep(db->pVfs, delay*1000);
146179146508
return 1;
146180146509
#else
146510
+ /* This case for unix systems that lack usleep() support. Sleeping
146511
+ ** must be done in increments of whole seconds */
146181146512
sqlite3 *db = (sqlite3 *)ptr;
146182
- int timeout = ((sqlite3 *)ptr)->busyTimeout;
146183
- if( (count+1)*1000 > timeout ){
146513
+ int tmout = ((sqlite3 *)ptr)->busyTimeout;
146514
+ if( (count+1)*1000 > tmout ){
146184146515
return 0;
146185146516
}
146186146517
sqlite3OsSleep(db->pVfs, 1000000);
146187146518
return 1;
146188146519
#endif
@@ -146189,18 +146520,29 @@
146189146520
}
146190146521
146191146522
/*
146192146523
** Invoke the given busy handler.
146193146524
**
146194
-** This routine is called when an operation failed with a lock.
146525
+** This routine is called when an operation failed to acquire a
146526
+** lock on VFS file pFile.
146527
+**
146195146528
** If this routine returns non-zero, the lock is retried. If it
146196146529
** returns 0, the operation aborts with an SQLITE_BUSY error.
146197146530
*/
146198
-SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
146531
+SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p, sqlite3_file *pFile){
146199146532
int rc;
146200
- if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
146201
- rc = p->xFunc(p->pArg, p->nBusy);
146533
+ if( p->xBusyHandler==0 || p->nBusy<0 ) return 0;
146534
+ if( p->bExtraFileArg ){
146535
+ /* Add an extra parameter with the pFile pointer to the end of the
146536
+ ** callback argument list */
146537
+ int (*xTra)(void*,int,sqlite3_file*);
146538
+ xTra = (int(*)(void*,int,sqlite3_file*))p->xBusyHandler;
146539
+ rc = xTra(p->pBusyArg, p->nBusy, pFile);
146540
+ }else{
146541
+ /* Legacy style busy handler callback */
146542
+ rc = p->xBusyHandler(p->pBusyArg, p->nBusy);
146543
+ }
146202146544
if( rc==0 ){
146203146545
p->nBusy = -1;
146204146546
}else{
146205146547
p->nBusy++;
146206146548
}
@@ -146218,13 +146560,14 @@
146218146560
){
146219146561
#ifdef SQLITE_ENABLE_API_ARMOR
146220146562
if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
146221146563
#endif
146222146564
sqlite3_mutex_enter(db->mutex);
146223
- db->busyHandler.xFunc = xBusy;
146224
- db->busyHandler.pArg = pArg;
146565
+ db->busyHandler.xBusyHandler = xBusy;
146566
+ db->busyHandler.pBusyArg = pArg;
146225146567
db->busyHandler.nBusy = 0;
146568
+ db->busyHandler.bExtraFileArg = 0;
146226146569
db->busyTimeout = 0;
146227146570
sqlite3_mutex_leave(db->mutex);
146228146571
return SQLITE_OK;
146229146572
}
146230146573
@@ -146268,12 +146611,14 @@
146268146611
SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
146269146612
#ifdef SQLITE_ENABLE_API_ARMOR
146270146613
if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
146271146614
#endif
146272146615
if( ms>0 ){
146273
- sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
146616
+ sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback,
146617
+ (void*)db);
146274146618
db->busyTimeout = ms;
146619
+ db->busyHandler.bExtraFileArg = 1;
146275146620
}else{
146276146621
sqlite3_busy_handler(db, 0, 0);
146277146622
}
146278146623
return SQLITE_OK;
146279146624
}
@@ -180266,12 +180611,12 @@
180266180611
** of a patchset change is associated with the correspondingly positioned
180267180612
** table column, counting from left to right within the CREATE TABLE
180268180613
** statement.
180269180614
**
180270180615
** For a DELETE change, all fields within the record except those associated
180271
-** with PRIMARY KEY columns are set to "undefined". The PRIMARY KEY fields
180272
-** contain the values identifying the row to delete.
180616
+** with PRIMARY KEY columns are omitted. The PRIMARY KEY fields contain the
180617
+** values identifying the row to delete.
180273180618
**
180274180619
** For an UPDATE change, all fields except those associated with PRIMARY KEY
180275180620
** columns and columns that are modified by the UPDATE are set to "undefined".
180276180621
** PRIMARY KEY fields contain the values identifying the table row to update,
180277180622
** and fields associated with modified columns contain the new column values.
@@ -180550,11 +180895,11 @@
180550180895
** the type byte).
180551180896
*/
180552180897
static int sessionSerialLen(u8 *a){
180553180898
int e = *a;
180554180899
int n;
180555
- if( e==0 ) return 1;
180900
+ if( e==0 || e==0xFF ) return 1;
180556180901
if( e==SQLITE_NULL ) return 1;
180557180902
if( e==SQLITE_INTEGER || e==SQLITE_FLOAT ) return 9;
180558180903
return sessionVarintGet(&a[1], &n) + 1 + n;
180559180904
}
180560180905
@@ -180630,11 +180975,11 @@
180630180975
for(iCol=0; iCol<pTab->nCol; iCol++){
180631180976
if( pTab->abPK[iCol] ){
180632180977
int n1 = sessionSerialLen(a1);
180633180978
int n2 = sessionSerialLen(a2);
180634180979
180635
- if( pTab->abPK[iCol] && (n1!=n2 || memcmp(a1, a2, n1)) ){
180980
+ if( n1!=n2 || memcmp(a1, a2, n1) ){
180636180981
return 0;
180637180982
}
180638180983
a1 += n1;
180639180984
a2 += n2;
180640180985
}else{
@@ -180873,11 +181218,11 @@
180873181218
if( eType==SQLITE_TEXT ){
180874181219
z = sqlite3_value_text(pVal);
180875181220
}else{
180876181221
z = sqlite3_value_blob(pVal);
180877181222
}
180878
- if( memcmp(a, z, n) ) return 0;
181223
+ if( n>0 && memcmp(a, z, n) ) return 0;
180879181224
a += n;
180880181225
}
180881181226
}
180882181227
}
180883181228
@@ -182217,10 +182562,11 @@
182217182562
if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
182218182563
zSql = sqlite3_mprintf(
182219182564
"SELECT tbl, ?2, stat FROM %Q.sqlite_stat1 WHERE tbl IS ?1 AND "
182220182565
"idx IS (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", zDb
182221182566
);
182567
+ if( zSql==0 ) rc = SQLITE_NOMEM;
182222182568
}else{
182223182569
int i;
182224182570
const char *zSep = "";
182225182571
SessionBuffer buf = {0, 0, 0};
182226182572
@@ -182952,11 +183298,12 @@
182952183298
** changes in the changeset.
182953183299
*/
182954183300
static int sessionChangesetNext(
182955183301
sqlite3_changeset_iter *p, /* Changeset iterator */
182956183302
u8 **paRec, /* If non-NULL, store record pointer here */
182957
- int *pnRec /* If non-NULL, store size of record here */
183303
+ int *pnRec, /* If non-NULL, store size of record here */
183304
+ int *pbNew /* If non-NULL, true if new table */
182958183305
){
182959183306
int i;
182960183307
u8 op;
182961183308
182962183309
assert( (paRec==0 && pnRec==0) || (paRec && pnRec) );
@@ -182987,10 +183334,11 @@
182987183334
sessionDiscardData(&p->in);
182988183335
p->in.iCurrent = p->in.iNext;
182989183336
182990183337
op = p->in.aData[p->in.iNext++];
182991183338
while( op=='T' || op=='P' ){
183339
+ if( pbNew ) *pbNew = 1;
182992183340
p->bPatchset = (op=='P');
182993183341
if( sessionChangesetReadTblhdr(p) ) return p->rc;
182994183342
if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
182995183343
p->in.iCurrent = p->in.iNext;
182996183344
if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
@@ -183065,11 +183413,11 @@
183065183413
**
183066183414
** This function may not be called on iterators passed to a conflict handler
183067183415
** callback by changeset_apply().
183068183416
*/
183069183417
SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *p){
183070
- return sessionChangesetNext(p, 0, 0);
183418
+ return sessionChangesetNext(p, 0, 0, 0);
183071183419
}
183072183420
183073183421
/*
183074183422
** The following function extracts information on the current change
183075183423
** from a changeset iterator. It may only be called after changeset_next()
@@ -183444,10 +183792,12 @@
183444183792
const char **azCol; /* Array of column names */
183445183793
u8 *abPK; /* Boolean array - true if column is in PK */
183446183794
int bStat1; /* True if table is sqlite_stat1 */
183447183795
int bDeferConstraints; /* True to defer constraints */
183448183796
SessionBuffer constraints; /* Deferred constraints are stored here */
183797
+ SessionBuffer rebase; /* Rebase information (if any) here */
183798
+ int bRebaseStarted; /* If table header is already in rebase */
183449183799
};
183450183800
183451183801
/*
183452183802
** Formulate a statement to DELETE a row from database db. Assuming a table
183453183803
** structure like this:
@@ -183710,11 +184060,10 @@
183710184060
"DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS "
183711184061
"CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END "
183712184062
"AND (?4 OR stat IS ?3)"
183713184063
);
183714184064
}
183715
- assert( rc==SQLITE_OK );
183716184065
return rc;
183717184066
}
183718184067
183719184068
/*
183720184069
** A wrapper around sqlite3_bind_value() that detects an extra problem.
@@ -183824,10 +184173,58 @@
183824184173
if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect);
183825184174
}
183826184175
183827184176
return rc;
183828184177
}
184178
+
184179
+/*
184180
+** This function is called from within sqlite3changset_apply_v2() when
184181
+** a conflict is encountered and resolved using conflict resolution
184182
+** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE)..
184183
+** It adds a conflict resolution record to the buffer in
184184
+** SessionApplyCtx.rebase, which will eventually be returned to the caller
184185
+** of apply_v2() as the "rebase" buffer.
184186
+**
184187
+** Return SQLITE_OK if successful, or an SQLite error code otherwise.
184188
+*/
184189
+static int sessionRebaseAdd(
184190
+ SessionApplyCtx *p, /* Apply context */
184191
+ int eType, /* Conflict resolution (OMIT or REPLACE) */
184192
+ sqlite3_changeset_iter *pIter /* Iterator pointing at current change */
184193
+){
184194
+ int rc = SQLITE_OK;
184195
+ int i;
184196
+ int eOp = pIter->op;
184197
+ if( p->bRebaseStarted==0 ){
184198
+ /* Append a table-header to the rebase buffer */
184199
+ const char *zTab = pIter->zTab;
184200
+ sessionAppendByte(&p->rebase, 'T', &rc);
184201
+ sessionAppendVarint(&p->rebase, p->nCol, &rc);
184202
+ sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
184203
+ sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
184204
+ p->bRebaseStarted = 1;
184205
+ }
184206
+
184207
+ assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT );
184208
+ assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE );
184209
+
184210
+ sessionAppendByte(&p->rebase,
184211
+ (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc
184212
+ );
184213
+ sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc);
184214
+ for(i=0; i<p->nCol; i++){
184215
+ sqlite3_value *pVal = 0;
184216
+ if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){
184217
+ sqlite3changeset_old(pIter, i, &pVal);
184218
+ }else{
184219
+ sqlite3changeset_new(pIter, i, &pVal);
184220
+ }
184221
+ sessionAppendValue(&p->rebase, pVal, &rc);
184222
+ }
184223
+
184224
+ return rc;
184225
+}
183829184226
183830184227
/*
183831184228
** Invoke the conflict handler for the change that the changeset iterator
183832184229
** currently points to.
183833184230
**
@@ -183900,11 +184297,11 @@
183900184297
/* Instead of invoking the conflict handler, append the change blob
183901184298
** to the SessionApplyCtx.constraints buffer. */
183902184299
u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
183903184300
int nBlob = pIter->in.iNext - pIter->in.iCurrent;
183904184301
sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
183905
- res = SQLITE_CHANGESET_OMIT;
184302
+ return SQLITE_OK;
183906184303
}else{
183907184304
/* No other row with the new.* primary key. */
183908184305
res = xConflict(pCtx, eType+1, pIter);
183909184306
if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE;
183910184307
}
@@ -183926,10 +184323,13 @@
183926184323
183927184324
default:
183928184325
rc = SQLITE_MISUSE;
183929184326
break;
183930184327
}
184328
+ if( rc==SQLITE_OK ){
184329
+ rc = sessionRebaseAdd(p, res, pIter);
184330
+ }
183931184331
}
183932184332
183933184333
return rc;
183934184334
}
183935184335
@@ -184101,46 +184501,46 @@
184101184501
int bReplace = 0;
184102184502
int bRetry = 0;
184103184503
int rc;
184104184504
184105184505
rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
184106
- assert( rc==SQLITE_OK || (bRetry==0 && bReplace==0) );
184107
-
184108
- /* If the bRetry flag is set, the change has not been applied due to an
184109
- ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
184110
- ** a row with the correct PK is present in the db, but one or more other
184111
- ** fields do not contain the expected values) and the conflict handler
184112
- ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
184113
- ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
184114
- ** the SQLITE_CHANGESET_DATA problem. */
184115
- if( bRetry ){
184116
- assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
184117
- rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
184118
- }
184119
-
184120
- /* If the bReplace flag is set, the change is an INSERT that has not
184121
- ** been performed because the database already contains a row with the
184122
- ** specified primary key and the conflict handler returned
184123
- ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
184124
- ** before reattempting the INSERT. */
184125
- else if( bReplace ){
184126
- assert( pIter->op==SQLITE_INSERT );
184127
- rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
184128
- if( rc==SQLITE_OK ){
184129
- rc = sessionBindRow(pIter,
184130
- sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
184131
- sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
184132
- }
184133
- if( rc==SQLITE_OK ){
184134
- sqlite3_step(pApply->pDelete);
184135
- rc = sqlite3_reset(pApply->pDelete);
184136
- }
184137
- if( rc==SQLITE_OK ){
184138
- rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
184139
- }
184140
- if( rc==SQLITE_OK ){
184141
- rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
184506
+ if( rc==SQLITE_OK ){
184507
+ /* If the bRetry flag is set, the change has not been applied due to an
184508
+ ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
184509
+ ** a row with the correct PK is present in the db, but one or more other
184510
+ ** fields do not contain the expected values) and the conflict handler
184511
+ ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
184512
+ ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
184513
+ ** the SQLITE_CHANGESET_DATA problem. */
184514
+ if( bRetry ){
184515
+ assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
184516
+ rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
184517
+ }
184518
+
184519
+ /* If the bReplace flag is set, the change is an INSERT that has not
184520
+ ** been performed because the database already contains a row with the
184521
+ ** specified primary key and the conflict handler returned
184522
+ ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
184523
+ ** before reattempting the INSERT. */
184524
+ else if( bReplace ){
184525
+ assert( pIter->op==SQLITE_INSERT );
184526
+ rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
184527
+ if( rc==SQLITE_OK ){
184528
+ rc = sessionBindRow(pIter,
184529
+ sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
184530
+ sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
184531
+ }
184532
+ if( rc==SQLITE_OK ){
184533
+ sqlite3_step(pApply->pDelete);
184534
+ rc = sqlite3_reset(pApply->pDelete);
184535
+ }
184536
+ if( rc==SQLITE_OK ){
184537
+ rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
184538
+ }
184539
+ if( rc==SQLITE_OK ){
184540
+ rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
184541
+ }
184142184542
}
184143184543
}
184144184544
184145184545
return rc;
184146184546
}
@@ -184212,11 +184612,12 @@
184212184612
int(*xConflict)(
184213184613
void *pCtx, /* Copy of fifth arg to _apply() */
184214184614
int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
184215184615
sqlite3_changeset_iter *p /* Handle describing change and conflict */
184216184616
),
184217
- void *pCtx /* First argument passed to xConflict */
184617
+ void *pCtx, /* First argument passed to xConflict */
184618
+ void **ppRebase, int *pnRebase /* OUT: Rebase information */
184218184619
){
184219184620
int schemaMismatch = 0;
184220184621
int rc; /* Return code */
184221184622
const char *zTab = 0; /* Name of current table */
184222184623
int nTab = 0; /* Result of sqlite3Strlen30(zTab) */
@@ -184250,13 +184651,22 @@
184250184651
sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
184251184652
sqlite3_finalize(sApply.pDelete);
184252184653
sqlite3_finalize(sApply.pUpdate);
184253184654
sqlite3_finalize(sApply.pInsert);
184254184655
sqlite3_finalize(sApply.pSelect);
184255
- memset(&sApply, 0, sizeof(sApply));
184256184656
sApply.db = db;
184657
+ sApply.pDelete = 0;
184658
+ sApply.pUpdate = 0;
184659
+ sApply.pInsert = 0;
184660
+ sApply.pSelect = 0;
184661
+ sApply.nCol = 0;
184662
+ sApply.azCol = 0;
184663
+ sApply.abPK = 0;
184664
+ sApply.bStat1 = 0;
184257184665
sApply.bDeferConstraints = 1;
184666
+ sApply.bRebaseStarted = 0;
184667
+ memset(&sApply.constraints, 0, sizeof(SessionBuffer));
184258184668
184259184669
/* If an xFilter() callback was specified, invoke it now. If the
184260184670
** xFilter callback returns zero, skip this table. If it returns
184261184671
** non-zero, proceed. */
184262184672
schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
@@ -184362,19 +184772,55 @@
184362184772
}else{
184363184773
sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
184364184774
sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
184365184775
}
184366184776
184777
+ if( rc==SQLITE_OK && bPatchset==0 && ppRebase && pnRebase ){
184778
+ *ppRebase = (void*)sApply.rebase.aBuf;
184779
+ *pnRebase = sApply.rebase.nBuf;
184780
+ sApply.rebase.aBuf = 0;
184781
+ }
184367184782
sqlite3_finalize(sApply.pInsert);
184368184783
sqlite3_finalize(sApply.pDelete);
184369184784
sqlite3_finalize(sApply.pUpdate);
184370184785
sqlite3_finalize(sApply.pSelect);
184371184786
sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
184372184787
sqlite3_free((char*)sApply.constraints.aBuf);
184788
+ sqlite3_free((char*)sApply.rebase.aBuf);
184373184789
sqlite3_mutex_leave(sqlite3_db_mutex(db));
184374184790
return rc;
184375184791
}
184792
+
184793
+/*
184794
+** Apply the changeset passed via pChangeset/nChangeset to the main
184795
+** database attached to handle "db".
184796
+*/
184797
+SQLITE_API int sqlite3changeset_apply_v2(
184798
+ sqlite3 *db, /* Apply change to "main" db of this handle */
184799
+ int nChangeset, /* Size of changeset in bytes */
184800
+ void *pChangeset, /* Changeset blob */
184801
+ int(*xFilter)(
184802
+ void *pCtx, /* Copy of sixth arg to _apply() */
184803
+ const char *zTab /* Table name */
184804
+ ),
184805
+ int(*xConflict)(
184806
+ void *pCtx, /* Copy of sixth arg to _apply() */
184807
+ int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
184808
+ sqlite3_changeset_iter *p /* Handle describing change and conflict */
184809
+ ),
184810
+ void *pCtx, /* First argument passed to xConflict */
184811
+ void **ppRebase, int *pnRebase
184812
+){
184813
+ sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
184814
+ int rc = sqlite3changeset_start(&pIter, nChangeset, pChangeset);
184815
+ if( rc==SQLITE_OK ){
184816
+ rc = sessionChangesetApply(
184817
+ db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase
184818
+ );
184819
+ }
184820
+ return rc;
184821
+}
184376184822
184377184823
/*
184378184824
** Apply the changeset passed via pChangeset/nChangeset to the main database
184379184825
** attached to handle "db". Invoke the supplied conflict handler callback
184380184826
** to resolve any conflicts encountered while applying the change.
@@ -184392,23 +184838,45 @@
184392184838
int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
184393184839
sqlite3_changeset_iter *p /* Handle describing change and conflict */
184394184840
),
184395184841
void *pCtx /* First argument passed to xConflict */
184396184842
){
184397
- sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
184398
- int rc = sqlite3changeset_start(&pIter, nChangeset, pChangeset);
184399
- if( rc==SQLITE_OK ){
184400
- rc = sessionChangesetApply(db, pIter, xFilter, xConflict, pCtx);
184401
- }
184402
- return rc;
184843
+ return sqlite3changeset_apply_v2(
184844
+ db, nChangeset, pChangeset, xFilter, xConflict, pCtx, 0, 0
184845
+ );
184403184846
}
184404184847
184405184848
/*
184406184849
** Apply the changeset passed via xInput/pIn to the main database
184407184850
** attached to handle "db". Invoke the supplied conflict handler callback
184408184851
** to resolve any conflicts encountered while applying the change.
184409184852
*/
184853
+SQLITE_API int sqlite3changeset_apply_v2_strm(
184854
+ sqlite3 *db, /* Apply change to "main" db of this handle */
184855
+ int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
184856
+ void *pIn, /* First arg for xInput */
184857
+ int(*xFilter)(
184858
+ void *pCtx, /* Copy of sixth arg to _apply() */
184859
+ const char *zTab /* Table name */
184860
+ ),
184861
+ int(*xConflict)(
184862
+ void *pCtx, /* Copy of sixth arg to _apply() */
184863
+ int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
184864
+ sqlite3_changeset_iter *p /* Handle describing change and conflict */
184865
+ ),
184866
+ void *pCtx, /* First argument passed to xConflict */
184867
+ void **ppRebase, int *pnRebase
184868
+){
184869
+ sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
184870
+ int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
184871
+ if( rc==SQLITE_OK ){
184872
+ rc = sessionChangesetApply(
184873
+ db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase
184874
+ );
184875
+ }
184876
+ return rc;
184877
+}
184410184878
SQLITE_API int sqlite3changeset_apply_strm(
184411184879
sqlite3 *db, /* Apply change to "main" db of this handle */
184412184880
int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
184413184881
void *pIn, /* First arg for xInput */
184414184882
int(*xFilter)(
@@ -184420,16 +184888,13 @@
184420184888
int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
184421184889
sqlite3_changeset_iter *p /* Handle describing change and conflict */
184422184890
),
184423184891
void *pCtx /* First argument passed to xConflict */
184424184892
){
184425
- sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
184426
- int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
184427
- if( rc==SQLITE_OK ){
184428
- rc = sessionChangesetApply(db, pIter, xFilter, xConflict, pCtx);
184429
- }
184430
- return rc;
184893
+ return sqlite3changeset_apply_v2_strm(
184894
+ db, xInput, pIn, xFilter, xConflict, pCtx, 0, 0
184895
+ );
184431184896
}
184432184897
184433184898
/*
184434184899
** sqlite3_changegroup handle.
184435184900
*/
@@ -184444,31 +184909,90 @@
184444184909
** part of an sqlite3changeset_concat() operation. A new change object is
184445184910
** allocated and a pointer to it stored in *ppNew.
184446184911
*/
184447184912
static int sessionChangeMerge(
184448184913
SessionTable *pTab, /* Table structure */
184914
+ int bRebase, /* True for a rebase hash-table */
184449184915
int bPatchset, /* True for patchsets */
184450184916
SessionChange *pExist, /* Existing change */
184451184917
int op2, /* Second change operation */
184452184918
int bIndirect, /* True if second change is indirect */
184453184919
u8 *aRec, /* Second change record */
184454184920
int nRec, /* Number of bytes in aRec */
184455184921
SessionChange **ppNew /* OUT: Merged change */
184456184922
){
184457184923
SessionChange *pNew = 0;
184924
+ int rc = SQLITE_OK;
184458184925
184459184926
if( !pExist ){
184460184927
pNew = (SessionChange *)sqlite3_malloc(sizeof(SessionChange) + nRec);
184461184928
if( !pNew ){
184462184929
return SQLITE_NOMEM;
184463184930
}
184464184931
memset(pNew, 0, sizeof(SessionChange));
184465184932
pNew->op = op2;
184466184933
pNew->bIndirect = bIndirect;
184467
- pNew->nRecord = nRec;
184468184934
pNew->aRecord = (u8*)&pNew[1];
184469
- memcpy(pNew->aRecord, aRec, nRec);
184935
+ if( bIndirect==0 || bRebase==0 ){
184936
+ pNew->nRecord = nRec;
184937
+ memcpy(pNew->aRecord, aRec, nRec);
184938
+ }else{
184939
+ int i;
184940
+ u8 *pIn = aRec;
184941
+ u8 *pOut = pNew->aRecord;
184942
+ for(i=0; i<pTab->nCol; i++){
184943
+ int nIn = sessionSerialLen(pIn);
184944
+ if( *pIn==0 ){
184945
+ *pOut++ = 0;
184946
+ }else if( pTab->abPK[i]==0 ){
184947
+ *pOut++ = 0xFF;
184948
+ }else{
184949
+ memcpy(pOut, pIn, nIn);
184950
+ pOut += nIn;
184951
+ }
184952
+ pIn += nIn;
184953
+ }
184954
+ pNew->nRecord = pOut - pNew->aRecord;
184955
+ }
184956
+ }else if( bRebase ){
184957
+ if( pExist->op==SQLITE_DELETE && pExist->bIndirect ){
184958
+ *ppNew = pExist;
184959
+ }else{
184960
+ int nByte = nRec + pExist->nRecord + sizeof(SessionChange);
184961
+ pNew = (SessionChange*)sqlite3_malloc(nByte);
184962
+ if( pNew==0 ){
184963
+ rc = SQLITE_NOMEM;
184964
+ }else{
184965
+ int i;
184966
+ u8 *a1 = pExist->aRecord;
184967
+ u8 *a2 = aRec;
184968
+ u8 *pOut;
184969
+
184970
+ memset(pNew, 0, nByte);
184971
+ pNew->bIndirect = bIndirect || pExist->bIndirect;
184972
+ pNew->op = op2;
184973
+ pOut = pNew->aRecord = (u8*)&pNew[1];
184974
+
184975
+ for(i=0; i<pTab->nCol; i++){
184976
+ int n1 = sessionSerialLen(a1);
184977
+ int n2 = sessionSerialLen(a2);
184978
+ if( *a1==0xFF || (pTab->abPK[i]==0 && bIndirect) ){
184979
+ *pOut++ = 0xFF;
184980
+ }else if( *a2==0 ){
184981
+ memcpy(pOut, a1, n1);
184982
+ pOut += n1;
184983
+ }else{
184984
+ memcpy(pOut, a2, n2);
184985
+ pOut += n2;
184986
+ }
184987
+ a1 += n1;
184988
+ a2 += n2;
184989
+ }
184990
+ pNew->nRecord = pOut - pNew->aRecord;
184991
+ }
184992
+ sqlite3_free(pExist);
184993
+ }
184470184994
}else{
184471184995
int op1 = pExist->op;
184472184996
184473184997
/*
184474184998
** op1=INSERT, op2=INSERT -> Unsupported. Discard op2.
@@ -184558,28 +185082,28 @@
184558185082
sqlite3_free(pExist);
184559185083
}
184560185084
}
184561185085
184562185086
*ppNew = pNew;
184563
- return SQLITE_OK;
185087
+ return rc;
184564185088
}
184565185089
184566185090
/*
184567185091
** Add all changes in the changeset traversed by the iterator passed as
184568185092
** the first argument to the changegroup hash tables.
184569185093
*/
184570185094
static int sessionChangesetToHash(
184571185095
sqlite3_changeset_iter *pIter, /* Iterator to read from */
184572
- sqlite3_changegroup *pGrp /* Changegroup object to add changeset to */
185096
+ sqlite3_changegroup *pGrp, /* Changegroup object to add changeset to */
185097
+ int bRebase /* True if hash table is for rebasing */
184573185098
){
184574185099
u8 *aRec;
184575185100
int nRec;
184576185101
int rc = SQLITE_OK;
184577185102
SessionTable *pTab = 0;
184578185103
184579
-
184580
- while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec) ){
185104
+ while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, 0) ){
184581185105
const char *zNew;
184582185106
int nCol;
184583185107
int op;
184584185108
int iHash;
184585185109
int bIndirect;
@@ -184655,11 +185179,11 @@
184655185179
pTab->nEntry--;
184656185180
break;
184657185181
}
184658185182
}
184659185183
184660
- rc = sessionChangeMerge(pTab,
185184
+ rc = sessionChangeMerge(pTab, bRebase,
184661185185
pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
184662185186
);
184663185187
if( rc ) break;
184664185188
if( pChange ){
184665185189
pChange->pNext = pTab->apChange[iHash];
@@ -184763,11 +185287,11 @@
184763185287
sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
184764185288
int rc; /* Return code */
184765185289
184766185290
rc = sqlite3changeset_start(&pIter, nData, pData);
184767185291
if( rc==SQLITE_OK ){
184768
- rc = sessionChangesetToHash(pIter, pGrp);
185292
+ rc = sessionChangesetToHash(pIter, pGrp, 0);
184769185293
}
184770185294
sqlite3changeset_finalize(pIter);
184771185295
return rc;
184772185296
}
184773185297
@@ -184794,11 +185318,11 @@
184794185318
sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
184795185319
int rc; /* Return code */
184796185320
184797185321
rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
184798185322
if( rc==SQLITE_OK ){
184799
- rc = sessionChangesetToHash(pIter, pGrp);
185323
+ rc = sessionChangesetToHash(pIter, pGrp, 0);
184800185324
}
184801185325
sqlite3changeset_finalize(pIter);
184802185326
return rc;
184803185327
}
184804185328
@@ -184878,10 +185402,353 @@
184878185402
}
184879185403
sqlite3changegroup_delete(pGrp);
184880185404
184881185405
return rc;
184882185406
}
185407
+
185408
+/*
185409
+** Changeset rebaser handle.
185410
+*/
185411
+struct sqlite3_rebaser {
185412
+ sqlite3_changegroup grp; /* Hash table */
185413
+};
185414
+
185415
+/*
185416
+** Buffers a1 and a2 must both contain a sessions module record nCol
185417
+** fields in size. This function appends an nCol sessions module
185418
+** record to buffer pBuf that is a copy of a1, except that for
185419
+** each field that is undefined in a1[], swap in the field from a2[].
185420
+*/
185421
+static void sessionAppendRecordMerge(
185422
+ SessionBuffer *pBuf, /* Buffer to append to */
185423
+ int nCol, /* Number of columns in each record */
185424
+ u8 *a1, int n1, /* Record 1 */
185425
+ u8 *a2, int n2, /* Record 2 */
185426
+ int *pRc /* IN/OUT: error code */
185427
+){
185428
+ sessionBufferGrow(pBuf, n1+n2, pRc);
185429
+ if( *pRc==SQLITE_OK ){
185430
+ int i;
185431
+ u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
185432
+ for(i=0; i<nCol; i++){
185433
+ int nn1 = sessionSerialLen(a1);
185434
+ int nn2 = sessionSerialLen(a2);
185435
+ if( *a1==0 || *a1==0xFF ){
185436
+ memcpy(pOut, a2, nn2);
185437
+ pOut += nn2;
185438
+ }else{
185439
+ memcpy(pOut, a1, nn1);
185440
+ pOut += nn1;
185441
+ }
185442
+ a1 += nn1;
185443
+ a2 += nn2;
185444
+ }
185445
+
185446
+ pBuf->nBuf = pOut-pBuf->aBuf;
185447
+ assert( pBuf->nBuf<=pBuf->nAlloc );
185448
+ }
185449
+}
185450
+
185451
+/*
185452
+** This function is called when rebasing a local UPDATE change against one
185453
+** or more remote UPDATE changes. The aRec/nRec buffer contains the current
185454
+** old.* and new.* records for the change. The rebase buffer (a single
185455
+** record) is in aChange/nChange. The rebased change is appended to buffer
185456
+** pBuf.
185457
+**
185458
+** Rebasing the UPDATE involves:
185459
+**
185460
+** * Removing any changes to fields for which the corresponding field
185461
+** in the rebase buffer is set to "replaced" (type 0xFF). If this
185462
+** means the UPDATE change updates no fields, nothing is appended
185463
+** to the output buffer.
185464
+**
185465
+** * For each field modified by the local change for which the
185466
+** corresponding field in the rebase buffer is not "undefined" (0x00)
185467
+** or "replaced" (0xFF), the old.* value is replaced by the value
185468
+** in the rebase buffer.
185469
+*/
185470
+static void sessionAppendPartialUpdate(
185471
+ SessionBuffer *pBuf, /* Append record here */
185472
+ sqlite3_changeset_iter *pIter, /* Iterator pointed at local change */
185473
+ u8 *aRec, int nRec, /* Local change */
185474
+ u8 *aChange, int nChange, /* Record to rebase against */
185475
+ int *pRc /* IN/OUT: Return Code */
185476
+){
185477
+ sessionBufferGrow(pBuf, 2+nRec+nChange, pRc);
185478
+ if( *pRc==SQLITE_OK ){
185479
+ int bData = 0;
185480
+ u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
185481
+ int i;
185482
+ u8 *a1 = aRec;
185483
+ u8 *a2 = aChange;
185484
+
185485
+ *pOut++ = SQLITE_UPDATE;
185486
+ *pOut++ = pIter->bIndirect;
185487
+ for(i=0; i<pIter->nCol; i++){
185488
+ int n1 = sessionSerialLen(a1);
185489
+ int n2 = sessionSerialLen(a2);
185490
+ if( pIter->abPK[i] || a2[0]==0 ){
185491
+ if( !pIter->abPK[i] ) bData = 1;
185492
+ memcpy(pOut, a1, n1);
185493
+ pOut += n1;
185494
+ }else if( a2[0]!=0xFF ){
185495
+ bData = 1;
185496
+ memcpy(pOut, a2, n2);
185497
+ pOut += n2;
185498
+ }else{
185499
+ *pOut++ = '\0';
185500
+ }
185501
+ a1 += n1;
185502
+ a2 += n2;
185503
+ }
185504
+ if( bData ){
185505
+ a2 = aChange;
185506
+ for(i=0; i<pIter->nCol; i++){
185507
+ int n1 = sessionSerialLen(a1);
185508
+ int n2 = sessionSerialLen(a2);
185509
+ if( pIter->abPK[i] || a2[0]!=0xFF ){
185510
+ memcpy(pOut, a1, n1);
185511
+ pOut += n1;
185512
+ }else{
185513
+ *pOut++ = '\0';
185514
+ }
185515
+ a1 += n1;
185516
+ a2 += n2;
185517
+ }
185518
+ pBuf->nBuf = (pOut - pBuf->aBuf);
185519
+ }
185520
+ }
185521
+}
185522
+
185523
+/*
185524
+** pIter is configured to iterate through a changeset. This function rebases
185525
+** that changeset according to the current configuration of the rebaser
185526
+** object passed as the first argument. If no error occurs and argument xOutput
185527
+** is not NULL, then the changeset is returned to the caller by invoking
185528
+** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL,
185529
+** then (*ppOut) is set to point to a buffer containing the rebased changeset
185530
+** before this function returns. In this case (*pnOut) is set to the size of
185531
+** the buffer in bytes. It is the responsibility of the caller to eventually
185532
+** free the (*ppOut) buffer using sqlite3_free().
185533
+**
185534
+** If an error occurs, an SQLite error code is returned. If ppOut and
185535
+** pnOut are not NULL, then the two output parameters are set to 0 before
185536
+** returning.
185537
+*/
185538
+static int sessionRebase(
185539
+ sqlite3_rebaser *p, /* Rebaser hash table */
185540
+ sqlite3_changeset_iter *pIter, /* Input data */
185541
+ int (*xOutput)(void *pOut, const void *pData, int nData),
185542
+ void *pOut, /* Context for xOutput callback */
185543
+ int *pnOut, /* OUT: Number of bytes in output changeset */
185544
+ void **ppOut /* OUT: Inverse of pChangeset */
185545
+){
185546
+ int rc = SQLITE_OK;
185547
+ u8 *aRec = 0;
185548
+ int nRec = 0;
185549
+ int bNew = 0;
185550
+ SessionTable *pTab = 0;
185551
+ SessionBuffer sOut = {0,0,0};
185552
+
185553
+ while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, &bNew) ){
185554
+ SessionChange *pChange = 0;
185555
+ int bDone = 0;
185556
+
185557
+ if( bNew ){
185558
+ const char *zTab = pIter->zTab;
185559
+ for(pTab=p->grp.pList; pTab; pTab=pTab->pNext){
185560
+ if( 0==sqlite3_stricmp(pTab->zName, zTab) ) break;
185561
+ }
185562
+ bNew = 0;
185563
+
185564
+ /* A patchset may not be rebased */
185565
+ if( pIter->bPatchset ){
185566
+ rc = SQLITE_ERROR;
185567
+ }
185568
+
185569
+ /* Append a table header to the output for this new table */
185570
+ sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
185571
+ sessionAppendVarint(&sOut, pIter->nCol, &rc);
185572
+ sessionAppendBlob(&sOut, pIter->abPK, pIter->nCol, &rc);
185573
+ sessionAppendBlob(&sOut,(u8*)pIter->zTab,(int)strlen(pIter->zTab)+1,&rc);
185574
+ }
185575
+
185576
+ if( pTab && rc==SQLITE_OK ){
185577
+ int iHash = sessionChangeHash(pTab, 0, aRec, pTab->nChange);
185578
+
185579
+ for(pChange=pTab->apChange[iHash]; pChange; pChange=pChange->pNext){
185580
+ if( sessionChangeEqual(pTab, 0, aRec, 0, pChange->aRecord) ){
185581
+ break;
185582
+ }
185583
+ }
185584
+ }
185585
+
185586
+ if( pChange ){
185587
+ assert( pChange->op==SQLITE_DELETE || pChange->op==SQLITE_INSERT );
185588
+ switch( pIter->op ){
185589
+ case SQLITE_INSERT:
185590
+ if( pChange->op==SQLITE_INSERT ){
185591
+ bDone = 1;
185592
+ if( pChange->bIndirect==0 ){
185593
+ sessionAppendByte(&sOut, SQLITE_UPDATE, &rc);
185594
+ sessionAppendByte(&sOut, pIter->bIndirect, &rc);
185595
+ sessionAppendBlob(&sOut, pChange->aRecord, pChange->nRecord, &rc);
185596
+ sessionAppendBlob(&sOut, aRec, nRec, &rc);
185597
+ }
185598
+ }
185599
+ break;
185600
+
185601
+ case SQLITE_UPDATE:
185602
+ bDone = 1;
185603
+ if( pChange->op==SQLITE_DELETE ){
185604
+ if( pChange->bIndirect==0 ){
185605
+ u8 *pCsr = aRec;
185606
+ sessionSkipRecord(&pCsr, pIter->nCol);
185607
+ sessionAppendByte(&sOut, SQLITE_INSERT, &rc);
185608
+ sessionAppendByte(&sOut, pIter->bIndirect, &rc);
185609
+ sessionAppendRecordMerge(&sOut, pIter->nCol,
185610
+ pCsr, nRec-(pCsr-aRec),
185611
+ pChange->aRecord, pChange->nRecord, &rc
185612
+ );
185613
+ }
185614
+ }else{
185615
+ sessionAppendPartialUpdate(&sOut, pIter,
185616
+ aRec, nRec, pChange->aRecord, pChange->nRecord, &rc
185617
+ );
185618
+ }
185619
+ break;
185620
+
185621
+ default:
185622
+ assert( pIter->op==SQLITE_DELETE );
185623
+ bDone = 1;
185624
+ if( pChange->op==SQLITE_INSERT ){
185625
+ sessionAppendByte(&sOut, SQLITE_DELETE, &rc);
185626
+ sessionAppendByte(&sOut, pIter->bIndirect, &rc);
185627
+ sessionAppendRecordMerge(&sOut, pIter->nCol,
185628
+ pChange->aRecord, pChange->nRecord, aRec, nRec, &rc
185629
+ );
185630
+ }
185631
+ break;
185632
+ }
185633
+ }
185634
+
185635
+ if( bDone==0 ){
185636
+ sessionAppendByte(&sOut, pIter->op, &rc);
185637
+ sessionAppendByte(&sOut, pIter->bIndirect, &rc);
185638
+ sessionAppendBlob(&sOut, aRec, nRec, &rc);
185639
+ }
185640
+ if( rc==SQLITE_OK && xOutput && sOut.nBuf>SESSIONS_STRM_CHUNK_SIZE ){
185641
+ rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
185642
+ sOut.nBuf = 0;
185643
+ }
185644
+ if( rc ) break;
185645
+ }
185646
+
185647
+ if( rc!=SQLITE_OK ){
185648
+ sqlite3_free(sOut.aBuf);
185649
+ memset(&sOut, 0, sizeof(sOut));
185650
+ }
185651
+
185652
+ if( rc==SQLITE_OK ){
185653
+ if( xOutput ){
185654
+ if( sOut.nBuf>0 ){
185655
+ rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
185656
+ }
185657
+ }else{
185658
+ *ppOut = (void*)sOut.aBuf;
185659
+ *pnOut = sOut.nBuf;
185660
+ sOut.aBuf = 0;
185661
+ }
185662
+ }
185663
+ sqlite3_free(sOut.aBuf);
185664
+ return rc;
185665
+}
185666
+
185667
+/*
185668
+** Create a new rebaser object.
185669
+*/
185670
+SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew){
185671
+ int rc = SQLITE_OK;
185672
+ sqlite3_rebaser *pNew;
185673
+
185674
+ pNew = sqlite3_malloc(sizeof(sqlite3_rebaser));
185675
+ if( pNew==0 ){
185676
+ rc = SQLITE_NOMEM;
185677
+ }else{
185678
+ memset(pNew, 0, sizeof(sqlite3_rebaser));
185679
+ }
185680
+ *ppNew = pNew;
185681
+ return rc;
185682
+}
185683
+
185684
+/*
185685
+** Call this one or more times to configure a rebaser.
185686
+*/
185687
+SQLITE_API int sqlite3rebaser_configure(
185688
+ sqlite3_rebaser *p,
185689
+ int nRebase, const void *pRebase
185690
+){
185691
+ sqlite3_changeset_iter *pIter = 0; /* Iterator opened on pData/nData */
185692
+ int rc; /* Return code */
185693
+ rc = sqlite3changeset_start(&pIter, nRebase, (void*)pRebase);
185694
+ if( rc==SQLITE_OK ){
185695
+ rc = sessionChangesetToHash(pIter, &p->grp, 1);
185696
+ }
185697
+ sqlite3changeset_finalize(pIter);
185698
+ return rc;
185699
+}
185700
+
185701
+/*
185702
+** Rebase a changeset according to current rebaser configuration
185703
+*/
185704
+SQLITE_API int sqlite3rebaser_rebase(
185705
+ sqlite3_rebaser *p,
185706
+ int nIn, const void *pIn,
185707
+ int *pnOut, void **ppOut
185708
+){
185709
+ sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
185710
+ int rc = sqlite3changeset_start(&pIter, nIn, (void*)pIn);
185711
+
185712
+ if( rc==SQLITE_OK ){
185713
+ rc = sessionRebase(p, pIter, 0, 0, pnOut, ppOut);
185714
+ sqlite3changeset_finalize(pIter);
185715
+ }
185716
+
185717
+ return rc;
185718
+}
185719
+
185720
+/*
185721
+** Rebase a changeset according to current rebaser configuration
185722
+*/
185723
+SQLITE_API int sqlite3rebaser_rebase_strm(
185724
+ sqlite3_rebaser *p,
185725
+ int (*xInput)(void *pIn, void *pData, int *pnData),
185726
+ void *pIn,
185727
+ int (*xOutput)(void *pOut, const void *pData, int nData),
185728
+ void *pOut
185729
+){
185730
+ sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
185731
+ int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
185732
+
185733
+ if( rc==SQLITE_OK ){
185734
+ rc = sessionRebase(p, pIter, xOutput, pOut, 0, 0);
185735
+ sqlite3changeset_finalize(pIter);
185736
+ }
185737
+
185738
+ return rc;
185739
+}
185740
+
185741
+/*
185742
+** Destroy a rebaser object
185743
+*/
185744
+SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
185745
+ if( p ){
185746
+ sessionDeleteTable(p->grp.pList);
185747
+ sqlite3_free(p);
185748
+ }
185749
+}
184883185750
184884185751
#endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
184885185752
184886185753
/************** End of sqlite3session.c **************************************/
184887185754
/************** Begin file json1.c *******************************************/
@@ -204697,11 +205564,11 @@
204697205564
int nArg, /* Number of args */
204698205565
sqlite3_value **apUnused /* Function arguments */
204699205566
){
204700205567
assert( nArg==0 );
204701205568
UNUSED_PARAM2(nArg, apUnused);
204702
- sqlite3_result_text(pCtx, "fts5: 2018-03-22 17:13:44 eb4f452e354065d610ff57a6a9312ad119b6b0cc467f9dff105f0718bc27ef01", -1, SQLITE_TRANSIENT);
205569
+ sqlite3_result_text(pCtx, "fts5: 2018-03-26 16:37:53 6c40c5574f4ae9795a142d01a8f84afd1b72678ea5f6bfca14a8646c4e862605", -1, SQLITE_TRANSIENT);
204703205570
}
204704205571
204705205572
static int fts5Init(sqlite3 *db){
204706205573
static const sqlite3_module fts5Mod = {
204707205574
/* iVersion */ 2,
@@ -208967,12 +209834,12 @@
208967209834
}
208968209835
#endif /* SQLITE_CORE */
208969209836
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
208970209837
208971209838
/************** End of stmt.c ************************************************/
208972
-#if __LINE__!=208972
209839
+#if __LINE__!=209839
208973209840
#undef SQLITE_SOURCE_ID
208974
-#define SQLITE_SOURCE_ID "2018-03-22 17:13:44 eb4f452e354065d610ff57a6a9312ad119b6b0cc467f9dff105f0718bc27alt2"
209841
+#define SQLITE_SOURCE_ID "2018-03-28 15:56:55 eb29b3369e76ec1df25a5484d8ec5fb924e23d5c70aaa4d794b2b17ee187alt2"
208975209842
#endif
208976209843
/* Return the source-id for this library */
208977209844
SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
208978209845
/************************** End of sqlite3.c ******************************/
208979209846
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1147,11 +1147,11 @@
1147 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
1148 ** [sqlite_version()] and [sqlite_source_id()].
1149 */
1150 #define SQLITE_VERSION "3.23.0"
1151 #define SQLITE_VERSION_NUMBER 3023000
1152 #define SQLITE_SOURCE_ID "2018-03-22 17:13:44 eb4f452e354065d610ff57a6a9312ad119b6b0cc467f9dff105f0718bc27ef01"
1153
1154 /*
1155 ** CAPI3REF: Run-Time Library Version Numbers
1156 ** KEYWORDS: sqlite3_version sqlite3_sourceid
1157 **
@@ -2086,10 +2086,16 @@
2086 ** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
2087 ** ^This file control takes the file descriptor out of batch write mode
2088 ** so that all subsequent write operations are independent.
2089 ** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
2090 ** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
 
 
 
 
 
 
2091 ** </ul>
2092 */
2093 #define SQLITE_FCNTL_LOCKSTATE 1
2094 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
2095 #define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -2120,10 +2126,11 @@
2120 #define SQLITE_FCNTL_WIN32_GET_HANDLE 29
2121 #define SQLITE_FCNTL_PDB 30
2122 #define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE 31
2123 #define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE 32
2124 #define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE 33
 
2125
2126 /* deprecated names */
2127 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
2128 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
2129 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -9833,11 +9840,11 @@
9833
9834 /*
9835 ** CAPI3REF: Deserialize a database
9836 **
9837 ** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
9838 ** [database connection] D to disconnection from database S and then
9839 ** reopen S as an in-memory database based on the serialization contained
9840 ** in P. The serialized database P is N bytes in size. M is the size of
9841 ** the buffer P, which might be larger than N. If M is larger than N, and
9842 ** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
9843 ** permitted to add content to the in-memory database as long as the total
@@ -10974,23 +10981,22 @@
10974 SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
10975
10976 /*
10977 ** CAPI3REF: Apply A Changeset To A Database
10978 **
10979 ** Apply a changeset to a database. This function attempts to update the
10980 ** "main" database attached to handle db with the changes found in the
10981 ** changeset passed via the second and third arguments.
10982 **
10983 ** The fourth argument (xFilter) passed to this function is the "filter
10984 ** callback". If it is not NULL, then for each table affected by at least one
10985 ** change in the changeset, the filter callback is invoked with
10986 ** the table name as the second argument, and a copy of the context pointer
10987 ** passed as the sixth argument to this function as the first. If the "filter
10988 ** callback" returns zero, then no attempt is made to apply any changes to
10989 ** the table. Otherwise, if the return value is non-zero or the xFilter
10990 ** argument to this function is NULL, all changes related to the table are
10991 ** attempted.
10992 **
10993 ** For each table that is not excluded by the filter callback, this function
10994 ** tests that the target database contains a compatible table. A table is
10995 ** considered compatible if all of the following are true:
10996 **
@@ -11031,11 +11037,11 @@
11031 ** the documentation for the three
11032 ** [SQLITE_CHANGESET_OMIT|available return values] for details.
11033 **
11034 ** <dl>
11035 ** <dt>DELETE Changes<dd>
11036 ** For each DELETE change, this function checks if the target database
11037 ** contains a row with the same primary key value (or values) as the
11038 ** original row values stored in the changeset. If it does, and the values
11039 ** stored in all non-primary key columns also match the values stored in
11040 ** the changeset the row is deleted from the target database.
11041 **
@@ -11076,11 +11082,11 @@
11076 ** This includes the case where the INSERT operation is re-attempted because
11077 ** an earlier call to the conflict handler function returned
11078 ** [SQLITE_CHANGESET_REPLACE].
11079 **
11080 ** <dt>UPDATE Changes<dd>
11081 ** For each UPDATE change, this function checks if the target database
11082 ** contains a row with the same primary key value (or values) as the
11083 ** original row values stored in the changeset. If it does, and the values
11084 ** stored in all modified non-primary key columns also match the values
11085 ** stored in the changeset the row is updated within the target database.
11086 **
@@ -11107,15 +11113,25 @@
11107 ** It is safe to execute SQL statements, including those that write to the
11108 ** table that the callback related to, from within the xConflict callback.
11109 ** This can be used to further customize the applications conflict
11110 ** resolution strategy.
11111 **
11112 ** All changes made by this function are enclosed in a savepoint transaction.
11113 ** If any other error (aside from a constraint failure when attempting to
11114 ** write to the target database) occurs, then the savepoint transaction is
11115 ** rolled back, restoring the target database to its original state, and an
11116 ** SQLite error code returned.
 
 
 
 
 
 
 
 
 
 
11117 */
11118 SQLITE_API int sqlite3changeset_apply(
11119 sqlite3 *db, /* Apply change to "main" db of this handle */
11120 int nChangeset, /* Size of changeset in bytes */
11121 void *pChangeset, /* Changeset blob */
@@ -11127,10 +11143,26 @@
11127 void *pCtx, /* Copy of sixth arg to _apply() */
11128 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
11129 sqlite3_changeset_iter *p /* Handle describing change and conflict */
11130 ),
11131 void *pCtx /* First argument passed to xConflict */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11132 );
11133
11134 /*
11135 ** CAPI3REF: Constants Passed To The Conflict Handler
11136 **
@@ -11225,10 +11257,165 @@
11225 */
11226 #define SQLITE_CHANGESET_OMIT 0
11227 #define SQLITE_CHANGESET_REPLACE 1
11228 #define SQLITE_CHANGESET_ABORT 2
11229
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11230 /*
11231 ** CAPI3REF: Streaming Versions of API functions.
11232 **
11233 ** The six streaming API xxx_strm() functions serve similar purposes to the
11234 ** corresponding non-streaming API functions:
@@ -11328,10 +11515,26 @@
11328 void *pCtx, /* Copy of sixth arg to _apply() */
11329 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
11330 sqlite3_changeset_iter *p /* Handle describing change and conflict */
11331 ),
11332 void *pCtx /* First argument passed to xConflict */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11333 );
11334 SQLITE_API int sqlite3changeset_concat_strm(
11335 int (*xInputA)(void *pIn, void *pData, int *pnData),
11336 void *pInA,
11337 int (*xInputB)(void *pIn, void *pData, int *pnData),
@@ -11365,10 +11568,17 @@
11365 void *pIn
11366 );
11367 SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
11368 int (*xOutput)(void *pOut, const void *pData, int nData),
11369 void *pOut
 
 
 
 
 
 
 
11370 );
11371
11372
11373 /*
11374 ** Make sure we can call this stuff from C++.
@@ -13245,13 +13455,14 @@
13245 ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
13246 ** callback is currently invoked only from within pager.c.
13247 */
13248 typedef struct BusyHandler BusyHandler;
13249 struct BusyHandler {
13250 int (*xFunc)(void *,int); /* The busy callback */
13251 void *pArg; /* First arg to busy callback */
13252 int nBusy; /* Incremented with each busy call */
 
13253 };
13254
13255 /*
13256 ** Name of the master database table. The master database table
13257 ** is a special table that holds the names and attributes of all
@@ -14456,11 +14667,11 @@
14456 );
14457 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3*);
14458 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
14459
14460 /* Functions used to configure a Pager object. */
14461 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
14462 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
14463 #ifdef SQLITE_HAS_CODEC
14464 SQLITE_PRIVATE void sqlite3PagerAlignReserve(Pager*,Pager*);
14465 #endif
14466 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
@@ -14542,10 +14753,15 @@
14542 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
14543 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
14544 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
14545 SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*);
14546 SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
 
 
 
 
 
14547
14548 /* Functions used to truncate the database file. */
14549 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
14550
14551 SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16);
@@ -18099,11 +18315,11 @@
18099 SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
18100 SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
18101 SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
18102 SQLITE_PRIVATE char sqlite3AffinityType(const char*, u8*);
18103 SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
18104 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
18105 SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
18106 SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
18107 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
18108 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
18109 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
@@ -21070,11 +21286,13 @@
21070 ** is only a hint and can be safely ignored. The sqlite3OsFileControlHint()
21071 ** routine has no return value since the return value would be meaningless.
21072 */
21073 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
21074 #ifdef SQLITE_TEST
21075 if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){
 
 
21076 /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
21077 ** is using a regular VFS, it is called after the corresponding
21078 ** transaction has been committed. Injecting a fault at this point
21079 ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
21080 ** but the transaction is committed anyway.
@@ -31026,10 +31244,13 @@
31026 int openFlags; /* The flags specified at open() */
31027 #endif
31028 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
31029 unsigned fsFlags; /* cached details from statfs() */
31030 #endif
 
 
 
31031 #if OS_VXWORKS
31032 struct vxworksFileId *pId; /* Unique file ID */
31033 #endif
31034 #ifdef SQLITE_DEBUG
31035 /* The next group of variables are used to track whether or not the
@@ -32459,10 +32680,47 @@
32459 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
32460
32461 *pResOut = reserved;
32462 return rc;
32463 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32464
32465 /*
32466 ** Attempt to set a system-lock on the file pFile. The lock is
32467 ** described by pLock.
32468 **
@@ -32492,19 +32750,19 @@
32492 assert( pInode->nLock==0 );
32493 lock.l_whence = SEEK_SET;
32494 lock.l_start = SHARED_FIRST;
32495 lock.l_len = SHARED_SIZE;
32496 lock.l_type = F_WRLCK;
32497 rc = osFcntl(pFile->h, F_SETLK, &lock);
32498 if( rc<0 ) return rc;
32499 pInode->bProcessLock = 1;
32500 pInode->nLock++;
32501 }else{
32502 rc = 0;
32503 }
32504 }else{
32505 rc = osFcntl(pFile->h, F_SETLK, pLock);
32506 }
32507 return rc;
32508 }
32509
32510 /*
@@ -34860,10 +35118,16 @@
34860 }
34861 case SQLITE_FCNTL_HAS_MOVED: {
34862 *(int*)pArg = fileHasMoved(pFile);
34863 return SQLITE_OK;
34864 }
 
 
 
 
 
 
34865 #if SQLITE_MAX_MMAP_SIZE>0
34866 case SQLITE_FCNTL_MMAP_SIZE: {
34867 i64 newLimit = *(i64*)pArg;
34868 int rc = SQLITE_OK;
34869 if( newLimit>sqlite3GlobalConfig.mxMmap ){
@@ -35179,11 +35443,11 @@
35179 /* Initialize the locking parameters */
35180 f.l_type = lockType;
35181 f.l_whence = SEEK_SET;
35182 f.l_start = ofst;
35183 f.l_len = n;
35184 rc = osFcntl(pShmNode->h, F_SETLK, &f);
35185 rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
35186 }
35187
35188 /* Update the global lock state and do debug tracing */
35189 #ifdef SQLITE_DEBUG
@@ -45039,11 +45303,11 @@
45039 ** May you find forgiveness for yourself and forgive others.
45040 ** May you share freely, never taking more than you give.
45041 **
45042 ******************************************************************************
45043 **
45044 ** This file implements in-memory VFS. A database is held as a contiguous
45045 ** block of memory.
45046 **
45047 ** This file also implements interface sqlite3_serialize() and
45048 ** sqlite3_deserialize().
45049 */
@@ -52527,11 +52791,11 @@
52527 **
52528 ** If the busy-handler callback returns non-zero, the lock is
52529 ** retried. If it returns zero, then the SQLITE_BUSY error is
52530 ** returned to the caller of the pager API function.
52531 */
52532 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
52533 Pager *pPager, /* Pager object */
52534 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
52535 void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
52536 ){
52537 pPager->xBusyHandler = xBusyHandler;
@@ -54516,10 +54780,11 @@
54516 Pager *pPager;
54517 assert( pPg!=0 );
54518 assert( pPg->pgno==1 );
54519 assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
54520 pPager = pPg->pPager;
 
54521 sqlite3PcacheRelease(pPg);
54522 pagerUnlockIfUnused(pPager);
54523 }
54524
54525 /*
@@ -55793,10 +56058,22 @@
55793 */
55794 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
55795 return pPager->fd;
55796 }
55797
 
 
 
 
 
 
 
 
 
 
 
 
55798 /*
55799 ** Return the file handle for the journal file (if it exists).
55800 ** This will be either the rollback journal or the WAL file.
55801 */
55802 SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){
@@ -56253,10 +56530,11 @@
56253 (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
56254 pPager->pBusyHandlerArg,
56255 pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
56256 pnLog, pnCkpt
56257 );
 
56258 }
56259 return rc;
56260 }
56261
56262 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
@@ -63529,11 +63807,12 @@
63529 */
63530 static int btreeInvokeBusyHandler(void *pArg){
63531 BtShared *pBt = (BtShared*)pArg;
63532 assert( pBt->db );
63533 assert( sqlite3_mutex_held(pBt->db->mutex) );
63534 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
 
63535 }
63536
63537 /*
63538 ** Open a database file.
63539 **
@@ -63707,11 +63986,11 @@
63707 if( rc!=SQLITE_OK ){
63708 goto btree_open_out;
63709 }
63710 pBt->openFlags = (u8)flags;
63711 pBt->db = db;
63712 sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
63713 p->pBt = pBt;
63714
63715 pBt->pCursor = 0;
63716 pBt->pPage1 = 0;
63717 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
@@ -64670,10 +64949,11 @@
64670 if( rc!=SQLITE_OK ){
64671 unlockBtreeIfUnused(pBt);
64672 }
64673 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
64674 btreeInvokeBusyHandler(pBt) );
 
64675
64676 if( rc==SQLITE_OK ){
64677 if( p->inTrans==TRANS_NONE ){
64678 pBt->nTransaction++;
64679 #ifndef SQLITE_OMIT_SHARED_CACHE
@@ -98715,20 +98995,35 @@
98715 ** This is the Expr node callback for sqlite3ExprImpliesNotNullRow().
98716 ** If the expression node requires that the table at pWalker->iCur
98717 ** have a non-NULL column, then set pWalker->eCode to 1 and abort.
98718 */
98719 static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
 
 
 
 
 
 
 
 
 
98720 if( ExprHasProperty(pExpr, EP_FromJoin) ) return WRC_Prune;
98721 switch( pExpr->op ){
98722 case TK_ISNULL:
98723 case TK_IS:
98724 case TK_OR:
 
 
98725 case TK_FUNCTION:
98726 case TK_AGG_FUNCTION:
 
 
 
 
 
98727 return WRC_Prune;
98728 case TK_COLUMN:
98729 case TK_AGG_COLUMN:
98730 if( pWalker->u.iCur==pExpr->iTable ){
98731 pWalker->eCode = 1;
98732 return WRC_Abort;
98733 }
98734 return WRC_Prune;
@@ -98740,10 +99035,17 @@
98740 /*
98741 ** Return true (non-zero) if expression p can only be true if at least
98742 ** one column of table iTab is non-null. In other words, return true
98743 ** if expression p will always be NULL or false if every column of iTab
98744 ** is NULL.
 
 
 
 
 
 
 
98745 **
98746 ** Terms of p that are marked with EP_FromJoin (and hence that come from
98747 ** the ON or USING clauses of LEFT JOINS) are excluded from the analysis.
98748 **
98749 ** This routine is used to check if a LEFT JOIN can be converted into
@@ -101005,11 +101307,11 @@
101005 }
101006 if( pTab->tnum==0 ){
101007 /* Do not gather statistics on views or virtual tables */
101008 return;
101009 }
101010 if( sqlite3_strlike("sqlite_%", pTab->zName, 0)==0 ){
101011 /* Do not gather statistics on system tables */
101012 return;
101013 }
101014 assert( sqlite3BtreeHoldsAllMutexes(db) );
101015 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
@@ -104066,11 +104368,11 @@
104066 if( !sqlite3ExprIsConstantOrFunction(pExpr, db->init.busy) ){
104067 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
104068 pCol->zName);
104069 }else{
104070 /* A copy of pExpr is used instead of the original, as pExpr contains
104071 ** tokens that point to volatile memory.
104072 */
104073 Expr x;
104074 sqlite3ExprDelete(db, pCol->pDflt);
104075 memset(&x, 0, sizeof(x));
104076 x.op = TK_SPAN;
@@ -104988,11 +105290,11 @@
104988 Table *pSelTab; /* A fake table from which we get the result set */
104989 Select *pSel; /* Copy of the SELECT that implements the view */
104990 int nErr = 0; /* Number of errors encountered */
104991 int n; /* Temporarily holds the number of cursors assigned */
104992 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
104993 #ifndef SQLITE_OMIT_VIRTUALTABLE
104994 int rc;
104995 #endif
104996 #ifndef SQLITE_OMIT_AUTHORIZATION
104997 sqlite3_xauth xAuth; /* Saved xAuth pointer */
104998 #endif
@@ -123778,12 +124080,13 @@
123778 ** (2) The inner query is the recursive part of a common table expression.
123779 **
123780 ** (3) The inner query has a LIMIT clause (since the changes to the WHERE
123781 ** close would change the meaning of the LIMIT).
123782 **
123783 ** (4) (** This restriction was removed on 2018-03-21. It used to read:
123784 ** The inner query is the right operand of a LEFT JOIN. **)
 
123785 **
123786 ** (5) The WHERE clause expression originates in the ON or USING clause
123787 ** of a LEFT JOIN where iCursor is not the right-hand table of that
123788 ** left join. An example:
123789 **
@@ -123801,11 +124104,12 @@
123801 */
123802 static int pushDownWhereTerms(
123803 Parse *pParse, /* Parse context (for malloc() and error reporting) */
123804 Select *pSubq, /* The subquery whose WHERE clause is to be augmented */
123805 Expr *pWhere, /* The WHERE clause of the outer query */
123806 int iCursor /* Cursor number of the subquery */
 
123807 ){
123808 Expr *pNew;
123809 int nChng = 0;
123810 if( pWhere==0 ) return 0;
123811 if( pSubq->selFlags & SF_Recursive ) return 0; /* restriction (2) */
@@ -123825,13 +124129,20 @@
123825
123826 if( pSubq->pLimit!=0 ){
123827 return 0; /* restriction (3) */
123828 }
123829 while( pWhere->op==TK_AND ){
123830 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, iCursor);
 
123831 pWhere = pWhere->pLeft;
123832 }
 
 
 
 
 
 
123833 if( ExprHasProperty(pWhere,EP_FromJoin) && pWhere->iRightJoinTable!=iCursor ){
123834 return 0; /* restriction (5) */
123835 }
123836 if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
123837 nChng++;
@@ -125295,11 +125606,12 @@
125295
125296 /* Make copies of constant WHERE-clause terms in the outer query down
125297 ** inside the subquery. This can help the subquery to run more efficiently.
125298 */
125299 if( OptimizationEnabled(db, SQLITE_PushDown)
125300 && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor)
 
125301 ){
125302 #if SELECTTRACE_ENABLED
125303 if( sqlite3SelectTrace & 0x100 ){
125304 SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n"));
125305 sqlite3TreeViewSelect(0, p, 0);
@@ -146146,43 +146458,62 @@
146146 /*
146147 ** This routine implements a busy callback that sleeps and tries
146148 ** again until a timeout value is reached. The timeout value is
146149 ** an integer number of milliseconds passed in as the first
146150 ** argument.
 
 
 
146151 */
146152 static int sqliteDefaultBusyCallback(
146153 void *ptr, /* Database connection */
146154 int count /* Number of times table has been busy */
 
146155 ){
146156 #if SQLITE_OS_WIN || HAVE_USLEEP
 
 
146157 static const u8 delays[] =
146158 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
146159 static const u8 totals[] =
146160 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
146161 # define NDELAY ArraySize(delays)
146162 sqlite3 *db = (sqlite3 *)ptr;
146163 int timeout = db->busyTimeout;
146164 int delay, prior;
146165
 
 
 
 
 
 
 
 
 
 
 
146166 assert( count>=0 );
146167 if( count < NDELAY ){
146168 delay = delays[count];
146169 prior = totals[count];
146170 }else{
146171 delay = delays[NDELAY-1];
146172 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
146173 }
146174 if( prior + delay > timeout ){
146175 delay = timeout - prior;
146176 if( delay<=0 ) return 0;
146177 }
146178 sqlite3OsSleep(db->pVfs, delay*1000);
146179 return 1;
146180 #else
 
 
146181 sqlite3 *db = (sqlite3 *)ptr;
146182 int timeout = ((sqlite3 *)ptr)->busyTimeout;
146183 if( (count+1)*1000 > timeout ){
146184 return 0;
146185 }
146186 sqlite3OsSleep(db->pVfs, 1000000);
146187 return 1;
146188 #endif
@@ -146189,18 +146520,29 @@
146189 }
146190
146191 /*
146192 ** Invoke the given busy handler.
146193 **
146194 ** This routine is called when an operation failed with a lock.
 
 
146195 ** If this routine returns non-zero, the lock is retried. If it
146196 ** returns 0, the operation aborts with an SQLITE_BUSY error.
146197 */
146198 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
146199 int rc;
146200 if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
146201 rc = p->xFunc(p->pArg, p->nBusy);
 
 
 
 
 
 
 
 
 
146202 if( rc==0 ){
146203 p->nBusy = -1;
146204 }else{
146205 p->nBusy++;
146206 }
@@ -146218,13 +146560,14 @@
146218 ){
146219 #ifdef SQLITE_ENABLE_API_ARMOR
146220 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
146221 #endif
146222 sqlite3_mutex_enter(db->mutex);
146223 db->busyHandler.xFunc = xBusy;
146224 db->busyHandler.pArg = pArg;
146225 db->busyHandler.nBusy = 0;
 
146226 db->busyTimeout = 0;
146227 sqlite3_mutex_leave(db->mutex);
146228 return SQLITE_OK;
146229 }
146230
@@ -146268,12 +146611,14 @@
146268 SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
146269 #ifdef SQLITE_ENABLE_API_ARMOR
146270 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
146271 #endif
146272 if( ms>0 ){
146273 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
 
146274 db->busyTimeout = ms;
 
146275 }else{
146276 sqlite3_busy_handler(db, 0, 0);
146277 }
146278 return SQLITE_OK;
146279 }
@@ -180266,12 +180611,12 @@
180266 ** of a patchset change is associated with the correspondingly positioned
180267 ** table column, counting from left to right within the CREATE TABLE
180268 ** statement.
180269 **
180270 ** For a DELETE change, all fields within the record except those associated
180271 ** with PRIMARY KEY columns are set to "undefined". The PRIMARY KEY fields
180272 ** contain the values identifying the row to delete.
180273 **
180274 ** For an UPDATE change, all fields except those associated with PRIMARY KEY
180275 ** columns and columns that are modified by the UPDATE are set to "undefined".
180276 ** PRIMARY KEY fields contain the values identifying the table row to update,
180277 ** and fields associated with modified columns contain the new column values.
@@ -180550,11 +180895,11 @@
180550 ** the type byte).
180551 */
180552 static int sessionSerialLen(u8 *a){
180553 int e = *a;
180554 int n;
180555 if( e==0 ) return 1;
180556 if( e==SQLITE_NULL ) return 1;
180557 if( e==SQLITE_INTEGER || e==SQLITE_FLOAT ) return 9;
180558 return sessionVarintGet(&a[1], &n) + 1 + n;
180559 }
180560
@@ -180630,11 +180975,11 @@
180630 for(iCol=0; iCol<pTab->nCol; iCol++){
180631 if( pTab->abPK[iCol] ){
180632 int n1 = sessionSerialLen(a1);
180633 int n2 = sessionSerialLen(a2);
180634
180635 if( pTab->abPK[iCol] && (n1!=n2 || memcmp(a1, a2, n1)) ){
180636 return 0;
180637 }
180638 a1 += n1;
180639 a2 += n2;
180640 }else{
@@ -180873,11 +181218,11 @@
180873 if( eType==SQLITE_TEXT ){
180874 z = sqlite3_value_text(pVal);
180875 }else{
180876 z = sqlite3_value_blob(pVal);
180877 }
180878 if( memcmp(a, z, n) ) return 0;
180879 a += n;
180880 }
180881 }
180882 }
180883
@@ -182217,10 +182562,11 @@
182217 if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
182218 zSql = sqlite3_mprintf(
182219 "SELECT tbl, ?2, stat FROM %Q.sqlite_stat1 WHERE tbl IS ?1 AND "
182220 "idx IS (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", zDb
182221 );
 
182222 }else{
182223 int i;
182224 const char *zSep = "";
182225 SessionBuffer buf = {0, 0, 0};
182226
@@ -182952,11 +183298,12 @@
182952 ** changes in the changeset.
182953 */
182954 static int sessionChangesetNext(
182955 sqlite3_changeset_iter *p, /* Changeset iterator */
182956 u8 **paRec, /* If non-NULL, store record pointer here */
182957 int *pnRec /* If non-NULL, store size of record here */
 
182958 ){
182959 int i;
182960 u8 op;
182961
182962 assert( (paRec==0 && pnRec==0) || (paRec && pnRec) );
@@ -182987,10 +183334,11 @@
182987 sessionDiscardData(&p->in);
182988 p->in.iCurrent = p->in.iNext;
182989
182990 op = p->in.aData[p->in.iNext++];
182991 while( op=='T' || op=='P' ){
 
182992 p->bPatchset = (op=='P');
182993 if( sessionChangesetReadTblhdr(p) ) return p->rc;
182994 if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
182995 p->in.iCurrent = p->in.iNext;
182996 if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
@@ -183065,11 +183413,11 @@
183065 **
183066 ** This function may not be called on iterators passed to a conflict handler
183067 ** callback by changeset_apply().
183068 */
183069 SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *p){
183070 return sessionChangesetNext(p, 0, 0);
183071 }
183072
183073 /*
183074 ** The following function extracts information on the current change
183075 ** from a changeset iterator. It may only be called after changeset_next()
@@ -183444,10 +183792,12 @@
183444 const char **azCol; /* Array of column names */
183445 u8 *abPK; /* Boolean array - true if column is in PK */
183446 int bStat1; /* True if table is sqlite_stat1 */
183447 int bDeferConstraints; /* True to defer constraints */
183448 SessionBuffer constraints; /* Deferred constraints are stored here */
 
 
183449 };
183450
183451 /*
183452 ** Formulate a statement to DELETE a row from database db. Assuming a table
183453 ** structure like this:
@@ -183710,11 +184060,10 @@
183710 "DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS "
183711 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END "
183712 "AND (?4 OR stat IS ?3)"
183713 );
183714 }
183715 assert( rc==SQLITE_OK );
183716 return rc;
183717 }
183718
183719 /*
183720 ** A wrapper around sqlite3_bind_value() that detects an extra problem.
@@ -183824,10 +184173,58 @@
183824 if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect);
183825 }
183826
183827 return rc;
183828 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183829
183830 /*
183831 ** Invoke the conflict handler for the change that the changeset iterator
183832 ** currently points to.
183833 **
@@ -183900,11 +184297,11 @@
183900 /* Instead of invoking the conflict handler, append the change blob
183901 ** to the SessionApplyCtx.constraints buffer. */
183902 u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
183903 int nBlob = pIter->in.iNext - pIter->in.iCurrent;
183904 sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
183905 res = SQLITE_CHANGESET_OMIT;
183906 }else{
183907 /* No other row with the new.* primary key. */
183908 res = xConflict(pCtx, eType+1, pIter);
183909 if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE;
183910 }
@@ -183926,10 +184323,13 @@
183926
183927 default:
183928 rc = SQLITE_MISUSE;
183929 break;
183930 }
 
 
 
183931 }
183932
183933 return rc;
183934 }
183935
@@ -184101,46 +184501,46 @@
184101 int bReplace = 0;
184102 int bRetry = 0;
184103 int rc;
184104
184105 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
184106 assert( rc==SQLITE_OK || (bRetry==0 && bReplace==0) );
184107
184108 /* If the bRetry flag is set, the change has not been applied due to an
184109 ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
184110 ** a row with the correct PK is present in the db, but one or more other
184111 ** fields do not contain the expected values) and the conflict handler
184112 ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
184113 ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
184114 ** the SQLITE_CHANGESET_DATA problem. */
184115 if( bRetry ){
184116 assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
184117 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
184118 }
184119
184120 /* If the bReplace flag is set, the change is an INSERT that has not
184121 ** been performed because the database already contains a row with the
184122 ** specified primary key and the conflict handler returned
184123 ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
184124 ** before reattempting the INSERT. */
184125 else if( bReplace ){
184126 assert( pIter->op==SQLITE_INSERT );
184127 rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
184128 if( rc==SQLITE_OK ){
184129 rc = sessionBindRow(pIter,
184130 sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
184131 sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
184132 }
184133 if( rc==SQLITE_OK ){
184134 sqlite3_step(pApply->pDelete);
184135 rc = sqlite3_reset(pApply->pDelete);
184136 }
184137 if( rc==SQLITE_OK ){
184138 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
184139 }
184140 if( rc==SQLITE_OK ){
184141 rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
184142 }
184143 }
184144
184145 return rc;
184146 }
@@ -184212,11 +184612,12 @@
184212 int(*xConflict)(
184213 void *pCtx, /* Copy of fifth arg to _apply() */
184214 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
184215 sqlite3_changeset_iter *p /* Handle describing change and conflict */
184216 ),
184217 void *pCtx /* First argument passed to xConflict */
 
184218 ){
184219 int schemaMismatch = 0;
184220 int rc; /* Return code */
184221 const char *zTab = 0; /* Name of current table */
184222 int nTab = 0; /* Result of sqlite3Strlen30(zTab) */
@@ -184250,13 +184651,22 @@
184250 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
184251 sqlite3_finalize(sApply.pDelete);
184252 sqlite3_finalize(sApply.pUpdate);
184253 sqlite3_finalize(sApply.pInsert);
184254 sqlite3_finalize(sApply.pSelect);
184255 memset(&sApply, 0, sizeof(sApply));
184256 sApply.db = db;
 
 
 
 
 
 
 
 
184257 sApply.bDeferConstraints = 1;
 
 
184258
184259 /* If an xFilter() callback was specified, invoke it now. If the
184260 ** xFilter callback returns zero, skip this table. If it returns
184261 ** non-zero, proceed. */
184262 schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
@@ -184362,19 +184772,55 @@
184362 }else{
184363 sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
184364 sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
184365 }
184366
 
 
 
 
 
184367 sqlite3_finalize(sApply.pInsert);
184368 sqlite3_finalize(sApply.pDelete);
184369 sqlite3_finalize(sApply.pUpdate);
184370 sqlite3_finalize(sApply.pSelect);
184371 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
184372 sqlite3_free((char*)sApply.constraints.aBuf);
 
184373 sqlite3_mutex_leave(sqlite3_db_mutex(db));
184374 return rc;
184375 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184376
184377 /*
184378 ** Apply the changeset passed via pChangeset/nChangeset to the main database
184379 ** attached to handle "db". Invoke the supplied conflict handler callback
184380 ** to resolve any conflicts encountered while applying the change.
@@ -184392,23 +184838,45 @@
184392 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
184393 sqlite3_changeset_iter *p /* Handle describing change and conflict */
184394 ),
184395 void *pCtx /* First argument passed to xConflict */
184396 ){
184397 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
184398 int rc = sqlite3changeset_start(&pIter, nChangeset, pChangeset);
184399 if( rc==SQLITE_OK ){
184400 rc = sessionChangesetApply(db, pIter, xFilter, xConflict, pCtx);
184401 }
184402 return rc;
184403 }
184404
184405 /*
184406 ** Apply the changeset passed via xInput/pIn to the main database
184407 ** attached to handle "db". Invoke the supplied conflict handler callback
184408 ** to resolve any conflicts encountered while applying the change.
184409 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184410 SQLITE_API int sqlite3changeset_apply_strm(
184411 sqlite3 *db, /* Apply change to "main" db of this handle */
184412 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
184413 void *pIn, /* First arg for xInput */
184414 int(*xFilter)(
@@ -184420,16 +184888,13 @@
184420 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
184421 sqlite3_changeset_iter *p /* Handle describing change and conflict */
184422 ),
184423 void *pCtx /* First argument passed to xConflict */
184424 ){
184425 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
184426 int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
184427 if( rc==SQLITE_OK ){
184428 rc = sessionChangesetApply(db, pIter, xFilter, xConflict, pCtx);
184429 }
184430 return rc;
184431 }
184432
184433 /*
184434 ** sqlite3_changegroup handle.
184435 */
@@ -184444,31 +184909,90 @@
184444 ** part of an sqlite3changeset_concat() operation. A new change object is
184445 ** allocated and a pointer to it stored in *ppNew.
184446 */
184447 static int sessionChangeMerge(
184448 SessionTable *pTab, /* Table structure */
 
184449 int bPatchset, /* True for patchsets */
184450 SessionChange *pExist, /* Existing change */
184451 int op2, /* Second change operation */
184452 int bIndirect, /* True if second change is indirect */
184453 u8 *aRec, /* Second change record */
184454 int nRec, /* Number of bytes in aRec */
184455 SessionChange **ppNew /* OUT: Merged change */
184456 ){
184457 SessionChange *pNew = 0;
 
184458
184459 if( !pExist ){
184460 pNew = (SessionChange *)sqlite3_malloc(sizeof(SessionChange) + nRec);
184461 if( !pNew ){
184462 return SQLITE_NOMEM;
184463 }
184464 memset(pNew, 0, sizeof(SessionChange));
184465 pNew->op = op2;
184466 pNew->bIndirect = bIndirect;
184467 pNew->nRecord = nRec;
184468 pNew->aRecord = (u8*)&pNew[1];
184469 memcpy(pNew->aRecord, aRec, nRec);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184470 }else{
184471 int op1 = pExist->op;
184472
184473 /*
184474 ** op1=INSERT, op2=INSERT -> Unsupported. Discard op2.
@@ -184558,28 +185082,28 @@
184558 sqlite3_free(pExist);
184559 }
184560 }
184561
184562 *ppNew = pNew;
184563 return SQLITE_OK;
184564 }
184565
184566 /*
184567 ** Add all changes in the changeset traversed by the iterator passed as
184568 ** the first argument to the changegroup hash tables.
184569 */
184570 static int sessionChangesetToHash(
184571 sqlite3_changeset_iter *pIter, /* Iterator to read from */
184572 sqlite3_changegroup *pGrp /* Changegroup object to add changeset to */
 
184573 ){
184574 u8 *aRec;
184575 int nRec;
184576 int rc = SQLITE_OK;
184577 SessionTable *pTab = 0;
184578
184579
184580 while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec) ){
184581 const char *zNew;
184582 int nCol;
184583 int op;
184584 int iHash;
184585 int bIndirect;
@@ -184655,11 +185179,11 @@
184655 pTab->nEntry--;
184656 break;
184657 }
184658 }
184659
184660 rc = sessionChangeMerge(pTab,
184661 pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
184662 );
184663 if( rc ) break;
184664 if( pChange ){
184665 pChange->pNext = pTab->apChange[iHash];
@@ -184763,11 +185287,11 @@
184763 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
184764 int rc; /* Return code */
184765
184766 rc = sqlite3changeset_start(&pIter, nData, pData);
184767 if( rc==SQLITE_OK ){
184768 rc = sessionChangesetToHash(pIter, pGrp);
184769 }
184770 sqlite3changeset_finalize(pIter);
184771 return rc;
184772 }
184773
@@ -184794,11 +185318,11 @@
184794 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
184795 int rc; /* Return code */
184796
184797 rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
184798 if( rc==SQLITE_OK ){
184799 rc = sessionChangesetToHash(pIter, pGrp);
184800 }
184801 sqlite3changeset_finalize(pIter);
184802 return rc;
184803 }
184804
@@ -184878,10 +185402,353 @@
184878 }
184879 sqlite3changegroup_delete(pGrp);
184880
184881 return rc;
184882 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184883
184884 #endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
184885
184886 /************** End of sqlite3session.c **************************************/
184887 /************** Begin file json1.c *******************************************/
@@ -204697,11 +205564,11 @@
204697 int nArg, /* Number of args */
204698 sqlite3_value **apUnused /* Function arguments */
204699 ){
204700 assert( nArg==0 );
204701 UNUSED_PARAM2(nArg, apUnused);
204702 sqlite3_result_text(pCtx, "fts5: 2018-03-22 17:13:44 eb4f452e354065d610ff57a6a9312ad119b6b0cc467f9dff105f0718bc27ef01", -1, SQLITE_TRANSIENT);
204703 }
204704
204705 static int fts5Init(sqlite3 *db){
204706 static const sqlite3_module fts5Mod = {
204707 /* iVersion */ 2,
@@ -208967,12 +209834,12 @@
208967 }
208968 #endif /* SQLITE_CORE */
208969 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
208970
208971 /************** End of stmt.c ************************************************/
208972 #if __LINE__!=208972
208973 #undef SQLITE_SOURCE_ID
208974 #define SQLITE_SOURCE_ID "2018-03-22 17:13:44 eb4f452e354065d610ff57a6a9312ad119b6b0cc467f9dff105f0718bc27alt2"
208975 #endif
208976 /* Return the source-id for this library */
208977 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
208978 /************************** End of sqlite3.c ******************************/
208979
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1147,11 +1147,11 @@
1147 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
1148 ** [sqlite_version()] and [sqlite_source_id()].
1149 */
1150 #define SQLITE_VERSION "3.23.0"
1151 #define SQLITE_VERSION_NUMBER 3023000
1152 #define SQLITE_SOURCE_ID "2018-03-28 15:56:55 eb29b3369e76ec1df25a5484d8ec5fb924e23d5c70aaa4d794b2b17ee187alt1"
1153
1154 /*
1155 ** CAPI3REF: Run-Time Library Version Numbers
1156 ** KEYWORDS: sqlite3_version sqlite3_sourceid
1157 **
@@ -2086,10 +2086,16 @@
2086 ** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
2087 ** ^This file control takes the file descriptor out of batch write mode
2088 ** so that all subsequent write operations are independent.
2089 ** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
2090 ** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
2091 **
2092 ** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
2093 ** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode causes attempts to obtain
2094 ** a file lock using the xLock or xShmLock methods of the VFS to wait
2095 ** for up to M milliseconds before failing, where M is the single
2096 ** unsigned integer parameter.
2097 ** </ul>
2098 */
2099 #define SQLITE_FCNTL_LOCKSTATE 1
2100 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
2101 #define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -2120,10 +2126,11 @@
2126 #define SQLITE_FCNTL_WIN32_GET_HANDLE 29
2127 #define SQLITE_FCNTL_PDB 30
2128 #define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE 31
2129 #define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE 32
2130 #define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE 33
2131 #define SQLITE_FCNTL_LOCK_TIMEOUT 34
2132
2133 /* deprecated names */
2134 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
2135 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
2136 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -9833,11 +9840,11 @@
9840
9841 /*
9842 ** CAPI3REF: Deserialize a database
9843 **
9844 ** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
9845 ** [database connection] D to disconnect from database S and then
9846 ** reopen S as an in-memory database based on the serialization contained
9847 ** in P. The serialized database P is N bytes in size. M is the size of
9848 ** the buffer P, which might be larger than N. If M is larger than N, and
9849 ** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
9850 ** permitted to add content to the in-memory database as long as the total
@@ -10974,23 +10981,22 @@
10981 SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
10982
10983 /*
10984 ** CAPI3REF: Apply A Changeset To A Database
10985 **
10986 ** Apply a changeset or patchset to a database. These functions attempt to
10987 ** update the "main" database attached to handle db with the changes found in
10988 ** the changeset passed via the second and third arguments.
10989 **
10990 ** The fourth argument (xFilter) passed to these functions is the "filter
10991 ** callback". If it is not NULL, then for each table affected by at least one
10992 ** change in the changeset, the filter callback is invoked with
10993 ** the table name as the second argument, and a copy of the context pointer
10994 ** passed as the sixth argument as the first. If the "filter callback"
10995 ** returns zero, then no attempt is made to apply any changes to the table.
10996 ** Otherwise, if the return value is non-zero or the xFilter argument to
10997 ** is NULL, all changes related to the table are attempted.
 
10998 **
10999 ** For each table that is not excluded by the filter callback, this function
11000 ** tests that the target database contains a compatible table. A table is
11001 ** considered compatible if all of the following are true:
11002 **
@@ -11031,11 +11037,11 @@
11037 ** the documentation for the three
11038 ** [SQLITE_CHANGESET_OMIT|available return values] for details.
11039 **
11040 ** <dl>
11041 ** <dt>DELETE Changes<dd>
11042 ** For each DELETE change, the function checks if the target database
11043 ** contains a row with the same primary key value (or values) as the
11044 ** original row values stored in the changeset. If it does, and the values
11045 ** stored in all non-primary key columns also match the values stored in
11046 ** the changeset the row is deleted from the target database.
11047 **
@@ -11076,11 +11082,11 @@
11082 ** This includes the case where the INSERT operation is re-attempted because
11083 ** an earlier call to the conflict handler function returned
11084 ** [SQLITE_CHANGESET_REPLACE].
11085 **
11086 ** <dt>UPDATE Changes<dd>
11087 ** For each UPDATE change, the function checks if the target database
11088 ** contains a row with the same primary key value (or values) as the
11089 ** original row values stored in the changeset. If it does, and the values
11090 ** stored in all modified non-primary key columns also match the values
11091 ** stored in the changeset the row is updated within the target database.
11092 **
@@ -11107,15 +11113,25 @@
11113 ** It is safe to execute SQL statements, including those that write to the
11114 ** table that the callback related to, from within the xConflict callback.
11115 ** This can be used to further customize the applications conflict
11116 ** resolution strategy.
11117 **
11118 ** All changes made by these functions are enclosed in a savepoint transaction.
11119 ** If any other error (aside from a constraint failure when attempting to
11120 ** write to the target database) occurs, then the savepoint transaction is
11121 ** rolled back, restoring the target database to its original state, and an
11122 ** SQLite error code returned.
11123 **
11124 ** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
11125 ** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
11126 ** may set (*ppRebase) to point to a "rebase" that may be used with the
11127 ** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
11128 ** is set to the size of the buffer in bytes. It is the responsibility of the
11129 ** caller to eventually free any such buffer using sqlite3_free(). The buffer
11130 ** is only allocated and populated if one or more conflicts were encountered
11131 ** while applying the patchset. See comments surrounding the sqlite3_rebaser
11132 ** APIs for further details.
11133 */
11134 SQLITE_API int sqlite3changeset_apply(
11135 sqlite3 *db, /* Apply change to "main" db of this handle */
11136 int nChangeset, /* Size of changeset in bytes */
11137 void *pChangeset, /* Changeset blob */
@@ -11127,10 +11143,26 @@
11143 void *pCtx, /* Copy of sixth arg to _apply() */
11144 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
11145 sqlite3_changeset_iter *p /* Handle describing change and conflict */
11146 ),
11147 void *pCtx /* First argument passed to xConflict */
11148 );
11149 SQLITE_API int sqlite3changeset_apply_v2(
11150 sqlite3 *db, /* Apply change to "main" db of this handle */
11151 int nChangeset, /* Size of changeset in bytes */
11152 void *pChangeset, /* Changeset blob */
11153 int(*xFilter)(
11154 void *pCtx, /* Copy of sixth arg to _apply() */
11155 const char *zTab /* Table name */
11156 ),
11157 int(*xConflict)(
11158 void *pCtx, /* Copy of sixth arg to _apply() */
11159 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
11160 sqlite3_changeset_iter *p /* Handle describing change and conflict */
11161 ),
11162 void *pCtx, /* First argument passed to xConflict */
11163 void **ppRebase, int *pnRebase
11164 );
11165
11166 /*
11167 ** CAPI3REF: Constants Passed To The Conflict Handler
11168 **
@@ -11225,10 +11257,165 @@
11257 */
11258 #define SQLITE_CHANGESET_OMIT 0
11259 #define SQLITE_CHANGESET_REPLACE 1
11260 #define SQLITE_CHANGESET_ABORT 2
11261
11262 /*
11263 ** CAPI3REF: Rebasing changesets
11264 ** EXPERIMENTAL
11265 **
11266 ** Suppose there is a site hosting a database in state S0. And that
11267 ** modifications are made that move that database to state S1 and a
11268 ** changeset recorded (the "local" changeset). Then, a changeset based
11269 ** on S0 is received from another site (the "remote" changeset) and
11270 ** applied to the database. The database is then in state
11271 ** (S1+"remote"), where the exact state depends on any conflict
11272 ** resolution decisions (OMIT or REPLACE) made while applying "remote".
11273 ** Rebasing a changeset is to update it to take those conflict
11274 ** resolution decisions into account, so that the same conflicts
11275 ** do not have to be resolved elsewhere in the network.
11276 **
11277 ** For example, if both the local and remote changesets contain an
11278 ** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
11279 **
11280 ** local: INSERT INTO t1 VALUES(1, 'v1');
11281 ** remote: INSERT INTO t1 VALUES(1, 'v2');
11282 **
11283 ** and the conflict resolution is REPLACE, then the INSERT change is
11284 ** removed from the local changeset (it was overridden). Or, if the
11285 ** conflict resolution was "OMIT", then the local changeset is modified
11286 ** to instead contain:
11287 **
11288 ** UPDATE t1 SET b = 'v2' WHERE a=1;
11289 **
11290 ** Changes within the local changeset are rebased as follows:
11291 **
11292 ** <dl>
11293 ** <dt>Local INSERT<dd>
11294 ** This may only conflict with a remote INSERT. If the conflict
11295 ** resolution was OMIT, then add an UPDATE change to the rebased
11296 ** changeset. Or, if the conflict resolution was REPLACE, add
11297 ** nothing to the rebased changeset.
11298 **
11299 ** <dt>Local DELETE<dd>
11300 ** This may conflict with a remote UPDATE or DELETE. In both cases the
11301 ** only possible resolution is OMIT. If the remote operation was a
11302 ** DELETE, then add no change to the rebased changeset. If the remote
11303 ** operation was an UPDATE, then the old.* fields of change are updated
11304 ** to reflect the new.* values in the UPDATE.
11305 **
11306 ** <dt>Local UPDATE<dd>
11307 ** This may conflict with a remote UPDATE or DELETE. If it conflicts
11308 ** with a DELETE, and the conflict resolution was OMIT, then the update
11309 ** is changed into an INSERT. Any undefined values in the new.* record
11310 ** from the update change are filled in using the old.* values from
11311 ** the conflicting DELETE. Or, if the conflict resolution was REPLACE,
11312 ** the UPDATE change is simply omitted from the rebased changeset.
11313 **
11314 ** If conflict is with a remote UPDATE and the resolution is OMIT, then
11315 ** the old.* values are rebased using the new.* values in the remote
11316 ** change. Or, if the resolution is REPLACE, then the change is copied
11317 ** into the rebased changeset with updates to columns also updated by
11318 ** the conflicting remote UPDATE removed. If this means no columns would
11319 ** be updated, the change is omitted.
11320 ** </dl>
11321 **
11322 ** A local change may be rebased against multiple remote changes
11323 ** simultaneously. If a single key is modified by multiple remote
11324 ** changesets, they are combined as follows before the local changeset
11325 ** is rebased:
11326 **
11327 ** <ul>
11328 ** <li> If there has been one or more REPLACE resolutions on a
11329 ** key, it is rebased according to a REPLACE.
11330 **
11331 ** <li> If there have been no REPLACE resolutions on a key, then
11332 ** the local changeset is rebased according to the most recent
11333 ** of the OMIT resolutions.
11334 ** </ul>
11335 **
11336 ** Note that conflict resolutions from multiple remote changesets are
11337 ** combined on a per-field basis, not per-row. This means that in the
11338 ** case of multiple remote UPDATE operations, some fields of a single
11339 ** local change may be rebased for REPLACE while others are rebased for
11340 ** OMIT.
11341 **
11342 ** In order to rebase a local changeset, the remote changeset must first
11343 ** be applied to the local database using sqlite3changeset_apply_v2() and
11344 ** the buffer of rebase information captured. Then:
11345 **
11346 ** <ol>
11347 ** <li> An sqlite3_rebaser object is created by calling
11348 ** sqlite3rebaser_create().
11349 ** <li> The new object is configured with the rebase buffer obtained from
11350 ** sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
11351 ** If the local changeset is to be rebased against multiple remote
11352 ** changesets, then sqlite3rebaser_configure() should be called
11353 ** multiple times, in the same order that the multiple
11354 ** sqlite3changeset_apply_v2() calls were made.
11355 ** <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
11356 ** <li> The sqlite3_rebaser object is deleted by calling
11357 ** sqlite3rebaser_delete().
11358 ** </ol>
11359 */
11360 typedef struct sqlite3_rebaser sqlite3_rebaser;
11361
11362 /*
11363 ** CAPI3REF: Create a changeset rebaser object.
11364 ** EXPERIMENTAL
11365 **
11366 ** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
11367 ** point to the new object and return SQLITE_OK. Otherwise, if an error
11368 ** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew)
11369 ** to NULL.
11370 */
11371 SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
11372
11373 /*
11374 ** CAPI3REF: Configure a changeset rebaser object.
11375 ** EXPERIMENTAL
11376 **
11377 ** Configure the changeset rebaser object to rebase changesets according
11378 ** to the conflict resolutions described by buffer pRebase (size nRebase
11379 ** bytes), which must have been obtained from a previous call to
11380 ** sqlite3changeset_apply_v2().
11381 */
11382 SQLITE_API int sqlite3rebaser_configure(
11383 sqlite3_rebaser*,
11384 int nRebase, const void *pRebase
11385 );
11386
11387 /*
11388 ** CAPI3REF: Rebase a changeset
11389 ** EXPERIMENTAL
11390 **
11391 ** Argument pIn must point to a buffer containing a changeset nIn bytes
11392 ** in size. This function allocates and populates a buffer with a copy
11393 ** of the changeset rebased rebased according to the configuration of the
11394 ** rebaser object passed as the first argument. If successful, (*ppOut)
11395 ** is set to point to the new buffer containing the rebased changset and
11396 ** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
11397 ** responsibility of the caller to eventually free the new buffer using
11398 ** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
11399 ** are set to zero and an SQLite error code returned.
11400 */
11401 SQLITE_API int sqlite3rebaser_rebase(
11402 sqlite3_rebaser*,
11403 int nIn, const void *pIn,
11404 int *pnOut, void **ppOut
11405 );
11406
11407 /*
11408 ** CAPI3REF: Delete a changeset rebaser object.
11409 ** EXPERIMENTAL
11410 **
11411 ** Delete the changeset rebaser object and all associated resources. There
11412 ** should be one call to this function for each successful invocation
11413 ** of sqlite3rebaser_create().
11414 */
11415 SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p);
11416
11417 /*
11418 ** CAPI3REF: Streaming Versions of API functions.
11419 **
11420 ** The six streaming API xxx_strm() functions serve similar purposes to the
11421 ** corresponding non-streaming API functions:
@@ -11328,10 +11515,26 @@
11515 void *pCtx, /* Copy of sixth arg to _apply() */
11516 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
11517 sqlite3_changeset_iter *p /* Handle describing change and conflict */
11518 ),
11519 void *pCtx /* First argument passed to xConflict */
11520 );
11521 SQLITE_API int sqlite3changeset_apply_v2_strm(
11522 sqlite3 *db, /* Apply change to "main" db of this handle */
11523 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
11524 void *pIn, /* First arg for xInput */
11525 int(*xFilter)(
11526 void *pCtx, /* Copy of sixth arg to _apply() */
11527 const char *zTab /* Table name */
11528 ),
11529 int(*xConflict)(
11530 void *pCtx, /* Copy of sixth arg to _apply() */
11531 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
11532 sqlite3_changeset_iter *p /* Handle describing change and conflict */
11533 ),
11534 void *pCtx, /* First argument passed to xConflict */
11535 void **ppRebase, int *pnRebase
11536 );
11537 SQLITE_API int sqlite3changeset_concat_strm(
11538 int (*xInputA)(void *pIn, void *pData, int *pnData),
11539 void *pInA,
11540 int (*xInputB)(void *pIn, void *pData, int *pnData),
@@ -11365,10 +11568,17 @@
11568 void *pIn
11569 );
11570 SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
11571 int (*xOutput)(void *pOut, const void *pData, int nData),
11572 void *pOut
11573 );
11574 SQLITE_API int sqlite3rebaser_rebase_strm(
11575 sqlite3_rebaser *pRebaser,
11576 int (*xInput)(void *pIn, void *pData, int *pnData),
11577 void *pIn,
11578 int (*xOutput)(void *pOut, const void *pData, int nData),
11579 void *pOut
11580 );
11581
11582
11583 /*
11584 ** Make sure we can call this stuff from C++.
@@ -13245,13 +13455,14 @@
13455 ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
13456 ** callback is currently invoked only from within pager.c.
13457 */
13458 typedef struct BusyHandler BusyHandler;
13459 struct BusyHandler {
13460 int (*xBusyHandler)(void *,int); /* The busy callback */
13461 void *pBusyArg; /* First arg to busy callback */
13462 int nBusy; /* Incremented with each busy call */
13463 u8 bExtraFileArg; /* Include sqlite3_file as callback arg */
13464 };
13465
13466 /*
13467 ** Name of the master database table. The master database table
13468 ** is a special table that holds the names and attributes of all
@@ -14456,11 +14667,11 @@
14667 );
14668 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3*);
14669 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
14670
14671 /* Functions used to configure a Pager object. */
14672 SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *);
14673 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
14674 #ifdef SQLITE_HAS_CODEC
14675 SQLITE_PRIVATE void sqlite3PagerAlignReserve(Pager*,Pager*);
14676 #endif
14677 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
@@ -14542,10 +14753,15 @@
14753 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
14754 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
14755 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
14756 SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*);
14757 SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
14758 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
14759 SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager);
14760 #else
14761 # define sqlite3PagerResetLockTimeout(X)
14762 #endif
14763
14764 /* Functions used to truncate the database file. */
14765 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
14766
14767 SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16);
@@ -18099,11 +18315,11 @@
18315 SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
18316 SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
18317 SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
18318 SQLITE_PRIVATE char sqlite3AffinityType(const char*, u8*);
18319 SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
18320 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*, sqlite3_file*);
18321 SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
18322 SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
18323 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
18324 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
18325 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
@@ -21070,11 +21286,13 @@
21286 ** is only a hint and can be safely ignored. The sqlite3OsFileControlHint()
21287 ** routine has no return value since the return value would be meaningless.
21288 */
21289 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
21290 #ifdef SQLITE_TEST
21291 if( op!=SQLITE_FCNTL_COMMIT_PHASETWO
21292 && op!=SQLITE_FCNTL_LOCK_TIMEOUT
21293 ){
21294 /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
21295 ** is using a regular VFS, it is called after the corresponding
21296 ** transaction has been committed. Injecting a fault at this point
21297 ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
21298 ** but the transaction is committed anyway.
@@ -31026,10 +31244,13 @@
31244 int openFlags; /* The flags specified at open() */
31245 #endif
31246 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
31247 unsigned fsFlags; /* cached details from statfs() */
31248 #endif
31249 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
31250 unsigned iBusyTimeout; /* Wait this many millisec on locks */
31251 #endif
31252 #if OS_VXWORKS
31253 struct vxworksFileId *pId; /* Unique file ID */
31254 #endif
31255 #ifdef SQLITE_DEBUG
31256 /* The next group of variables are used to track whether or not the
@@ -32459,10 +32680,47 @@
32680 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
32681
32682 *pResOut = reserved;
32683 return rc;
32684 }
32685
32686 /*
32687 ** Set a posix-advisory-lock.
32688 **
32689 ** There are two versions of this routine. If compiled with
32690 ** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter
32691 ** which is a pointer to a unixFile. If the unixFile->iBusyTimeout
32692 ** value is set, then it is the number of milliseconds to wait before
32693 ** failing the lock. The iBusyTimeout value is always reset back to
32694 ** zero on each call.
32695 **
32696 ** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
32697 ** attempt to set the lock.
32698 */
32699 #ifndef SQLITE_ENABLE_SETLK_TIMEOUT
32700 # define osSetPosixAdvisoryLock(h,x,t) osFcntl(h,F_SETLK,x)
32701 #else
32702 static int osSetPosixAdvisoryLock(
32703 int h, /* The file descriptor on which to take the lock */
32704 struct flock *pLock, /* The description of the lock */
32705 unixFile *pFile /* Structure holding timeout value */
32706 ){
32707 int rc = osFcntl(h,F_SETLK,pLock);
32708 while( rc<0 && pFile->iBusyTimeout>0 ){
32709 /* On systems that support some kind of blocking file lock with a timeout,
32710 ** make appropriate changes here to invoke that blocking file lock. On
32711 ** generic posix, however, there is no such API. So we simply try the
32712 ** lock once every millisecond until either the timeout expires, or until
32713 ** the lock is obtained. */
32714 usleep(1000);
32715 rc = osFcntl(h,F_SETLK,pLock);
32716 pFile->iBusyTimeout--;
32717 }
32718 return rc;
32719 }
32720 #endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
32721
32722
32723 /*
32724 ** Attempt to set a system-lock on the file pFile. The lock is
32725 ** described by pLock.
32726 **
@@ -32492,19 +32750,19 @@
32750 assert( pInode->nLock==0 );
32751 lock.l_whence = SEEK_SET;
32752 lock.l_start = SHARED_FIRST;
32753 lock.l_len = SHARED_SIZE;
32754 lock.l_type = F_WRLCK;
32755 rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
32756 if( rc<0 ) return rc;
32757 pInode->bProcessLock = 1;
32758 pInode->nLock++;
32759 }else{
32760 rc = 0;
32761 }
32762 }else{
32763 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
32764 }
32765 return rc;
32766 }
32767
32768 /*
@@ -34860,10 +35118,16 @@
35118 }
35119 case SQLITE_FCNTL_HAS_MOVED: {
35120 *(int*)pArg = fileHasMoved(pFile);
35121 return SQLITE_OK;
35122 }
35123 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
35124 case SQLITE_FCNTL_LOCK_TIMEOUT: {
35125 pFile->iBusyTimeout = *(int*)pArg;
35126 return SQLITE_OK;
35127 }
35128 #endif
35129 #if SQLITE_MAX_MMAP_SIZE>0
35130 case SQLITE_FCNTL_MMAP_SIZE: {
35131 i64 newLimit = *(i64*)pArg;
35132 int rc = SQLITE_OK;
35133 if( newLimit>sqlite3GlobalConfig.mxMmap ){
@@ -35179,11 +35443,11 @@
35443 /* Initialize the locking parameters */
35444 f.l_type = lockType;
35445 f.l_whence = SEEK_SET;
35446 f.l_start = ofst;
35447 f.l_len = n;
35448 rc = osSetPosixAdvisoryLock(pShmNode->h, &f, pFile);
35449 rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
35450 }
35451
35452 /* Update the global lock state and do debug tracing */
35453 #ifdef SQLITE_DEBUG
@@ -45039,11 +45303,11 @@
45303 ** May you find forgiveness for yourself and forgive others.
45304 ** May you share freely, never taking more than you give.
45305 **
45306 ******************************************************************************
45307 **
45308 ** This file implements an in-memory VFS. A database is held as a contiguous
45309 ** block of memory.
45310 **
45311 ** This file also implements interface sqlite3_serialize() and
45312 ** sqlite3_deserialize().
45313 */
@@ -52527,11 +52791,11 @@
52791 **
52792 ** If the busy-handler callback returns non-zero, the lock is
52793 ** retried. If it returns zero, then the SQLITE_BUSY error is
52794 ** returned to the caller of the pager API function.
52795 */
52796 SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(
52797 Pager *pPager, /* Pager object */
52798 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
52799 void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
52800 ){
52801 pPager->xBusyHandler = xBusyHandler;
@@ -54516,10 +54780,11 @@
54780 Pager *pPager;
54781 assert( pPg!=0 );
54782 assert( pPg->pgno==1 );
54783 assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
54784 pPager = pPg->pPager;
54785 sqlite3PagerResetLockTimeout(pPager);
54786 sqlite3PcacheRelease(pPg);
54787 pagerUnlockIfUnused(pPager);
54788 }
54789
54790 /*
@@ -55793,10 +56058,22 @@
56058 */
56059 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
56060 return pPager->fd;
56061 }
56062
56063 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
56064 /*
56065 ** Reset the lock timeout for pager.
56066 */
56067 SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager){
56068 if( isOpen(pPager->fd) ){
56069 int x = 0;
56070 sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_LOCK_TIMEOUT, &x);
56071 }
56072 }
56073 #endif
56074
56075 /*
56076 ** Return the file handle for the journal file (if it exists).
56077 ** This will be either the rollback journal or the WAL file.
56078 */
56079 SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){
@@ -56253,10 +56530,11 @@
56530 (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
56531 pPager->pBusyHandlerArg,
56532 pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
56533 pnLog, pnCkpt
56534 );
56535 sqlite3PagerResetLockTimeout(pPager);
56536 }
56537 return rc;
56538 }
56539
56540 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
@@ -63529,11 +63807,12 @@
63807 */
63808 static int btreeInvokeBusyHandler(void *pArg){
63809 BtShared *pBt = (BtShared*)pArg;
63810 assert( pBt->db );
63811 assert( sqlite3_mutex_held(pBt->db->mutex) );
63812 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler,
63813 sqlite3PagerFile(pBt->pPager));
63814 }
63815
63816 /*
63817 ** Open a database file.
63818 **
@@ -63707,11 +63986,11 @@
63986 if( rc!=SQLITE_OK ){
63987 goto btree_open_out;
63988 }
63989 pBt->openFlags = (u8)flags;
63990 pBt->db = db;
63991 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
63992 p->pBt = pBt;
63993
63994 pBt->pCursor = 0;
63995 pBt->pPage1 = 0;
63996 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
@@ -64670,10 +64949,11 @@
64949 if( rc!=SQLITE_OK ){
64950 unlockBtreeIfUnused(pBt);
64951 }
64952 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
64953 btreeInvokeBusyHandler(pBt) );
64954 sqlite3PagerResetLockTimeout(pBt->pPager);
64955
64956 if( rc==SQLITE_OK ){
64957 if( p->inTrans==TRANS_NONE ){
64958 pBt->nTransaction++;
64959 #ifndef SQLITE_OMIT_SHARED_CACHE
@@ -98715,20 +98995,35 @@
98995 ** This is the Expr node callback for sqlite3ExprImpliesNotNullRow().
98996 ** If the expression node requires that the table at pWalker->iCur
98997 ** have a non-NULL column, then set pWalker->eCode to 1 and abort.
98998 */
98999 static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
99000 /* This routine is only called for WHERE clause expressions and so it
99001 ** cannot have any TK_AGG_COLUMN entries because those are only found
99002 ** in HAVING clauses. We can get a TK_AGG_FUNCTION in a WHERE clause,
99003 ** but that is an illegal construct and the query will be rejected at
99004 ** a later stage of processing, so the TK_AGG_FUNCTION case does not
99005 ** need to be considered here. */
99006 assert( pExpr->op!=TK_AGG_COLUMN );
99007 testcase( pExpr->op==TK_AGG_FUNCTION );
99008
99009 if( ExprHasProperty(pExpr, EP_FromJoin) ) return WRC_Prune;
99010 switch( pExpr->op ){
99011 case TK_ISNULL:
99012 case TK_IS:
99013 case TK_OR:
99014 case TK_CASE:
99015 case TK_IN:
99016 case TK_FUNCTION:
99017 testcase( pExpr->op==TK_ISNULL );
99018 testcase( pExpr->op==TK_IS );
99019 testcase( pExpr->op==TK_OR );
99020 testcase( pExpr->op==TK_CASE );
99021 testcase( pExpr->op==TK_IN );
99022 testcase( pExpr->op==TK_FUNCTION );
99023 return WRC_Prune;
99024 case TK_COLUMN:
 
99025 if( pWalker->u.iCur==pExpr->iTable ){
99026 pWalker->eCode = 1;
99027 return WRC_Abort;
99028 }
99029 return WRC_Prune;
@@ -98740,10 +99035,17 @@
99035 /*
99036 ** Return true (non-zero) if expression p can only be true if at least
99037 ** one column of table iTab is non-null. In other words, return true
99038 ** if expression p will always be NULL or false if every column of iTab
99039 ** is NULL.
99040 **
99041 ** False negatives are acceptable. In other words, it is ok to return
99042 ** zero even if expression p will never be true of every column of iTab
99043 ** is NULL. A false negative is merely a missed optimization opportunity.
99044 **
99045 ** False positives are not allowed, however. A false positive may result
99046 ** in an incorrect answer.
99047 **
99048 ** Terms of p that are marked with EP_FromJoin (and hence that come from
99049 ** the ON or USING clauses of LEFT JOINS) are excluded from the analysis.
99050 **
99051 ** This routine is used to check if a LEFT JOIN can be converted into
@@ -101005,11 +101307,11 @@
101307 }
101308 if( pTab->tnum==0 ){
101309 /* Do not gather statistics on views or virtual tables */
101310 return;
101311 }
101312 if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
101313 /* Do not gather statistics on system tables */
101314 return;
101315 }
101316 assert( sqlite3BtreeHoldsAllMutexes(db) );
101317 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
@@ -104066,11 +104368,11 @@
104368 if( !sqlite3ExprIsConstantOrFunction(pExpr, db->init.busy) ){
104369 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
104370 pCol->zName);
104371 }else{
104372 /* A copy of pExpr is used instead of the original, as pExpr contains
104373 ** tokens that point to volatile memory.
104374 */
104375 Expr x;
104376 sqlite3ExprDelete(db, pCol->pDflt);
104377 memset(&x, 0, sizeof(x));
104378 x.op = TK_SPAN;
@@ -104988,11 +105290,11 @@
105290 Table *pSelTab; /* A fake table from which we get the result set */
105291 Select *pSel; /* Copy of the SELECT that implements the view */
105292 int nErr = 0; /* Number of errors encountered */
105293 int n; /* Temporarily holds the number of cursors assigned */
105294 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
105295 #ifndef SQLITE_OMIT_VIRTUALTABLE
105296 int rc;
105297 #endif
105298 #ifndef SQLITE_OMIT_AUTHORIZATION
105299 sqlite3_xauth xAuth; /* Saved xAuth pointer */
105300 #endif
@@ -123778,12 +124080,13 @@
124080 ** (2) The inner query is the recursive part of a common table expression.
124081 **
124082 ** (3) The inner query has a LIMIT clause (since the changes to the WHERE
124083 ** close would change the meaning of the LIMIT).
124084 **
124085 ** (4) The inner query is the right operand of a LEFT JOIN and the
124086 ** expression to be pushed down does not come from the ON clause
124087 ** on that LEFT JOIN.
124088 **
124089 ** (5) The WHERE clause expression originates in the ON or USING clause
124090 ** of a LEFT JOIN where iCursor is not the right-hand table of that
124091 ** left join. An example:
124092 **
@@ -123801,11 +124104,12 @@
124104 */
124105 static int pushDownWhereTerms(
124106 Parse *pParse, /* Parse context (for malloc() and error reporting) */
124107 Select *pSubq, /* The subquery whose WHERE clause is to be augmented */
124108 Expr *pWhere, /* The WHERE clause of the outer query */
124109 int iCursor, /* Cursor number of the subquery */
124110 int isLeftJoin /* True if pSubq is the right term of a LEFT JOIN */
124111 ){
124112 Expr *pNew;
124113 int nChng = 0;
124114 if( pWhere==0 ) return 0;
124115 if( pSubq->selFlags & SF_Recursive ) return 0; /* restriction (2) */
@@ -123825,13 +124129,20 @@
124129
124130 if( pSubq->pLimit!=0 ){
124131 return 0; /* restriction (3) */
124132 }
124133 while( pWhere->op==TK_AND ){
124134 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight,
124135 iCursor, isLeftJoin);
124136 pWhere = pWhere->pLeft;
124137 }
124138 if( isLeftJoin
124139 && (ExprHasProperty(pWhere,EP_FromJoin)==0
124140 || pWhere->iRightJoinTable!=iCursor)
124141 ){
124142 return 0; /* restriction (4) */
124143 }
124144 if( ExprHasProperty(pWhere,EP_FromJoin) && pWhere->iRightJoinTable!=iCursor ){
124145 return 0; /* restriction (5) */
124146 }
124147 if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
124148 nChng++;
@@ -125295,11 +125606,12 @@
125606
125607 /* Make copies of constant WHERE-clause terms in the outer query down
125608 ** inside the subquery. This can help the subquery to run more efficiently.
125609 */
125610 if( OptimizationEnabled(db, SQLITE_PushDown)
125611 && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor,
125612 (pItem->fg.jointype & JT_OUTER)!=0)
125613 ){
125614 #if SELECTTRACE_ENABLED
125615 if( sqlite3SelectTrace & 0x100 ){
125616 SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n"));
125617 sqlite3TreeViewSelect(0, p, 0);
@@ -146146,43 +146458,62 @@
146458 /*
146459 ** This routine implements a busy callback that sleeps and tries
146460 ** again until a timeout value is reached. The timeout value is
146461 ** an integer number of milliseconds passed in as the first
146462 ** argument.
146463 **
146464 ** Return non-zero to retry the lock. Return zero to stop trying
146465 ** and cause SQLite to return SQLITE_BUSY.
146466 */
146467 static int sqliteDefaultBusyCallback(
146468 void *ptr, /* Database connection */
146469 int count, /* Number of times table has been busy */
146470 sqlite3_file *pFile /* The file on which the lock occurred */
146471 ){
146472 #if SQLITE_OS_WIN || HAVE_USLEEP
146473 /* This case is for systems that have support for sleeping for fractions of
146474 ** a second. Examples: All windows systems, unix systems with usleep() */
146475 static const u8 delays[] =
146476 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
146477 static const u8 totals[] =
146478 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
146479 # define NDELAY ArraySize(delays)
146480 sqlite3 *db = (sqlite3 *)ptr;
146481 int tmout = db->busyTimeout;
146482 int delay, prior;
146483
146484 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
146485 if( sqlite3OsFileControl(pFile,SQLITE_FCNTL_LOCK_TIMEOUT,&tmout)==SQLITE_OK ){
146486 if( count ){
146487 tmout = 0;
146488 sqlite3OsFileControl(pFile, SQLITE_FCNTL_LOCK_TIMEOUT, &tmout);
146489 return 0;
146490 }else{
146491 return 1;
146492 }
146493 }
146494 #endif
146495 assert( count>=0 );
146496 if( count < NDELAY ){
146497 delay = delays[count];
146498 prior = totals[count];
146499 }else{
146500 delay = delays[NDELAY-1];
146501 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
146502 }
146503 if( prior + delay > tmout ){
146504 delay = tmout - prior;
146505 if( delay<=0 ) return 0;
146506 }
146507 sqlite3OsSleep(db->pVfs, delay*1000);
146508 return 1;
146509 #else
146510 /* This case for unix systems that lack usleep() support. Sleeping
146511 ** must be done in increments of whole seconds */
146512 sqlite3 *db = (sqlite3 *)ptr;
146513 int tmout = ((sqlite3 *)ptr)->busyTimeout;
146514 if( (count+1)*1000 > tmout ){
146515 return 0;
146516 }
146517 sqlite3OsSleep(db->pVfs, 1000000);
146518 return 1;
146519 #endif
@@ -146189,18 +146520,29 @@
146520 }
146521
146522 /*
146523 ** Invoke the given busy handler.
146524 **
146525 ** This routine is called when an operation failed to acquire a
146526 ** lock on VFS file pFile.
146527 **
146528 ** If this routine returns non-zero, the lock is retried. If it
146529 ** returns 0, the operation aborts with an SQLITE_BUSY error.
146530 */
146531 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p, sqlite3_file *pFile){
146532 int rc;
146533 if( p->xBusyHandler==0 || p->nBusy<0 ) return 0;
146534 if( p->bExtraFileArg ){
146535 /* Add an extra parameter with the pFile pointer to the end of the
146536 ** callback argument list */
146537 int (*xTra)(void*,int,sqlite3_file*);
146538 xTra = (int(*)(void*,int,sqlite3_file*))p->xBusyHandler;
146539 rc = xTra(p->pBusyArg, p->nBusy, pFile);
146540 }else{
146541 /* Legacy style busy handler callback */
146542 rc = p->xBusyHandler(p->pBusyArg, p->nBusy);
146543 }
146544 if( rc==0 ){
146545 p->nBusy = -1;
146546 }else{
146547 p->nBusy++;
146548 }
@@ -146218,13 +146560,14 @@
146560 ){
146561 #ifdef SQLITE_ENABLE_API_ARMOR
146562 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
146563 #endif
146564 sqlite3_mutex_enter(db->mutex);
146565 db->busyHandler.xBusyHandler = xBusy;
146566 db->busyHandler.pBusyArg = pArg;
146567 db->busyHandler.nBusy = 0;
146568 db->busyHandler.bExtraFileArg = 0;
146569 db->busyTimeout = 0;
146570 sqlite3_mutex_leave(db->mutex);
146571 return SQLITE_OK;
146572 }
146573
@@ -146268,12 +146611,14 @@
146611 SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
146612 #ifdef SQLITE_ENABLE_API_ARMOR
146613 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
146614 #endif
146615 if( ms>0 ){
146616 sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback,
146617 (void*)db);
146618 db->busyTimeout = ms;
146619 db->busyHandler.bExtraFileArg = 1;
146620 }else{
146621 sqlite3_busy_handler(db, 0, 0);
146622 }
146623 return SQLITE_OK;
146624 }
@@ -180266,12 +180611,12 @@
180611 ** of a patchset change is associated with the correspondingly positioned
180612 ** table column, counting from left to right within the CREATE TABLE
180613 ** statement.
180614 **
180615 ** For a DELETE change, all fields within the record except those associated
180616 ** with PRIMARY KEY columns are omitted. The PRIMARY KEY fields contain the
180617 ** values identifying the row to delete.
180618 **
180619 ** For an UPDATE change, all fields except those associated with PRIMARY KEY
180620 ** columns and columns that are modified by the UPDATE are set to "undefined".
180621 ** PRIMARY KEY fields contain the values identifying the table row to update,
180622 ** and fields associated with modified columns contain the new column values.
@@ -180550,11 +180895,11 @@
180895 ** the type byte).
180896 */
180897 static int sessionSerialLen(u8 *a){
180898 int e = *a;
180899 int n;
180900 if( e==0 || e==0xFF ) return 1;
180901 if( e==SQLITE_NULL ) return 1;
180902 if( e==SQLITE_INTEGER || e==SQLITE_FLOAT ) return 9;
180903 return sessionVarintGet(&a[1], &n) + 1 + n;
180904 }
180905
@@ -180630,11 +180975,11 @@
180975 for(iCol=0; iCol<pTab->nCol; iCol++){
180976 if( pTab->abPK[iCol] ){
180977 int n1 = sessionSerialLen(a1);
180978 int n2 = sessionSerialLen(a2);
180979
180980 if( n1!=n2 || memcmp(a1, a2, n1) ){
180981 return 0;
180982 }
180983 a1 += n1;
180984 a2 += n2;
180985 }else{
@@ -180873,11 +181218,11 @@
181218 if( eType==SQLITE_TEXT ){
181219 z = sqlite3_value_text(pVal);
181220 }else{
181221 z = sqlite3_value_blob(pVal);
181222 }
181223 if( n>0 && memcmp(a, z, n) ) return 0;
181224 a += n;
181225 }
181226 }
181227 }
181228
@@ -182217,10 +182562,11 @@
182562 if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
182563 zSql = sqlite3_mprintf(
182564 "SELECT tbl, ?2, stat FROM %Q.sqlite_stat1 WHERE tbl IS ?1 AND "
182565 "idx IS (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", zDb
182566 );
182567 if( zSql==0 ) rc = SQLITE_NOMEM;
182568 }else{
182569 int i;
182570 const char *zSep = "";
182571 SessionBuffer buf = {0, 0, 0};
182572
@@ -182952,11 +183298,12 @@
183298 ** changes in the changeset.
183299 */
183300 static int sessionChangesetNext(
183301 sqlite3_changeset_iter *p, /* Changeset iterator */
183302 u8 **paRec, /* If non-NULL, store record pointer here */
183303 int *pnRec, /* If non-NULL, store size of record here */
183304 int *pbNew /* If non-NULL, true if new table */
183305 ){
183306 int i;
183307 u8 op;
183308
183309 assert( (paRec==0 && pnRec==0) || (paRec && pnRec) );
@@ -182987,10 +183334,11 @@
183334 sessionDiscardData(&p->in);
183335 p->in.iCurrent = p->in.iNext;
183336
183337 op = p->in.aData[p->in.iNext++];
183338 while( op=='T' || op=='P' ){
183339 if( pbNew ) *pbNew = 1;
183340 p->bPatchset = (op=='P');
183341 if( sessionChangesetReadTblhdr(p) ) return p->rc;
183342 if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
183343 p->in.iCurrent = p->in.iNext;
183344 if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
@@ -183065,11 +183413,11 @@
183413 **
183414 ** This function may not be called on iterators passed to a conflict handler
183415 ** callback by changeset_apply().
183416 */
183417 SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *p){
183418 return sessionChangesetNext(p, 0, 0, 0);
183419 }
183420
183421 /*
183422 ** The following function extracts information on the current change
183423 ** from a changeset iterator. It may only be called after changeset_next()
@@ -183444,10 +183792,12 @@
183792 const char **azCol; /* Array of column names */
183793 u8 *abPK; /* Boolean array - true if column is in PK */
183794 int bStat1; /* True if table is sqlite_stat1 */
183795 int bDeferConstraints; /* True to defer constraints */
183796 SessionBuffer constraints; /* Deferred constraints are stored here */
183797 SessionBuffer rebase; /* Rebase information (if any) here */
183798 int bRebaseStarted; /* If table header is already in rebase */
183799 };
183800
183801 /*
183802 ** Formulate a statement to DELETE a row from database db. Assuming a table
183803 ** structure like this:
@@ -183710,11 +184060,10 @@
184060 "DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS "
184061 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END "
184062 "AND (?4 OR stat IS ?3)"
184063 );
184064 }
 
184065 return rc;
184066 }
184067
184068 /*
184069 ** A wrapper around sqlite3_bind_value() that detects an extra problem.
@@ -183824,10 +184173,58 @@
184173 if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect);
184174 }
184175
184176 return rc;
184177 }
184178
184179 /*
184180 ** This function is called from within sqlite3changset_apply_v2() when
184181 ** a conflict is encountered and resolved using conflict resolution
184182 ** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE)..
184183 ** It adds a conflict resolution record to the buffer in
184184 ** SessionApplyCtx.rebase, which will eventually be returned to the caller
184185 ** of apply_v2() as the "rebase" buffer.
184186 **
184187 ** Return SQLITE_OK if successful, or an SQLite error code otherwise.
184188 */
184189 static int sessionRebaseAdd(
184190 SessionApplyCtx *p, /* Apply context */
184191 int eType, /* Conflict resolution (OMIT or REPLACE) */
184192 sqlite3_changeset_iter *pIter /* Iterator pointing at current change */
184193 ){
184194 int rc = SQLITE_OK;
184195 int i;
184196 int eOp = pIter->op;
184197 if( p->bRebaseStarted==0 ){
184198 /* Append a table-header to the rebase buffer */
184199 const char *zTab = pIter->zTab;
184200 sessionAppendByte(&p->rebase, 'T', &rc);
184201 sessionAppendVarint(&p->rebase, p->nCol, &rc);
184202 sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
184203 sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
184204 p->bRebaseStarted = 1;
184205 }
184206
184207 assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT );
184208 assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE );
184209
184210 sessionAppendByte(&p->rebase,
184211 (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc
184212 );
184213 sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc);
184214 for(i=0; i<p->nCol; i++){
184215 sqlite3_value *pVal = 0;
184216 if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){
184217 sqlite3changeset_old(pIter, i, &pVal);
184218 }else{
184219 sqlite3changeset_new(pIter, i, &pVal);
184220 }
184221 sessionAppendValue(&p->rebase, pVal, &rc);
184222 }
184223
184224 return rc;
184225 }
184226
184227 /*
184228 ** Invoke the conflict handler for the change that the changeset iterator
184229 ** currently points to.
184230 **
@@ -183900,11 +184297,11 @@
184297 /* Instead of invoking the conflict handler, append the change blob
184298 ** to the SessionApplyCtx.constraints buffer. */
184299 u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
184300 int nBlob = pIter->in.iNext - pIter->in.iCurrent;
184301 sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
184302 return SQLITE_OK;
184303 }else{
184304 /* No other row with the new.* primary key. */
184305 res = xConflict(pCtx, eType+1, pIter);
184306 if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE;
184307 }
@@ -183926,10 +184323,13 @@
184323
184324 default:
184325 rc = SQLITE_MISUSE;
184326 break;
184327 }
184328 if( rc==SQLITE_OK ){
184329 rc = sessionRebaseAdd(p, res, pIter);
184330 }
184331 }
184332
184333 return rc;
184334 }
184335
@@ -184101,46 +184501,46 @@
184501 int bReplace = 0;
184502 int bRetry = 0;
184503 int rc;
184504
184505 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
184506 if( rc==SQLITE_OK ){
184507 /* If the bRetry flag is set, the change has not been applied due to an
184508 ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
184509 ** a row with the correct PK is present in the db, but one or more other
184510 ** fields do not contain the expected values) and the conflict handler
184511 ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
184512 ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
184513 ** the SQLITE_CHANGESET_DATA problem. */
184514 if( bRetry ){
184515 assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
184516 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
184517 }
184518
184519 /* If the bReplace flag is set, the change is an INSERT that has not
184520 ** been performed because the database already contains a row with the
184521 ** specified primary key and the conflict handler returned
184522 ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
184523 ** before reattempting the INSERT. */
184524 else if( bReplace ){
184525 assert( pIter->op==SQLITE_INSERT );
184526 rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
184527 if( rc==SQLITE_OK ){
184528 rc = sessionBindRow(pIter,
184529 sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
184530 sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
184531 }
184532 if( rc==SQLITE_OK ){
184533 sqlite3_step(pApply->pDelete);
184534 rc = sqlite3_reset(pApply->pDelete);
184535 }
184536 if( rc==SQLITE_OK ){
184537 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
184538 }
184539 if( rc==SQLITE_OK ){
184540 rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
184541 }
184542 }
184543 }
184544
184545 return rc;
184546 }
@@ -184212,11 +184612,12 @@
184612 int(*xConflict)(
184613 void *pCtx, /* Copy of fifth arg to _apply() */
184614 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
184615 sqlite3_changeset_iter *p /* Handle describing change and conflict */
184616 ),
184617 void *pCtx, /* First argument passed to xConflict */
184618 void **ppRebase, int *pnRebase /* OUT: Rebase information */
184619 ){
184620 int schemaMismatch = 0;
184621 int rc; /* Return code */
184622 const char *zTab = 0; /* Name of current table */
184623 int nTab = 0; /* Result of sqlite3Strlen30(zTab) */
@@ -184250,13 +184651,22 @@
184651 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
184652 sqlite3_finalize(sApply.pDelete);
184653 sqlite3_finalize(sApply.pUpdate);
184654 sqlite3_finalize(sApply.pInsert);
184655 sqlite3_finalize(sApply.pSelect);
 
184656 sApply.db = db;
184657 sApply.pDelete = 0;
184658 sApply.pUpdate = 0;
184659 sApply.pInsert = 0;
184660 sApply.pSelect = 0;
184661 sApply.nCol = 0;
184662 sApply.azCol = 0;
184663 sApply.abPK = 0;
184664 sApply.bStat1 = 0;
184665 sApply.bDeferConstraints = 1;
184666 sApply.bRebaseStarted = 0;
184667 memset(&sApply.constraints, 0, sizeof(SessionBuffer));
184668
184669 /* If an xFilter() callback was specified, invoke it now. If the
184670 ** xFilter callback returns zero, skip this table. If it returns
184671 ** non-zero, proceed. */
184672 schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
@@ -184362,19 +184772,55 @@
184772 }else{
184773 sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
184774 sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
184775 }
184776
184777 if( rc==SQLITE_OK && bPatchset==0 && ppRebase && pnRebase ){
184778 *ppRebase = (void*)sApply.rebase.aBuf;
184779 *pnRebase = sApply.rebase.nBuf;
184780 sApply.rebase.aBuf = 0;
184781 }
184782 sqlite3_finalize(sApply.pInsert);
184783 sqlite3_finalize(sApply.pDelete);
184784 sqlite3_finalize(sApply.pUpdate);
184785 sqlite3_finalize(sApply.pSelect);
184786 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
184787 sqlite3_free((char*)sApply.constraints.aBuf);
184788 sqlite3_free((char*)sApply.rebase.aBuf);
184789 sqlite3_mutex_leave(sqlite3_db_mutex(db));
184790 return rc;
184791 }
184792
184793 /*
184794 ** Apply the changeset passed via pChangeset/nChangeset to the main
184795 ** database attached to handle "db".
184796 */
184797 SQLITE_API int sqlite3changeset_apply_v2(
184798 sqlite3 *db, /* Apply change to "main" db of this handle */
184799 int nChangeset, /* Size of changeset in bytes */
184800 void *pChangeset, /* Changeset blob */
184801 int(*xFilter)(
184802 void *pCtx, /* Copy of sixth arg to _apply() */
184803 const char *zTab /* Table name */
184804 ),
184805 int(*xConflict)(
184806 void *pCtx, /* Copy of sixth arg to _apply() */
184807 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
184808 sqlite3_changeset_iter *p /* Handle describing change and conflict */
184809 ),
184810 void *pCtx, /* First argument passed to xConflict */
184811 void **ppRebase, int *pnRebase
184812 ){
184813 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
184814 int rc = sqlite3changeset_start(&pIter, nChangeset, pChangeset);
184815 if( rc==SQLITE_OK ){
184816 rc = sessionChangesetApply(
184817 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase
184818 );
184819 }
184820 return rc;
184821 }
184822
184823 /*
184824 ** Apply the changeset passed via pChangeset/nChangeset to the main database
184825 ** attached to handle "db". Invoke the supplied conflict handler callback
184826 ** to resolve any conflicts encountered while applying the change.
@@ -184392,23 +184838,45 @@
184838 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
184839 sqlite3_changeset_iter *p /* Handle describing change and conflict */
184840 ),
184841 void *pCtx /* First argument passed to xConflict */
184842 ){
184843 return sqlite3changeset_apply_v2(
184844 db, nChangeset, pChangeset, xFilter, xConflict, pCtx, 0, 0
184845 );
 
 
 
184846 }
184847
184848 /*
184849 ** Apply the changeset passed via xInput/pIn to the main database
184850 ** attached to handle "db". Invoke the supplied conflict handler callback
184851 ** to resolve any conflicts encountered while applying the change.
184852 */
184853 SQLITE_API int sqlite3changeset_apply_v2_strm(
184854 sqlite3 *db, /* Apply change to "main" db of this handle */
184855 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
184856 void *pIn, /* First arg for xInput */
184857 int(*xFilter)(
184858 void *pCtx, /* Copy of sixth arg to _apply() */
184859 const char *zTab /* Table name */
184860 ),
184861 int(*xConflict)(
184862 void *pCtx, /* Copy of sixth arg to _apply() */
184863 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
184864 sqlite3_changeset_iter *p /* Handle describing change and conflict */
184865 ),
184866 void *pCtx, /* First argument passed to xConflict */
184867 void **ppRebase, int *pnRebase
184868 ){
184869 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
184870 int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
184871 if( rc==SQLITE_OK ){
184872 rc = sessionChangesetApply(
184873 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase
184874 );
184875 }
184876 return rc;
184877 }
184878 SQLITE_API int sqlite3changeset_apply_strm(
184879 sqlite3 *db, /* Apply change to "main" db of this handle */
184880 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
184881 void *pIn, /* First arg for xInput */
184882 int(*xFilter)(
@@ -184420,16 +184888,13 @@
184888 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
184889 sqlite3_changeset_iter *p /* Handle describing change and conflict */
184890 ),
184891 void *pCtx /* First argument passed to xConflict */
184892 ){
184893 return sqlite3changeset_apply_v2_strm(
184894 db, xInput, pIn, xFilter, xConflict, pCtx, 0, 0
184895 );
 
 
 
184896 }
184897
184898 /*
184899 ** sqlite3_changegroup handle.
184900 */
@@ -184444,31 +184909,90 @@
184909 ** part of an sqlite3changeset_concat() operation. A new change object is
184910 ** allocated and a pointer to it stored in *ppNew.
184911 */
184912 static int sessionChangeMerge(
184913 SessionTable *pTab, /* Table structure */
184914 int bRebase, /* True for a rebase hash-table */
184915 int bPatchset, /* True for patchsets */
184916 SessionChange *pExist, /* Existing change */
184917 int op2, /* Second change operation */
184918 int bIndirect, /* True if second change is indirect */
184919 u8 *aRec, /* Second change record */
184920 int nRec, /* Number of bytes in aRec */
184921 SessionChange **ppNew /* OUT: Merged change */
184922 ){
184923 SessionChange *pNew = 0;
184924 int rc = SQLITE_OK;
184925
184926 if( !pExist ){
184927 pNew = (SessionChange *)sqlite3_malloc(sizeof(SessionChange) + nRec);
184928 if( !pNew ){
184929 return SQLITE_NOMEM;
184930 }
184931 memset(pNew, 0, sizeof(SessionChange));
184932 pNew->op = op2;
184933 pNew->bIndirect = bIndirect;
 
184934 pNew->aRecord = (u8*)&pNew[1];
184935 if( bIndirect==0 || bRebase==0 ){
184936 pNew->nRecord = nRec;
184937 memcpy(pNew->aRecord, aRec, nRec);
184938 }else{
184939 int i;
184940 u8 *pIn = aRec;
184941 u8 *pOut = pNew->aRecord;
184942 for(i=0; i<pTab->nCol; i++){
184943 int nIn = sessionSerialLen(pIn);
184944 if( *pIn==0 ){
184945 *pOut++ = 0;
184946 }else if( pTab->abPK[i]==0 ){
184947 *pOut++ = 0xFF;
184948 }else{
184949 memcpy(pOut, pIn, nIn);
184950 pOut += nIn;
184951 }
184952 pIn += nIn;
184953 }
184954 pNew->nRecord = pOut - pNew->aRecord;
184955 }
184956 }else if( bRebase ){
184957 if( pExist->op==SQLITE_DELETE && pExist->bIndirect ){
184958 *ppNew = pExist;
184959 }else{
184960 int nByte = nRec + pExist->nRecord + sizeof(SessionChange);
184961 pNew = (SessionChange*)sqlite3_malloc(nByte);
184962 if( pNew==0 ){
184963 rc = SQLITE_NOMEM;
184964 }else{
184965 int i;
184966 u8 *a1 = pExist->aRecord;
184967 u8 *a2 = aRec;
184968 u8 *pOut;
184969
184970 memset(pNew, 0, nByte);
184971 pNew->bIndirect = bIndirect || pExist->bIndirect;
184972 pNew->op = op2;
184973 pOut = pNew->aRecord = (u8*)&pNew[1];
184974
184975 for(i=0; i<pTab->nCol; i++){
184976 int n1 = sessionSerialLen(a1);
184977 int n2 = sessionSerialLen(a2);
184978 if( *a1==0xFF || (pTab->abPK[i]==0 && bIndirect) ){
184979 *pOut++ = 0xFF;
184980 }else if( *a2==0 ){
184981 memcpy(pOut, a1, n1);
184982 pOut += n1;
184983 }else{
184984 memcpy(pOut, a2, n2);
184985 pOut += n2;
184986 }
184987 a1 += n1;
184988 a2 += n2;
184989 }
184990 pNew->nRecord = pOut - pNew->aRecord;
184991 }
184992 sqlite3_free(pExist);
184993 }
184994 }else{
184995 int op1 = pExist->op;
184996
184997 /*
184998 ** op1=INSERT, op2=INSERT -> Unsupported. Discard op2.
@@ -184558,28 +185082,28 @@
185082 sqlite3_free(pExist);
185083 }
185084 }
185085
185086 *ppNew = pNew;
185087 return rc;
185088 }
185089
185090 /*
185091 ** Add all changes in the changeset traversed by the iterator passed as
185092 ** the first argument to the changegroup hash tables.
185093 */
185094 static int sessionChangesetToHash(
185095 sqlite3_changeset_iter *pIter, /* Iterator to read from */
185096 sqlite3_changegroup *pGrp, /* Changegroup object to add changeset to */
185097 int bRebase /* True if hash table is for rebasing */
185098 ){
185099 u8 *aRec;
185100 int nRec;
185101 int rc = SQLITE_OK;
185102 SessionTable *pTab = 0;
185103
185104 while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, 0) ){
 
185105 const char *zNew;
185106 int nCol;
185107 int op;
185108 int iHash;
185109 int bIndirect;
@@ -184655,11 +185179,11 @@
185179 pTab->nEntry--;
185180 break;
185181 }
185182 }
185183
185184 rc = sessionChangeMerge(pTab, bRebase,
185185 pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
185186 );
185187 if( rc ) break;
185188 if( pChange ){
185189 pChange->pNext = pTab->apChange[iHash];
@@ -184763,11 +185287,11 @@
185287 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
185288 int rc; /* Return code */
185289
185290 rc = sqlite3changeset_start(&pIter, nData, pData);
185291 if( rc==SQLITE_OK ){
185292 rc = sessionChangesetToHash(pIter, pGrp, 0);
185293 }
185294 sqlite3changeset_finalize(pIter);
185295 return rc;
185296 }
185297
@@ -184794,11 +185318,11 @@
185318 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
185319 int rc; /* Return code */
185320
185321 rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
185322 if( rc==SQLITE_OK ){
185323 rc = sessionChangesetToHash(pIter, pGrp, 0);
185324 }
185325 sqlite3changeset_finalize(pIter);
185326 return rc;
185327 }
185328
@@ -184878,10 +185402,353 @@
185402 }
185403 sqlite3changegroup_delete(pGrp);
185404
185405 return rc;
185406 }
185407
185408 /*
185409 ** Changeset rebaser handle.
185410 */
185411 struct sqlite3_rebaser {
185412 sqlite3_changegroup grp; /* Hash table */
185413 };
185414
185415 /*
185416 ** Buffers a1 and a2 must both contain a sessions module record nCol
185417 ** fields in size. This function appends an nCol sessions module
185418 ** record to buffer pBuf that is a copy of a1, except that for
185419 ** each field that is undefined in a1[], swap in the field from a2[].
185420 */
185421 static void sessionAppendRecordMerge(
185422 SessionBuffer *pBuf, /* Buffer to append to */
185423 int nCol, /* Number of columns in each record */
185424 u8 *a1, int n1, /* Record 1 */
185425 u8 *a2, int n2, /* Record 2 */
185426 int *pRc /* IN/OUT: error code */
185427 ){
185428 sessionBufferGrow(pBuf, n1+n2, pRc);
185429 if( *pRc==SQLITE_OK ){
185430 int i;
185431 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
185432 for(i=0; i<nCol; i++){
185433 int nn1 = sessionSerialLen(a1);
185434 int nn2 = sessionSerialLen(a2);
185435 if( *a1==0 || *a1==0xFF ){
185436 memcpy(pOut, a2, nn2);
185437 pOut += nn2;
185438 }else{
185439 memcpy(pOut, a1, nn1);
185440 pOut += nn1;
185441 }
185442 a1 += nn1;
185443 a2 += nn2;
185444 }
185445
185446 pBuf->nBuf = pOut-pBuf->aBuf;
185447 assert( pBuf->nBuf<=pBuf->nAlloc );
185448 }
185449 }
185450
185451 /*
185452 ** This function is called when rebasing a local UPDATE change against one
185453 ** or more remote UPDATE changes. The aRec/nRec buffer contains the current
185454 ** old.* and new.* records for the change. The rebase buffer (a single
185455 ** record) is in aChange/nChange. The rebased change is appended to buffer
185456 ** pBuf.
185457 **
185458 ** Rebasing the UPDATE involves:
185459 **
185460 ** * Removing any changes to fields for which the corresponding field
185461 ** in the rebase buffer is set to "replaced" (type 0xFF). If this
185462 ** means the UPDATE change updates no fields, nothing is appended
185463 ** to the output buffer.
185464 **
185465 ** * For each field modified by the local change for which the
185466 ** corresponding field in the rebase buffer is not "undefined" (0x00)
185467 ** or "replaced" (0xFF), the old.* value is replaced by the value
185468 ** in the rebase buffer.
185469 */
185470 static void sessionAppendPartialUpdate(
185471 SessionBuffer *pBuf, /* Append record here */
185472 sqlite3_changeset_iter *pIter, /* Iterator pointed at local change */
185473 u8 *aRec, int nRec, /* Local change */
185474 u8 *aChange, int nChange, /* Record to rebase against */
185475 int *pRc /* IN/OUT: Return Code */
185476 ){
185477 sessionBufferGrow(pBuf, 2+nRec+nChange, pRc);
185478 if( *pRc==SQLITE_OK ){
185479 int bData = 0;
185480 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
185481 int i;
185482 u8 *a1 = aRec;
185483 u8 *a2 = aChange;
185484
185485 *pOut++ = SQLITE_UPDATE;
185486 *pOut++ = pIter->bIndirect;
185487 for(i=0; i<pIter->nCol; i++){
185488 int n1 = sessionSerialLen(a1);
185489 int n2 = sessionSerialLen(a2);
185490 if( pIter->abPK[i] || a2[0]==0 ){
185491 if( !pIter->abPK[i] ) bData = 1;
185492 memcpy(pOut, a1, n1);
185493 pOut += n1;
185494 }else if( a2[0]!=0xFF ){
185495 bData = 1;
185496 memcpy(pOut, a2, n2);
185497 pOut += n2;
185498 }else{
185499 *pOut++ = '\0';
185500 }
185501 a1 += n1;
185502 a2 += n2;
185503 }
185504 if( bData ){
185505 a2 = aChange;
185506 for(i=0; i<pIter->nCol; i++){
185507 int n1 = sessionSerialLen(a1);
185508 int n2 = sessionSerialLen(a2);
185509 if( pIter->abPK[i] || a2[0]!=0xFF ){
185510 memcpy(pOut, a1, n1);
185511 pOut += n1;
185512 }else{
185513 *pOut++ = '\0';
185514 }
185515 a1 += n1;
185516 a2 += n2;
185517 }
185518 pBuf->nBuf = (pOut - pBuf->aBuf);
185519 }
185520 }
185521 }
185522
185523 /*
185524 ** pIter is configured to iterate through a changeset. This function rebases
185525 ** that changeset according to the current configuration of the rebaser
185526 ** object passed as the first argument. If no error occurs and argument xOutput
185527 ** is not NULL, then the changeset is returned to the caller by invoking
185528 ** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL,
185529 ** then (*ppOut) is set to point to a buffer containing the rebased changeset
185530 ** before this function returns. In this case (*pnOut) is set to the size of
185531 ** the buffer in bytes. It is the responsibility of the caller to eventually
185532 ** free the (*ppOut) buffer using sqlite3_free().
185533 **
185534 ** If an error occurs, an SQLite error code is returned. If ppOut and
185535 ** pnOut are not NULL, then the two output parameters are set to 0 before
185536 ** returning.
185537 */
185538 static int sessionRebase(
185539 sqlite3_rebaser *p, /* Rebaser hash table */
185540 sqlite3_changeset_iter *pIter, /* Input data */
185541 int (*xOutput)(void *pOut, const void *pData, int nData),
185542 void *pOut, /* Context for xOutput callback */
185543 int *pnOut, /* OUT: Number of bytes in output changeset */
185544 void **ppOut /* OUT: Inverse of pChangeset */
185545 ){
185546 int rc = SQLITE_OK;
185547 u8 *aRec = 0;
185548 int nRec = 0;
185549 int bNew = 0;
185550 SessionTable *pTab = 0;
185551 SessionBuffer sOut = {0,0,0};
185552
185553 while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, &bNew) ){
185554 SessionChange *pChange = 0;
185555 int bDone = 0;
185556
185557 if( bNew ){
185558 const char *zTab = pIter->zTab;
185559 for(pTab=p->grp.pList; pTab; pTab=pTab->pNext){
185560 if( 0==sqlite3_stricmp(pTab->zName, zTab) ) break;
185561 }
185562 bNew = 0;
185563
185564 /* A patchset may not be rebased */
185565 if( pIter->bPatchset ){
185566 rc = SQLITE_ERROR;
185567 }
185568
185569 /* Append a table header to the output for this new table */
185570 sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
185571 sessionAppendVarint(&sOut, pIter->nCol, &rc);
185572 sessionAppendBlob(&sOut, pIter->abPK, pIter->nCol, &rc);
185573 sessionAppendBlob(&sOut,(u8*)pIter->zTab,(int)strlen(pIter->zTab)+1,&rc);
185574 }
185575
185576 if( pTab && rc==SQLITE_OK ){
185577 int iHash = sessionChangeHash(pTab, 0, aRec, pTab->nChange);
185578
185579 for(pChange=pTab->apChange[iHash]; pChange; pChange=pChange->pNext){
185580 if( sessionChangeEqual(pTab, 0, aRec, 0, pChange->aRecord) ){
185581 break;
185582 }
185583 }
185584 }
185585
185586 if( pChange ){
185587 assert( pChange->op==SQLITE_DELETE || pChange->op==SQLITE_INSERT );
185588 switch( pIter->op ){
185589 case SQLITE_INSERT:
185590 if( pChange->op==SQLITE_INSERT ){
185591 bDone = 1;
185592 if( pChange->bIndirect==0 ){
185593 sessionAppendByte(&sOut, SQLITE_UPDATE, &rc);
185594 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
185595 sessionAppendBlob(&sOut, pChange->aRecord, pChange->nRecord, &rc);
185596 sessionAppendBlob(&sOut, aRec, nRec, &rc);
185597 }
185598 }
185599 break;
185600
185601 case SQLITE_UPDATE:
185602 bDone = 1;
185603 if( pChange->op==SQLITE_DELETE ){
185604 if( pChange->bIndirect==0 ){
185605 u8 *pCsr = aRec;
185606 sessionSkipRecord(&pCsr, pIter->nCol);
185607 sessionAppendByte(&sOut, SQLITE_INSERT, &rc);
185608 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
185609 sessionAppendRecordMerge(&sOut, pIter->nCol,
185610 pCsr, nRec-(pCsr-aRec),
185611 pChange->aRecord, pChange->nRecord, &rc
185612 );
185613 }
185614 }else{
185615 sessionAppendPartialUpdate(&sOut, pIter,
185616 aRec, nRec, pChange->aRecord, pChange->nRecord, &rc
185617 );
185618 }
185619 break;
185620
185621 default:
185622 assert( pIter->op==SQLITE_DELETE );
185623 bDone = 1;
185624 if( pChange->op==SQLITE_INSERT ){
185625 sessionAppendByte(&sOut, SQLITE_DELETE, &rc);
185626 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
185627 sessionAppendRecordMerge(&sOut, pIter->nCol,
185628 pChange->aRecord, pChange->nRecord, aRec, nRec, &rc
185629 );
185630 }
185631 break;
185632 }
185633 }
185634
185635 if( bDone==0 ){
185636 sessionAppendByte(&sOut, pIter->op, &rc);
185637 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
185638 sessionAppendBlob(&sOut, aRec, nRec, &rc);
185639 }
185640 if( rc==SQLITE_OK && xOutput && sOut.nBuf>SESSIONS_STRM_CHUNK_SIZE ){
185641 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
185642 sOut.nBuf = 0;
185643 }
185644 if( rc ) break;
185645 }
185646
185647 if( rc!=SQLITE_OK ){
185648 sqlite3_free(sOut.aBuf);
185649 memset(&sOut, 0, sizeof(sOut));
185650 }
185651
185652 if( rc==SQLITE_OK ){
185653 if( xOutput ){
185654 if( sOut.nBuf>0 ){
185655 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
185656 }
185657 }else{
185658 *ppOut = (void*)sOut.aBuf;
185659 *pnOut = sOut.nBuf;
185660 sOut.aBuf = 0;
185661 }
185662 }
185663 sqlite3_free(sOut.aBuf);
185664 return rc;
185665 }
185666
185667 /*
185668 ** Create a new rebaser object.
185669 */
185670 SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew){
185671 int rc = SQLITE_OK;
185672 sqlite3_rebaser *pNew;
185673
185674 pNew = sqlite3_malloc(sizeof(sqlite3_rebaser));
185675 if( pNew==0 ){
185676 rc = SQLITE_NOMEM;
185677 }else{
185678 memset(pNew, 0, sizeof(sqlite3_rebaser));
185679 }
185680 *ppNew = pNew;
185681 return rc;
185682 }
185683
185684 /*
185685 ** Call this one or more times to configure a rebaser.
185686 */
185687 SQLITE_API int sqlite3rebaser_configure(
185688 sqlite3_rebaser *p,
185689 int nRebase, const void *pRebase
185690 ){
185691 sqlite3_changeset_iter *pIter = 0; /* Iterator opened on pData/nData */
185692 int rc; /* Return code */
185693 rc = sqlite3changeset_start(&pIter, nRebase, (void*)pRebase);
185694 if( rc==SQLITE_OK ){
185695 rc = sessionChangesetToHash(pIter, &p->grp, 1);
185696 }
185697 sqlite3changeset_finalize(pIter);
185698 return rc;
185699 }
185700
185701 /*
185702 ** Rebase a changeset according to current rebaser configuration
185703 */
185704 SQLITE_API int sqlite3rebaser_rebase(
185705 sqlite3_rebaser *p,
185706 int nIn, const void *pIn,
185707 int *pnOut, void **ppOut
185708 ){
185709 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
185710 int rc = sqlite3changeset_start(&pIter, nIn, (void*)pIn);
185711
185712 if( rc==SQLITE_OK ){
185713 rc = sessionRebase(p, pIter, 0, 0, pnOut, ppOut);
185714 sqlite3changeset_finalize(pIter);
185715 }
185716
185717 return rc;
185718 }
185719
185720 /*
185721 ** Rebase a changeset according to current rebaser configuration
185722 */
185723 SQLITE_API int sqlite3rebaser_rebase_strm(
185724 sqlite3_rebaser *p,
185725 int (*xInput)(void *pIn, void *pData, int *pnData),
185726 void *pIn,
185727 int (*xOutput)(void *pOut, const void *pData, int nData),
185728 void *pOut
185729 ){
185730 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
185731 int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
185732
185733 if( rc==SQLITE_OK ){
185734 rc = sessionRebase(p, pIter, xOutput, pOut, 0, 0);
185735 sqlite3changeset_finalize(pIter);
185736 }
185737
185738 return rc;
185739 }
185740
185741 /*
185742 ** Destroy a rebaser object
185743 */
185744 SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
185745 if( p ){
185746 sessionDeleteTable(p->grp.pList);
185747 sqlite3_free(p);
185748 }
185749 }
185750
185751 #endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
185752
185753 /************** End of sqlite3session.c **************************************/
185754 /************** Begin file json1.c *******************************************/
@@ -204697,11 +205564,11 @@
205564 int nArg, /* Number of args */
205565 sqlite3_value **apUnused /* Function arguments */
205566 ){
205567 assert( nArg==0 );
205568 UNUSED_PARAM2(nArg, apUnused);
205569 sqlite3_result_text(pCtx, "fts5: 2018-03-26 16:37:53 6c40c5574f4ae9795a142d01a8f84afd1b72678ea5f6bfca14a8646c4e862605", -1, SQLITE_TRANSIENT);
205570 }
205571
205572 static int fts5Init(sqlite3 *db){
205573 static const sqlite3_module fts5Mod = {
205574 /* iVersion */ 2,
@@ -208967,12 +209834,12 @@
209834 }
209835 #endif /* SQLITE_CORE */
209836 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
209837
209838 /************** End of stmt.c ************************************************/
209839 #if __LINE__!=209839
209840 #undef SQLITE_SOURCE_ID
209841 #define SQLITE_SOURCE_ID "2018-03-28 15:56:55 eb29b3369e76ec1df25a5484d8ec5fb924e23d5c70aaa4d794b2b17ee187alt2"
209842 #endif
209843 /* Return the source-id for this library */
209844 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
209845 /************************** End of sqlite3.c ******************************/
209846
+224 -14
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -123,11 +123,11 @@
123123
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
124124
** [sqlite_version()] and [sqlite_source_id()].
125125
*/
126126
#define SQLITE_VERSION "3.23.0"
127127
#define SQLITE_VERSION_NUMBER 3023000
128
-#define SQLITE_SOURCE_ID "2018-03-22 17:13:44 eb4f452e354065d610ff57a6a9312ad119b6b0cc467f9dff105f0718bc27ef01"
128
+#define SQLITE_SOURCE_ID "2018-03-28 15:56:55 eb29b3369e76ec1df25a5484d8ec5fb924e23d5c70aaa4d794b2b17ee187alt1"
129129
130130
/*
131131
** CAPI3REF: Run-Time Library Version Numbers
132132
** KEYWORDS: sqlite3_version sqlite3_sourceid
133133
**
@@ -1062,10 +1062,16 @@
10621062
** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
10631063
** ^This file control takes the file descriptor out of batch write mode
10641064
** so that all subsequent write operations are independent.
10651065
** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
10661066
** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
1067
+**
1068
+** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
1069
+** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode causes attempts to obtain
1070
+** a file lock using the xLock or xShmLock methods of the VFS to wait
1071
+** for up to M milliseconds before failing, where M is the single
1072
+** unsigned integer parameter.
10671073
** </ul>
10681074
*/
10691075
#define SQLITE_FCNTL_LOCKSTATE 1
10701076
#define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
10711077
#define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -1096,10 +1102,11 @@
10961102
#define SQLITE_FCNTL_WIN32_GET_HANDLE 29
10971103
#define SQLITE_FCNTL_PDB 30
10981104
#define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE 31
10991105
#define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE 32
11001106
#define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE 33
1107
+#define SQLITE_FCNTL_LOCK_TIMEOUT 34
11011108
11021109
/* deprecated names */
11031110
#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
11041111
#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
11051112
#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -8809,11 +8816,11 @@
88098816
88108817
/*
88118818
** CAPI3REF: Deserialize a database
88128819
**
88138820
** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
8814
-** [database connection] D to disconnection from database S and then
8821
+** [database connection] D to disconnect from database S and then
88158822
** reopen S as an in-memory database based on the serialization contained
88168823
** in P. The serialized database P is N bytes in size. M is the size of
88178824
** the buffer P, which might be larger than N. If M is larger than N, and
88188825
** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
88198826
** permitted to add content to the in-memory database as long as the total
@@ -9950,23 +9957,22 @@
99509957
SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
99519958
99529959
/*
99539960
** CAPI3REF: Apply A Changeset To A Database
99549961
**
9955
-** Apply a changeset to a database. This function attempts to update the
9956
-** "main" database attached to handle db with the changes found in the
9957
-** changeset passed via the second and third arguments.
9962
+** Apply a changeset or patchset to a database. These functions attempt to
9963
+** update the "main" database attached to handle db with the changes found in
9964
+** the changeset passed via the second and third arguments.
99589965
**
9959
-** The fourth argument (xFilter) passed to this function is the "filter
9966
+** The fourth argument (xFilter) passed to these functions is the "filter
99609967
** callback". If it is not NULL, then for each table affected by at least one
99619968
** change in the changeset, the filter callback is invoked with
99629969
** the table name as the second argument, and a copy of the context pointer
9963
-** passed as the sixth argument to this function as the first. If the "filter
9964
-** callback" returns zero, then no attempt is made to apply any changes to
9965
-** the table. Otherwise, if the return value is non-zero or the xFilter
9966
-** argument to this function is NULL, all changes related to the table are
9967
-** attempted.
9970
+** passed as the sixth argument as the first. If the "filter callback"
9971
+** returns zero, then no attempt is made to apply any changes to the table.
9972
+** Otherwise, if the return value is non-zero or the xFilter argument to
9973
+** is NULL, all changes related to the table are attempted.
99689974
**
99699975
** For each table that is not excluded by the filter callback, this function
99709976
** tests that the target database contains a compatible table. A table is
99719977
** considered compatible if all of the following are true:
99729978
**
@@ -10007,11 +10013,11 @@
1000710013
** the documentation for the three
1000810014
** [SQLITE_CHANGESET_OMIT|available return values] for details.
1000910015
**
1001010016
** <dl>
1001110017
** <dt>DELETE Changes<dd>
10012
-** For each DELETE change, this function checks if the target database
10018
+** For each DELETE change, the function checks if the target database
1001310019
** contains a row with the same primary key value (or values) as the
1001410020
** original row values stored in the changeset. If it does, and the values
1001510021
** stored in all non-primary key columns also match the values stored in
1001610022
** the changeset the row is deleted from the target database.
1001710023
**
@@ -10052,11 +10058,11 @@
1005210058
** This includes the case where the INSERT operation is re-attempted because
1005310059
** an earlier call to the conflict handler function returned
1005410060
** [SQLITE_CHANGESET_REPLACE].
1005510061
**
1005610062
** <dt>UPDATE Changes<dd>
10057
-** For each UPDATE change, this function checks if the target database
10063
+** For each UPDATE change, the function checks if the target database
1005810064
** contains a row with the same primary key value (or values) as the
1005910065
** original row values stored in the changeset. If it does, and the values
1006010066
** stored in all modified non-primary key columns also match the values
1006110067
** stored in the changeset the row is updated within the target database.
1006210068
**
@@ -10083,15 +10089,25 @@
1008310089
** It is safe to execute SQL statements, including those that write to the
1008410090
** table that the callback related to, from within the xConflict callback.
1008510091
** This can be used to further customize the applications conflict
1008610092
** resolution strategy.
1008710093
**
10088
-** All changes made by this function are enclosed in a savepoint transaction.
10094
+** All changes made by these functions are enclosed in a savepoint transaction.
1008910095
** If any other error (aside from a constraint failure when attempting to
1009010096
** write to the target database) occurs, then the savepoint transaction is
1009110097
** rolled back, restoring the target database to its original state, and an
1009210098
** SQLite error code returned.
10099
+**
10100
+** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
10101
+** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
10102
+** may set (*ppRebase) to point to a "rebase" that may be used with the
10103
+** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
10104
+** is set to the size of the buffer in bytes. It is the responsibility of the
10105
+** caller to eventually free any such buffer using sqlite3_free(). The buffer
10106
+** is only allocated and populated if one or more conflicts were encountered
10107
+** while applying the patchset. See comments surrounding the sqlite3_rebaser
10108
+** APIs for further details.
1009310109
*/
1009410110
SQLITE_API int sqlite3changeset_apply(
1009510111
sqlite3 *db, /* Apply change to "main" db of this handle */
1009610112
int nChangeset, /* Size of changeset in bytes */
1009710113
void *pChangeset, /* Changeset blob */
@@ -10103,10 +10119,26 @@
1010310119
void *pCtx, /* Copy of sixth arg to _apply() */
1010410120
int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
1010510121
sqlite3_changeset_iter *p /* Handle describing change and conflict */
1010610122
),
1010710123
void *pCtx /* First argument passed to xConflict */
10124
+);
10125
+SQLITE_API int sqlite3changeset_apply_v2(
10126
+ sqlite3 *db, /* Apply change to "main" db of this handle */
10127
+ int nChangeset, /* Size of changeset in bytes */
10128
+ void *pChangeset, /* Changeset blob */
10129
+ int(*xFilter)(
10130
+ void *pCtx, /* Copy of sixth arg to _apply() */
10131
+ const char *zTab /* Table name */
10132
+ ),
10133
+ int(*xConflict)(
10134
+ void *pCtx, /* Copy of sixth arg to _apply() */
10135
+ int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
10136
+ sqlite3_changeset_iter *p /* Handle describing change and conflict */
10137
+ ),
10138
+ void *pCtx, /* First argument passed to xConflict */
10139
+ void **ppRebase, int *pnRebase
1010810140
);
1010910141
1011010142
/*
1011110143
** CAPI3REF: Constants Passed To The Conflict Handler
1011210144
**
@@ -10201,10 +10233,165 @@
1020110233
*/
1020210234
#define SQLITE_CHANGESET_OMIT 0
1020310235
#define SQLITE_CHANGESET_REPLACE 1
1020410236
#define SQLITE_CHANGESET_ABORT 2
1020510237
10238
+/*
10239
+** CAPI3REF: Rebasing changesets
10240
+** EXPERIMENTAL
10241
+**
10242
+** Suppose there is a site hosting a database in state S0. And that
10243
+** modifications are made that move that database to state S1 and a
10244
+** changeset recorded (the "local" changeset). Then, a changeset based
10245
+** on S0 is received from another site (the "remote" changeset) and
10246
+** applied to the database. The database is then in state
10247
+** (S1+"remote"), where the exact state depends on any conflict
10248
+** resolution decisions (OMIT or REPLACE) made while applying "remote".
10249
+** Rebasing a changeset is to update it to take those conflict
10250
+** resolution decisions into account, so that the same conflicts
10251
+** do not have to be resolved elsewhere in the network.
10252
+**
10253
+** For example, if both the local and remote changesets contain an
10254
+** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
10255
+**
10256
+** local: INSERT INTO t1 VALUES(1, 'v1');
10257
+** remote: INSERT INTO t1 VALUES(1, 'v2');
10258
+**
10259
+** and the conflict resolution is REPLACE, then the INSERT change is
10260
+** removed from the local changeset (it was overridden). Or, if the
10261
+** conflict resolution was "OMIT", then the local changeset is modified
10262
+** to instead contain:
10263
+**
10264
+** UPDATE t1 SET b = 'v2' WHERE a=1;
10265
+**
10266
+** Changes within the local changeset are rebased as follows:
10267
+**
10268
+** <dl>
10269
+** <dt>Local INSERT<dd>
10270
+** This may only conflict with a remote INSERT. If the conflict
10271
+** resolution was OMIT, then add an UPDATE change to the rebased
10272
+** changeset. Or, if the conflict resolution was REPLACE, add
10273
+** nothing to the rebased changeset.
10274
+**
10275
+** <dt>Local DELETE<dd>
10276
+** This may conflict with a remote UPDATE or DELETE. In both cases the
10277
+** only possible resolution is OMIT. If the remote operation was a
10278
+** DELETE, then add no change to the rebased changeset. If the remote
10279
+** operation was an UPDATE, then the old.* fields of change are updated
10280
+** to reflect the new.* values in the UPDATE.
10281
+**
10282
+** <dt>Local UPDATE<dd>
10283
+** This may conflict with a remote UPDATE or DELETE. If it conflicts
10284
+** with a DELETE, and the conflict resolution was OMIT, then the update
10285
+** is changed into an INSERT. Any undefined values in the new.* record
10286
+** from the update change are filled in using the old.* values from
10287
+** the conflicting DELETE. Or, if the conflict resolution was REPLACE,
10288
+** the UPDATE change is simply omitted from the rebased changeset.
10289
+**
10290
+** If conflict is with a remote UPDATE and the resolution is OMIT, then
10291
+** the old.* values are rebased using the new.* values in the remote
10292
+** change. Or, if the resolution is REPLACE, then the change is copied
10293
+** into the rebased changeset with updates to columns also updated by
10294
+** the conflicting remote UPDATE removed. If this means no columns would
10295
+** be updated, the change is omitted.
10296
+** </dl>
10297
+**
10298
+** A local change may be rebased against multiple remote changes
10299
+** simultaneously. If a single key is modified by multiple remote
10300
+** changesets, they are combined as follows before the local changeset
10301
+** is rebased:
10302
+**
10303
+** <ul>
10304
+** <li> If there has been one or more REPLACE resolutions on a
10305
+** key, it is rebased according to a REPLACE.
10306
+**
10307
+** <li> If there have been no REPLACE resolutions on a key, then
10308
+** the local changeset is rebased according to the most recent
10309
+** of the OMIT resolutions.
10310
+** </ul>
10311
+**
10312
+** Note that conflict resolutions from multiple remote changesets are
10313
+** combined on a per-field basis, not per-row. This means that in the
10314
+** case of multiple remote UPDATE operations, some fields of a single
10315
+** local change may be rebased for REPLACE while others are rebased for
10316
+** OMIT.
10317
+**
10318
+** In order to rebase a local changeset, the remote changeset must first
10319
+** be applied to the local database using sqlite3changeset_apply_v2() and
10320
+** the buffer of rebase information captured. Then:
10321
+**
10322
+** <ol>
10323
+** <li> An sqlite3_rebaser object is created by calling
10324
+** sqlite3rebaser_create().
10325
+** <li> The new object is configured with the rebase buffer obtained from
10326
+** sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
10327
+** If the local changeset is to be rebased against multiple remote
10328
+** changesets, then sqlite3rebaser_configure() should be called
10329
+** multiple times, in the same order that the multiple
10330
+** sqlite3changeset_apply_v2() calls were made.
10331
+** <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
10332
+** <li> The sqlite3_rebaser object is deleted by calling
10333
+** sqlite3rebaser_delete().
10334
+** </ol>
10335
+*/
10336
+typedef struct sqlite3_rebaser sqlite3_rebaser;
10337
+
10338
+/*
10339
+** CAPI3REF: Create a changeset rebaser object.
10340
+** EXPERIMENTAL
10341
+**
10342
+** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
10343
+** point to the new object and return SQLITE_OK. Otherwise, if an error
10344
+** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew)
10345
+** to NULL.
10346
+*/
10347
+SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
10348
+
10349
+/*
10350
+** CAPI3REF: Configure a changeset rebaser object.
10351
+** EXPERIMENTAL
10352
+**
10353
+** Configure the changeset rebaser object to rebase changesets according
10354
+** to the conflict resolutions described by buffer pRebase (size nRebase
10355
+** bytes), which must have been obtained from a previous call to
10356
+** sqlite3changeset_apply_v2().
10357
+*/
10358
+SQLITE_API int sqlite3rebaser_configure(
10359
+ sqlite3_rebaser*,
10360
+ int nRebase, const void *pRebase
10361
+);
10362
+
10363
+/*
10364
+** CAPI3REF: Rebase a changeset
10365
+** EXPERIMENTAL
10366
+**
10367
+** Argument pIn must point to a buffer containing a changeset nIn bytes
10368
+** in size. This function allocates and populates a buffer with a copy
10369
+** of the changeset rebased rebased according to the configuration of the
10370
+** rebaser object passed as the first argument. If successful, (*ppOut)
10371
+** is set to point to the new buffer containing the rebased changset and
10372
+** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
10373
+** responsibility of the caller to eventually free the new buffer using
10374
+** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
10375
+** are set to zero and an SQLite error code returned.
10376
+*/
10377
+SQLITE_API int sqlite3rebaser_rebase(
10378
+ sqlite3_rebaser*,
10379
+ int nIn, const void *pIn,
10380
+ int *pnOut, void **ppOut
10381
+);
10382
+
10383
+/*
10384
+** CAPI3REF: Delete a changeset rebaser object.
10385
+** EXPERIMENTAL
10386
+**
10387
+** Delete the changeset rebaser object and all associated resources. There
10388
+** should be one call to this function for each successful invocation
10389
+** of sqlite3rebaser_create().
10390
+*/
10391
+SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p);
10392
+
1020610393
/*
1020710394
** CAPI3REF: Streaming Versions of API functions.
1020810395
**
1020910396
** The six streaming API xxx_strm() functions serve similar purposes to the
1021010397
** corresponding non-streaming API functions:
@@ -10304,10 +10491,26 @@
1030410491
void *pCtx, /* Copy of sixth arg to _apply() */
1030510492
int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
1030610493
sqlite3_changeset_iter *p /* Handle describing change and conflict */
1030710494
),
1030810495
void *pCtx /* First argument passed to xConflict */
10496
+);
10497
+SQLITE_API int sqlite3changeset_apply_v2_strm(
10498
+ sqlite3 *db, /* Apply change to "main" db of this handle */
10499
+ int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
10500
+ void *pIn, /* First arg for xInput */
10501
+ int(*xFilter)(
10502
+ void *pCtx, /* Copy of sixth arg to _apply() */
10503
+ const char *zTab /* Table name */
10504
+ ),
10505
+ int(*xConflict)(
10506
+ void *pCtx, /* Copy of sixth arg to _apply() */
10507
+ int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
10508
+ sqlite3_changeset_iter *p /* Handle describing change and conflict */
10509
+ ),
10510
+ void *pCtx, /* First argument passed to xConflict */
10511
+ void **ppRebase, int *pnRebase
1030910512
);
1031010513
SQLITE_API int sqlite3changeset_concat_strm(
1031110514
int (*xInputA)(void *pIn, void *pData, int *pnData),
1031210515
void *pInA,
1031310516
int (*xInputB)(void *pIn, void *pData, int *pnData),
@@ -10341,10 +10544,17 @@
1034110544
void *pIn
1034210545
);
1034310546
SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
1034410547
int (*xOutput)(void *pOut, const void *pData, int nData),
1034510548
void *pOut
10549
+);
10550
+SQLITE_API int sqlite3rebaser_rebase_strm(
10551
+ sqlite3_rebaser *pRebaser,
10552
+ int (*xInput)(void *pIn, void *pData, int *pnData),
10553
+ void *pIn,
10554
+ int (*xOutput)(void *pOut, const void *pData, int nData),
10555
+ void *pOut
1034610556
);
1034710557
1034810558
1034910559
/*
1035010560
** Make sure we can call this stuff from C++.
1035110561
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -123,11 +123,11 @@
123 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
124 ** [sqlite_version()] and [sqlite_source_id()].
125 */
126 #define SQLITE_VERSION "3.23.0"
127 #define SQLITE_VERSION_NUMBER 3023000
128 #define SQLITE_SOURCE_ID "2018-03-22 17:13:44 eb4f452e354065d610ff57a6a9312ad119b6b0cc467f9dff105f0718bc27ef01"
129
130 /*
131 ** CAPI3REF: Run-Time Library Version Numbers
132 ** KEYWORDS: sqlite3_version sqlite3_sourceid
133 **
@@ -1062,10 +1062,16 @@
1062 ** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
1063 ** ^This file control takes the file descriptor out of batch write mode
1064 ** so that all subsequent write operations are independent.
1065 ** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
1066 ** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
 
 
 
 
 
 
1067 ** </ul>
1068 */
1069 #define SQLITE_FCNTL_LOCKSTATE 1
1070 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
1071 #define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -1096,10 +1102,11 @@
1096 #define SQLITE_FCNTL_WIN32_GET_HANDLE 29
1097 #define SQLITE_FCNTL_PDB 30
1098 #define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE 31
1099 #define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE 32
1100 #define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE 33
 
1101
1102 /* deprecated names */
1103 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1104 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1105 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -8809,11 +8816,11 @@
8809
8810 /*
8811 ** CAPI3REF: Deserialize a database
8812 **
8813 ** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
8814 ** [database connection] D to disconnection from database S and then
8815 ** reopen S as an in-memory database based on the serialization contained
8816 ** in P. The serialized database P is N bytes in size. M is the size of
8817 ** the buffer P, which might be larger than N. If M is larger than N, and
8818 ** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
8819 ** permitted to add content to the in-memory database as long as the total
@@ -9950,23 +9957,22 @@
9950 SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
9951
9952 /*
9953 ** CAPI3REF: Apply A Changeset To A Database
9954 **
9955 ** Apply a changeset to a database. This function attempts to update the
9956 ** "main" database attached to handle db with the changes found in the
9957 ** changeset passed via the second and third arguments.
9958 **
9959 ** The fourth argument (xFilter) passed to this function is the "filter
9960 ** callback". If it is not NULL, then for each table affected by at least one
9961 ** change in the changeset, the filter callback is invoked with
9962 ** the table name as the second argument, and a copy of the context pointer
9963 ** passed as the sixth argument to this function as the first. If the "filter
9964 ** callback" returns zero, then no attempt is made to apply any changes to
9965 ** the table. Otherwise, if the return value is non-zero or the xFilter
9966 ** argument to this function is NULL, all changes related to the table are
9967 ** attempted.
9968 **
9969 ** For each table that is not excluded by the filter callback, this function
9970 ** tests that the target database contains a compatible table. A table is
9971 ** considered compatible if all of the following are true:
9972 **
@@ -10007,11 +10013,11 @@
10007 ** the documentation for the three
10008 ** [SQLITE_CHANGESET_OMIT|available return values] for details.
10009 **
10010 ** <dl>
10011 ** <dt>DELETE Changes<dd>
10012 ** For each DELETE change, this function checks if the target database
10013 ** contains a row with the same primary key value (or values) as the
10014 ** original row values stored in the changeset. If it does, and the values
10015 ** stored in all non-primary key columns also match the values stored in
10016 ** the changeset the row is deleted from the target database.
10017 **
@@ -10052,11 +10058,11 @@
10052 ** This includes the case where the INSERT operation is re-attempted because
10053 ** an earlier call to the conflict handler function returned
10054 ** [SQLITE_CHANGESET_REPLACE].
10055 **
10056 ** <dt>UPDATE Changes<dd>
10057 ** For each UPDATE change, this function checks if the target database
10058 ** contains a row with the same primary key value (or values) as the
10059 ** original row values stored in the changeset. If it does, and the values
10060 ** stored in all modified non-primary key columns also match the values
10061 ** stored in the changeset the row is updated within the target database.
10062 **
@@ -10083,15 +10089,25 @@
10083 ** It is safe to execute SQL statements, including those that write to the
10084 ** table that the callback related to, from within the xConflict callback.
10085 ** This can be used to further customize the applications conflict
10086 ** resolution strategy.
10087 **
10088 ** All changes made by this function are enclosed in a savepoint transaction.
10089 ** If any other error (aside from a constraint failure when attempting to
10090 ** write to the target database) occurs, then the savepoint transaction is
10091 ** rolled back, restoring the target database to its original state, and an
10092 ** SQLite error code returned.
 
 
 
 
 
 
 
 
 
 
10093 */
10094 SQLITE_API int sqlite3changeset_apply(
10095 sqlite3 *db, /* Apply change to "main" db of this handle */
10096 int nChangeset, /* Size of changeset in bytes */
10097 void *pChangeset, /* Changeset blob */
@@ -10103,10 +10119,26 @@
10103 void *pCtx, /* Copy of sixth arg to _apply() */
10104 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
10105 sqlite3_changeset_iter *p /* Handle describing change and conflict */
10106 ),
10107 void *pCtx /* First argument passed to xConflict */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10108 );
10109
10110 /*
10111 ** CAPI3REF: Constants Passed To The Conflict Handler
10112 **
@@ -10201,10 +10233,165 @@
10201 */
10202 #define SQLITE_CHANGESET_OMIT 0
10203 #define SQLITE_CHANGESET_REPLACE 1
10204 #define SQLITE_CHANGESET_ABORT 2
10205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10206 /*
10207 ** CAPI3REF: Streaming Versions of API functions.
10208 **
10209 ** The six streaming API xxx_strm() functions serve similar purposes to the
10210 ** corresponding non-streaming API functions:
@@ -10304,10 +10491,26 @@
10304 void *pCtx, /* Copy of sixth arg to _apply() */
10305 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
10306 sqlite3_changeset_iter *p /* Handle describing change and conflict */
10307 ),
10308 void *pCtx /* First argument passed to xConflict */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10309 );
10310 SQLITE_API int sqlite3changeset_concat_strm(
10311 int (*xInputA)(void *pIn, void *pData, int *pnData),
10312 void *pInA,
10313 int (*xInputB)(void *pIn, void *pData, int *pnData),
@@ -10341,10 +10544,17 @@
10341 void *pIn
10342 );
10343 SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
10344 int (*xOutput)(void *pOut, const void *pData, int nData),
10345 void *pOut
 
 
 
 
 
 
 
10346 );
10347
10348
10349 /*
10350 ** Make sure we can call this stuff from C++.
10351
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -123,11 +123,11 @@
123 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
124 ** [sqlite_version()] and [sqlite_source_id()].
125 */
126 #define SQLITE_VERSION "3.23.0"
127 #define SQLITE_VERSION_NUMBER 3023000
128 #define SQLITE_SOURCE_ID "2018-03-28 15:56:55 eb29b3369e76ec1df25a5484d8ec5fb924e23d5c70aaa4d794b2b17ee187alt1"
129
130 /*
131 ** CAPI3REF: Run-Time Library Version Numbers
132 ** KEYWORDS: sqlite3_version sqlite3_sourceid
133 **
@@ -1062,10 +1062,16 @@
1062 ** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
1063 ** ^This file control takes the file descriptor out of batch write mode
1064 ** so that all subsequent write operations are independent.
1065 ** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
1066 ** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
1067 **
1068 ** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
1069 ** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode causes attempts to obtain
1070 ** a file lock using the xLock or xShmLock methods of the VFS to wait
1071 ** for up to M milliseconds before failing, where M is the single
1072 ** unsigned integer parameter.
1073 ** </ul>
1074 */
1075 #define SQLITE_FCNTL_LOCKSTATE 1
1076 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
1077 #define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
@@ -1096,10 +1102,11 @@
1102 #define SQLITE_FCNTL_WIN32_GET_HANDLE 29
1103 #define SQLITE_FCNTL_PDB 30
1104 #define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE 31
1105 #define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE 32
1106 #define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE 33
1107 #define SQLITE_FCNTL_LOCK_TIMEOUT 34
1108
1109 /* deprecated names */
1110 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1111 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1112 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -8809,11 +8816,11 @@
8816
8817 /*
8818 ** CAPI3REF: Deserialize a database
8819 **
8820 ** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
8821 ** [database connection] D to disconnect from database S and then
8822 ** reopen S as an in-memory database based on the serialization contained
8823 ** in P. The serialized database P is N bytes in size. M is the size of
8824 ** the buffer P, which might be larger than N. If M is larger than N, and
8825 ** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
8826 ** permitted to add content to the in-memory database as long as the total
@@ -9950,23 +9957,22 @@
9957 SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
9958
9959 /*
9960 ** CAPI3REF: Apply A Changeset To A Database
9961 **
9962 ** Apply a changeset or patchset to a database. These functions attempt to
9963 ** update the "main" database attached to handle db with the changes found in
9964 ** the changeset passed via the second and third arguments.
9965 **
9966 ** The fourth argument (xFilter) passed to these functions is the "filter
9967 ** callback". If it is not NULL, then for each table affected by at least one
9968 ** change in the changeset, the filter callback is invoked with
9969 ** the table name as the second argument, and a copy of the context pointer
9970 ** passed as the sixth argument as the first. If the "filter callback"
9971 ** returns zero, then no attempt is made to apply any changes to the table.
9972 ** Otherwise, if the return value is non-zero or the xFilter argument to
9973 ** is NULL, all changes related to the table are attempted.
 
9974 **
9975 ** For each table that is not excluded by the filter callback, this function
9976 ** tests that the target database contains a compatible table. A table is
9977 ** considered compatible if all of the following are true:
9978 **
@@ -10007,11 +10013,11 @@
10013 ** the documentation for the three
10014 ** [SQLITE_CHANGESET_OMIT|available return values] for details.
10015 **
10016 ** <dl>
10017 ** <dt>DELETE Changes<dd>
10018 ** For each DELETE change, the function checks if the target database
10019 ** contains a row with the same primary key value (or values) as the
10020 ** original row values stored in the changeset. If it does, and the values
10021 ** stored in all non-primary key columns also match the values stored in
10022 ** the changeset the row is deleted from the target database.
10023 **
@@ -10052,11 +10058,11 @@
10058 ** This includes the case where the INSERT operation is re-attempted because
10059 ** an earlier call to the conflict handler function returned
10060 ** [SQLITE_CHANGESET_REPLACE].
10061 **
10062 ** <dt>UPDATE Changes<dd>
10063 ** For each UPDATE change, the function checks if the target database
10064 ** contains a row with the same primary key value (or values) as the
10065 ** original row values stored in the changeset. If it does, and the values
10066 ** stored in all modified non-primary key columns also match the values
10067 ** stored in the changeset the row is updated within the target database.
10068 **
@@ -10083,15 +10089,25 @@
10089 ** It is safe to execute SQL statements, including those that write to the
10090 ** table that the callback related to, from within the xConflict callback.
10091 ** This can be used to further customize the applications conflict
10092 ** resolution strategy.
10093 **
10094 ** All changes made by these functions are enclosed in a savepoint transaction.
10095 ** If any other error (aside from a constraint failure when attempting to
10096 ** write to the target database) occurs, then the savepoint transaction is
10097 ** rolled back, restoring the target database to its original state, and an
10098 ** SQLite error code returned.
10099 **
10100 ** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
10101 ** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
10102 ** may set (*ppRebase) to point to a "rebase" that may be used with the
10103 ** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
10104 ** is set to the size of the buffer in bytes. It is the responsibility of the
10105 ** caller to eventually free any such buffer using sqlite3_free(). The buffer
10106 ** is only allocated and populated if one or more conflicts were encountered
10107 ** while applying the patchset. See comments surrounding the sqlite3_rebaser
10108 ** APIs for further details.
10109 */
10110 SQLITE_API int sqlite3changeset_apply(
10111 sqlite3 *db, /* Apply change to "main" db of this handle */
10112 int nChangeset, /* Size of changeset in bytes */
10113 void *pChangeset, /* Changeset blob */
@@ -10103,10 +10119,26 @@
10119 void *pCtx, /* Copy of sixth arg to _apply() */
10120 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
10121 sqlite3_changeset_iter *p /* Handle describing change and conflict */
10122 ),
10123 void *pCtx /* First argument passed to xConflict */
10124 );
10125 SQLITE_API int sqlite3changeset_apply_v2(
10126 sqlite3 *db, /* Apply change to "main" db of this handle */
10127 int nChangeset, /* Size of changeset in bytes */
10128 void *pChangeset, /* Changeset blob */
10129 int(*xFilter)(
10130 void *pCtx, /* Copy of sixth arg to _apply() */
10131 const char *zTab /* Table name */
10132 ),
10133 int(*xConflict)(
10134 void *pCtx, /* Copy of sixth arg to _apply() */
10135 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
10136 sqlite3_changeset_iter *p /* Handle describing change and conflict */
10137 ),
10138 void *pCtx, /* First argument passed to xConflict */
10139 void **ppRebase, int *pnRebase
10140 );
10141
10142 /*
10143 ** CAPI3REF: Constants Passed To The Conflict Handler
10144 **
@@ -10201,10 +10233,165 @@
10233 */
10234 #define SQLITE_CHANGESET_OMIT 0
10235 #define SQLITE_CHANGESET_REPLACE 1
10236 #define SQLITE_CHANGESET_ABORT 2
10237
10238 /*
10239 ** CAPI3REF: Rebasing changesets
10240 ** EXPERIMENTAL
10241 **
10242 ** Suppose there is a site hosting a database in state S0. And that
10243 ** modifications are made that move that database to state S1 and a
10244 ** changeset recorded (the "local" changeset). Then, a changeset based
10245 ** on S0 is received from another site (the "remote" changeset) and
10246 ** applied to the database. The database is then in state
10247 ** (S1+"remote"), where the exact state depends on any conflict
10248 ** resolution decisions (OMIT or REPLACE) made while applying "remote".
10249 ** Rebasing a changeset is to update it to take those conflict
10250 ** resolution decisions into account, so that the same conflicts
10251 ** do not have to be resolved elsewhere in the network.
10252 **
10253 ** For example, if both the local and remote changesets contain an
10254 ** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
10255 **
10256 ** local: INSERT INTO t1 VALUES(1, 'v1');
10257 ** remote: INSERT INTO t1 VALUES(1, 'v2');
10258 **
10259 ** and the conflict resolution is REPLACE, then the INSERT change is
10260 ** removed from the local changeset (it was overridden). Or, if the
10261 ** conflict resolution was "OMIT", then the local changeset is modified
10262 ** to instead contain:
10263 **
10264 ** UPDATE t1 SET b = 'v2' WHERE a=1;
10265 **
10266 ** Changes within the local changeset are rebased as follows:
10267 **
10268 ** <dl>
10269 ** <dt>Local INSERT<dd>
10270 ** This may only conflict with a remote INSERT. If the conflict
10271 ** resolution was OMIT, then add an UPDATE change to the rebased
10272 ** changeset. Or, if the conflict resolution was REPLACE, add
10273 ** nothing to the rebased changeset.
10274 **
10275 ** <dt>Local DELETE<dd>
10276 ** This may conflict with a remote UPDATE or DELETE. In both cases the
10277 ** only possible resolution is OMIT. If the remote operation was a
10278 ** DELETE, then add no change to the rebased changeset. If the remote
10279 ** operation was an UPDATE, then the old.* fields of change are updated
10280 ** to reflect the new.* values in the UPDATE.
10281 **
10282 ** <dt>Local UPDATE<dd>
10283 ** This may conflict with a remote UPDATE or DELETE. If it conflicts
10284 ** with a DELETE, and the conflict resolution was OMIT, then the update
10285 ** is changed into an INSERT. Any undefined values in the new.* record
10286 ** from the update change are filled in using the old.* values from
10287 ** the conflicting DELETE. Or, if the conflict resolution was REPLACE,
10288 ** the UPDATE change is simply omitted from the rebased changeset.
10289 **
10290 ** If conflict is with a remote UPDATE and the resolution is OMIT, then
10291 ** the old.* values are rebased using the new.* values in the remote
10292 ** change. Or, if the resolution is REPLACE, then the change is copied
10293 ** into the rebased changeset with updates to columns also updated by
10294 ** the conflicting remote UPDATE removed. If this means no columns would
10295 ** be updated, the change is omitted.
10296 ** </dl>
10297 **
10298 ** A local change may be rebased against multiple remote changes
10299 ** simultaneously. If a single key is modified by multiple remote
10300 ** changesets, they are combined as follows before the local changeset
10301 ** is rebased:
10302 **
10303 ** <ul>
10304 ** <li> If there has been one or more REPLACE resolutions on a
10305 ** key, it is rebased according to a REPLACE.
10306 **
10307 ** <li> If there have been no REPLACE resolutions on a key, then
10308 ** the local changeset is rebased according to the most recent
10309 ** of the OMIT resolutions.
10310 ** </ul>
10311 **
10312 ** Note that conflict resolutions from multiple remote changesets are
10313 ** combined on a per-field basis, not per-row. This means that in the
10314 ** case of multiple remote UPDATE operations, some fields of a single
10315 ** local change may be rebased for REPLACE while others are rebased for
10316 ** OMIT.
10317 **
10318 ** In order to rebase a local changeset, the remote changeset must first
10319 ** be applied to the local database using sqlite3changeset_apply_v2() and
10320 ** the buffer of rebase information captured. Then:
10321 **
10322 ** <ol>
10323 ** <li> An sqlite3_rebaser object is created by calling
10324 ** sqlite3rebaser_create().
10325 ** <li> The new object is configured with the rebase buffer obtained from
10326 ** sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
10327 ** If the local changeset is to be rebased against multiple remote
10328 ** changesets, then sqlite3rebaser_configure() should be called
10329 ** multiple times, in the same order that the multiple
10330 ** sqlite3changeset_apply_v2() calls were made.
10331 ** <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
10332 ** <li> The sqlite3_rebaser object is deleted by calling
10333 ** sqlite3rebaser_delete().
10334 ** </ol>
10335 */
10336 typedef struct sqlite3_rebaser sqlite3_rebaser;
10337
10338 /*
10339 ** CAPI3REF: Create a changeset rebaser object.
10340 ** EXPERIMENTAL
10341 **
10342 ** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
10343 ** point to the new object and return SQLITE_OK. Otherwise, if an error
10344 ** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew)
10345 ** to NULL.
10346 */
10347 SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
10348
10349 /*
10350 ** CAPI3REF: Configure a changeset rebaser object.
10351 ** EXPERIMENTAL
10352 **
10353 ** Configure the changeset rebaser object to rebase changesets according
10354 ** to the conflict resolutions described by buffer pRebase (size nRebase
10355 ** bytes), which must have been obtained from a previous call to
10356 ** sqlite3changeset_apply_v2().
10357 */
10358 SQLITE_API int sqlite3rebaser_configure(
10359 sqlite3_rebaser*,
10360 int nRebase, const void *pRebase
10361 );
10362
10363 /*
10364 ** CAPI3REF: Rebase a changeset
10365 ** EXPERIMENTAL
10366 **
10367 ** Argument pIn must point to a buffer containing a changeset nIn bytes
10368 ** in size. This function allocates and populates a buffer with a copy
10369 ** of the changeset rebased rebased according to the configuration of the
10370 ** rebaser object passed as the first argument. If successful, (*ppOut)
10371 ** is set to point to the new buffer containing the rebased changset and
10372 ** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
10373 ** responsibility of the caller to eventually free the new buffer using
10374 ** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
10375 ** are set to zero and an SQLite error code returned.
10376 */
10377 SQLITE_API int sqlite3rebaser_rebase(
10378 sqlite3_rebaser*,
10379 int nIn, const void *pIn,
10380 int *pnOut, void **ppOut
10381 );
10382
10383 /*
10384 ** CAPI3REF: Delete a changeset rebaser object.
10385 ** EXPERIMENTAL
10386 **
10387 ** Delete the changeset rebaser object and all associated resources. There
10388 ** should be one call to this function for each successful invocation
10389 ** of sqlite3rebaser_create().
10390 */
10391 SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p);
10392
10393 /*
10394 ** CAPI3REF: Streaming Versions of API functions.
10395 **
10396 ** The six streaming API xxx_strm() functions serve similar purposes to the
10397 ** corresponding non-streaming API functions:
@@ -10304,10 +10491,26 @@
10491 void *pCtx, /* Copy of sixth arg to _apply() */
10492 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
10493 sqlite3_changeset_iter *p /* Handle describing change and conflict */
10494 ),
10495 void *pCtx /* First argument passed to xConflict */
10496 );
10497 SQLITE_API int sqlite3changeset_apply_v2_strm(
10498 sqlite3 *db, /* Apply change to "main" db of this handle */
10499 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
10500 void *pIn, /* First arg for xInput */
10501 int(*xFilter)(
10502 void *pCtx, /* Copy of sixth arg to _apply() */
10503 const char *zTab /* Table name */
10504 ),
10505 int(*xConflict)(
10506 void *pCtx, /* Copy of sixth arg to _apply() */
10507 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
10508 sqlite3_changeset_iter *p /* Handle describing change and conflict */
10509 ),
10510 void *pCtx, /* First argument passed to xConflict */
10511 void **ppRebase, int *pnRebase
10512 );
10513 SQLITE_API int sqlite3changeset_concat_strm(
10514 int (*xInputA)(void *pIn, void *pData, int *pnData),
10515 void *pInA,
10516 int (*xInputB)(void *pIn, void *pData, int *pnData),
@@ -10341,10 +10544,17 @@
10544 void *pIn
10545 );
10546 SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
10547 int (*xOutput)(void *pOut, const void *pData, int nData),
10548 void *pOut
10549 );
10550 SQLITE_API int sqlite3rebaser_rebase_strm(
10551 sqlite3_rebaser *pRebaser,
10552 int (*xInput)(void *pIn, void *pData, int *pnData),
10553 void *pIn,
10554 int (*xOutput)(void *pOut, const void *pData, int nData),
10555 void *pOut
10556 );
10557
10558
10559 /*
10560 ** Make sure we can call this stuff from C++.
10561

Keyboard Shortcuts

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