Update this branch with the latest changes from sorter-opt.
FossilOrigin-Name: 08c0b19b89ea62d40c85bab64d9c80e02eaa8d5d
diff --git a/Makefile.in b/Makefile.in
index 4ac0693..295a6bc 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -536,6 +536,9 @@
-o $@ $(TOP)/src/shell.c libsqlite3.la \
$(LIBREADLINE) $(TLIBS) -rpath "$(libdir)"
+sqldiff$(EXE): $(TOP)/tool/sqldiff.c sqlite3.c sqlite3.h
+ $(LTLINK) -o $@ $(TOP)/tool/sqldiff.c sqlite3.c $(TLIBS)
+
mptester$(EXE): sqlite3.c $(TOP)/mptest/mptest.c
$(LTLINK) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \
$(TLIBS) -rpath "$(libdir)"
@@ -1059,7 +1062,7 @@
$(INSTALL) -m 0644 sqlite3.pc $(DESTDIR)$(pkgconfigdir)
pkgIndex.tcl:
- echo 'package ifneeded sqlite3 $(RELEASE) [list load $(TCLLIBDIR)/libtclsqlite3.so sqlite3]' > $@
+ echo 'package ifneeded sqlite3 $(RELEASE) [list load $(TCLLIBDIR)/libtclsqlite3$(SHLIB_SUFFIX) sqlite3]' > $@
tcl_install: lib_install libtclsqlite3.la pkgIndex.tcl
$(INSTALL) -d $(DESTDIR)$(TCLLIBDIR)
$(LTINSTALL) libtclsqlite3.la $(DESTDIR)$(TCLLIBDIR)
diff --git a/Makefile.msc b/Makefile.msc
index 837fc00..b49a3eb 100644
--- a/Makefile.msc
+++ b/Makefile.msc
@@ -331,7 +331,7 @@
# These are additional compiler options used for the core library.
#
!IFNDEF CORE_COMPILE_OPTS
-!IF $(USE_STDCALL)!=0
+!IF $(DYNAMIC_SHELL)!=0
CORE_COMPILE_OPTS = $(CORE_CCONV_OPTS) -DSQLITE_API=__declspec(dllexport)
!ELSE
CORE_COMPILE_OPTS = $(CORE_CCONV_OPTS)
@@ -342,7 +342,7 @@
# when linking.
#
!IFNDEF CORE_LINK_DEP
-!IF $(USE_STDCALL)!=0
+!IF $(DYNAMIC_SHELL)!=0
CORE_LINK_DEP =
!ELSE
CORE_LINK_DEP = sqlite3.def
@@ -352,7 +352,7 @@
# These are additional linker options used for the core library.
#
!IFNDEF CORE_LINK_OPTS
-!IF $(USE_STDCALL)!=0
+!IF $(DYNAMIC_SHELL)!=0
CORE_LINK_OPTS =
!ELSE
CORE_LINK_OPTS = /DEF:sqlite3.def
@@ -1154,10 +1154,27 @@
$(LTLINK) $(SHELL_COMPILE_OPTS) $(READLINE_FLAGS) $(TOP)\src\shell.c \
/link /pdb:sqlite3sh.pdb $(LTLINKOPTS) $(SHELL_LINK_OPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LIBREADLINE) $(LTLIBS) $(TLIBS)
+sqldiff.exe: $(TOP)\tool\sqldiff.c sqlite3.c sqlite3.h
+ $(LTLINK) $(TOP)\tool\sqldiff.c sqlite3.c
+
mptester.exe: $(TOP)\mptest\mptest.c $(SHELL_CORE_DEP) $(LIBRESOBJS) sqlite3.h
$(LTLINK) $(SHELL_COMPILE_OPTS) $(TOP)\mptest\mptest.c \
/link $(LTLINKOPTS) $(LTLIBPATHS) $(SHELL_LINK_OPTS) $(LIBRESOBJS) $(LIBREADLINE) $(LTLIBS) $(TLIBS)
+MPTEST1 = mptester mptest.db $(TOP)/mptest/crash01.test --repeat 20
+MPTEST2 = mptester mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20
+
+mptest: mptester.exe
+ del /Q mptest.db 2>NUL
+ $(MPTEST1) --journalmode DELETE
+ $(MPTEST2) --journalmode WAL
+ $(MPTEST1) --journalmode WAL
+ $(MPTEST2) --journalmode PERSIST
+ $(MPTEST1) --journalmode PERSIST
+ $(MPTEST2) --journalmode TRUNCATE
+ $(MPTEST1) --journalmode TRUNCATE
+ $(MPTEST2) --journalmode DELETE
+
# This target creates a directory named "tsrc" and fills it with
# copies of all of the C source code and header files needed to
# build on the target system. Some of the C source code and header
diff --git a/configure b/configure
index 0c519e2..d259324 100755
--- a/configure
+++ b/configure
Binary files differ
diff --git a/configure.ac b/configure.ac
index 00ecf45..6a11888 100644
--- a/configure.ac
+++ b/configure.ac
@@ -430,6 +430,7 @@
AC_SUBST(TCL_STUB_LIB_FILE)
AC_SUBST(TCL_STUB_LIB_FLAG)
AC_SUBST(TCL_STUB_LIB_SPEC)
+ AC_SUBST(TCL_SHLIB_SUFFIX)
fi
fi
if test "${use_tcl}" = "no" ; then
diff --git a/ext/fts3/fts3.c b/ext/fts3/fts3.c
index c924632..42b9663 100644
--- a/ext/fts3/fts3.c
+++ b/ext/fts3/fts3.c
@@ -1019,7 +1019,8 @@
const char *zTbl, /* Name of content table */
const char ***pazCol, /* OUT: Malloc'd array of column names */
int *pnCol, /* OUT: Size of array *pazCol */
- int *pnStr /* OUT: Bytes of string content */
+ int *pnStr, /* OUT: Bytes of string content */
+ char **pzErr /* OUT: error message */
){
int rc = SQLITE_OK; /* Return code */
char *zSql; /* "SELECT *" statement on zTbl */
@@ -1030,6 +1031,9 @@
rc = SQLITE_NOMEM;
}else{
rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
+ if( rc!=SQLITE_OK ){
+ *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
+ }
}
sqlite3_free(zSql);
@@ -1108,7 +1112,7 @@
const char **aCol; /* Array of column names */
sqlite3_tokenizer *pTokenizer = 0; /* Tokenizer for this table */
- int nIndex; /* Size of aIndex[] array */
+ int nIndex = 0; /* Size of aIndex[] array */
struct Fts3Index *aIndex = 0; /* Array of indexes for this table */
/* The results of parsing supported FTS4 key=value options: */
@@ -1281,7 +1285,7 @@
if( nCol==0 ){
sqlite3_free((void*)aCol);
aCol = 0;
- rc = fts3ContentColumns(db, argv[1], zContent, &aCol, &nCol, &nString);
+ rc = fts3ContentColumns(db, argv[1], zContent,&aCol,&nCol,&nString,pzErr);
/* If a languageid= option was specified, remove the language id
** column from the aCol[] array. */
@@ -2502,26 +2506,33 @@
**
** The right-hand input doclist is overwritten by this function.
*/
-static void fts3DoclistPhraseMerge(
+static int fts3DoclistPhraseMerge(
int bDescDoclist, /* True if arguments are desc */
int nDist, /* Distance from left to right (1=adjacent) */
char *aLeft, int nLeft, /* Left doclist */
- char *aRight, int *pnRight /* IN/OUT: Right/output doclist */
+ char **paRight, int *pnRight /* IN/OUT: Right/output doclist */
){
sqlite3_int64 i1 = 0;
sqlite3_int64 i2 = 0;
sqlite3_int64 iPrev = 0;
+ char *aRight = *paRight;
char *pEnd1 = &aLeft[nLeft];
char *pEnd2 = &aRight[*pnRight];
char *p1 = aLeft;
char *p2 = aRight;
char *p;
int bFirstOut = 0;
- char *aOut = aRight;
+ char *aOut;
assert( nDist>0 );
-
+ if( bDescDoclist ){
+ aOut = sqlite3_malloc(*pnRight + FTS3_VARINT_MAX);
+ if( aOut==0 ) return SQLITE_NOMEM;
+ }else{
+ aOut = aRight;
+ }
p = aOut;
+
fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
@@ -2550,6 +2561,12 @@
}
*pnRight = (int)(p - aOut);
+ if( bDescDoclist ){
+ sqlite3_free(aRight);
+ *paRight = aOut;
+ }
+
+ return SQLITE_OK;
}
/*
@@ -2674,8 +2691,22 @@
){
if( pTS->aaOutput[0]==0 ){
/* If this is the first term selected, copy the doclist to the output
- ** buffer using memcpy(). */
- pTS->aaOutput[0] = sqlite3_malloc(nDoclist);
+ ** buffer using memcpy().
+ **
+ ** Add FTS3_VARINT_MAX bytes of unused space to the end of the
+ ** allocation. This is so as to ensure that the buffer is big enough
+ ** to hold the current doclist AND'd with any other doclist. If the
+ ** doclists are stored in order=ASC order, this padding would not be
+ ** required (since the size of [doclistA AND doclistB] is always less
+ ** than or equal to the size of [doclistA] in that case). But this is
+ ** not true for order=DESC. For example, a doclist containing (1, -1)
+ ** may be smaller than (-1), as in the first example the -1 may be stored
+ ** as a single-byte delta, whereas in the second it must be stored as a
+ ** FTS3_VARINT_MAX byte varint.
+ **
+ ** Similar padding is added in the fts3DoclistOrMerge() function.
+ */
+ pTS->aaOutput[0] = sqlite3_malloc(nDoclist + FTS3_VARINT_MAX + 1);
pTS->anOutput[0] = nDoclist;
if( pTS->aaOutput[0] ){
memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
@@ -3931,14 +3962,17 @@
** This function assumes that pList points to a buffer allocated using
** sqlite3_malloc(). This function takes responsibility for eventually
** freeing the buffer.
+**
+** SQLITE_OK is returned if successful, or SQLITE_NOMEM if an error occurs.
*/
-static void fts3EvalPhraseMergeToken(
+static int fts3EvalPhraseMergeToken(
Fts3Table *pTab, /* FTS Table pointer */
Fts3Phrase *p, /* Phrase to merge pList/nList into */
int iToken, /* Token pList/nList corresponds to */
char *pList, /* Pointer to doclist */
int nList /* Number of bytes in pList */
){
+ int rc = SQLITE_OK;
assert( iToken!=p->iDoclistToken );
if( pList==0 ){
@@ -3977,13 +4011,16 @@
nDiff = p->iDoclistToken - iToken;
}
- fts3DoclistPhraseMerge(pTab->bDescIdx, nDiff, pLeft, nLeft, pRight,&nRight);
+ rc = fts3DoclistPhraseMerge(
+ pTab->bDescIdx, nDiff, pLeft, nLeft, &pRight, &nRight
+ );
sqlite3_free(pLeft);
p->doclist.aAll = pRight;
p->doclist.nAll = nRight;
}
if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
+ return rc;
}
/*
@@ -4009,7 +4046,7 @@
char *pThis = 0;
rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
if( rc==SQLITE_OK ){
- fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
+ rc = fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
}
}
assert( pToken->pSegcsr==0 );
@@ -4812,8 +4849,12 @@
rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
assert( rc==SQLITE_OK || pList==0 );
if( rc==SQLITE_OK ){
+ rc = fts3EvalPhraseMergeToken(
+ pTab, pTC->pPhrase, pTC->iToken,pList,nList
+ );
+ }
+ if( rc==SQLITE_OK ){
int nCount;
- fts3EvalPhraseMergeToken(pTab, pTC->pPhrase, pTC->iToken,pList,nList);
nCount = fts3DoclistCountDocids(
pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
);
diff --git a/main.mk b/main.mk
index 7d21dbb..c5328dc 100644
--- a/main.mk
+++ b/main.mk
@@ -404,10 +404,27 @@
$(TOP)/src/shell.c \
libsqlite3.a $(LIBREADLINE) $(TLIBS) $(THREADLIB)
+sqldiff$(EXE): $(TOP)/tool/sqldiff.c sqlite3.c sqlite3.h
+ $(TCCX) -o sqldiff$(EXE) -DSQLITE_THREADSAFE=0 \
+ $(TOP)/tool/sqldiff.c sqlite3.c $(TLIBS) $(THREADLIB)
+
mptester$(EXE): sqlite3.c $(TOP)/mptest/mptest.c
$(TCCX) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \
$(TLIBS) $(THREADLIB)
+MPTEST1=./mptester$(EXE) mptest.db $(TOP)/mptest/crash01.test --repeat 20
+MPTEST2=./mptester$(EXE) mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20
+mptest: mptester$(EXE)
+ rm -f mptest.db
+ $(MPTEST1) --journalmode DELETE
+ $(MPTEST2) --journalmode WAL
+ $(MPTEST1) --journalmode WAL
+ $(MPTEST2) --journalmode PERSIST
+ $(MPTEST1) --journalmode PERSIST
+ $(MPTEST2) --journalmode TRUNCATE
+ $(MPTEST1) --journalmode TRUNCATE
+ $(MPTEST2) --journalmode DELETE
+
sqlite3.o: sqlite3.c
$(TCCX) -I. -c sqlite3.c
diff --git a/manifest b/manifest
index 896a9a9..4a301e6 100644
--- a/manifest
+++ b/manifest
@@ -1,9 +1,9 @@
-C Merge\ssorter\soptimizations\swith\sthis\sbranch.
-D 2015-03-30T15:45:45.183
+C Update\sthis\sbranch\swith\sthe\slatest\schanges\sfrom\ssorter-opt.
+D 2015-04-10T08:28:24.183
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
-F Makefile.in 88a3e6261286db378fdffa1124cad11b3c05f5bb
+F Makefile.in 5f78b1ab81b64e7c57a75d170832443e66c0880a
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
-F Makefile.msc 3481e2dd260968742c715542db178c2bdd5db99f
+F Makefile.msc fa6a6de11af800d89f86e8a4266fd40a46008347
F Makefile.vxworks e1b65dea203f054e71653415bd8f96dcaed47858
F README.md d58e3bebc0a4145e0f2a87994015fdb575a8e866
F VERSION 319eb1ced4b4d17a67730f2b7b85f15c1346cb60
@@ -38,8 +38,8 @@
F config.guess 226d9a188c6196f3033ffc651cbc9dcee1a42977
F config.h.in 42b71ad3fe21c9e88fa59e8458ca1a6bc72eb0c0
F config.sub 9ebe4c3b3dab6431ece34f16828b594fb420da55
-F configure 613b220c2f2c7adcd50eb5ee4144ab581a150b47 x
-F configure.ac 6a8d145aea6d81f0b90013340780e43ed74fd5f4
+F configure 8b18c2378805a1d8aaca85d293671f450dd3c723 x
+F configure.ac 0b775d383c536bbaafc1e46dd3cbb81a7ea11aeb
F contrib/sqlitecon.tcl 210a913ad63f9f991070821e599d600bd913e0ad
F doc/lemon.html 334dbf6621b8fb8790297ec1abf3cfa4621709d1
F doc/pager-invariants.txt 27fed9a70ddad2088750c4a2b493b63853da2710
@@ -78,7 +78,7 @@
F ext/fts3/README.syntax a19711dc5458c20734b8e485e75fb1981ec2427a
F ext/fts3/README.tokenizers e0a8b81383ea60d0334d274fadf305ea14a8c314
F ext/fts3/README.txt 8c18f41574404623b76917b9da66fcb0ab38328d
-F ext/fts3/fts3.c 2a1cf23133d0c75ce296d17440c44115f8413ec7
+F ext/fts3/fts3.c 57d863c3bd360e575ecc293570af7c9b0bdd2209
F ext/fts3/fts3.h 3a10a0af180d502cecc50df77b1b22df142817fe
F ext/fts3/fts3Int.h 394858c12a17740f7a1f6bd372c4606d4425a8d1
F ext/fts3/fts3_aux.c 5c211e17a64885faeb16b9ba7772f9d5445c2365
@@ -152,7 +152,7 @@
F install-sh 9d4de14ab9fb0facae2f48780b874848cbf2f895 x
F ltmain.sh 3ff0879076df340d2e23ae905484d8c15d5fdea8
F magic.txt 8273bf49ba3b0c8559cb2774495390c31fd61c60
-F main.mk 04c49c495795b18a7b70053eef285be1e4d43fa4
+F main.mk ddffac494a82d42772df9fe30d3a78acf4f7cb41
F mkopcodec.awk c2ff431854d702cdd2d779c9c0d1f58fa16fa4ea
F mkopcodeh.awk c6b3fa301db6ef7ac916b14c60868aeaec1337b5
F mkso.sh fd21c06b063bb16a5d25deea1752c2da6ac3ed83
@@ -160,7 +160,7 @@
F mptest/config02.test 4415dfe36c48785f751e16e32c20b077c28ae504
F mptest/crash01.test 61e61469e257df0850df4293d7d4d6c2af301421
F mptest/crash02.subtest f4ef05adcd15d60e5d2bd654204f2c008b519df8
-F mptest/mptest.c 1e464f41f1bbc6578d6925043da56170f83aea96
+F mptest/mptest.c dae6de83eddac3ef97fc4111632f6066760f939a
F mptest/multiwrite01.test dab5c5f8f9534971efce679152c5146da265222d
F spec.template 86a4a43b99ebb3e75e6b9a735d5fd293a24e90ca
F sqlite.pc.in 42b7bf0d02e08b9e77734a47798d1a55a9e0716b
@@ -173,12 +173,12 @@
F src/backup.c ff743689c4d6c5cb55ad42ed9d174b2b3e71f1e3
F src/bitvec.c 19a4ba637bd85f8f63fc8c9bae5ade9fb05ec1cb
F src/btmutex.c 45a968cc85afed9b5e6cf55bf1f42f8d18107f79
-F src/btree.c 4f305e554d7d207375c3e29ab0335bd5a473a125
+F src/btree.c 2caf598165f3608fde8abac2b243826616ce54b7
F src/btree.h 969adc948e89e449220ff0ff724c94bb2a52e9f1
-F src/btreeInt.h 2bfefc01875d8da066504c233ec259fcb3b2ef72
-F src/build.c 0419bba592c22f6d00e6d57a2ca7136720d02c1a
+F src/btreeInt.h 973a22a6fd61350b454ad614832b1f0a5e25a1e4
+F src/build.c fa4795bc795077388aa4e746e1b25ef97bc10489
F src/callback.c 7b44ce59674338ad48b0e84e7b72f935ea4f68b0
-F src/complete.c 198a0066ba60ab06fc00fba1998d870a4d575463
+F src/complete.c a5cf5b4b56390cfb7b8636e8f7ddef90258dd575
F src/ctime.c 98f89724adc891a1a4c655bee04e33e716e05887
F src/date.c e4d50b3283696836ec1036b695ead9a19e37a5ac
F src/delete.c 37964e6c1d73ff49cbea9ff690c9605fb15f600e
@@ -190,13 +190,13 @@
F src/hash.c 4263fbc955f26c2e8cdc0cf214bc42435aa4e4f5
F src/hash.h c8f3c31722cf3277d03713909761e152a5b81094
F src/hwtime.h d32741c8f4df852c7d959236615444e2b1063b08
-F src/insert.c 9f92c04e9bc6056ffd6129767749f2f16d5cd309
+F src/insert.c 5be66348c733c6f6cf9281aa99a6d49de3fc1897
F src/journal.c b4124532212b6952f42eb2c12fa3c25701d8ba8d
F src/legacy.c ba1863ea58c4c840335a84ec276fc2b25e22bc4e
F src/lempar.c 7274c97d24bb46631e504332ccd3bd1b37841770
F src/loadext.c 86bd4e2fccd520b748cba52492ab60c4a770f660
-F src/main.c 569d45ba9eb4fbdd631d53f440bcdb4a35ab1505
-F src/malloc.c e818a0db9ac0898f9dc74002f3a5baca32232d05
+F src/main.c 40e333960d53f7d50ee8ce09d40431c87ea653f2
+F src/malloc.c 6a370b83d54e4bbf6f94021221c2a311cff26a18
F src/mem0.c 6a55ebe57c46ca1a7d98da93aaa07f99f1059645
F src/mem1.c abe6ee469b6c5a35c7f22bfeb9c9bac664a1c987
F src/mem2.c f1940d9e91948dd6a908fbb9ce3835c36b5d83c3
@@ -214,8 +214,8 @@
F src/os.h 3e57a24e2794a94d3cf2342c6d9a884888cd96bf
F src/os_common.h 92815ed65f805560b66166e3583470ff94478f04
F src/os_setup.h c9d4553b5aaa6f73391448b265b89bed0b890faa
-F src/os_unix.c a4dadbc2da41599e99093e91e276c38c17a73b89
-F src/os_win.c 8223e7db5b7c4a81d8b161098ac3959400434cdb
+F src/os_unix.c 25b80a3d167da44226a2084dc9e89a6cb1f02e2e
+F src/os_win.c 03d27be3a20048ef52a648d5f0a15f5edda9f2a3
F src/os_win.h eb7a47aa17b26b77eb97e4823f20a00b8bda12ca
F src/pager.c 4120a49ecd37697e28f5ed807f470b9c0b88410c
F src/pager.h c3476e7c89cdf1c6914e50a11f3714e30b4e0a77
@@ -226,16 +226,16 @@
F src/pragma.c ac4f3f856b4234e85f55b0f069698a4766011100
F src/pragma.h 09c89bca58e9a44de2116cc8272b8d454657129f
F src/prepare.c 173a5a499138451b2561614ecb87d78f9f4644b9
-F src/printf.c 8da9a2687a396daa19860f4dc90975d319304744
+F src/printf.c 8ae1fa9d30c1200a9268a390ba9e9cea9197b27a
F src/random.c ba2679f80ec82c4190062d756f22d0c358180696
-F src/resolve.c f4d79e31ffa5820c2e3d1740baa5e9b190425f2b
+F src/resolve.c 41aa91af56d960e9414ce1d7c17cfb68e0d1c6cb
F src/rowset.c eccf6af6d620aaa4579bd3b72c1b6395d9e9fa1e
-F src/select.c 72ffb62e2879956302140e9f6e6ae88aee36b0e5
-F src/shell.c 3ae1e53878d2804fe77b8c8f1f6ca287a0e5d80e
+F src/select.c c28c52e353287434fac8473e56ee4be848d12c9d
+F src/shell.c 84a1593bd86aaa14f4da8a8f9b16fbc239d262aa
F src/sqlite.h.in 278602140d49575e8708e643161f4263e428a02a
F src/sqlite3.rc 992c9f5fb8285ae285d6be28240a7e8d3a7f2bad
F src/sqlite3ext.h 17d487c3c91b0b8c584a32fbeb393f6f795eea7d
-F src/sqliteInt.h c622684344dd0f008ba5198342426aacec36d9f2
+F src/sqliteInt.h 6ee18690cb02494d7aa381584102701ec783fc5d
F src/sqliteLimit.h 216557999cb45f2e3578ed53ebefe228d779cb46
F src/status.c f266ad8a2892d659b74f0f50cb6a88b6e7c12179
F src/table.c e7a09215315a978057fb42c640f890160dbcc45e
@@ -288,21 +288,21 @@
F src/test_wsd.c 41cadfd9d97fe8e3e4e44f61a4a8ccd6f7ca8fe9
F src/threads.c 6bbcc9fe50c917864d48287b4792d46d6e873481
F src/tokenize.c a8d270b06e5f709930f7b67cf70a847969cb5bf3
-F src/trigger.c 25571661fdeae8c7f975ff40ffec205520a3f92f
+F src/trigger.c 69a91bed7c94e46223e37ffccfeeb35e34b999ac
F src/update.c 250cefb96cfc7ca8f86f92ecf6b6b6c7e289daf7
F src/utf.c fc6b889ba0779b7722634cdeaa25f1930d93820c
F src/util.c 98a7627ca48ad3265b6940915a1d08355eb3fc7e
-F src/vacuum.c 9460b9de7b2d4e34b0d374894aa6c8a0632be8ec
-F src/vdbe.c 5cb31321523c4723a70f965c996feedf89e24c1f
+F src/vacuum.c 2ddd5cad2a7b9cef7f9e431b8c7771634c6b1701
+F src/vdbe.c 3c28266d50807ab9194874392dccb8f78d2078b4
F src/vdbe.h 7e538ecf47dccb307ea2d087c3ddc2dd8d70e79d
F src/vdbeInt.h 9cbaa84f53ddd2d09a0cf61a94337a3a035d08a0
F src/vdbeapi.c 583d56b129dd27f12bed518270de9ebe521e6a75
F src/vdbeaux.c a20504ae52392459fa08402fda3f195f19d7c79d
F src/vdbeblob.c 4f2e8e075d238392df98c5e03a64342465b03f90
F src/vdbemem.c c0dc81285b7571b0a31c40f17846fe2397ec1cd9
-F src/vdbesort.c 7b3684665ea51d642b0e664fa4d0b0d08d61d80c
+F src/vdbesort.c 74a41fcd3adc22bc47ede68443d0b3e26ae13bb8
F src/vdbetrace.c 7e4222955e07dd707a2f360c0eb73452be1cb010
-F src/vtab.c 62d49237bd8f3be4863815a39387b0f9897fa5e1
+F src/vtab.c ff722a886ed61e2e2889ee221b0a4f6dcaabb8e1
F src/vxworks.h c18586c8edc1bddbc15c004fa16aeb1e1342b4fb
F src/wal.c 878c8e1a51cb2ec45c395d26b7d5cd9e1a098e4a
F src/wal.h df01efe09c5cb8c8e391ff1715cca294f89668a4
@@ -340,7 +340,7 @@
F test/async4.test 1787e3952128aa10238bf39945126de7ca23685a
F test/async5.test 383ab533fdb9f7ad228cc99ee66e1acb34cc0dc0
F test/atof1.test 08a61df9365c341f334a65f4348897312d8f3db7
-F test/attach.test 0d112b7713611fdf0340260192749737135fda5f
+F test/attach.test 437107943f14d131cf5efc2ae5305a94d7cb1d58
F test/attach2.test 0ec5defa340363de6cd50fd595046465e9aaba2d
F test/attach3.test 359eb65d00102cdfcef6fa4e81dc1648f8f80b27
F test/attach4.test 53bf502f17647c6d6c5add46dda6bac8b6f4665c
@@ -433,6 +433,7 @@
F test/corruptG.test 1ab3bf97ee7bdba70e0ff3ba2320657df55d1804
F test/corruptH.test 5dd4fa98c6c1ed33b178f9e8a48c4fdd3cfc9067
F test/corruptI.test 221ad8b7f0a9ac6b80fc577e73b5ad8cdea31243
+F test/corruptJ.test 9e29e7a81ee3b6ac50f77ea7a9e2f3fa03f32d91
F test/cost.test 19d314526616ce4473eb4e4e450fcb94499ce318
F test/count.test 42a251178e32f617eda33f76236a7f79825a50b5
F test/coveridxscan.test cdb47d01acc4a634a34fd25abe85189e0d0f1e62
@@ -484,7 +485,7 @@
F test/e_uri.test 5ae33760fb2039c61aa2d90886f1664664173585
F test/e_vacuum.test a83cbb0b1f52cfda735909609f9f6422d1655bc3
F test/e_wal.test ae9a593207a77d711443ee69ffe081fda9243625
-F test/e_walauto.test ca70cf75c07a6cb1874ced101dd426da76625649
+F test/e_walauto.test 6544af03423abc61b53cfb976839385ddc2a0a70
F test/e_walckpt.test 65e29b6631e51f210f83e4ff11571e647ba93608
F test/e_walhook.test da3ea8b3483d1af72190337bda50155a91a4b664
F test/enc.test e54531cd6bf941ee6760be041dff19a104c7acea
@@ -587,13 +588,13 @@
F test/fts3expr3.test 9e91b8edbcb197bf2e92161aa7696446d96dce5f
F test/fts3expr4.test e1be1248566f43c252d4404d52914f1fc4bfa065
F test/fts3fault.test cb72dccb0a3b9f730f16c5240f3fcb9303eb1660
-F test/fts3fault2.test 3198eef2804deea7cac8403e771d9cbcb752d887
+F test/fts3fault2.test f953bb3cf903988172270a9a0aafd5a890b0f98f
F test/fts3first.test dbdedd20914c8d539aa3206c9b34a23775644641
F test/fts3join.test 53e66a0c21eb568580674a43b21c059acb26f499
F test/fts3malloc.test b0e4c133b8d61d4f6d112d8110f8320e9e453ef6
F test/fts3matchinfo.test 58544fa4d254000fa4e7f494b0a832f7ba61d45e
F test/fts3near.test 7e3354d46f155a822b59c0e957fd2a70c1d7e905
-F test/fts3prefix.test 9f68e3598a139c23ec47d09299420e0fc4c72a83
+F test/fts3prefix.test fa794eaab0bdae466494947b0b153d7844478ab2
F test/fts3prefix2.test e1f0a822ca661dced7f12ce392e14eaf65609dce
F test/fts3query.test c838b18f2b859e15fd31c64be3d79ef1556803ca
F test/fts3rnd.test 1320d8826a845e38a96e769562bf83d7a92a15d0
@@ -605,7 +606,7 @@
F test/fts3varint.test 752c08ed5d32c5d7dc211b056f4ed68a76b7e36e
F test/fts4aa.test 10aac8e9d62c7357590acfabe3fad01e9a9ce1cb
F test/fts4check.test 74d77f6cdb768ac49df5afda575cef14ae3d239a
-F test/fts4content.test 2e7252557d6d24afa101d9ba1de710d6140e6d06
+F test/fts4content.test abb0c77bc3da3df64fec72e00844d2257a90025d
F test/fts4docid.test e33c383cfbdff0284685604d256f347a18fdbf01
F test/fts4growth.test df10fde9f47cf5c71861e63fd8efcd573c4f7e53
F test/fts4growth2.test 2f063be1902a73cd087355837c52fed42ac11a5d
@@ -797,7 +798,7 @@
F test/pragma.test ad99d05e411c7687302124be56f3b362204be041
F test/pragma2.test f624a496a95ee878e81e59961eade66d5c00c028
F test/pragma3.test 6f849ccffeee7e496d2f2b5e74152306c0b8757c
-F test/printf.test ec9870c4dce8686a37818e0bf1aba6e6a1863552
+F test/printf.test b3ff34e73d59124140eaf89f7672e21bc2ca5fcc
F test/printf2.test b4acd4bf8734243257f01ddefa17c4fb090acc8a
F test/progress.test a282973d1d17f08071bc58a77d6b80f2a81c354d
F test/ptrchng.test ef1aa72d6cf35a2bbd0869a649b744e9d84977fc
@@ -845,7 +846,7 @@
F test/select7.test 7fd2ef598cfabb6b9ff6ac13973b91d0527df49d
F test/select8.test 391de11bdd52339c30580dabbbbe97e3e9a3c79d
F test/select9.test aebc2bb0c3bc44606125033cbcaac2c8d1f33a95
-F test/selectA.test 64b88a80271c1710966e50e633380696b60a12a4
+F test/selectA.test e452bdb975f488ea46d091382a9185b5853ed2c7
F test/selectB.test 954e4e49cf1f896d61794e440669e03a27ceea25
F test/selectC.test 871fb55d884d3de5943c4057ebd22c2459e71977
F test/selectD.test b0f02a04ef7737decb24e08be2c39b9664b43394
@@ -1144,7 +1145,7 @@
F test/wal_common.tcl a98f17fba96206122eff624db0ab13ec377be4fe
F test/walbak.test b9f68e39646375c2b877be906babcc15d38b4877
F test/walbig.test f437473a16cfb314867c6b5d1dbcd519e73e3434
-F test/walblock.test ffc761cd467a93ccd8cd998a23be2f21b95a83b1
+F test/walblock.test be48f3a75eff0b4456209f26b3ce186c2015497d
F test/walcksum.test 9afeb96240296c08c72fc524d199c912cfe34daa
F test/walcrash.test 451d79e528add5c42764cea74aa2750754171b25
F test/walcrash2.test a0edab4e5390f03b99a790de89aad15d6ec70b36
@@ -1182,7 +1183,7 @@
F test/wherelimit.test 5e9fd41e79bb2b2d588ed999d641d9c965619b31
F test/wild001.test bca33f499866f04c24510d74baf1e578d4e44b1c
F test/win32heap.test ea19770974795cff26e11575e12d422dbd16893c
-F test/win32lock.test 71642fa56e9b06e5cfffe6bad67cb8c1eb2c555a
+F test/win32lock.test fbf107c91d8f5512be5a5b87c4c42ab9fdd54972
F test/win32longpath.test 169c75a3b2e43481f4a62122510210c67b08f26d
F test/with1.test 9df5cd8a62148b3d9ef8597aea563e3863018bcd
F test/with2.test ee227a663586aa09771cafd4fa269c5217eaf775
@@ -1206,7 +1207,7 @@
F tool/genfkey.README cf68fddd4643bbe3ff8e31b8b6d8b0a1b85e20f4
F tool/genfkey.test 4196a8928b78f51d54ef58e99e99401ab2f0a7e5
F tool/getlock.c f4c39b651370156cae979501a7b156bdba50e7ce
-F tool/lemon.c 1864c4fe4a72b1bb28f1792b60504804fe82c5d2
+F tool/lemon.c ae5f61e3b164d35955777b20d6febcbaf0950702
F tool/lempar.c 01ca97f87610d1dac6d8cd96ab109ab1130e76dc
F tool/logest.c eef612f8adf4d0993dafed0416064cf50d5d33c6
F tool/mkautoconfamal.sh d1a2da0e15b2ed33d60af35c7e9d483f13a8eb9f
@@ -1227,6 +1228,7 @@
F tool/rollback-test.c 9fc98427d1e23e84429d7e6d07d9094fbdec65a5
F tool/showdb.c 63cdef19e7fbca0c164b096ef8aef3bb9e9dd222
F tool/showjournal.c 053eb1cc774710c6890b7dd6293300cc297b16a5
+F tool/showlocks.c 9920bcc64f58378ff1118caead34147201f48c68
F tool/showstat4.c 9515faa8ec176599d4a8288293ba8ec61f7b728a
F tool/showwal.c 85cb36d4fe3e93e2fbd63e786e0d1ce42d0c4fad
F tool/soak1.tcl 8d407956e1a45b485a8e072470a3e629a27037fe
@@ -1238,6 +1240,7 @@
F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
F tool/split-sqlite3c.tcl d9be87f1c340285a3e081eb19b4a247981ed290c
+F tool/sqldiff.c 050763654cb28d23c4d9516deb348c8632e432cd
F tool/stack_usage.tcl f8e71b92cdb099a147dad572375595eae55eca43
F tool/symbols-mingw.sh 4dbcea7e74768305384c9fd2ed2b41bbf9f0414d
F tool/symbols.sh fec58532668296d7c7dc48be9c87f75ccdb5814f
@@ -1248,7 +1251,7 @@
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
-P aeb8e9a9f2092fda0c3e051f135e1d65cf77ff13 601e7b6b8e6bfabda03b70f75094c9014e3a3c49
-R ecee2ba850684cc6832777f6c1e18b7d
+P 9bf1cfb4d99328bb95567330fdae76518f6386e2 60be9c1c1ad0d8250a99cadda820dff40a31c94e
+R 172efa9ba854731d65055b3485ef57e0
U dan
-Z 755e374c8f5315221aeb3673026dbb0a
+Z a9138cfc25929a66b1eec293ef992a21
diff --git a/manifest.uuid b/manifest.uuid
index e0f7a91..b839b48 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-9bf1cfb4d99328bb95567330fdae76518f6386e2
\ No newline at end of file
+08c0b19b89ea62d40c85bab64d9c80e02eaa8d5d
\ No newline at end of file
diff --git a/mptest/mptest.c b/mptest/mptest.c
index 40c14bc..52ff6a4 100644
--- a/mptest/mptest.c
+++ b/mptest/mptest.c
@@ -1312,6 +1312,9 @@
GETPID(), iClient);
}else{
if( g.iTrace>0 ){
+ printf("BEGIN: %s", argv[0]);
+ for(i=1; i<argc; i++) printf(" %s", argv[i]);
+ printf("\n");
printf("With SQLite " SQLITE_VERSION " " SQLITE_SOURCE_ID "\n" );
for(i=0; (zCOption = sqlite3_compileoption_get(i))!=0; i++){
printf("-DSQLITE_%s\n", zCOption);
@@ -1324,6 +1327,17 @@
}
rc = sqlite3_open_v2(g.zDbFile, &g.db, openFlags, g.zVfs);
if( rc ) fatalError("cannot open [%s]", g.zDbFile);
+ if( zJMode ){
+#if defined(_WIN32)
+ if( sqlite3_stricmp(zJMode,"persist")==0
+ || sqlite3_stricmp(zJMode,"truncate")==0
+ ){
+ printf("Changing journal mode to DELETE from %s", zJMode);
+ zJMode = "DELETE";
+ }
+#endif
+ runSql("PRAGMA journal_mode=%Q;", zJMode);
+ }
sqlite3_enable_load_extension(g.db, 1);
sqlite3_busy_handler(g.db, busyHandler, 0);
sqlite3_create_function(g.db, "vfsname", 0, SQLITE_UTF8, 0,
@@ -1355,7 +1369,6 @@
fatalError("missing script filename");
}
if( n>1 ) unrecognizedArguments(argv[0], n, argv+2);
- if( zJMode ) runSql("PRAGMA journal_mode=%Q;", zJMode);
runSql(
"DROP TABLE IF EXISTS task;\n"
"DROP TABLE IF EXISTS counters;\n"
@@ -1409,6 +1422,9 @@
maybeClose(g.pErrLog);
if( iClient==0 ){
printf("Summary: %d errors out of %d tests\n", g.nError, g.nTest);
+ printf("END: %s", argv[0]);
+ for(i=1; i<argc; i++) printf(" %s", argv[i]);
+ printf("\n");
}
return g.nError>0;
}
diff --git a/src/btree.c b/src/btree.c
index 52f2942..789796d 100644
--- a/src/btree.c
+++ b/src/btree.c
@@ -1959,16 +1959,18 @@
*/
if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){
if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
+ int nFilename = sqlite3Strlen30(zFilename)+1;
int nFullPathname = pVfs->mxPathname+1;
- char *zFullPathname = sqlite3Malloc(nFullPathname);
+ char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename));
MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
+
p->sharable = 1;
if( !zFullPathname ){
sqlite3_free(p);
return SQLITE_NOMEM;
}
if( isMemdb ){
- memcpy(zFullPathname, zFilename, sqlite3Strlen30(zFilename)+1);
+ memcpy(zFullPathname, zFilename, nFilename);
}else{
rc = sqlite3OsFullPathname(pVfs, zFilename,
nFullPathname, zFullPathname);
@@ -7983,9 +7985,13 @@
if( pgno>btreePagecount(pBt) ){
return SQLITE_CORRUPT_BKPT;
}
-
rc = getAndInitPage(pBt, pgno, &pPage, 0);
if( rc ) return rc;
+ if( pPage->bBusy ){
+ rc = SQLITE_CORRUPT_BKPT;
+ goto cleardatabasepage_out;
+ }
+ pPage->bBusy = 1;
hdr = pPage->hdrOffset;
for(i=0; i<pPage->nCell; i++){
pCell = findCell(pPage, i);
@@ -8010,6 +8016,7 @@
}
cleardatabasepage_out:
+ pPage->bBusy = 0;
releasePage(pPage);
return rc;
}
diff --git a/src/btreeInt.h b/src/btreeInt.h
index 87d0ef1..33ef641 100644
--- a/src/btreeInt.h
+++ b/src/btreeInt.h
@@ -280,6 +280,7 @@
u8 hdrOffset; /* 100 for page 1. 0 otherwise */
u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
u8 max1bytePayload; /* min(maxLocal,127) */
+ u8 bBusy; /* Prevent endless loops on corrupt database files */
u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */
u16 cellOffset; /* Index in aData of first cell pointer */
diff --git a/src/build.c b/src/build.c
index fcf96bd..9d84655 100644
--- a/src/build.c
+++ b/src/build.c
@@ -2763,7 +2763,8 @@
addr2 = sqlite3VdbeCurrentAddr(v);
}
sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
- sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
+ sqlite3VdbeAddOp3(v, OP_Last, iIdx, 0, -1);
+ sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0);
sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
sqlite3ReleaseTempReg(pParse, regRecord);
sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
@@ -3776,7 +3777,6 @@
void sqlite3SrcListShiftJoinType(SrcList *p){
if( p ){
int i;
- assert( p->a || p->nSrc==0 );
for(i=p->nSrc-1; i>0; i--){
p->a[i].jointype = p->a[i-1].jointype;
}
diff --git a/src/complete.c b/src/complete.c
index f7a35cc..a12184e 100644
--- a/src/complete.c
+++ b/src/complete.c
@@ -269,7 +269,7 @@
int sqlite3_complete16(const void *zSql){
sqlite3_value *pVal;
char const *zSql8;
- int rc = SQLITE_NOMEM;
+ int rc;
#ifndef SQLITE_OMIT_AUTOINIT
rc = sqlite3_initialize();
diff --git a/src/insert.c b/src/insert.c
index 3b86802..0da5fee 100644
--- a/src/insert.c
+++ b/src/insert.c
@@ -1852,6 +1852,7 @@
int onError, /* How to handle constraint errors */
int iDbDest /* The database of pDest */
){
+ sqlite3 *db = pParse->db;
ExprList *pEList; /* The result set of the SELECT */
Table *pSrc; /* The table in the FROM clause of SELECT */
Index *pSrcIdx, *pDestIdx; /* Source and destination indices */
@@ -1999,11 +2000,11 @@
** the extra complication to make this rule less restrictive is probably
** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
*/
- if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
+ if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
return 0;
}
#endif
- if( (pParse->db->flags & SQLITE_CountRows)!=0 ){
+ if( (db->flags & SQLITE_CountRows)!=0 ){
return 0; /* xfer opt does not play well with PRAGMA count_changes */
}
@@ -2014,7 +2015,7 @@
#ifdef SQLITE_TEST
sqlite3_xferopt_count++;
#endif
- iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
+ iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
v = sqlite3GetVdbe(pParse);
sqlite3CodeVerifySchema(pParse, iDbSrc);
iSrc = pParse->nTab++;
@@ -2024,14 +2025,18 @@
regRowid = sqlite3GetTempReg(pParse);
sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
assert( HasRowid(pDest) || destHasUniqueIdx );
- if( (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
+ if( (db->flags & SQLITE_Vacuum)==0 && (
+ (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
|| destHasUniqueIdx /* (2) */
|| (onError!=OE_Abort && onError!=OE_Rollback) /* (3) */
- ){
+ )){
/* In some circumstances, we are able to run the xfer optimization
- ** only if the destination table is initially empty. This code makes
- ** that determination. Conditions under which the destination must
- ** be empty:
+ ** only if the destination table is initially empty. Unless the
+ ** SQLITE_Vacuum flag is set, this block generates code to make
+ ** that determination. If SQLITE_Vacuum is set, then the destination
+ ** table is always empty.
+ **
+ ** Conditions under which the destination must be empty:
**
** (1) There is no INTEGER PRIMARY KEY but there are indices.
** (If the destination is not initially empty, the rowid fields
@@ -2074,6 +2079,7 @@
sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
}
for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
+ u8 useSeekResult = 0;
for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
}
@@ -2087,7 +2093,33 @@
VdbeComment((v, "%s", pDestIdx->zName));
addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
+ if( db->flags & SQLITE_Vacuum ){
+ /* This INSERT command is part of a VACUUM operation, which guarantees
+ ** that the destination table is empty. If all indexed columns use
+ ** collation sequence BINARY, then it can also be assumed that the
+ ** index will be populated by inserting keys in strictly sorted
+ ** order. In this case, instead of seeking within the b-tree as part
+ ** of every OP_IdxInsert opcode, an OP_Last is added before the
+ ** OP_IdxInsert to seek to the point within the b-tree where each key
+ ** should be inserted. This is faster.
+ **
+ ** If any of the indexed columns use a collation sequence other than
+ ** BINARY, this optimization is disabled. This is because the user
+ ** might change the definition of a collation sequence and then run
+ ** a VACUUM command. In that case keys may not be written in strictly
+ ** sorted order. */
+ int i;
+ for(i=0; i<pSrcIdx->nColumn; i++){
+ char *zColl = pSrcIdx->azColl[i];
+ if( zColl && sqlite3_stricmp("BINARY", zColl) ) break;
+ }
+ if( i==pSrcIdx->nColumn ){
+ useSeekResult = OPFLAG_USESEEKRESULT;
+ sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
+ }
+ }
sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
+ sqlite3VdbeChangeP5(v, useSeekResult);
sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
sqlite3VdbeJumpHere(v, addr1);
sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
diff --git a/src/main.c b/src/main.c
index 6967131..d9ee77f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -62,7 +62,7 @@
** I/O active are written using this function. These messages
** are intended for debugging activity only.
*/
-/* not-private */ void (*sqlite3IoTrace)(const char*, ...) = 0;
+SQLITE_API void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...) = 0;
#endif
/*
diff --git a/src/malloc.c b/src/malloc.c
index 264d046..f06e27d 100644
--- a/src/malloc.c
+++ b/src/malloc.c
@@ -162,6 +162,7 @@
** Initialize the memory allocation subsystem.
*/
int sqlite3MallocInit(void){
+ int rc;
if( sqlite3GlobalConfig.m.xMalloc==0 ){
sqlite3MemSetDefault();
}
@@ -197,7 +198,9 @@
sqlite3GlobalConfig.szPage = 0;
sqlite3GlobalConfig.nPage = 0;
}
- return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
+ rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
+ if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
+ return rc;
}
/*
diff --git a/src/os_unix.c b/src/os_unix.c
index a9c883a..1d867d7 100644
--- a/src/os_unix.c
+++ b/src/os_unix.c
@@ -3783,7 +3783,7 @@
unixFile *pFile = (unixFile*)id;
switch( op ){
case SQLITE_FCNTL_WAL_BLOCK: {
- pFile->ctrlFlags |= UNIXFILE_BLOCK;
+ /* pFile->ctrlFlags |= UNIXFILE_BLOCK; // Deferred feature */
return SQLITE_OK;
}
case SQLITE_FCNTL_LOCKSTATE: {
diff --git a/src/os_win.c b/src/os_win.c
index 7463778..ef2f553 100644
--- a/src/os_win.c
+++ b/src/os_win.c
@@ -197,8 +197,10 @@
#endif /* SQLITE_OS_WINRT */
/*
-** This file mapping API is common to both Win32 and WinRT.
+** These file mapping APIs are common to both Win32 and WinRT.
*/
+
+WINBASEAPI BOOL WINAPI FlushViewOfFile(LPCVOID, SIZE_T);
WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
#endif /* SQLITE_WIN32_FILEMAPPING_API */
@@ -1083,6 +1085,15 @@
#define osUuidCreateSequential \
((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[78].pCurrent)
+#if !defined(SQLITE_NO_SYNC) && SQLITE_MAX_MMAP_SIZE>0
+ { "FlushViewOfFile", (SYSCALL)FlushViewOfFile, 0 },
+#else
+ { "FlushViewOfFile", (SYSCALL)0, 0 },
+#endif
+
+#define osFlushViewOfFile \
+ ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent)
+
}; /* End of the overrideable system calls */
/*
@@ -1966,11 +1977,11 @@
/*
** Log a I/O error retry episode.
*/
-static void winLogIoerr(int nRetry){
+static void winLogIoerr(int nRetry, int lineno){
if( nRetry ){
- sqlite3_log(SQLITE_IOERR,
- "delayed %dms for lock/sharing conflict",
- winIoerrRetryDelay*nRetry*(nRetry+1)/2
+ sqlite3_log(SQLITE_NOTICE,
+ "delayed %dms for lock/sharing conflict at line %d",
+ winIoerrRetryDelay*nRetry*(nRetry+1)/2, lineno
);
}
}
@@ -2450,7 +2461,8 @@
assert( pFile->pShm==0 );
#endif
assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
- OSTRACE(("CLOSE file=%p\n", pFile->h));
+ OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p\n",
+ osGetCurrentProcessId(), pFile, pFile->h));
#if SQLITE_MAX_MMAP_SIZE>0
winUnmapfile(pFile);
@@ -2479,7 +2491,8 @@
pFile->h = NULL;
}
OpenCounter(-1);
- OSTRACE(("CLOSE file=%p, rc=%s\n", pFile->h, rc ? "ok" : "failed"));
+ OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p, rc=%s\n",
+ osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
return rc ? SQLITE_OK
: winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
"winClose", pFile->zPath);
@@ -2507,7 +2520,8 @@
assert( amt>0 );
assert( offset>=0 );
SimulateIOError(return SQLITE_IOERR_READ);
- OSTRACE(("READ file=%p, buffer=%p, amount=%d, offset=%lld, lock=%d\n",
+ OSTRACE(("READ pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
+ "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
pFile->h, pBuf, amt, offset, pFile->locktype));
#if SQLITE_MAX_MMAP_SIZE>0
@@ -2516,7 +2530,8 @@
if( offset<pFile->mmapSize ){
if( offset+amt <= pFile->mmapSize ){
memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
- OSTRACE(("READ-MMAP file=%p, rc=SQLITE_OK\n", pFile->h));
+ OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
+ osGetCurrentProcessId(), pFile, pFile->h));
return SQLITE_OK;
}else{
int nCopy = (int)(pFile->mmapSize - offset);
@@ -2530,7 +2545,8 @@
#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
if( winSeekFile(pFile, offset) ){
- OSTRACE(("READ file=%p, rc=SQLITE_FULL\n", pFile->h));
+ OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
+ osGetCurrentProcessId(), pFile, pFile->h));
return SQLITE_FULL;
}
while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
@@ -2544,19 +2560,22 @@
DWORD lastErrno;
if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
pFile->lastErrno = lastErrno;
- OSTRACE(("READ file=%p, rc=SQLITE_IOERR_READ\n", pFile->h));
+ OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_READ\n",
+ osGetCurrentProcessId(), pFile, pFile->h));
return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
"winRead", pFile->zPath);
}
- winLogIoerr(nRetry);
+ winLogIoerr(nRetry, __LINE__);
if( nRead<(DWORD)amt ){
/* Unread parts of the buffer must be zero-filled */
memset(&((char*)pBuf)[nRead], 0, amt-nRead);
- OSTRACE(("READ file=%p, rc=SQLITE_IOERR_SHORT_READ\n", pFile->h));
+ OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_SHORT_READ\n",
+ osGetCurrentProcessId(), pFile, pFile->h));
return SQLITE_IOERR_SHORT_READ;
}
- OSTRACE(("READ file=%p, rc=SQLITE_OK\n", pFile->h));
+ OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
+ osGetCurrentProcessId(), pFile, pFile->h));
return SQLITE_OK;
}
@@ -2579,7 +2598,8 @@
SimulateIOError(return SQLITE_IOERR_WRITE);
SimulateDiskfullError(return SQLITE_FULL);
- OSTRACE(("WRITE file=%p, buffer=%p, amount=%d, offset=%lld, lock=%d\n",
+ OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
+ "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
pFile->h, pBuf, amt, offset, pFile->locktype));
#if SQLITE_MAX_MMAP_SIZE>0
@@ -2588,7 +2608,8 @@
if( offset<pFile->mmapSize ){
if( offset+amt <= pFile->mmapSize ){
memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
- OSTRACE(("WRITE-MMAP file=%p, rc=SQLITE_OK\n", pFile->h));
+ OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
+ osGetCurrentProcessId(), pFile, pFile->h));
return SQLITE_OK;
}else{
int nCopy = (int)(pFile->mmapSize - offset);
@@ -2651,17 +2672,20 @@
if( rc ){
if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
|| ( pFile->lastErrno==ERROR_DISK_FULL )){
- OSTRACE(("WRITE file=%p, rc=SQLITE_FULL\n", pFile->h));
+ OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
+ osGetCurrentProcessId(), pFile, pFile->h));
return winLogError(SQLITE_FULL, pFile->lastErrno,
"winWrite1", pFile->zPath);
}
- OSTRACE(("WRITE file=%p, rc=SQLITE_IOERR_WRITE\n", pFile->h));
+ OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_WRITE\n",
+ osGetCurrentProcessId(), pFile, pFile->h));
return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
"winWrite2", pFile->zPath);
}else{
- winLogIoerr(nRetry);
+ winLogIoerr(nRetry, __LINE__);
}
- OSTRACE(("WRITE file=%p, rc=SQLITE_OK\n", pFile->h));
+ OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
+ osGetCurrentProcessId(), pFile, pFile->h));
return SQLITE_OK;
}
@@ -2675,8 +2699,8 @@
assert( pFile );
SimulateIOError(return SQLITE_IOERR_TRUNCATE);
- OSTRACE(("TRUNCATE file=%p, size=%lld, lock=%d\n",
- pFile->h, nByte, pFile->locktype));
+ OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n",
+ osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
/* If the user has configured a chunk-size for this file, truncate the
** file so that it consists of an integer number of chunks (i.e. the
@@ -2708,7 +2732,8 @@
}
#endif
- OSTRACE(("TRUNCATE file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
+ OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, rc=%s\n",
+ osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
return rc;
}
@@ -2753,8 +2778,9 @@
*/
SimulateDiskfullError( return SQLITE_FULL );
- OSTRACE(("SYNC file=%p, flags=%x, lock=%d\n",
- pFile->h, flags, pFile->locktype));
+ OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, flags=%x, lock=%d\n",
+ osGetCurrentProcessId(), pFile, pFile->h, flags,
+ pFile->locktype));
#ifndef SQLITE_TEST
UNUSED_PARAMETER(flags);
@@ -2769,19 +2795,38 @@
** no-op
*/
#ifdef SQLITE_NO_SYNC
- OSTRACE(("SYNC-NOP file=%p, rc=SQLITE_OK\n", pFile->h));
+ OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
+ osGetCurrentProcessId(), pFile, pFile->h));
return SQLITE_OK;
#else
+#if SQLITE_MAX_MMAP_SIZE>0
+ if( pFile->pMapRegion ){
+ if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
+ OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
+ "rc=SQLITE_OK\n", osGetCurrentProcessId(),
+ pFile, pFile->pMapRegion));
+ }else{
+ pFile->lastErrno = osGetLastError();
+ OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
+ "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(),
+ pFile, pFile->pMapRegion));
+ return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
+ "winSync1", pFile->zPath);
+ }
+ }
+#endif
rc = osFlushFileBuffers(pFile->h);
SimulateIOError( rc=FALSE );
if( rc ){
- OSTRACE(("SYNC file=%p, rc=SQLITE_OK\n", pFile->h));
+ OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
+ osGetCurrentProcessId(), pFile, pFile->h));
return SQLITE_OK;
}else{
pFile->lastErrno = osGetLastError();
- OSTRACE(("SYNC file=%p, rc=SQLITE_IOERR_FSYNC\n", pFile->h));
+ OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_FSYNC\n",
+ osGetCurrentProcessId(), pFile, pFile->h));
return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
- "winSync", pFile->zPath);
+ "winSync2", pFile->zPath);
}
#endif
}
@@ -4752,7 +4797,7 @@
}
}
#endif
- winLogIoerr(cnt);
+ winLogIoerr(cnt, __LINE__);
OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
@@ -4936,7 +4981,7 @@
if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
rc = winLogError(SQLITE_IOERR_DELETE, lastErrno, "winDelete", zFilename);
}else{
- winLogIoerr(cnt);
+ winLogIoerr(cnt, __LINE__);
}
sqlite3_free(zConverted);
OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
@@ -4986,7 +5031,7 @@
attr = sAttrData.dwFileAttributes;
}
}else{
- winLogIoerr(cnt);
+ winLogIoerr(cnt, __LINE__);
if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
sqlite3_free(zConverted);
return winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess",
@@ -5555,7 +5600,7 @@
/* Double-check that the aSyscall[] array has been constructed
** correctly. See ticket [bb3a86e890c8e96ab] */
- assert( ArraySize(aSyscall)==79 );
+ assert( ArraySize(aSyscall)==80 );
/* get memory map allocation granularity */
memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
diff --git a/src/printf.c b/src/printf.c
index 81efa05..9714fa1 100644
--- a/src/printf.c
+++ b/src/printf.c
@@ -261,15 +261,19 @@
}
if( width<0 ){
flag_leftjustify = 1;
- width = -width;
+ width = width >= -2147483647 ? -width : 0;
}
c = *++fmt;
}else{
+ unsigned wx = 0;
while( c>='0' && c<='9' ){
- width = width*10 + c - '0';
+ wx = wx*10 + c - '0';
c = *++fmt;
}
+ testcase( wx>0x7fffffff );
+ width = wx & 0x7fffffff;
}
+
/* Get the precision */
if( c=='.' ){
precision = 0;
@@ -280,13 +284,18 @@
}else{
precision = va_arg(ap,int);
}
- if( precision<0 ) precision = -precision;
c = *++fmt;
+ if( precision<0 ){
+ precision = precision >= -2147483647 ? -precision : -1;
+ }
}else{
+ unsigned px = 0;
while( c>='0' && c<='9' ){
- precision = precision*10 + c - '0';
+ px = px*10 + c - '0';
c = *++fmt;
}
+ testcase( px>0x7fffffff );
+ precision = px & 0x7fffffff;
}
}else{
precision = -1;
@@ -450,7 +459,8 @@
else prefix = 0;
}
if( xtype==etGENERIC && precision>0 ) precision--;
- for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
+ testcase( precision>0xfff );
+ for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){}
if( xtype==etFLOAT ) realvalue += rounder;
/* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
exp = 0;
@@ -505,8 +515,9 @@
}else{
e2 = exp;
}
- if( MAX(e2,0)+precision+width > etBUFSIZE - 15 ){
- bufpt = zExtra = sqlite3Malloc( MAX(e2,0)+precision+width+15 );
+ if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){
+ bufpt = zExtra
+ = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 );
if( bufpt==0 ){
setStrAccumError(pAccum, STRACCUM_NOMEM);
return;
@@ -738,7 +749,7 @@
*/
static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
char *zNew;
- assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */
+ assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
if( p->accError ){
testcase(p->accError==STRACCUM_TOOBIG);
testcase(p->accError==STRACCUM_NOMEM);
@@ -787,7 +798,10 @@
** Append N copies of character c to the given string buffer.
*/
void sqlite3AppendChar(StrAccum *p, int N, char c){
- if( p->nChar+N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ) return;
+ testcase( p->nChar + (i64)N > 0x7fffffff );
+ if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
+ return;
+ }
while( (N--)>0 ) p->zText[p->nChar++] = c;
}
diff --git a/src/resolve.c b/src/resolve.c
index 47df724..a7b14cd 100644
--- a/src/resolve.c
+++ b/src/resolve.c
@@ -1186,6 +1186,20 @@
sqlite3ResolveExprNames(&sNC, p->pOffset) ){
return WRC_Abort;
}
+
+ /* If the SF_Converted flags is set, then this Select object was
+ ** was created by the convertCompoundSelectToSubquery() function.
+ ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
+ ** as if it were part of the sub-query, not the parent. This block
+ ** moves the pOrderBy down to the sub-query. It will be moved back
+ ** after the names have been resolved. */
+ if( p->selFlags & SF_Converted ){
+ Select *pSub = p->pSrc->a[0].pSelect;
+ assert( p->pSrc->nSrc==1 && isCompound==0 && p->pOrderBy );
+ assert( pSub->pPrior && pSub->pOrderBy==0 );
+ pSub->pOrderBy = p->pOrderBy;
+ p->pOrderBy = 0;
+ }
/* Recursively resolve names in all subqueries
*/
@@ -1268,6 +1282,17 @@
sNC.pNext = 0;
sNC.ncFlags |= NC_AllowAgg;
+ /* If this is a converted compound query, move the ORDER BY clause from
+ ** the sub-query back to the parent query. At this point each term
+ ** within the ORDER BY clause has been transformed to an integer value.
+ ** These integers will be replaced by copies of the corresponding result
+ ** set expressions by the call to resolveOrderGroupBy() below. */
+ if( p->selFlags & SF_Converted ){
+ Select *pSub = p->pSrc->a[0].pSelect;
+ p->pOrderBy = pSub->pOrderBy;
+ pSub->pOrderBy = 0;
+ }
+
/* Process the ORDER BY clause for singleton SELECT statements.
** The ORDER BY clause for compounds SELECT statements is handled
** below, after all of the result-sets for all of the elements of
diff --git a/src/select.c b/src/select.c
index 8fd0f15..90aaa84 100644
--- a/src/select.c
+++ b/src/select.c
@@ -3884,6 +3884,8 @@
p->pPrior = 0;
p->pNext = 0;
p->selFlags &= ~SF_Compound;
+ assert( (p->selFlags & SF_Converted)==0 );
+ p->selFlags |= SF_Converted;
assert( pNew->pPrior!=0 );
pNew->pPrior->pNext = pNew;
pNew->pLimit = 0;
diff --git a/src/shell.c b/src/shell.c
index b31ea16..7ff3eb6 100644
--- a/src/shell.c
+++ b/src/shell.c
@@ -370,7 +370,7 @@
** is written to iotrace.
*/
#ifdef SQLITE_ENABLE_IOTRACE
-static void iotracePrintf(const char *zFormat, ...){
+static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
va_list ap;
char *z;
if( iotrace==0 ) return;
@@ -3144,7 +3144,7 @@
#ifdef SQLITE_ENABLE_IOTRACE
if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
- extern void (*sqlite3IoTrace)(const char*, ...);
+ SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
if( iotrace && iotrace!=stdout ) fclose(iotrace);
iotrace = 0;
if( nArg<2 ){
@@ -3803,9 +3803,9 @@
azArg[2],
integerValue(azArg[3]),
integerValue(azArg[4]));
+ fprintf(p->out, "%d (0x%08x)\n", rc, rc);
}else{
- fprintf(stderr,"Usage: .testctrl initmode dbName onoff tnum\n");
- rc = 1;
+ fprintf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n");
}
break;
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index 176d88e..61ae018 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -1226,6 +1226,7 @@
#define SQLITE_DeferFKs 0x01000000 /* Defer all FK constraints */
#define SQLITE_QueryOnly 0x02000000 /* Disable database changes */
#define SQLITE_VdbeEQP 0x04000000 /* Debug EXPLAIN QUERY PLAN */
+#define SQLITE_Vacuum 0x08000000 /* Currently in a VACUUM */
/*
@@ -2389,6 +2390,7 @@
#define SF_MaybeConvert 0x0400 /* Need convertCompoundSelectToSubquery() */
#define SF_Recursive 0x0800 /* The recursive part of a recursive CTE */
#define SF_MinMaxAgg 0x1000 /* Aggregate containing min() or max() */
+#define SF_Converted 0x2000 /* By convertCompoundSelectToSubquery() */
/*
@@ -3803,7 +3805,7 @@
#ifdef SQLITE_ENABLE_IOTRACE
# define IOTRACE(A) if( sqlite3IoTrace ){ sqlite3IoTrace A; }
void sqlite3VdbeIOTraceSql(Vdbe*);
-SQLITE_EXTERN void (*sqlite3IoTrace)(const char*,...);
+SQLITE_API SQLITE_EXTERN void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...);
#else
# define IOTRACE(A)
# define sqlite3VdbeIOTraceSql(X)
diff --git a/src/trigger.c b/src/trigger.c
index d2e7b5a..ed152d2 100644
--- a/src/trigger.c
+++ b/src/trigger.c
@@ -680,7 +680,6 @@
pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
if( pSrc ){
assert( pSrc->nSrc>0 );
- assert( pSrc->a!=0 );
iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
if( iDb==0 || iDb>=2 ){
sqlite3 *db = pParse->db;
diff --git a/src/vacuum.c b/src/vacuum.c
index dca43e2..adc802e 100644
--- a/src/vacuum.c
+++ b/src/vacuum.c
@@ -250,6 +250,8 @@
** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
** the contents to the temporary database.
*/
+ assert( (db->flags & SQLITE_Vacuum)==0 );
+ db->flags |= SQLITE_Vacuum;
rc = execExecSql(db, pzErrMsg,
"SELECT 'INSERT INTO vacuum_db.' || quote(name) "
"|| ' SELECT * FROM main.' || quote(name) || ';'"
@@ -257,6 +259,8 @@
"WHERE type = 'table' AND name!='sqlite_sequence' "
" AND coalesce(rootpage,1)>0"
);
+ assert( (db->flags & SQLITE_Vacuum)!=0 );
+ db->flags &= ~SQLITE_Vacuum;
if( rc!=SQLITE_OK ) goto end_of_vacuum;
/* Copy over the sequence table
diff --git a/src/vdbe.c b/src/vdbe.c
index 8fea505..187610e 100644
--- a/src/vdbe.c
+++ b/src/vdbe.c
@@ -1920,11 +1920,15 @@
testcase( pIn1->flags & MEM_Int );
testcase( pIn1->flags & MEM_Real );
sqlite3VdbeMemStringify(pIn1, encoding, 1);
+ testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
+ flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
}
if( (pIn3->flags & MEM_Str)==0 && (pIn3->flags & (MEM_Int|MEM_Real))!=0 ){
testcase( pIn3->flags & MEM_Int );
testcase( pIn3->flags & MEM_Real );
sqlite3VdbeMemStringify(pIn3, encoding, 1);
+ testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
+ flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
}
}
assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
@@ -1961,7 +1965,9 @@
}
}
/* Undo any changes made by applyAffinity() to the input registers. */
+ assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
pIn1->flags = flags1;
+ assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
pIn3->flags = flags3;
break;
}
@@ -4484,7 +4490,7 @@
break;
}
-/* Opcode: Last P1 P2 * * *
+/* Opcode: Last P1 P2 P3 * *
**
** The next use of the Rowid or Column or Prev instruction for P1
** will refer to the last entry in the database table or index.
@@ -4511,6 +4517,7 @@
pC->nullRow = (u8)res;
pC->deferredMoveto = 0;
pC->cacheStatus = CACHE_STALE;
+ pC->seekResult = pOp->p3;
#ifdef SQLITE_DEBUG
pC->seekOp = OP_Last;
#endif
diff --git a/src/vdbesort.c b/src/vdbesort.c
index 547dce2..f4b995c 100644
--- a/src/vdbesort.c
+++ b/src/vdbesort.c
@@ -1656,7 +1656,7 @@
pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
bCached = 0;
}else{
- bCached = (pReadr1->pFd!=0);
+ if( pReadr1->pFd ) bCached = 0;
pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
}
diff --git a/src/vtab.c b/src/vtab.c
index 23f49ba..d17aa14 100644
--- a/src/vtab.c
+++ b/src/vtab.c
@@ -24,6 +24,8 @@
struct VtabCtx {
VTable *pVTable; /* The virtual table being constructed */
Table *pTab; /* The Table object to which the virtual table belongs */
+ VtabCtx *pPrior; /* Parent context (if any) */
+ int bDeclared; /* True after sqlite3_declare_vtab() is called */
};
/*
@@ -487,15 +489,27 @@
int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
char **pzErr
){
- VtabCtx sCtx, *pPriorCtx;
+ VtabCtx sCtx;
VTable *pVTable;
int rc;
const char *const*azArg = (const char *const*)pTab->azModuleArg;
int nArg = pTab->nModuleArg;
char *zErr = 0;
- char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
+ char *zModuleName;
int iDb;
+ VtabCtx *pCtx;
+ /* Check that the virtual-table is not already being initialized */
+ for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
+ if( pCtx->pTab==pTab ){
+ *pzErr = sqlite3MPrintf(db,
+ "vtable constructor called recursively: %s", pTab->zName
+ );
+ return SQLITE_LOCKED;
+ }
+ }
+
+ zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
if( !zModuleName ){
return SQLITE_NOMEM;
}
@@ -516,11 +530,13 @@
assert( xConstruct );
sCtx.pTab = pTab;
sCtx.pVTable = pVTable;
- pPriorCtx = db->pVtabCtx;
+ sCtx.pPrior = db->pVtabCtx;
+ sCtx.bDeclared = 0;
db->pVtabCtx = &sCtx;
rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
- db->pVtabCtx = pPriorCtx;
+ db->pVtabCtx = sCtx.pPrior;
if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
+ assert( sCtx.pTab==pTab );
if( SQLITE_OK!=rc ){
if( zErr==0 ){
@@ -536,7 +552,7 @@
memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
pVTable->pVtab->pModule = pMod->pModule;
pVTable->nRef = 1;
- if( sCtx.pTab ){
+ if( sCtx.bDeclared==0 ){
const char *zFormat = "vtable constructor did not declare schema: %s";
*pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
sqlite3VtabUnlock(pVTable);
@@ -706,8 +722,8 @@
** virtual table module.
*/
int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
+ VtabCtx *pCtx = db->pVtabCtx;
Parse *pParse;
-
int rc = SQLITE_OK;
Table *pTab;
char *zErr = 0;
@@ -718,11 +734,12 @@
}
#endif
sqlite3_mutex_enter(db->mutex);
- if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){
+ if( !pCtx || pCtx->bDeclared ){
sqlite3Error(db, SQLITE_MISUSE);
sqlite3_mutex_leave(db->mutex);
return SQLITE_MISUSE_BKPT;
}
+ pTab = pCtx->pTab;
assert( (pTab->tabFlags & TF_Virtual)!=0 );
pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
@@ -745,7 +762,7 @@
pParse->pNewTable->nCol = 0;
pParse->pNewTable->aCol = 0;
}
- db->pVtabCtx->pTab = 0;
+ pCtx->bDeclared = 1;
}else{
sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
sqlite3DbFree(db, zErr);
diff --git a/test/attach.test b/test/attach.test
index be5f988..31c24e6 100644
--- a/test/attach.test
+++ b/test/attach.test
@@ -859,4 +859,16 @@
}] 9 end
} {4 noname {} 5 inmem {}}
+# Attach with a very long URI filename.
+#
+db close
+sqlite3 db test.db -uri 1
+do_execsql_test attach-11.1 {
+ ATTACH printf('file:%09000x/x.db?mode=memory&cache=shared',1) AS aux1;
+ CREATE TABLE aux1.t1(x,y);
+ INSERT INTO aux1.t1(x,y) VALUES(1,2),(3,4);
+ SELECT * FROM aux1.t1;
+} {1 2 3 4}
+
+
finish_test
diff --git a/test/corruptJ.test b/test/corruptJ.test
new file mode 100644
index 0000000..c08e628
--- /dev/null
+++ b/test/corruptJ.test
@@ -0,0 +1,80 @@
+# 2015-03-30
+#
+# The author disclaims copyright to this source code. In place of
+# a legal notice, here is a blessing:
+#
+# May you do good and not evil.
+# May you find forgiveness for yourself and forgive others.
+# May you share freely, never taking more than you give.
+#
+#***********************************************************************
+#
+# Corruption consisting of a database page that thinks it is a child
+# of itself.
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set testprefix corruptJ
+
+if {[permutation]=="mmap"} {
+ finish_test
+ return
+}
+
+# Do not use a codec for tests in this file, as the database file is
+# manipulated directly using tcl scripts (using the [hexio_write] command).
+#
+do_not_use_codec
+database_may_be_corrupt
+
+# Initialize the database.
+#
+do_execsql_test 1.1 {
+ PRAGMA page_size=1024;
+ PRAGMA auto_vacuum=0;
+ CREATE TABLE t1(a,b);
+ WITH RECURSIVE c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<10)
+ INSERT INTO t1(a,b) SELECT i, zeroblob(700) FROM c;
+} {}
+db close
+
+# Corrupt the root page of the t1 table such that the left-child pointer
+# for the very first cell points back to the root. Then try to DROP the
+# table. The clearDatabasePage() routine should not loop.
+#
+do_test 1.2 {
+ hexio_write test.db [expr {2*1024-2}] 02
+ sqlite3 db test.db
+ catchsql { DROP TABLE t1 }
+} {1 {database disk image is malformed}}
+
+# Similar test using a WITHOUT ROWID table
+#
+do_test 2.1 {
+ db close
+ forcedelete test.db
+ sqlite3 db test.db
+ db eval {
+ PRAGMA page_size=1024;
+ PRAGMA auto_vacuum=0;
+ CREATE TABLE t1(a,b,PRIMARY KEY(a,b)) WITHOUT ROWID;
+ WITH RECURSIVE c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<100)
+ INSERT INTO t1(a,b) SELECT i, zeroblob(200) FROM c;
+ }
+} {}
+
+# The table is three levels deep. Corrupt the left child of an intermediate
+# page so that it points back to the root page.
+#
+do_test 2.2 {
+ db close
+ hexio_read test.db [expr {9*1024+391}] 8
+} {00000008814D0401}
+do_test 2.2b {
+ hexio_write test.db [expr {9*1024+391}] 00000002
+ sqlite3 db test.db
+ catchsql { PRAGMA secure_delete=ON; DROP TABLE t1; }
+} {1 {database disk image is malformed}}
+
+finish_test
diff --git a/test/e_walauto.test b/test/e_walauto.test
index b624b24..a1f4eb7 100644
--- a/test/e_walauto.test
+++ b/test/e_walauto.test
@@ -15,6 +15,14 @@
source $testdir/wal_common.tcl
set testprefix e_walauto
+# Do not run this test on OpenBSD, as it depends on read() and mmap both
+# accessing the same coherent view of the "test.db-shm" file. This doesn't
+# work on OpenBSD.
+#
+if {$tcl_platform(os) == "OpenBSD"} {
+ finish_test
+ return
+}
proc read_nbackfill {} {
seek $::shmfd 96
diff --git a/test/fts3fault2.test b/test/fts3fault2.test
index f2d10bc..030ff73 100644
--- a/test/fts3fault2.test
+++ b/test/fts3fault2.test
@@ -155,4 +155,23 @@
}
}
+reset_db
+do_test 6.0 {
+ execsql {
+ CREATE VIRTUAL TABLE t6 USING fts4(x,order=DESC);
+ INSERT INTO t6(docid, x) VALUES(-1,'a b');
+ INSERT INTO t6(docid, x) VALUES(1, 'b');
+ }
+ faultsim_save_and_close
+} {}
+
+do_faultsim_test 6.1 -faults oom* -prep {
+ faultsim_restore_and_reopen
+ db eval {SELECT * FROM sqlite_master}
+} -body {
+ execsql { SELECT docid FROM t6 WHERE t6 MATCH '"a* b"' }
+} -test {
+ faultsim_test_result {0 -1}
+}
+
finish_test
diff --git a/test/fts3prefix.test b/test/fts3prefix.test
index 8ffabe8..e8d2b2b 100644
--- a/test/fts3prefix.test
+++ b/test/fts3prefix.test
@@ -274,4 +274,22 @@
SELECT md5sum(quote(root)) FROM t1_segdir;
} [db eval {SELECT md5sum(quote(root)) FROM t2_segdir}]
+
+do_execsql_test 7.0 {
+ CREATE VIRTUAL TABLE t6 USING fts4(x,order=DESC);
+ INSERT INTO t6(docid, x) VALUES(-1,'a b');
+ INSERT INTO t6(docid, x) VALUES(1, 'b');
+}
+do_execsql_test 7.1 {
+ SELECT docid FROM t6 WHERE t6 MATCH '"a* b"';
+} {-1}
+do_execsql_test 7.2 {
+ SELECT docid FROM t6 WHERE t6 MATCH 'a*';
+} {-1}
+do_execsql_test 7.3 {
+ SELECT docid FROM t6 WHERE t6 MATCH 'a* b';
+} {-1}
+
+
+
finish_test
diff --git a/test/fts4content.test b/test/fts4content.test
index 6b2cd3c..481c6ec 100644
--- a/test/fts4content.test
+++ b/test/fts4content.test
@@ -48,6 +48,9 @@
#
# 9.* - Test using content=xxx where xxx is a virtual table.
#
+# 11.* - Test that circular references (e.g. "t1(content=t1)") are
+# detected.
+#
do_execsql_test 1.1.1 {
CREATE TABLE t1(a, b, c);
@@ -406,7 +409,7 @@
#
do_catchsql_test 6.1.1 {
CREATE VIRTUAL TABLE ft7 USING fts4(content=t7);
-} {1 {vtable constructor failed: ft7}}
+} {1 {no such table: main.t7}}
do_execsql_test 6.2.1 {
CREATE TABLE t7(one, two);
@@ -433,7 +436,7 @@
}
do_catchsql_test 6.2.4 {
SELECT * FROM ft7;
-} {1 {vtable constructor failed: ft7}}
+} {1 {no such table: main.t7}}
do_execsql_test 6.2.5 {
CREATE TABLE t7(x, y);
INSERT INTO t7 VALUES('A B', 'B A');
@@ -622,4 +625,15 @@
{...c d [e] f g...}
}
+#-------------------------------------------------------------------------
+# Test cases 11.*
+#
+reset_db
+
+do_catchsql_test 11.1 {
+ CREATE VIRTUAL TABLE x1 USING fts4(content=x1);
+} {1 {vtable constructor called recursively: x1}}
+
+
finish_test
+
diff --git a/test/printf.test b/test/printf.test
index 7322272..6103d8a 100644
--- a/test/printf.test
+++ b/test/printf.test
@@ -472,6 +472,18 @@
sqlite3_mprintf_int {abc: (%#6d) (%#6x) (%#6o) :xyz}\
0xff676981 0xff676981 0xff676981
} {abc: (-9999999) (0xff676981) (037731664601) :xyz}
+do_test printf-1.17.1 {
+ sqlite3_mprintf_int {abd: %2147483647d %2147483647x %2147483647o} 1 1 1
+} {}
+do_test printf-1.17.2 {
+ sqlite3_mprintf_int {abd: %*d %x} 2147483647 1 1
+} {}
+do_test printf-1.17.3 {
+ sqlite3_mprintf_int {abd: %*d %x} -2147483648 1 1
+} {abd: 1 1}
+do_test printf-1.17.4 {
+ sqlite3_mprintf_int {abd: %.2147483648d %x %x} 1 1 1
+} {/.*/}
do_test printf-2.1.1.1 {
sqlite3_mprintf_double {abc: (%*.*f) :xyz} 1 1 0.001
} {abc: (0.0) :xyz}
@@ -526,6 +538,9 @@
do_test printf-2.1.2.9 {
sqlite3_mprintf_double {abc: %d %d (%1.1g) :xyz} 1 1 1.0e-20
} {abc: 1 1 (1e-20) :xyz}
+do_test printf-2.1.2.10 {
+ sqlite3_mprintf_double {abc: %*.*f} 2000000000 1000000000 1.0e-20
+} {abc: }
do_test printf-2.1.3.1 {
sqlite3_mprintf_double {abc: (%*.*f) :xyz} 1 1 1.0
} {abc: (1.0) :xyz}
@@ -3466,6 +3481,15 @@
do_test printf-3.6 {
sqlite3_mprintf_str {%d %d A String: (%-30s)} 1 2 {This is the string}
} [format {%d %d A String: (%-30s)} 1 2 {This is the string}]
+do_test printf-3.7 {
+ sqlite3_mprintf_str {%d A String: (%*s)} 1 2147483647 {This is the string}
+} []
+do_test printf-3.8 {
+ sqlite3_mprintf_str {%d A String: (%*s)} 1 -2147483648 {This is the string}
+} {1 A String: (This is the string)}
+do_test printf-3.9 {
+ sqlite3_mprintf_str {%d A String: (%.*s)} 1 -2147483648 {This is the string}
+} {1 A String: (This is the string)}
do_test snprintf-3.11 {
sqlite3_snprintf_str 2 {x%d %d %s} 10 10 {This is the string}
} {x}
@@ -3685,6 +3709,9 @@
do_test printf-13.6 {
sqlite3_mprintf_hexdouble %.20f fff8000000000000
} {NaN}
+do_test printf-13.7 {
+ sqlite3_mprintf_hexdouble %2147483648.10000f 4693b8b5b5056e17
+} {/100000000000000000000000000000000.00/}
do_test printf-14.1 {
sqlite3_mprintf_str {abc-%y-123} 0 0 {not used}
diff --git a/test/selectA.test b/test/selectA.test
index 6e593e8..0338338 100644
--- a/test/selectA.test
+++ b/test/selectA.test
@@ -1375,4 +1375,64 @@
} {/2 . 3 . 4 . 5 . 6 . 7 ./}
+proc strip_rnd {explain} {
+ regexp -all {sqlite_sq_[0123456789ABCDEF]*} $explain sqlite_sq
+}
+
+proc do_same_test {tn q1 args} {
+ set r2 [strip_rnd [db eval "EXPLAIN $q1"]]
+ set i 1
+ foreach q $args {
+ set tst [subst -nocommands {strip_rnd [db eval "EXPLAIN $q"]}]
+ uplevel do_test $tn.$i [list $tst] [list $r2]
+ incr i
+ }
+}
+
+do_execsql_test 5.0 {
+ CREATE TABLE t8(a, b);
+ CREATE TABLE t9(c, d);
+} {}
+
+do_same_test 5.1 {
+ SELECT a, b FROM t8 INTERSECT SELECT c, d FROM t9 ORDER BY a;
+} {
+ SELECT a, b FROM t8 INTERSECT SELECT c, d FROM t9 ORDER BY t8.a;
+} {
+ SELECT a, b FROM t8 INTERSECT SELECT c, d FROM t9 ORDER BY 1;
+} {
+ SELECT a, b FROM t8 INTERSECT SELECT c, d FROM t9 ORDER BY c;
+} {
+ SELECT a, b FROM t8 INTERSECT SELECT c, d FROM t9 ORDER BY t9.c;
+}
+
+do_same_test 5.2 {
+ SELECT a, b FROM t8 UNION SELECT c, d FROM t9 ORDER BY a COLLATE NOCASE
+} {
+ SELECT a, b FROM t8 UNION SELECT c, d FROM t9 ORDER BY t8.a COLLATE NOCASE
+} {
+ SELECT a, b FROM t8 UNION SELECT c, d FROM t9 ORDER BY 1 COLLATE NOCASE
+} {
+ SELECT a, b FROM t8 UNION SELECT c, d FROM t9 ORDER BY c COLLATE NOCASE
+} {
+ SELECT a, b FROM t8 UNION SELECT c, d FROM t9 ORDER BY t9.c COLLATE NOCASE
+}
+
+do_same_test 5.3 {
+ SELECT a, b FROM t8 EXCEPT SELECT c, d FROM t9 ORDER BY b, c COLLATE NOCASE
+} {
+ SELECT a, b FROM t8 EXCEPT SELECT c, d FROM t9 ORDER BY 2, 1 COLLATE NOCASE
+} {
+ SELECT a, b FROM t8 EXCEPT SELECT c, d FROM t9 ORDER BY d, a COLLATE NOCASE
+} {
+ SELECT a, b FROM t8 EXCEPT SELECT * FROM t9 ORDER BY t9.d, c COLLATE NOCASE
+} {
+ SELECT * FROM t8 EXCEPT SELECT c, d FROM t9 ORDER BY d, t8.a COLLATE NOCASE
+}
+
+do_catchsql_test 5.4 {
+ SELECT * FROM t8 UNION SELECT * FROM t9 ORDER BY a+b COLLATE NOCASE
+} {1 {1st ORDER BY term does not match any column in the result set}}
+
+
finish_test
diff --git a/test/walblock.test b/test/walblock.test
index 0b0b224..23167a8 100644
--- a/test/walblock.test
+++ b/test/walblock.test
@@ -15,6 +15,7 @@
source $testdir/lock_common.tcl
source $testdir/wal_common.tcl
+finish_test; return; # Feature currently not implemented.
ifcapable !wal {finish_test ; return }
if {$::tcl_platform(platform)!="unix"} { finish_test ; return }
set testprefix walblock
@@ -111,7 +112,3 @@
finish_test
-
-
-
-
diff --git a/test/win32lock.test b/test/win32lock.test
index cff1ed3..d1f3d1a 100644
--- a/test/win32lock.test
+++ b/test/win32lock.test
@@ -64,6 +64,7 @@
if {[info exists ::log] && $::log!=""} {
do_test win32lock-1.2-$delay1-log1 {
regsub {\d+} $::log # x
+ regsub { at line \d+} $x "" x
set x
} {{delayed #ms for lock/sharing conflict}}
}
@@ -112,6 +113,7 @@
if {[info exists ::log] && $::log!=""} {
do_test win32lock-2.2-$delay1-log1 {
regsub {\d+} $::log # x
+ regsub { at line \d+} $x "" x
set x
} {{delayed #ms for lock/sharing conflict}}
}
diff --git a/tool/lemon.c b/tool/lemon.c
index 4ada425..cc3066b 100644
--- a/tool/lemon.c
+++ b/tool/lemon.c
@@ -1731,7 +1731,7 @@
char *ep;
char *set[LISTSIZE];
int i;
- offset = (unsigned long)next - (unsigned long)list;
+ offset = (unsigned long)((char*)next - (char*)list);
for(i=0; i<LISTSIZE; i++) set[i] = 0;
while( list ){
ep = list;
@@ -1875,7 +1875,7 @@
if( *end ){
if( err ){
fprintf(err,"%sillegal character in floating-point argument.\n",emsg);
- errline(i,((unsigned long)end)-(unsigned long)argv[i],err);
+ errline(i,(int)((char*)end-(char*)argv[i]),err);
}
errcnt++;
}
@@ -1886,7 +1886,7 @@
if( *end ){
if( err ){
fprintf(err,"%sillegal character in integer argument.\n",emsg);
- errline(i,((unsigned long)end)-(unsigned long)argv[i],err);
+ errline(i,(int)((char*)end-(char*)argv[i]),err);
}
errcnt++;
}
diff --git a/tool/showlocks.c b/tool/showlocks.c
new file mode 100644
index 0000000..752c535
--- /dev/null
+++ b/tool/showlocks.c
@@ -0,0 +1,64 @@
+/*
+** This file implements a simple command-line utility that shows all of the
+** Posix Advisory Locks on a file.
+**
+** Usage:
+**
+** showlocks FILENAME
+**
+** To compile: gcc -o showlocks showlocks.c
+*/
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* This utility only looks for locks in the first 2 billion bytes */
+#define MX_LCK 2147483647
+
+/*
+** Print all locks on the inode of "fd" that occur in between
+** lwr and upr, inclusive.
+*/
+static int showLocksInRange(int fd, off_t lwr, off_t upr){
+ int cnt = 0;
+ struct flock x;
+
+ x.l_type = F_WRLCK;
+ x.l_whence = SEEK_SET;
+ x.l_start = lwr;
+ x.l_len = upr-lwr;
+ fcntl(fd, F_GETLK, &x);
+ if( x.l_type==F_UNLCK ) return 0;
+ printf("start: %-12d len: %-5d pid: %-5d type: %s\n",
+ (int)x.l_start, (int)x.l_len,
+ x.l_pid, x.l_type==F_WRLCK ? "WRLCK" : "RDLCK");
+ cnt++;
+ if( x.l_start>lwr ){
+ cnt += showLocksInRange(fd, lwr, x.l_start-1);
+ }
+ if( x.l_start+x.l_len<upr ){
+ cnt += showLocksInRange(fd, x.l_start+x.l_len+1, upr);
+ }
+ return cnt;
+}
+
+int main(int argc, char **argv){
+ int fd;
+ int cnt;
+
+ if( argc!=2 ){
+ fprintf(stderr, "Usage: %s FILENAME\n", argv[0]);
+ return 1;
+ }
+ fd = open(argv[1], O_RDWR, 0);
+ if( fd<0 ){
+ fprintf(stderr, "%s: cannot open %s\n", argv[0], argv[1]);
+ return 1;
+ }
+ cnt = showLocksInRange(fd, 0, MX_LCK);
+ if( cnt==0 ) printf("no locks\n");
+ close(fd);
+ return 0;
+}
diff --git a/tool/sqldiff.c b/tool/sqldiff.c
new file mode 100644
index 0000000..53c5977
--- /dev/null
+++ b/tool/sqldiff.c
@@ -0,0 +1,817 @@
+/*
+** 2015-04-06
+**
+** The author disclaims copyright to this source code. In place of
+** a legal notice, here is a blessing:
+**
+** May you do good and not evil.
+** May you find forgiveness for yourself and forgive others.
+** May you share freely, never taking more than you give.
+**
+*************************************************************************
+**
+** This is a utility problem that computes the differences in content
+** between two SQLite databases.
+*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <ctype.h>
+#include <string.h>
+#include "sqlite3.h"
+
+/*
+** All global variables are gathered into the "g" singleton.
+*/
+struct GlobalVars {
+ const char *zArgv0; /* Name of program */
+ int bSchemaOnly; /* Only show schema differences */
+ int bSchemaPK; /* Use the schema-defined PK, not the true PK */
+ unsigned fDebug; /* Debug flags */
+ sqlite3 *db; /* The database connection */
+} g;
+
+/*
+** Allowed values for g.fDebug
+*/
+#define DEBUG_COLUMN_NAMES 0x000001
+#define DEBUG_DIFF_SQL 0x000002
+
+/*
+** Dynamic string object
+*/
+typedef struct Str Str;
+struct Str {
+ char *z; /* Text of the string */
+ int nAlloc; /* Bytes allocated in z[] */
+ int nUsed; /* Bytes actually used in z[] */
+};
+
+/*
+** Initialize a Str object
+*/
+static void strInit(Str *p){
+ p->z = 0;
+ p->nAlloc = 0;
+ p->nUsed = 0;
+}
+
+/*
+** Print an error resulting from faulting command-line arguments and
+** abort the program.
+*/
+static void cmdlineError(const char *zFormat, ...){
+ va_list ap;
+ fprintf(stderr, "%s: ", g.zArgv0);
+ va_start(ap, zFormat);
+ vfprintf(stderr, zFormat, ap);
+ va_end(ap);
+ fprintf(stderr, "\n\"%s --help\" for more help\n", g.zArgv0);
+ exit(1);
+}
+
+/*
+** Print an error message for an error that occurs at runtime, then
+** abort the program.
+*/
+static void runtimeError(const char *zFormat, ...){
+ va_list ap;
+ fprintf(stderr, "%s: ", g.zArgv0);
+ va_start(ap, zFormat);
+ vfprintf(stderr, zFormat, ap);
+ va_end(ap);
+ fprintf(stderr, "\n");
+ exit(1);
+}
+
+/*
+** Free all memory held by a Str object
+*/
+static void strFree(Str *p){
+ sqlite3_free(p->z);
+ strInit(p);
+}
+
+/*
+** Add formatted text to the end of a Str object
+*/
+static void strPrintf(Str *p, const char *zFormat, ...){
+ int nNew;
+ for(;;){
+ if( p->z ){
+ va_list ap;
+ va_start(ap, zFormat);
+ sqlite3_vsnprintf(p->nAlloc-p->nUsed, p->z+p->nUsed, zFormat, ap);
+ va_end(ap);
+ nNew = (int)strlen(p->z + p->nUsed);
+ }else{
+ nNew = p->nAlloc;
+ }
+ if( p->nUsed+nNew < p->nAlloc-1 ){
+ p->nUsed += nNew;
+ break;
+ }
+ p->nAlloc = p->nAlloc*2 + 1000;
+ p->z = sqlite3_realloc(p->z, p->nAlloc);
+ if( p->z==0 ) runtimeError("out of memory");
+ }
+}
+
+
+
+/* Safely quote an SQL identifier. Use the minimum amount of transformation
+** necessary to allow the string to be used with %s.
+**
+** Space to hold the returned string is obtained from sqlite3_malloc(). The
+** caller is responsible for ensuring this space is freed when no longer
+** needed.
+*/
+static char *safeId(const char *zId){
+ /* All SQLite keywords, in alphabetical order */
+ static const char *azKeywords[] = {
+ "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS",
+ "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY",
+ "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT",
+ "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE",
+ "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE",
+ "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH",
+ "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN",
+ "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF",
+ "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER",
+ "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY",
+ "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL",
+ "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA",
+ "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP",
+ "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT",
+ "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP",
+ "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE",
+ "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE",
+ "WITH", "WITHOUT",
+ };
+ int lwr, upr, mid, c, i, x;
+ for(i=x=0; (c = zId[i])!=0; i++){
+ if( !isalpha(c) && c!='_' ){
+ if( i>0 && isdigit(c) ){
+ x++;
+ }else{
+ return sqlite3_mprintf("\"%w\"", zId);
+ }
+ }
+ }
+ if( x ) return sqlite3_mprintf("%s", zId);
+ lwr = 0;
+ upr = sizeof(azKeywords)/sizeof(azKeywords[0]) - 1;
+ while( lwr<=upr ){
+ mid = (lwr+upr)/2;
+ c = sqlite3_stricmp(azKeywords[mid], zId);
+ if( c==0 ) return sqlite3_mprintf("\"%w\"", zId);
+ if( c<0 ){
+ lwr = mid+1;
+ }else{
+ upr = mid-1;
+ }
+ }
+ return sqlite3_mprintf("%s", zId);
+}
+
+/*
+** Prepare a new SQL statement. Print an error and abort if anything
+** goes wrong.
+*/
+static sqlite3_stmt *db_vprepare(const char *zFormat, va_list ap){
+ char *zSql;
+ int rc;
+ sqlite3_stmt *pStmt;
+
+ zSql = sqlite3_vmprintf(zFormat, ap);
+ if( zSql==0 ) runtimeError("out of memory");
+ rc = sqlite3_prepare_v2(g.db, zSql, -1, &pStmt, 0);
+ if( rc ){
+ runtimeError("SQL statement error: %s\n\"%s\"", sqlite3_errmsg(g.db),
+ zSql);
+ }
+ sqlite3_free(zSql);
+ return pStmt;
+}
+static sqlite3_stmt *db_prepare(const char *zFormat, ...){
+ va_list ap;
+ sqlite3_stmt *pStmt;
+ va_start(ap, zFormat);
+ pStmt = db_vprepare(zFormat, ap);
+ va_end(ap);
+ return pStmt;
+}
+
+/*
+** Free a list of strings
+*/
+static void namelistFree(char **az){
+ if( az ){
+ int i;
+ for(i=0; az[i]; i++) sqlite3_free(az[i]);
+ sqlite3_free(az);
+ }
+}
+
+/*
+** Return a list of column names for the table zDb.zTab. Space to
+** hold the list is obtained from sqlite3_malloc() and should released
+** using namelistFree() when no longer needed.
+**
+** Primary key columns are listed first, followed by data columns.
+** The number of columns in the primary key is returned in *pnPkey.
+**
+** Normally, the "primary key" in the previous sentence is the true
+** primary key - the rowid or INTEGER PRIMARY KEY for ordinary tables
+** or the declared PRIMARY KEY for WITHOUT ROWID tables. However, if
+** the g.bSchemaPK flag is set, then the schema-defined PRIMARY KEY is
+** used in all cases. In that case, entries that have NULL values in
+** any of their primary key fields will be excluded from the analysis.
+**
+** If the primary key for a table is the rowid but rowid is inaccessible,
+** then this routine returns a NULL pointer.
+**
+** Examples:
+** CREATE TABLE t1(a INT UNIQUE, b INTEGER, c TEXT, PRIMARY KEY(c));
+** *pnPKey = 1;
+** az = { "rowid", "a", "b", "c", 0 } // Normal case
+** az = { "c", "a", "b", 0 } // g.bSchemaPK==1
+**
+** CREATE TABLE t2(a INT UNIQUE, b INTEGER, c TEXT, PRIMARY KEY(b));
+** *pnPKey = 1;
+** az = { "b", "a", "c", 0 }
+**
+** CREATE TABLE t3(x,y,z,PRIMARY KEY(y,z));
+** *pnPKey = 1 // Normal case
+** az = { "rowid", "x", "y", "z", 0 } // Normal case
+** *pnPKey = 2 // g.bSchemaPK==1
+** az = { "y", "x", "z", 0 } // g.bSchemaPK==1
+**
+** CREATE TABLE t4(x,y,z,PRIMARY KEY(y,z)) WITHOUT ROWID;
+** *pnPKey = 2
+** az = { "y", "z", "x", 0 }
+**
+** CREATE TABLE t5(rowid,_rowid_,oid);
+** az = 0 // The rowid is not accessible
+*/
+static char **columnNames(const char *zDb, const char *zTab, int *pnPKey){
+ char **az = 0; /* List of column names to be returned */
+ int naz = 0; /* Number of entries in az[] */
+ sqlite3_stmt *pStmt; /* SQL statement being run */
+ char *zPkIdxName = 0; /* Name of the PRIMARY KEY index */
+ int truePk = 0; /* PRAGMA table_info indentifies the PK to use */
+ int nPK = 0; /* Number of PRIMARY KEY columns */
+ int i, j; /* Loop counters */
+
+ if( g.bSchemaPK==0 ){
+ /* Normal case: Figure out what the true primary key is for the table.
+ ** * For WITHOUT ROWID tables, the true primary key is the same as
+ ** the schema PRIMARY KEY, which is guaranteed to be present.
+ ** * For rowid tables with an INTEGER PRIMARY KEY, the true primary
+ ** key is the INTEGER PRIMARY KEY.
+ ** * For all other rowid tables, the rowid is the true primary key.
+ */
+ pStmt = db_prepare("PRAGMA %s.index_list=%Q", zDb, zTab);
+ while( SQLITE_ROW==sqlite3_step(pStmt) ){
+ if( sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,3),"pk")==0 ){
+ zPkIdxName = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
+ break;
+ }
+ }
+ sqlite3_finalize(pStmt);
+ if( zPkIdxName ){
+ int nKey = 0;
+ int nCol = 0;
+ truePk = 0;
+ pStmt = db_prepare("PRAGMA %s.index_xinfo=%Q", zDb, zPkIdxName);
+ while( SQLITE_ROW==sqlite3_step(pStmt) ){
+ nCol++;
+ if( sqlite3_column_int(pStmt,5) ){ nKey++; continue; }
+ if( sqlite3_column_int(pStmt,1)>=0 ) truePk = 1;
+ }
+ if( nCol==nKey ) truePk = 1;
+ if( truePk ){
+ nPK = nKey;
+ }else{
+ nPK = 1;
+ }
+ sqlite3_finalize(pStmt);
+ sqlite3_free(zPkIdxName);
+ }else{
+ truePk = 1;
+ nPK = 1;
+ }
+ pStmt = db_prepare("PRAGMA %s.table_info=%Q", zDb, zTab);
+ }else{
+ /* The g.bSchemaPK==1 case: Use whatever primary key is declared
+ ** in the schema. The "rowid" will still be used as the primary key
+ ** if the table definition does not contain a PRIMARY KEY.
+ */
+ nPK = 0;
+ pStmt = db_prepare("PRAGMA %s.table_info=%Q", zDb, zTab);
+ while( SQLITE_ROW==sqlite3_step(pStmt) ){
+ if( sqlite3_column_int(pStmt,5)>0 ) nPK++;
+ }
+ sqlite3_reset(pStmt);
+ if( nPK==0 ) nPK = 1;
+ truePk = 1;
+ }
+ *pnPKey = nPK;
+ naz = nPK;
+ az = sqlite3_malloc( sizeof(char*)*(nPK+1) );
+ if( az==0 ) runtimeError("out of memory");
+ memset(az, 0, sizeof(char*)*(nPK+1));
+ while( SQLITE_ROW==sqlite3_step(pStmt) ){
+ int iPKey;
+ if( truePk && (iPKey = sqlite3_column_int(pStmt,5))>0 ){
+ az[iPKey-1] = safeId((char*)sqlite3_column_text(pStmt,1));
+ }else{
+ az = sqlite3_realloc(az, sizeof(char*)*(naz+2) );
+ if( az==0 ) runtimeError("out of memory");
+ az[naz++] = safeId((char*)sqlite3_column_text(pStmt,1));
+ }
+ }
+ sqlite3_finalize(pStmt);
+ if( az ) az[naz] = 0;
+ if( az[0]==0 ){
+ const char *azRowid[] = { "rowid", "_rowid_", "oid" };
+ for(i=0; i<sizeof(azRowid)/sizeof(azRowid[0]); i++){
+ for(j=1; j<naz; j++){
+ if( sqlite3_stricmp(az[j], azRowid[i])==0 ) break;
+ }
+ if( j>=naz ){
+ az[0] = sqlite3_mprintf("%s", azRowid[i]);
+ break;
+ }
+ }
+ if( az[0]==0 ){
+ for(i=1; i<naz; i++) sqlite3_free(az[i]);
+ sqlite3_free(az);
+ az = 0;
+ }
+ }
+ return az;
+}
+
+/*
+** Print the sqlite3_value X as an SQL literal.
+*/
+static void printQuoted(sqlite3_value *X){
+ switch( sqlite3_value_type(X) ){
+ case SQLITE_FLOAT: {
+ double r1;
+ char zBuf[50];
+ r1 = sqlite3_value_double(X);
+ sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
+ printf("%s", zBuf);
+ break;
+ }
+ case SQLITE_INTEGER: {
+ printf("%lld", sqlite3_value_int64(X));
+ break;
+ }
+ case SQLITE_BLOB: {
+ const unsigned char *zBlob = sqlite3_value_blob(X);
+ int nBlob = sqlite3_value_bytes(X);
+ if( zBlob ){
+ int i;
+ printf("x'");
+ for(i=0; i<nBlob; i++){
+ printf("%02x", zBlob[i]);
+ }
+ printf("'");
+ }else{
+ printf("NULL");
+ }
+ break;
+ }
+ case SQLITE_TEXT: {
+ const unsigned char *zArg = sqlite3_value_text(X);
+ int i, j;
+
+ if( zArg==0 ){
+ printf("NULL");
+ }else{
+ printf("'");
+ for(i=j=0; zArg[i]; i++){
+ if( zArg[i]=='\'' ){
+ printf("%.*s'", i-j+1, &zArg[j]);
+ j = i+1;
+ }
+ }
+ printf("%s'", &zArg[j]);
+ }
+ break;
+ }
+ case SQLITE_NULL: {
+ printf("NULL");
+ break;
+ }
+ }
+}
+
+/*
+** Output SQL that will recreate the aux.zTab table.
+*/
+static void dump_table(const char *zTab){
+ char *zId = safeId(zTab); /* Name of the table */
+ char **az = 0; /* List of columns */
+ int nPk; /* Number of true primary key columns */
+ int nCol; /* Number of data columns */
+ int i; /* Loop counter */
+ sqlite3_stmt *pStmt; /* SQL statement */
+ const char *zSep; /* Separator string */
+ Str ins; /* Beginning of the INSERT statement */
+
+ pStmt = db_prepare("SELECT sql FROM aux.sqlite_master WHERE name=%Q", zTab);
+ if( SQLITE_ROW==sqlite3_step(pStmt) ){
+ printf("%s;\n", sqlite3_column_text(pStmt,0));
+ }
+ sqlite3_finalize(pStmt);
+ if( !g.bSchemaOnly ){
+ az = columnNames("aux", zTab, &nPk);
+ strInit(&ins);
+ if( az==0 ){
+ pStmt = db_prepare("SELECT * FROM aux.%s", zId);
+ strPrintf(&ins,"INSERT INTO %s VALUES", zId);
+ }else{
+ Str sql;
+ strInit(&sql);
+ zSep = "SELECT";
+ for(i=0; az[i]; i++){
+ strPrintf(&sql, "%s %s", zSep, az[i]);
+ zSep = ",";
+ }
+ strPrintf(&sql," FROM aux.%s", zId);
+ zSep = " ORDER BY";
+ for(i=1; i<=nPk; i++){
+ strPrintf(&sql, "%s %d", zSep, i);
+ zSep = ",";
+ }
+ pStmt = db_prepare("%s", sql.z);
+ strFree(&sql);
+ strPrintf(&ins, "INSERT INTO %s", zId);
+ zSep = "(";
+ for(i=0; az[i]; i++){
+ strPrintf(&ins, "%s%s", zSep, az[i]);
+ zSep = ",";
+ }
+ strPrintf(&ins,") VALUES");
+ namelistFree(az);
+ }
+ nCol = sqlite3_column_count(pStmt);
+ while( SQLITE_ROW==sqlite3_step(pStmt) ){
+ printf("%s",ins.z);
+ zSep = "(";
+ for(i=0; i<nCol; i++){
+ printf("%s",zSep);
+ printQuoted(sqlite3_column_value(pStmt,i));
+ zSep = ",";
+ }
+ printf(");\n");
+ }
+ sqlite3_finalize(pStmt);
+ strFree(&ins);
+ } /* endif !g.bSchemaOnly */
+ pStmt = db_prepare("SELECT sql FROM aux.sqlite_master"
+ " WHERE type='index' AND tbl_name=%Q AND sql IS NOT NULL",
+ zTab);
+ while( SQLITE_ROW==sqlite3_step(pStmt) ){
+ printf("%s;\n", sqlite3_column_text(pStmt,0));
+ }
+ sqlite3_finalize(pStmt);
+}
+
+
+/*
+** Compute all differences for a single table.
+*/
+static void diff_one_table(const char *zTab){
+ char *zId = safeId(zTab); /* Name of table (translated for us in SQL) */
+ char **az = 0; /* Columns in main */
+ char **az2 = 0; /* Columns in aux */
+ int nPk; /* Primary key columns in main */
+ int nPk2; /* Primary key columns in aux */
+ int n; /* Number of columns in main */
+ int n2; /* Number of columns in aux */
+ int nQ; /* Number of output columns in the diff query */
+ int i; /* Loop counter */
+ const char *zSep; /* Separator string */
+ Str sql; /* Comparison query */
+ sqlite3_stmt *pStmt; /* Query statement to do the diff */
+
+ strInit(&sql);
+ if( g.fDebug==DEBUG_COLUMN_NAMES ){
+ /* Simply run columnNames() on all tables of the origin
+ ** database and show the results. This is used for testing
+ ** and debugging of the columnNames() function.
+ */
+ az = columnNames("aux",zTab, &nPk);
+ if( az==0 ){
+ printf("Rowid not accessible for %s\n", zId);
+ }else{
+ printf("%s:", zId);
+ for(i=0; az[i]; i++){
+ printf(" %s", az[i]);
+ if( i+1==nPk ) printf(" *");
+ }
+ printf("\n");
+ }
+ goto end_diff_one_table;
+ }
+
+
+ if( sqlite3_table_column_metadata(g.db,"aux",zTab,0,0,0,0,0,0) ){
+ if( !sqlite3_table_column_metadata(g.db,"main",zTab,0,0,0,0,0,0) ){
+ /* Table missing from second database. */
+ printf("DROP TABLE %s;\n", zId);
+ }
+ goto end_diff_one_table;
+ }
+
+ if( sqlite3_table_column_metadata(g.db,"main",zTab,0,0,0,0,0,0) ){
+ /* Table missing from source */
+ dump_table(zTab);
+ goto end_diff_one_table;
+ }
+
+ az = columnNames("main", zTab, &nPk);
+ az2 = columnNames("aux", zTab, &nPk2);
+ if( az && az2 ){
+ for(n=0; az[n]; n++){
+ if( sqlite3_stricmp(az[n],az2[n])!=0 ) break;
+ }
+ }
+ if( az==0
+ || az2==0
+ || nPk!=nPk2
+ || az[n]
+ ){
+ /* Schema mismatch */
+ printf("DROP TABLE %s;\n", zId);
+ dump_table(zTab);
+ goto end_diff_one_table;
+ }
+
+ /* Build the comparison query */
+ for(n2=n; az[n2]; n2++){}
+ nQ = nPk2+1+2*(n2-nPk2);
+ if( n2>nPk2 ){
+ zSep = "SELECT ";
+ for(i=0; i<nPk; i++){
+ strPrintf(&sql, "%sB.%s", zSep, az[i]);
+ zSep = ", ";
+ }
+ strPrintf(&sql, ", 1%s -- changed row\n", nPk==n ? "" : ",");
+ while( az[i] ){
+ strPrintf(&sql, " A.%s IS NOT B.%s, B.%s%s\n",
+ az[i], az[i], az[i], i==n2-1 ? "" : ",");
+ i++;
+ }
+ strPrintf(&sql, " FROM main.%s A, aux.%s B\n", zId, zId);
+ zSep = " WHERE";
+ for(i=0; i<nPk; i++){
+ strPrintf(&sql, "%s A.%s=B.%s", zSep, az[i], az[i]);
+ zSep = " AND";
+ }
+ zSep = "\n AND (";
+ while( az[i] ){
+ strPrintf(&sql, "%sA.%s IS NOT B.%s%s\n",
+ zSep, az[i], az[i], i==n2-1 ? ")" : "");
+ zSep = " OR ";
+ i++;
+ }
+ strPrintf(&sql, " UNION ALL\n");
+ }
+ zSep = "SELECT ";
+ for(i=0; i<nPk; i++){
+ strPrintf(&sql, "%sA.%s", zSep, az[i]);
+ zSep = ", ";
+ }
+ strPrintf(&sql, ", 2%s -- deleted row\n", nPk==n ? "" : ",");
+ while( az[i] ){
+ strPrintf(&sql, " NULL, NULL%s\n", i==n2-1 ? "" : ",");
+ i++;
+ }
+ strPrintf(&sql, " FROM main.%s A\n", zId);
+ strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM aux.%s B\n", zId);
+ zSep = " WHERE";
+ for(i=0; i<nPk; i++){
+ strPrintf(&sql, "%s A.%s=B.%s", zSep, az[i], az[i]);
+ zSep = " AND";
+ }
+ strPrintf(&sql, ")\n");
+ zSep = " UNION ALL\nSELECT ";
+ for(i=0; i<nPk; i++){
+ strPrintf(&sql, "%sB.%s", zSep, az[i]);
+ zSep = ", ";
+ }
+ strPrintf(&sql, ", 3%s -- inserted row\n", nPk==n ? "" : ",");
+ while( az2[i] ){
+ strPrintf(&sql, " 1, B.%s%s\n", az[i], i==n2-1 ? "" : ",");
+ i++;
+ }
+ strPrintf(&sql, " FROM aux.%s B\n", zId);
+ strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM main.%s A\n", zId);
+ zSep = " WHERE";
+ for(i=0; i<nPk; i++){
+ strPrintf(&sql, "%s A.%s=B.%s", zSep, az[i], az[i]);
+ zSep = " AND";
+ }
+ strPrintf(&sql, ")\n ORDER BY");
+ zSep = " ";
+ for(i=1; i<=nPk; i++){
+ strPrintf(&sql, "%s%d", zSep, i);
+ zSep = ", ";
+ }
+ strPrintf(&sql, ";\n");
+
+ if( g.fDebug & DEBUG_DIFF_SQL ){
+ printf("SQL for %s:\n%s\n", zId, sql.z);
+ goto end_diff_one_table;
+ }
+
+ /* Drop indexes that are missing in the destination */
+ pStmt = db_prepare(
+ "SELECT name FROM main.sqlite_master"
+ " WHERE type='index' AND tbl_name=%Q"
+ " AND sql IS NOT NULL"
+ " AND sql NOT IN (SELECT sql FROM aux.sqlite_master"
+ " WHERE type='index' AND tbl_name=%Q"
+ " AND sql IS NOT NULL)",
+ zTab, zTab);
+ while( SQLITE_ROW==sqlite3_step(pStmt) ){
+ char *z = safeId((const char*)sqlite3_column_text(pStmt,0));
+ printf("DROP INDEX %s;\n", z);
+ sqlite3_free(z);
+ }
+ sqlite3_finalize(pStmt);
+
+ /* Run the query and output differences */
+ if( !g.bSchemaOnly ){
+ pStmt = db_prepare(sql.z);
+ while( SQLITE_ROW==sqlite3_step(pStmt) ){
+ int iType = sqlite3_column_int(pStmt, nPk);
+ if( iType==1 || iType==2 ){
+ if( iType==1 ){ /* Change the content of a row */
+ printf("UPDATE %s", zId);
+ zSep = " SET";
+ for(i=nPk+1; i<nQ; i+=2){
+ if( sqlite3_column_int(pStmt,i)==0 ) continue;
+ printf("%s %s=", zSep, az2[(i+nPk-1)/2]);
+ zSep = ",";
+ printQuoted(sqlite3_column_value(pStmt,i+1));
+ }
+ }else{ /* Delete a row */
+ printf("DELETE FROM %s", zId);
+ }
+ zSep = " WHERE";
+ for(i=0; i<nPk; i++){
+ printf("%s %s=", zSep, az2[i]);
+ printQuoted(sqlite3_column_value(pStmt,i));
+ zSep = ",";
+ }
+ printf(";\n");
+ }else{ /* Insert a row */
+ printf("INSERT INTO %s(%s", zId, az2[0]);
+ for(i=1; az2[i]; i++) printf(",%s", az2[i]);
+ printf(") VALUES");
+ zSep = "(";
+ for(i=0; i<nPk2; i++){
+ printf("%s", zSep);
+ zSep = ",";
+ printQuoted(sqlite3_column_value(pStmt,i));
+ }
+ for(i=nPk2+2; i<nQ; i+=2){
+ printf(",");
+ printQuoted(sqlite3_column_value(pStmt,i));
+ }
+ printf(");\n");
+ }
+ }
+ sqlite3_finalize(pStmt);
+ } /* endif !g.bSchemaOnly */
+
+ /* Create indexes that are missing in the source */
+ pStmt = db_prepare(
+ "SELECT sql FROM aux.sqlite_master"
+ " WHERE type='index' AND tbl_name=%Q"
+ " AND sql IS NOT NULL"
+ " AND sql NOT IN (SELECT sql FROM main.sqlite_master"
+ " WHERE type='index' AND tbl_name=%Q"
+ " AND sql IS NOT NULL)",
+ zTab, zTab);
+ while( SQLITE_ROW==sqlite3_step(pStmt) ){
+ printf("%s;\n", sqlite3_column_text(pStmt,0));
+ }
+ sqlite3_finalize(pStmt);
+
+end_diff_one_table:
+ strFree(&sql);
+ sqlite3_free(zId);
+ namelistFree(az);
+ namelistFree(az2);
+ return;
+}
+
+/*
+** Print sketchy documentation for this utility program
+*/
+static void showHelp(void){
+ printf("Usage: %s [options] DB1 DB2\n", g.zArgv0);
+ printf(
+"Output SQL text that would transform DB1 into DB2.\n"
+"Options:\n"
+" --primarykey Use schema-defined PRIMARY KEYs\n"
+" --schema Show only differences in the schema\n"
+" --table TAB Show only differences in table TAB\n"
+ );
+}
+
+int main(int argc, char **argv){
+ const char *zDb1 = 0;
+ const char *zDb2 = 0;
+ int i;
+ int rc;
+ char *zErrMsg = 0;
+ char *zSql;
+ sqlite3_stmt *pStmt;
+ char *zTab = 0;
+
+ g.zArgv0 = argv[0];
+ for(i=1; i<argc; i++){
+ const char *z = argv[i];
+ if( z[0]=='-' ){
+ z++;
+ if( z[0]=='-' ) z++;
+ if( strcmp(z,"debug")==0 ){
+ g.fDebug = strtol(argv[++i], 0, 0);
+ }else
+ if( strcmp(z,"help")==0 ){
+ showHelp();
+ return 0;
+ }else
+ if( strcmp(z,"primarykey")==0 ){
+ g.bSchemaPK = 1;
+ }else
+ if( strcmp(z,"schema")==0 ){
+ g.bSchemaOnly = 1;
+ }else
+ if( strcmp(z,"table")==0 ){
+ zTab = argv[++i];
+ }else
+ {
+ cmdlineError("unknown option: %s", argv[i]);
+ }
+ }else if( zDb1==0 ){
+ zDb1 = argv[i];
+ }else if( zDb2==0 ){
+ zDb2 = argv[i];
+ }else{
+ cmdlineError("unknown argument: %s", argv[i]);
+ }
+ }
+ if( zDb2==0 ){
+ cmdlineError("two database arguments required");
+ }
+ rc = sqlite3_open(zDb1, &g.db);
+ if( rc ){
+ cmdlineError("cannot open database file \"%s\"", zDb1);
+ }
+ rc = sqlite3_exec(g.db, "SELECT * FROM sqlite_master", 0, 0, &zErrMsg);
+ if( rc || zErrMsg ){
+ cmdlineError("\"%s\" does not appear to be a valid SQLite database", zDb1);
+ }
+ zSql = sqlite3_mprintf("ATTACH %Q as aux;", zDb2);
+ rc = sqlite3_exec(g.db, zSql, 0, 0, &zErrMsg);
+ if( rc || zErrMsg ){
+ cmdlineError("cannot attach database \"%s\"", zDb2);
+ }
+ rc = sqlite3_exec(g.db, "SELECT * FROM aux.sqlite_master", 0, 0, &zErrMsg);
+ if( rc || zErrMsg ){
+ cmdlineError("\"%s\" does not appear to be a valid SQLite database", zDb2);
+ }
+
+ if( zTab ){
+ diff_one_table(zTab);
+ }else{
+ /* Handle tables one by one */
+ pStmt = db_prepare(
+ "SELECT name FROM main.sqlite_master\n"
+ " WHERE type='table' AND sql NOT LIKE 'CREATE VIRTUAL%%'\n"
+ " UNION\n"
+ "SELECT name FROM aux.sqlite_master\n"
+ " WHERE type='table' AND sql NOT LIKE 'CREATE VIRTUAL%%'\n"
+ " ORDER BY name"
+ );
+ while( SQLITE_ROW==sqlite3_step(pStmt) ){
+ diff_one_table((const char*)sqlite3_column_text(pStmt, 0));
+ }
+ sqlite3_finalize(pStmt);
+ }
+
+ /* TBD: Handle trigger differences */
+ /* TBD: Handle view differences */
+ sqlite3_close(g.db);
+ return 0;
+}