summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHans-Christoph Steiner <hans@eds.org>2013-08-13 15:43:01 -0400
committerHans-Christoph Steiner <hans@eds.org>2013-08-13 15:43:01 -0400
commit4228998fd796fa2f9e84fb73632e0a07cc7cd188 (patch)
tree15b2336f351468fedd0c39e9de4ad905a686f3b0
parentbdee7cf7d974b2f70d5934786c5666006e7360be (diff)
parent08119c361d1181b3e8f1abb429236e488a664753 (diff)
Merge tag 'upstream/2.2.1'
Upstream version 2.2.1 # gpg: Signature made Tue 13 Aug 2013 03:42:56 PM EDT using RSA key ID 374BBE81 # gpg: Good signature from "Hans-Christoph Steiner <hans@at.or.at>" # gpg: aka "[jpeg image of size 5408]" # gpg: aka "Hans-Christoph Steiner <hs420@nyu.edu>" # gpg: aka "Hans-Christoph Steiner <hans@eds.org>" # gpg: aka "Hans-Christoph Steiner <hans@guardianproject.info>" # gpg: aka "Hans-Christoph Steiner <hansi@nyu.edu>" # gpg: aka "Hans-Christoph Steiner <hans@guardianproject.info>"
-rw-r--r--.gitignore61
-rw-r--r--Makefile.in104
-rw-r--r--Makefile.msc121
-rw-r--r--Makefile.vxworks5
-rw-r--r--README1
-rw-r--r--VERSION2
-rwxr-xr-xconfigure190
-rw-r--r--configure.ac37
-rw-r--r--ext/async/README.txt8
-rw-r--r--ext/async/sqlite3async.c1
-rw-r--r--ext/async/sqlite3async.h4
-rw-r--r--ext/fts1/ft_hash.h2
-rw-r--r--ext/fts1/fts1_hash.h2
-rw-r--r--ext/fts2/fts2.c4
-rw-r--r--ext/fts2/fts2_hash.h2
-rw-r--r--ext/fts2/fts2_icu.c4
-rw-r--r--ext/fts2/fts2_tokenizer.c2
-rw-r--r--ext/fts2/fts2_tokenizer.h2
-rw-r--r--ext/fts3/fts3.c74
-rw-r--r--ext/fts3/fts3Int.h5
-rw-r--r--ext/fts3/fts3_aux.c29
-rw-r--r--ext/fts3/fts3_expr.c335
-rw-r--r--ext/fts3/fts3_hash.h2
-rw-r--r--ext/fts3/fts3_icu.c4
-rw-r--r--ext/fts3/fts3_snippet.c39
-rw-r--r--ext/fts3/fts3_test.c2
-rw-r--r--ext/fts3/fts3_tokenize_vtab.c454
-rw-r--r--ext/fts3/fts3_tokenizer.c10
-rw-r--r--ext/fts3/fts3_tokenizer.h2
-rw-r--r--ext/fts3/fts3_unicode.c2
-rw-r--r--ext/fts3/fts3_write.c113
-rw-r--r--ext/icu/README.txt2
-rw-r--r--ext/misc/amatch.c1483
-rw-r--r--ext/misc/closure.c948
-rw-r--r--ext/misc/fuzzer.c (renamed from src/test_fuzzer.c)64
-rw-r--r--ext/misc/ieee754.c131
-rw-r--r--ext/misc/nextchar.c265
-rw-r--r--ext/misc/regexp.c756
-rw-r--r--ext/misc/rot13.c114
-rw-r--r--ext/misc/spellfix.c (renamed from src/test_spellfix.c)66
-rw-r--r--ext/misc/wholenumber.c (renamed from src/test_wholenumber.c)73
-rw-r--r--ext/rtree/rtree.c16
-rw-r--r--ext/rtree/rtree1.test1
-rw-r--r--ext/rtree/rtree5.test2
-rw-r--r--magic.txt28
-rw-r--r--main.mk38
-rw-r--r--manifest577
-rw-r--r--manifest.uuid2
-rw-r--r--publish.sh138
-rw-r--r--publish_osx.sh35
-rw-r--r--sqlcipher.1 (renamed from sqlite3.1)30
-rw-r--r--sqlcipher.pc.in (renamed from sqlite3.pc.in)0
-rw-r--r--sqlcipher.xcodeproj/project.pbxproj2
-rw-r--r--src/alter.c6
-rw-r--r--src/analyze.c4
-rw-r--r--src/attach.c12
-rw-r--r--src/backup.c57
-rw-r--r--src/bitvec.c2
-rw-r--r--src/btree.c501
-rw-r--r--src/btree.h7
-rw-r--r--src/btreeInt.h1
-rw-r--r--src/build.c129
-rw-r--r--src/callback.c12
-rw-r--r--src/crypto.c222
-rw-r--r--src/crypto.h32
-rw-r--r--src/crypto_cc.c140
-rw-r--r--src/crypto_impl.c441
-rw-r--r--src/crypto_libtomcrypt.c215
-rw-r--r--src/crypto_openssl.c251
-rw-r--r--src/ctime.c31
-rw-r--r--src/delete.c43
-rw-r--r--src/expr.c241
-rw-r--r--src/fkey.c40
-rw-r--r--src/func.c193
-rw-r--r--src/global.c11
-rw-r--r--src/hash.c2
-rw-r--r--src/hash.h2
-rw-r--r--src/insert.c160
-rw-r--r--src/journal.c18
-rw-r--r--src/legacy.c12
-rw-r--r--src/lempar.c1
-rw-r--r--src/loadext.c87
-rw-r--r--src/main.c190
-rw-r--r--src/memjournal.c4
-rw-r--r--src/os.c20
-rw-r--r--src/os.h10
-rw-r--r--src/os_unix.c684
-rw-r--r--src/os_win.c968
-rw-r--r--src/pager.c401
-rw-r--r--src/pager.h21
-rw-r--r--src/parse.y53
-rw-r--r--src/pcache.h2
-rw-r--r--src/pragma.c221
-rw-r--r--src/prepare.c13
-rw-r--r--src/resolve.c212
-rw-r--r--src/select.c508
-rw-r--r--src/shell.c308
-rw-r--r--src/sqlcipher.h75
-rw-r--r--src/sqlite.h.in186
-rw-r--r--src/sqlite3ext.h41
-rw-r--r--src/sqliteInt.h400
-rw-r--r--src/status.c3
-rw-r--r--src/tclsqlite.c29
-rw-r--r--src/test1.c249
-rw-r--r--src/test2.c54
-rw-r--r--src/test3.c40
-rw-r--r--src/test4.c31
-rw-r--r--src/test6.c25
-rw-r--r--src/test7.c31
-rw-r--r--src/test8.c28
-rw-r--r--src/test_async.c8
-rw-r--r--src/test_autoext.c2
-rw-r--r--src/test_backup.c10
-rw-r--r--src/test_config.c14
-rw-r--r--src/test_fs.c333
-rw-r--r--src/test_intarray.c11
-rw-r--r--src/test_intarray.h11
-rw-r--r--src/test_malloc.c68
-rw-r--r--src/test_multiplex.c10
-rw-r--r--src/test_multiplex.h8
-rw-r--r--src/test_mutex.c12
-rw-r--r--src/test_quota.c29
-rw-r--r--src/test_rtree.c8
-rw-r--r--src/test_sqllog.c507
-rw-r--r--src/test_syscall.c43
-rw-r--r--src/test_thread.c16
-rw-r--r--src/test_vfs.c44
-rw-r--r--src/test_vfstrace.c4
-rw-r--r--src/trigger.c9
-rw-r--r--src/update.c6
-rw-r--r--src/utf.c16
-rw-r--r--src/util.c38
-rw-r--r--src/vacuum.c2
-rw-r--r--src/vdbe.c143
-rw-r--r--src/vdbe.h2
-rw-r--r--src/vdbeInt.h49
-rw-r--r--src/vdbeapi.c14
-rw-r--r--src/vdbeaux.c126
-rw-r--r--src/vdbeblob.c2
-rw-r--r--src/vdbemem.c2
-rw-r--r--src/vdbesort.c10
-rw-r--r--src/vdbetrace.c38
-rw-r--r--src/vtab.c12
-rw-r--r--src/wal.c74
-rw-r--r--src/wal.h5
-rw-r--r--src/walker.c17
-rw-r--r--src/where.c1798
-rw-r--r--test/8_3_names.test4
-rw-r--r--test/aggnested.test160
-rw-r--r--test/analyze6.test4
-rw-r--r--test/analyze7.test2
-rw-r--r--test/auth.test31
-rw-r--r--test/auth2.test12
-rw-r--r--test/autoindex1.test124
-rw-r--r--test/autovacuum.test2
-rw-r--r--test/backcompat.test2
-rw-r--r--test/backup4.test104
-rw-r--r--test/backup_ioerr.test4
-rw-r--r--test/bigfile.test1
-rw-r--r--test/bigfile2.test1
-rw-r--r--test/btreefault.test58
-rw-r--r--test/cache.test2
-rw-r--r--test/capi2.test14
-rw-r--r--test/check.test39
-rw-r--r--test/close.test79
-rw-r--r--test/closure01.test226
-rw-r--r--test/collate1.test1
-rw-r--r--test/collate3.test98
-rw-r--r--test/collate4.test27
-rw-r--r--test/collate5.test4
-rw-r--r--test/conflict.test4
-rw-r--r--test/corruptD.test4
-rw-r--r--test/corruptE.test2
-rw-r--r--test/coveridxscan.test93
-rw-r--r--test/crash5.test2
-rw-r--r--test/crash7.test34
-rw-r--r--test/crypto.test52
-rw-r--r--test/dbstatus2.test11
-rw-r--r--test/descidx3.test6
-rw-r--r--test/distinct.test24
-rw-r--r--test/e_createtable.test4
-rw-r--r--test/e_fkey.test57
-rw-r--r--test/e_insert.test25
-rw-r--r--test/e_select.test20
-rw-r--r--test/e_uri.test6
-rw-r--r--test/enc2.test2
-rw-r--r--test/eqp.test16
-rw-r--r--test/errmsg.test2
-rw-r--r--test/exclusive2.test8
-rw-r--r--test/filectrl.test6
-rw-r--r--test/filefmt.test35
-rw-r--r--test/fkey2.test36
-rw-r--r--test/fkey4.test2
-rw-r--r--test/fkey5.test310
-rw-r--r--test/fkey_malloc.test3
-rw-r--r--test/fts3ai.test5
-rw-r--r--test/fts3aux1.test66
-rw-r--r--test/fts3conf.test42
-rw-r--r--test/fts3expr3.test210
-rw-r--r--test/fts3matchinfo.test19
-rw-r--r--test/fts3near.test14
-rw-r--r--test/fts3tok1.test117
-rw-r--r--test/fts3tok_err.test49
-rw-r--r--test/fts4content.test101
-rw-r--r--test/fts4unicode.test17
-rw-r--r--test/full.test20
-rw-r--r--test/func.test29
-rw-r--r--test/fuzzer1.test9
-rw-r--r--test/fuzzerfault.test10
-rw-r--r--test/hook.test1
-rw-r--r--test/in5.test138
-rw-r--r--test/incrblob.test10
-rw-r--r--test/incrvacuum3.test154
-rw-r--r--test/incrvacuum_ioerr.test7
-rw-r--r--test/index5.test8
-rw-r--r--test/instr.test210
-rw-r--r--test/interrupt.test4
-rw-r--r--test/intpkey.test2
-rw-r--r--test/io.test77
-rw-r--r--test/ioerr6.test92
-rw-r--r--test/like.test6
-rw-r--r--test/limit.test147
-rw-r--r--test/loadext.test16
-rw-r--r--test/lock.test23
-rw-r--r--test/malloc.test2
-rw-r--r--test/malloc3.test82
-rw-r--r--test/mallocG.test5
-rw-r--r--test/malloc_common.tcl2
-rw-r--r--test/memdb.test2
-rw-r--r--test/minmax.test92
-rw-r--r--test/minmax2.test2
-rw-r--r--test/misc7.test28
-rw-r--r--test/mmap1.test331
-rw-r--r--test/mmap2.test87
-rw-r--r--test/notify2.test4
-rw-r--r--test/notnull.test27
-rw-r--r--test/numcast.test46
-rw-r--r--test/orderby1.test438
-rw-r--r--test/orderby2.test117
-rw-r--r--test/orderby3.test123
-rw-r--r--test/orderby4.test56
-rw-r--r--test/pager1.test336
-rw-r--r--test/pager2.test19
-rw-r--r--test/pagerfault.test299
-rw-r--r--test/pageropt.test7
-rw-r--r--test/permutations.test17
-rw-r--r--test/pragma.test68
-rw-r--r--test/regexp1.test211
-rw-r--r--test/releasetest.tcl39
-rw-r--r--test/resolver01.test39
-rw-r--r--test/selectA.test20
-rw-r--r--test/selectD.test174
-rw-r--r--test/selectE.test95
-rw-r--r--test/shared9.test230
-rw-r--r--test/sharedA.test188
-rw-r--r--test/shared_err.test5
-rw-r--r--test/shell1.test78
-rw-r--r--test/speed1p.test3
-rw-r--r--test/spellfix.test64
-rw-r--r--test/stat.test15
-rw-r--r--test/subquery.test2
-rw-r--r--test/subquery2.test21
-rw-r--r--test/syscall.test2
-rw-r--r--test/sysfault.test32
-rw-r--r--test/tclsqlite.test4
-rw-r--r--test/temptable.test2
-rw-r--r--test/tester.tcl128
-rw-r--r--test/thread001.test4
-rw-r--r--test/tkt-2d1a5c67d.test2
-rw-r--r--test/tkt-31338dca7e.test2
-rw-r--r--test/tkt-385a5b56b9.test3
-rw-r--r--test/tkt-4dd95f6943.test151
-rw-r--r--test/tkt-5d863f876e.test2
-rw-r--r--test/tkt-6bfb98dfc0.test61
-rw-r--r--test/tkt-78e04e52ea.test2
-rw-r--r--test/tkt-7a31705a7e6.test26
-rw-r--r--test/tkt-80ba201079.test2
-rw-r--r--test/tkt-a7b7803e.test84
-rw-r--r--test/tkt-cbd054fa6b.test4
-rw-r--r--test/tkt-fc7bd6358f.test78
-rw-r--r--test/tkt2409.test4
-rw-r--r--test/tkt2822.test17
-rw-r--r--test/tkt3457.test14
-rw-r--r--test/tkt3527.test8
-rw-r--r--test/tkt3762.test4
-rw-r--r--test/transitive1.test50
-rw-r--r--test/trigger1.test57
-rw-r--r--test/trigger3.test6
-rw-r--r--test/triggerA.test7
-rw-r--r--test/triggerC.test58
-rw-r--r--test/unique.test8
-rw-r--r--test/unordered.test2
-rw-r--r--test/view.test35
-rw-r--r--test/vtab1.test86
-rw-r--r--test/wal.test13
-rw-r--r--test/wal5.test12
-rw-r--r--test/wal8.test1
-rw-r--r--test/wal9.test94
-rw-r--r--test/walfault.test40
-rw-r--r--test/where.test68
-rw-r--r--test/where2.test45
-rw-r--r--test/where8.test32
-rw-r--r--test/where9.test41
-rw-r--r--test/whereD.test2
-rw-r--r--test/whereE.test62
-rw-r--r--test/whereF.test115
-rw-r--r--test/win32lock.test1
-rw-r--r--test/zerodamage.test56
-rwxr-xr-xtool/build-all-msvc.bat320
-rw-r--r--tool/build-shell.sh3
-rw-r--r--tool/mksqlite3c.tcl8
-rw-r--r--tool/mksqlite3h.tcl9
-rw-r--r--tool/mkvsix.tcl368
-rw-r--r--tool/showdb.c111
-rw-r--r--tool/showwal.c293
-rw-r--r--tool/spaceanal.tcl27
-rw-r--r--tool/stack_usage.tcl98
-rw-r--r--tool/vdbe-compress.tcl3
-rw-r--r--tool/win/sqlite.vsixbin32802 -> 32816 bytes
319 files changed, 22480 insertions, 4602 deletions
diff --git a/.gitignore b/.gitignore
index 59bb33f..0af1a93 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,34 +1,39 @@
+*.lo
+*.o
*.mode1v3
*.pbxuser
xcuserdata
xcuserdata/*
*.xcworkspace/*
*.xcworkspace
-.libs
-.target_source
-Makefile
-config.h
-config.log
-config.status
-keywordhash.h
-lemon
-lempar.c
-libsqlite3.la
-libtclsqlite3.la
-libtool
-mkkeywordhash
-opcodes.c
-opcodes.h
-parse.c
-parse.h
-parse.h.temp
-parse.out
-parse.y
-sqlite3
-sqlite3.c
-sqlite3.h
-*.lo
-*.o
-tsrc
-sqlite3.pc
-testfixture
+/Makefile
+/.libs
+/.target_source
+/config.h
+/config.log
+/config.status
+/keywordhash.h
+/mkkeywordhash
+/opcodes.c
+/opcodes.h
+/parse.c
+/parse.h
+/parse.h.temp
+/parse.out
+/tsrc
+/testfixture
+/lemon
+/libtool
+/libsqlite3.la
+/libtclsqlite3.la
+/sqlite3
+/sqlite3.c
+/sqlite3.h
+/lempar.c
+/parse.y
+/shell.c
+/sqlcipher
+/sqlite3.pc
+/sqlcipher.pc
+/sqlite3ext.h
+/libsqlcipher.la
diff --git a/Makefile.in b/Makefile.in
index dcda29b..76fb83e 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -46,7 +46,7 @@ TCC += @TCL_INCLUDE_SPEC@
# The library that programs using TCL must link against.
#
-LIBTCL = @TCL_LIB_SPEC@ @TCL_LIBS@
+LIBTCL = @TCL_LIB_SPEC@
# Compiler options needed for programs that use the readline() library.
#
@@ -135,12 +135,19 @@ LTLINK_EXTRAS += $(GCOV_LDFLAGS$(USE_GCOV))
# BEGIN CRYPTO
CRYPTOLIBOBJ = \
crypto.lo \
- crypto_impl.lo
+ crypto_impl.lo \
+ crypto_openssl.lo \
+ crypto_libtomcrypt.lo \
+ crypto_cc.lo
CRYPTOSRC = \
$(TOP)/src/crypto.h \
+ $(TOP)/src/sqlcipher.h \
$(TOP)/src/crypto.c \
- $(TOP)/src/crypto_impl.c
+ $(TOP)/src/crypto_impl.c \
+ $(TOP)/src/crypto_libtomcrypt.c \
+ $(TOP)/src/crypto_openssl.c \
+ $(TOP)/src/crypto_cc.c
# END CRYPTO
@@ -153,7 +160,7 @@ exec_prefix = @exec_prefix@
libdir = @libdir@
pkgconfigdir = $(libdir)/pkgconfig
bindir = @bindir@
-includedir = @includedir@
+includedir = @includedir@/sqlcipher
INSTALL = @INSTALL@
LIBTOOL = ./libtool
ALLOWRELEASE = @ALLOWRELEASE@
@@ -179,6 +186,7 @@ LIBOBJS0 = alter.lo analyze.lo attach.lo auth.lo \
expr.lo fault.lo fkey.lo \
fts3.lo fts3_aux.lo fts3_expr.lo fts3_hash.lo fts3_icu.lo \
fts3_porter.lo fts3_snippet.lo fts3_tokenizer.lo fts3_tokenizer1.lo \
+ fts3_tokenize_vtab.lo \
fts3_unicode.lo fts3_unicode2.lo fts3_write.lo \
func.lo global.lo hash.lo \
icu.lo insert.lo journal.lo legacy.lo loadext.lo \
@@ -329,6 +337,7 @@ SRC += \
$(TOP)/ext/fts3/fts3_tokenizer.h \
$(TOP)/ext/fts3/fts3_tokenizer.c \
$(TOP)/ext/fts3/fts3_tokenizer1.c \
+ $(TOP)/ext/fts3/fts3_tokenize_vtab.c \
$(TOP)/ext/fts3/fts3_unicode.c \
$(TOP)/ext/fts3/fts3_unicode2.c \
$(TOP)/ext/fts3/fts3_write.c
@@ -370,8 +379,8 @@ TESTSRC = \
$(TOP)/src/test_config.c \
$(TOP)/src/test_demovfs.c \
$(TOP)/src/test_devsym.c \
+ $(TOP)/src/test_fs.c \
$(TOP)/src/test_func.c \
- $(TOP)/src/test_fuzzer.c \
$(TOP)/src/test_hexio.c \
$(TOP)/src/test_init.c \
$(TOP)/src/test_intarray.c \
@@ -392,11 +401,22 @@ TESTSRC = \
$(TOP)/src/test_tclvar.c \
$(TOP)/src/test_thread.c \
$(TOP)/src/test_vfs.c \
- $(TOP)/src/test_wholenumber.c \
$(TOP)/src/test_wsd.c \
$(TOP)/ext/fts3/fts3_term.c \
$(TOP)/ext/fts3/fts3_test.c
+# Statically linked extensions
+#
+TESTSRC += \
+ $(TOP)/ext/misc/amatch.c \
+ $(TOP)/ext/misc/closure.c \
+ $(TOP)/ext/misc/fuzzer.c \
+ $(TOP)/ext/misc/ieee754.c \
+ $(TOP)/ext/misc/nextchar.c \
+ $(TOP)/ext/misc/regexp.c \
+ $(TOP)/ext/misc/spellfix.c \
+ $(TOP)/ext/misc/wholenumber.c
+
# Source code to the library files needed by the test fixture
#
TESTSRC2 = \
@@ -411,6 +431,7 @@ TESTSRC2 = \
$(TOP)/src/func.c \
$(TOP)/src/insert.c \
$(TOP)/src/wal.c \
+ $(TOP)/src/main.c \
$(TOP)/src/mem5.c \
$(TOP)/src/os.c \
$(TOP)/src/os_unix.c \
@@ -489,30 +510,35 @@ EXTHDR += \
# This is the default Makefile target. The objects listed here
# are what get build when you type just "make" with no arguments.
#
-all: sqlite3.h libsqlite3.la sqlite3$(TEXE) $(HAVE_TCL:1=libtclsqlite3.la)
+all: sqlite3.h libsqlcipher.la sqlcipher$(TEXE) $(HAVE_TCL:1=libtclsqlite3.la)
Makefile: $(TOP)/Makefile.in
./config.status
-sqlite3.pc: $(TOP)/sqlite3.pc.in
+sqlcipher.pc: $(TOP)/sqlcipher.pc.in
./config.status
-libsqlite3.la: $(LIBOBJ)
+libsqlcipher.la: $(LIBOBJ)
$(LTLINK) -o $@ $(LIBOBJ) $(TLIBS) \
${ALLOWRELEASE} -rpath "$(libdir)" -version-info "8:6:8"
-libtclsqlite3.la: tclsqlite.lo libsqlite3.la
+libtclsqlite3.la: tclsqlite.lo libsqlcipher.la
$(LTLINK) -o $@ tclsqlite.lo \
- libsqlite3.la @TCL_STUB_LIB_SPEC@ $(TLIBS) \
+ libsqlcipher.la @TCL_STUB_LIB_SPEC@ $(TLIBS) \
-rpath "$(TCLLIBDIR)" \
-version-info "8:6:8" \
-avoid-version
-sqlite3$(TEXE): $(TOP)/src/shell.c libsqlite3.la sqlite3.h
+sqlcipher$(TEXE): $(TOP)/src/shell.c libsqlcipher.la sqlite3.h
$(LTLINK) $(READLINE_FLAGS) \
- -o $@ $(TOP)/src/shell.c libsqlite3.la \
+ -o $@ $(TOP)/src/shell.c libsqlcipher.la \
$(LIBREADLINE) $(TLIBS) -rpath "$(libdir)"
+mptester$(EXE): sqlite3.c $(TOP)/mptest/mptest.c
+ $(LTLINK) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \
+ $(TLIBS) -rpath "$(libdir)"
+
+
# 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
@@ -530,6 +556,7 @@ sqlite3$(TEXE): $(TOP)/src/shell.c libsqlite3.la sqlite3.h
sqlite3.c: .target_source $(TOP)/tool/mksqlite3c.tcl
$(TCLSH_CMD) $(TOP)/tool/mksqlite3c.tcl
+ cp tsrc/shell.c tsrc/sqlite3ext.h .
tclsqlite3.c: sqlite3.c
echo '#ifndef USE_SYSTEM_SQLITE' >tclsqlite3.c
@@ -568,6 +595,12 @@ crypto.lo: $(TOP)/src/crypto.c $(HDR)
$(LTCOMPILE) -c $(TOP)/src/crypto.c
crypto_impl.lo: $(TOP)/src/crypto_impl.c $(HDR)
$(LTCOMPILE) -c $(TOP)/src/crypto_impl.c
+crypto_openssl.lo: $(TOP)/src/crypto_openssl.c $(HDR)
+ $(LTCOMPILE) -c $(TOP)/src/crypto_openssl.c
+crypto_libtomcrypt.lo: $(TOP)/src/crypto_libtomcrypt.c $(HDR)
+ $(LTCOMPILE) -c $(TOP)/src/crypto_libtomcrypt.c
+crypto_cc.lo: $(TOP)/src/crypto_cc.c $(HDR)
+ $(LTCOMPILE) -c $(TOP)/src/crypto_cc.c
# END CRYPTO
# Rules to build individual *.o files from files in the src directory.
@@ -788,9 +821,9 @@ tclsqlite-shell.lo: $(TOP)/src/tclsqlite.c $(HDR)
tclsqlite-stubs.lo: $(TOP)/src/tclsqlite.c $(HDR)
$(LTCOMPILE) -DUSE_TCL_STUBS=1 -o $@ -c $(TOP)/src/tclsqlite.c
-tclsqlite3$(TEXE): tclsqlite-shell.lo libsqlite3.la
+tclsqlcipher$(TEXE): tclsqlite-shell.lo libsqlcipher.la
$(LTLINK) -o $@ tclsqlite-shell.lo \
- libsqlite3.la $(LIBTCL)
+ libsqlcipher.la $(LIBTCL)
# Rules to build opcodes.c and opcodes.h
#
@@ -870,6 +903,9 @@ fts3_tokenizer.lo: $(TOP)/ext/fts3/fts3_tokenizer.c $(HDR) $(EXTHDR)
fts3_tokenizer1.lo: $(TOP)/ext/fts3/fts3_tokenizer1.c $(HDR) $(EXTHDR)
$(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_tokenizer1.c
+fts3_tokenize_vtab.lo: $(TOP)/ext/fts3/fts3_tokenize_vtab.c $(HDR) $(EXTHDR)
+ $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_tokenize_vtab.c
+
fts3_unicode.lo: $(TOP)/ext/fts3/fts3_unicode.c $(HDR) $(EXTHDR)
$(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_unicode.c
@@ -886,7 +922,7 @@ rtree.lo: $(TOP)/ext/rtree/rtree.c $(HDR) $(EXTHDR)
# Rules to build the 'testfixture' application.
#
# If using the amalgamation, use sqlite3.c directly to build the test
-# fixture. Otherwise link against libsqlite3.la. (This distinction is
+# fixture. Otherwise link against libsqlcipher.la. (This distinction is
# necessary because the test fixture requires non-API symbols which are
# hidden when the library is built via the amalgamation).
#
@@ -894,22 +930,26 @@ TESTFIXTURE_FLAGS = -DTCLSH=1 -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1
TESTFIXTURE_FLAGS += -DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" -DSQLITE_CORE
TESTFIXTURE_FLAGS += -DBUILD_sqlite
-TESTFIXTURE_SRC0 = $(TESTSRC2) libsqlite3.la
+TESTFIXTURE_SRC0 = $(TESTSRC2) libsqlcipher.la
TESTFIXTURE_SRC1 = sqlite3.c
-TESTFIXTURE_SRC = $(TESTSRC) $(TOP)/src/tclsqlite.c $(TESTFIXTURE_SRC$(USE_AMALGAMATION))
+TESTFIXTURE_SRC = $(TESTSRC) $(TOP)/src/tclsqlite.c
+TESTFIXTURE_SRC += $(TESTFIXTURE_SRC$(USE_AMALGAMATION))
testfixture$(TEXE): $(TESTFIXTURE_SRC)
$(LTLINK) -DSQLITE_NO_SYNC=1 $(TEMP_STORE) $(TESTFIXTURE_FLAGS) \
-o $@ $(TESTFIXTURE_SRC) $(LIBTCL) $(TLIBS)
-fulltest: testfixture$(TEXE) sqlite3$(TEXE)
+fulltest: testfixture$(TEXE) sqlcipher$(TEXE)
./testfixture$(TEXE) $(TOP)/test/all.test
-soaktest: testfixture$(TEXE) sqlite3$(TEXE)
+soaktest: testfixture$(TEXE) sqlcipher$(TEXE)
./testfixture$(TEXE) $(TOP)/test/all.test -soak=1
-test: testfixture$(TEXE) sqlite3$(TEXE)
+fulltestonly: testfixture$(TEXE) sqlcipher$(TEXE)
+ ./testfixture$(TEXE) $(TOP)/test/full.test
+
+test: testfixture$(TEXE) sqlcipher$(TEXE)
./testfixture$(TEXE) $(TOP)/test/veryquick.test
sqlite3_analyzer.c: sqlite3.c $(TOP)/src/test_stat.c $(TOP)/src/tclsqlite.c $(TOP)/tool/spaceanal.tcl
@@ -925,18 +965,18 @@ sqlite3_analyzer$(TEXE): sqlite3_analyzer.c
# Standard install and cleanup targets
#
-lib_install: libsqlite3.la
+lib_install: libsqlcipher.la
$(INSTALL) -d $(DESTDIR)$(libdir)
- $(LTINSTALL) libsqlite3.la $(DESTDIR)$(libdir)
+ $(LTINSTALL) libsqlcipher.la $(DESTDIR)$(libdir)
-install: sqlite3$(BEXE) lib_install sqlite3.h sqlite3.pc ${HAVE_TCL:1=tcl_install}
+install: sqlcipher$(BEXE) lib_install sqlite3.h sqlcipher.pc ${HAVE_TCL:1=tcl_install}
$(INSTALL) -d $(DESTDIR)$(bindir)
- $(LTINSTALL) sqlite3$(BEXE) $(DESTDIR)$(bindir)
+ $(LTINSTALL) sqlcipher$(BEXE) $(DESTDIR)$(bindir)
$(INSTALL) -d $(DESTDIR)$(includedir)
$(INSTALL) -m 0644 sqlite3.h $(DESTDIR)$(includedir)
$(INSTALL) -m 0644 $(TOP)/src/sqlite3ext.h $(DESTDIR)$(includedir)
$(INSTALL) -d $(DESTDIR)$(pkgconfigdir)
- $(INSTALL) -m 0644 sqlite3.pc $(DESTDIR)$(pkgconfigdir)
+ $(INSTALL) -m 0644 sqlcipher.pc $(DESTDIR)$(pkgconfigdir)
pkgIndex.tcl:
echo 'package ifneeded sqlite3 $(RELEASE) [list load $(TCLLIBDIR)/libtclsqlite3.so sqlite3]' > $@
@@ -947,24 +987,26 @@ tcl_install: lib_install libtclsqlite3.la pkgIndex.tcl
$(INSTALL) -m 0644 pkgIndex.tcl $(DESTDIR)$(TCLLIBDIR)
clean:
- rm -f *.lo *.la *.o sqlite3$(TEXE) libsqlite3.la
+ rm -f *.lo *.la *.o sqlcipher$(TEXE) libsqlcipher.la
rm -f sqlite3.h opcodes.*
rm -rf .libs .deps
rm -f lemon$(BEXE) lempar.c parse.* sqlite*.tar.gz
rm -f mkkeywordhash$(BEXE) keywordhash.h
- rm -f $(PUBLISH)
rm -f *.da *.bb *.bbg gmon.out
rm -rf quota2a quota2b quota2c
rm -rf tsrc .target_source
- rm -f tclsqlite3$(TEXE)
+ rm -f tclsqlcipher$(TEXE)
rm -f testfixture$(TEXE) test.db
rm -f sqlite3.dll sqlite3.lib sqlite3.exp sqlite3.def
rm -f sqlite3.c
+ rm -f sqlite3rc.h
+ rm -f shell.c sqlite3ext.h
rm -f sqlite3_analyzer$(TEXE) sqlite3_analyzer.c
- rm -f sqlite-output.vsix
+ rm -f sqlite-*-output.vsix
+ rm -f mptester mptester.exe
distclean: clean
- rm -f config.log config.status libtool Makefile sqlite3.pc
+ rm -f config.log config.status libtool Makefile sqlcipher.pc
#
# Windows section
diff --git a/Makefile.msc b/Makefile.msc
index 2b79c2b..57d7065 100644
--- a/Makefile.msc
+++ b/Makefile.msc
@@ -9,49 +9,75 @@ TOP = .
# Set this non-0 to create and use the SQLite amalgamation file.
#
+!IFNDEF USE_AMALGAMATION
USE_AMALGAMATION = 1
+!ENDIF
# Set this non-0 to use the International Components for Unicode (ICU).
#
+!IFNDEF USE_ICU
USE_ICU = 0
+!ENDIF
# Set this non-0 to dynamically link to the MSVC runtime library.
#
+!IFNDEF USE_CRT_DLL
USE_CRT_DLL = 0
+!ENDIF
# Set this non-0 to attempt setting the native compiler automatically
# for cross-compiling the command line tools needed during the compilation
# process.
#
+!IFNDEF XCOMPILE
XCOMPILE = 0
+!ENDIF
# Set this non-0 to use the native libraries paths for cross-compiling
# the command line tools needed during the compilation process.
#
+!IFNDEF USE_NATIVE_LIBPATHS
USE_NATIVE_LIBPATHS = 0
+!ENDIF
+
+# Set this 0 to skip the compiling and embedding of version resources.
+#
+!IFNDEF USE_RC
+USE_RC = 1
+!ENDIF
# Set this non-0 to compile binaries suitable for the WinRT environment.
# This setting does not apply to any binaries that require Tcl to operate
# properly (i.e. the text fixture, etc).
#
+!IFNDEF FOR_WINRT
FOR_WINRT = 0
+!ENDIF
# Set this non-0 to skip attempting to look for and/or link with the Tcl
# runtime library.
#
+!IFNDEF NO_TCL
NO_TCL = 0
+!ENDIF
# Set this to non-0 to create and use PDBs.
#
+!IFNDEF SYMBOLS
SYMBOLS = 1
+!ENDIF
# Set this to non-0 to use the SQLite debugging heap subsystem.
#
+!IFNDEF MEMDEBUG
MEMDEBUG = 0
+!ENDIF
# Set this to non-0 to use the Win32 native heap subsystem.
#
+!IFNDEF WIN32HEAP
WIN32HEAP = 0
+!ENDIF
# Set this to one of the following values to enable various debugging
# features. Each level includes the debugging options from the previous
@@ -64,7 +90,9 @@ WIN32HEAP = 0
# 4 == SQLITE_DEBUG_OS_TRACE: Enables output from the OSTRACE() macros.
# 5 == SQLITE_ENABLE_IOTRACE: Enables output from the IOTRACE() macros.
#
+!IFNDEF DEBUG
DEBUG = 0
+!ENDIF
# Check for the predefined command macro CC. This should point to the compiler
# binary for the target platform. If it is not defined, simply define it to
@@ -157,8 +185,8 @@ NLTLIBPATHS = "/LIBPATH:$(NCRTLIBPATH)" "/LIBPATH:$(NSDKLIBPATH)"
# will run on the target platform. (BCC and TCC are usually the
# same unless your are cross-compiling.)
#
-TCC = $(CC) -W3 -DSQLITE_OS_WIN=1 -I. -I$(TOP)\src -fp:precise
-RCC = $(RC) -DSQLITE_OS_WIN=1 -I. -I$(TOP)\src
+TCC = $(CC) -W3 -DSQLITE_OS_WIN=1 -I$(TOP) -I$(TOP)\src -fp:precise
+RCC = $(RC) -DSQLITE_OS_WIN=1 -I$(TOP) -I$(TOP)\src
# When compiling the library for use in the WinRT environment,
# the following compile-time options must be used as well to
@@ -168,8 +196,8 @@ RCC = $(RC) -DSQLITE_OS_WIN=1 -I. -I$(TOP)\src
!IF $(FOR_WINRT)!=0
TCC = $(TCC) -DSQLITE_OS_WINRT=1
RCC = $(RCC) -DSQLITE_OS_WINRT=1
-TCC = $(TCC) -DWINAPI_FAMILY=WINAPI_PARTITION_APP
-RCC = $(RCC) -DWINAPI_FAMILY=WINAPI_PARTITION_APP
+TCC = $(TCC) -DWINAPI_FAMILY=WINAPI_FAMILY_APP
+RCC = $(RCC) -DWINAPI_FAMILY=WINAPI_FAMILY_APP
!ENDIF
# Also, we need to dynamically link to the correct MSVC runtime
@@ -180,14 +208,18 @@ RCC = $(RCC) -DWINAPI_FAMILY=WINAPI_PARTITION_APP
!IF $(FOR_WINRT)!=0 || $(USE_CRT_DLL)!=0
!IF $(DEBUG)>0
TCC = $(TCC) -MDd
+BCC = $(BCC) -MDd
!ELSE
TCC = $(TCC) -MD
+BCC = $(BCC) -MD
!ENDIF
!ELSE
!IF $(DEBUG)>0
TCC = $(TCC) -MTd
+BCC = $(BCC) -MTd
!ELSE
TCC = $(TCC) -MT
+BCC = $(BCC) -MT
!ENDIF
!ENDIF
@@ -446,7 +478,7 @@ LIBOBJS0 = alter.lo analyze.lo attach.lo auth.lo \
expr.lo fault.lo fkey.lo \
fts3.lo fts3_aux.lo fts3_expr.lo fts3_hash.lo fts3_icu.lo \
fts3_porter.lo fts3_snippet.lo fts3_tokenizer.lo fts3_tokenizer1.lo \
- fts3_unicode.lo fts3_unicode2.lo fts3_write.lo \
+ fts3_tokenize_vtab.lo fts3_unicode.lo fts3_unicode2.lo fts3_write.lo \
func.lo global.lo hash.lo \
icu.lo insert.lo journal.lo legacy.lo loadext.lo \
main.lo malloc.lo mem0.lo mem1.lo mem2.lo mem3.lo mem5.lo \
@@ -472,6 +504,14 @@ LIBOBJ = $(LIBOBJS0)
LIBOBJ = $(LIBOBJS1)
!ENDIF
+# Determine if embedded resource compilation and usage are enabled.
+#
+!IF $(USE_RC)!=0
+LIBRESOBJS = sqlite3res.lo
+!ELSE
+LIBRESOBJS =
+!ENDIF
+
# All of the source code files.
#
SRC = \
@@ -598,6 +638,7 @@ SRC = $(SRC) \
$(TOP)\ext\fts3\fts3_tokenizer.h \
$(TOP)\ext\fts3\fts3_tokenizer.c \
$(TOP)\ext\fts3\fts3_tokenizer1.c \
+ $(TOP)\ext\fts3\fts3_tokenize_vtab.c \
$(TOP)\ext\fts3\fts3_unicode.c \
$(TOP)\ext\fts3\fts3_unicode2.c \
$(TOP)\ext\fts3\fts3_write.c
@@ -638,8 +679,8 @@ TESTSRC = \
$(TOP)\src\test_config.c \
$(TOP)\src\test_demovfs.c \
$(TOP)\src\test_devsym.c \
+ $(TOP)\src\test_fs.c \
$(TOP)\src\test_func.c \
- $(TOP)\src\test_fuzzer.c \
$(TOP)\src\test_hexio.c \
$(TOP)\src\test_init.c \
$(TOP)\src\test_intarray.c \
@@ -660,11 +701,23 @@ TESTSRC = \
$(TOP)\src\test_tclvar.c \
$(TOP)\src\test_thread.c \
$(TOP)\src\test_vfs.c \
- $(TOP)\src\test_wholenumber.c \
$(TOP)\src\test_wsd.c \
$(TOP)\ext\fts3\fts3_term.c \
$(TOP)\ext\fts3\fts3_test.c
+# Statically linked extensions
+#
+TESTEXT = \
+ $(TOP)\ext\misc\amatch.c \
+ $(TOP)\ext\misc\closure.c \
+ $(TOP)\ext\misc\fuzzer.c \
+ $(TOP)\ext\misc\ieee754.c \
+ $(TOP)\ext\misc\nextchar.c \
+ $(TOP)\ext\misc\regexp.c \
+ $(TOP)\ext\misc\spellfix.c \
+ $(TOP)\ext\misc\wholenumber.c
+
+
# Source code to the library files needed by the test fixture
#
TESTSRC2 = \
@@ -679,6 +732,7 @@ TESTSRC2 = \
$(TOP)\src\func.c \
$(TOP)\src\insert.c \
$(TOP)\src\wal.c \
+ $(TOP)\src\main.c \
$(TOP)\src\mem5.c \
$(TOP)\src\os.c \
$(TOP)\src\os_unix.c \
@@ -706,6 +760,7 @@ TESTSRC2 = \
$(TOP)\ext\fts3\fts3_aux.c \
$(TOP)\ext\fts3\fts3_expr.c \
$(TOP)\ext\fts3\fts3_tokenizer.c \
+ $(TOP)\ext\fts3\fts3_tokenize_vtab.c \
$(TOP)\ext\fts3\fts3_unicode.c \
$(TOP)\ext\fts3\fts3_unicode2.c \
$(TOP)\ext\fts3\fts3_write.c \
@@ -766,10 +821,14 @@ libsqlite3.lib: $(LIBOBJ)
libtclsqlite3.lib: tclsqlite.lo libsqlite3.lib
$(LTLIB) $(LTLIBOPTS) $(LTLIBPATHS) /OUT:$@ tclsqlite.lo libsqlite3.lib $(LIBTCL:tcl=tclstub) $(TLIBS)
-sqlite3.exe: $(TOP)\src\shell.c libsqlite3.lib sqlite3res.lo sqlite3.h
+sqlite3.exe: $(TOP)\src\shell.c libsqlite3.lib $(LIBRESOBJS) sqlite3.h
$(LTLINK) $(READLINE_FLAGS) \
$(TOP)\src\shell.c \
- /link $(LTLINKOPTS) $(LTLIBPATHS) libsqlite3.lib sqlite3res.lo $(LIBREADLINE) $(LTLIBS) $(TLIBS)
+ /link $(LTLINKOPTS) $(LTLIBPATHS) libsqlite3.lib $(LIBRESOBJS) $(LIBREADLINE) $(LTLIBS) $(TLIBS)
+
+mptester.exe: $(TOP)\mptest\mptest.c libsqlite3.lib $(LIBRESOBJS) sqlite3.h
+ $(LTLINK) $(TOP)\mptest\mptest.c \
+ /link $(LTLINKOPTS) $(LTLIBPATHS) libsqlite3.lib $(LIBRESOBJS) $(LIBREADLINE) $(LTLIBS) $(TLIBS)
# This target creates a directory named "tsrc" and fills it with
# copies of all of the C source code and header files needed to
@@ -788,9 +847,11 @@ sqlite3.exe: $(TOP)\src\shell.c libsqlite3.lib sqlite3res.lo sqlite3.h
sqlite3.c: .target_source $(TOP)\tool\mksqlite3c.tcl
$(TCLSH_CMD) $(TOP)\tool\mksqlite3c.tcl
+ copy tsrc\shell.c .
+ copy tsrc\sqlite3ext.h .
-sqlite3-all.c: sqlite3.c $(TOP)/tool/split-sqlite3c.tcl
- $(TCLSH_CMD) $(TOP)/tool/split-sqlite3c.tcl
+sqlite3-all.c: sqlite3.c $(TOP)\tool\split-sqlite3c.tcl
+ $(TCLSH_CMD) $(TOP)\tool\split-sqlite3c.tcl
# Rule to build the amalgamation
#
@@ -819,14 +880,16 @@ opcodes.lo: opcodes.c
# Rule to build the Win32 resources object file.
#
-sqlite3res.lo: $(TOP)\src\sqlite3.rc $(HDR)
+!IF $(USE_RC)!=0
+$(LIBRESOBJS): $(TOP)\src\sqlite3.rc $(HDR)
echo #ifndef SQLITE_RESOURCE_VERSION > sqlite3rc.h
- for /F %%V in ('type VERSION') do ( \
+ for /F %%V in ('type "$(TOP)\VERSION"') do ( \
echo #define SQLITE_RESOURCE_VERSION %%V \
| $(NAWK) "/.*/ { gsub(/[.]/,\",\");print }" >> sqlite3rc.h \
)
echo #endif >> sqlite3rc.h
- $(LTRCOMPILE) -fo sqlite3res.lo $(TOP)\src\sqlite3.rc
+ $(LTRCOMPILE) -fo $(LIBRESOBJS) $(TOP)\src\sqlite3.rc
+!ENDIF
# Rules to build individual *.lo files from files in the src directory.
#
@@ -1043,8 +1106,8 @@ tclsqlite.lo: $(TOP)\src\tclsqlite.c $(HDR)
tclsqlite-shell.lo: $(TOP)\src\tclsqlite.c $(HDR)
$(LTCOMPILE) -DTCLSH=1 -DBUILD_sqlite -I$(TCLINCDIR) -c $(TOP)\src\tclsqlite.c
-tclsqlite3.exe: tclsqlite-shell.lo libsqlite3.lib sqlite3res.lo
- $(LD) $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) /OUT:$@ libsqlite3.lib tclsqlite-shell.lo sqlite3res.lo $(LTLIBS) $(TLIBS)
+tclsqlite3.exe: tclsqlite-shell.lo libsqlite3.lib $(LIBRESOBJS)
+ $(LD) $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) /OUT:$@ libsqlite3.lib tclsqlite-shell.lo $(LIBRESOBJS) $(LTLIBS) $(TLIBS)
# Rules to build opcodes.c and opcodes.h
#
@@ -1126,6 +1189,9 @@ fts3_tokenizer.lo: $(TOP)\ext\fts3\fts3_tokenizer.c $(HDR) $(EXTHDR)
fts3_tokenizer1.lo: $(TOP)\ext\fts3\fts3_tokenizer1.c $(HDR) $(EXTHDR)
$(LTCOMPILE) -DSQLITE_CORE -c $(TOP)\ext\fts3\fts3_tokenizer1.c
+fts3_tokenize_vtab.lo: $(TOP)\ext\fts3\fts3_tokenize_vtab.c $(HDR) $(EXTHDR)
+ $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)\ext\fts3\fts3_tokenize_vtab.c
+
fts3_unicode.lo: $(TOP)\ext\fts3\fts3_unicode.c $(HDR) $(EXTHDR)
$(LTCOMPILE) -DSQLITE_CORE -c $(TOP)\ext\fts3\fts3_unicode.c
@@ -1149,19 +1215,19 @@ rtree.lo: $(TOP)\ext\rtree\rtree.c $(HDR) $(EXTHDR)
TESTFIXTURE_FLAGS = -DTCLSH=1 -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1
TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" -DSQLITE_CORE
-TESTFIXTURE_SRC0 = $(TESTSRC2) libsqlite3.lib
-TESTFIXTURE_SRC1 = sqlite3.c
+TESTFIXTURE_SRC0 = $(TESTEXT) $(TESTSRC2) libsqlite3.lib
+TESTFIXTURE_SRC1 = $(TESTEXT) sqlite3.c
!IF $(USE_AMALGAMATION)==0
TESTFIXTURE_SRC = $(TESTSRC) $(TOP)\src\tclsqlite.c $(TESTFIXTURE_SRC0)
!ELSE
TESTFIXTURE_SRC = $(TESTSRC) $(TOP)\src\tclsqlite.c $(TESTFIXTURE_SRC1)
!ENDIF
-testfixture.exe: $(TESTFIXTURE_SRC) sqlite3res.lo $(HDR)
+testfixture.exe: $(TESTFIXTURE_SRC) $(LIBRESOBJS) $(HDR)
$(LTLINK) -DSQLITE_NO_SYNC=1 $(TESTFIXTURE_FLAGS) \
-DBUILD_sqlite -I$(TCLINCDIR) \
$(TESTFIXTURE_SRC) \
- /link $(LTLINKOPTS) $(LTLIBPATHS) sqlite3res.lo $(LTLIBS) $(TLIBS)
+ /link $(LTLINKOPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LTLIBS) $(TLIBS)
fulltest: testfixture.exe sqlite3.exe
.\testfixture.exe $(TOP)\test\all.test
@@ -1169,6 +1235,9 @@ fulltest: testfixture.exe sqlite3.exe
soaktest: testfixture.exe sqlite3.exe
.\testfixture.exe $(TOP)\test\all.test -soak=1
+fulltestonly: testfixture.exe sqlite3.exe
+ .\testfixture.exe $(TOP)\test\full.test
+
test: testfixture.exe sqlite3.exe
.\testfixture.exe $(TOP)\test\veryquick.test
@@ -1179,9 +1248,9 @@ sqlite3_analyzer.c: sqlite3.c $(TOP)\src\test_stat.c $(TOP)\src\tclsqlite.c $(TO
$(NAWK) -f $(TOP)\tool\tostr.awk $(TOP)\tool\spaceanal.tcl >> $@
echo ; return zMainloop; } >> $@
-sqlite3_analyzer.exe: sqlite3_analyzer.c sqlite3res.lo
+sqlite3_analyzer.exe: sqlite3_analyzer.c $(LIBRESOBJS)
$(LTLINK) -DBUILD_sqlite -DTCLSH=2 -I$(TCLINCDIR) sqlite3_analyzer.c \
- /link $(LTLINKOPTS) $(LTLIBPATHS) sqlite3res.lo $(LTLIBS) $(TLIBS)
+ /link $(LTLINKOPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LTLIBS) $(TLIBS)
clean:
del /Q *.lo *.ilk *.lib *.obj *.pdb sqlite3.exe libsqlite3.lib
@@ -1201,8 +1270,10 @@ clean:
del /Q sqlite3.dll sqlite3.lib sqlite3.exp sqlite3.def
del /Q sqlite3.c
del /Q sqlite3rc.h
+ del /Q shell.c sqlite3ext.h
del /Q sqlite3_analyzer.exe sqlite3_analyzer.exp sqlite3_analyzer.c
- del /Q sqlite-output.vsix
+ del /Q sqlite-*-output.vsix
+ del /Q mptester.exe
# Dynamic link library section.
#
@@ -1214,5 +1285,5 @@ sqlite3.def: libsqlite3.lib
| $(NAWK) "/ 1 _?sqlite3_/ { sub(/^.* _?/,\"\");print }" \
| sort >> sqlite3.def
-sqlite3.dll: $(LIBOBJ) sqlite3res.lo sqlite3.def
- $(LD) $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) /DLL /DEF:sqlite3.def /OUT:$@ $(LIBOBJ) sqlite3res.lo $(LTLIBS) $(TLIBS)
+sqlite3.dll: $(LIBOBJ) $(LIBRESOBJS) sqlite3.def
+ $(LD) $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) /DLL /DEF:sqlite3.def /OUT:$@ $(LIBOBJ) $(LIBRESOBJS) $(LTLIBS) $(TLIBS)
diff --git a/Makefile.vxworks b/Makefile.vxworks
index 25bc853..c9058da 100644
--- a/Makefile.vxworks
+++ b/Makefile.vxworks
@@ -625,6 +625,9 @@ fulltest: testfixture$(EXE) sqlite3$(EXE)
soaktest: testfixture$(EXE) sqlite3$(EXE)
./testfixture$(EXE) $(TOP)/test/all.test -soak=1
+fulltestonly: testfixture$(EXE) sqlite3$(EXE)
+ ./testfixture$(EXE) $(TOP)/test/full.test
+
test: testfixture$(EXE) sqlite3$(EXE)
./testfixture$(EXE) $(TOP)/test/veryquick.test
@@ -659,4 +662,6 @@ clean:
rm -rf tsrc target_source
rm -f testloadext.dll libtestloadext.so
rm -f sqlite3.c fts?amal.c tclsqlite3.c
+ rm -f sqlite3rc.h
+ rm -f shell.c sqlite3ext.h
rm -f $(SHPREFIX)sqlite3.$(SO)
diff --git a/README b/README
index 1b0a461..0378dad 100644
--- a/README
+++ b/README
@@ -18,6 +18,7 @@ an iPhone data vault and password manager (http://getstrip.com).
- Good security practices (CBC mode, key derivation)
- Zero-configuration and application level cryptography
- Algorithms provided by the peer reviewed OpenSSL crypto library.
+- Configurable crypto providers
[Compiling]
diff --git a/VERSION b/VERSION
index b36faa1..9a4c437 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-3.7.14.1
+3.7.17
diff --git a/configure b/configure
index 7373415..69eb324 100755
--- a/configure
+++ b/configure
@@ -1,6 +1,6 @@
#! /bin/sh
# Guess values for system-dependent variables and create Makefiles.
-# Generated by GNU Autoconf 2.68 for sqlite 3.7.14.1.
+# Generated by GNU Autoconf 2.68 for sqlcipher 3.7.17.
#
#
# Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
@@ -704,10 +704,10 @@ MFLAGS=
MAKEFLAGS=
# Identity of this package.
-PACKAGE_NAME='sqlite'
-PACKAGE_TARNAME='sqlite'
-PACKAGE_VERSION='3.7.14.1'
-PACKAGE_STRING='sqlite 3.7.14.1'
+PACKAGE_NAME='sqlcipher'
+PACKAGE_TARNAME='sqlcipher'
+PACKAGE_VERSION='3.7.17'
+PACKAGE_STRING='sqlcipher 3.7.17'
PACKAGE_BUGREPORT=''
PACKAGE_URL=''
@@ -765,7 +765,6 @@ TCL_LIB_SPEC
TCL_LIB_FLAG
TCL_LIB_FILE
TCL_INCLUDE_SPEC
-TCL_LIBS
TCL_SRC_DIR
TCL_BIN_DIR
TCL_VERSION
@@ -875,6 +874,7 @@ enable_libtool_lock
enable_largefile
with_hints
enable_threadsafe
+with_crypto_lib
enable_cross_thread_connections
enable_releasemode
enable_tempstore
@@ -1440,7 +1440,7 @@ if test "$ac_init_help" = "long"; then
# Omit some internal or obsolete options to make the list less imposing.
# This message is too long to be a string in the A/UX 3.1 sh.
cat <<_ACEOF
-\`configure' configures sqlite 3.7.14.1 to adapt to many kinds of systems.
+\`configure' configures sqlcipher 3.7.17 to adapt to many kinds of systems.
Usage: $0 [OPTION]... [VAR=VALUE]...
@@ -1488,7 +1488,7 @@ Fine tuning of the installation directories:
--infodir=DIR info documentation [DATAROOTDIR/info]
--localedir=DIR locale-dependent data [DATAROOTDIR/locale]
--mandir=DIR man documentation [DATAROOTDIR/man]
- --docdir=DIR documentation root [DATAROOTDIR/doc/sqlite]
+ --docdir=DIR documentation root [DATAROOTDIR/doc/sqlcipher]
--htmldir=DIR html documentation [DOCDIR]
--dvidir=DIR dvi documentation [DOCDIR]
--pdfdir=DIR pdf documentation [DOCDIR]
@@ -1505,7 +1505,7 @@ fi
if test -n "$ac_init_help"; then
case $ac_init_help in
- short | recursive ) echo "Configuration of sqlite 3.7.14.1:";;
+ short | recursive ) echo "Configuration of sqlcipher 3.7.17:";;
esac
cat <<\_ACEOF
@@ -1540,6 +1540,7 @@ Optional Packages:
both]
--with-gnu-ld assume the C compiler uses GNU ld [default=no]
--with-hints=FILE Read configuration options from FILE
+ --with-crypto-lib Specify which crypto library to use
--with-tcl=DIR directory containing tcl configuration
(tclConfig.sh)
--with-readline-lib specify readline library
@@ -1622,7 +1623,7 @@ fi
test -n "$ac_init_help" && exit $ac_status
if $ac_init_version; then
cat <<\_ACEOF
-sqlite configure 3.7.14.1
+sqlcipher configure 3.7.17
generated by GNU Autoconf 2.68
Copyright (C) 2010 Free Software Foundation, Inc.
@@ -2041,7 +2042,7 @@ cat >config.log <<_ACEOF
This file contains any messages produced by compilers while
running configure, to aid debugging if configure makes a mistake.
-It was created by sqlite $as_me 3.7.14.1, which was
+It was created by sqlcipher $as_me 3.7.17, which was
generated by GNU Autoconf 2.68. Invocation command line was
$ $0 $@
@@ -3903,13 +3904,13 @@ if ${lt_cv_nm_interface+:} false; then :
else
lt_cv_nm_interface="BSD nm"
echo "int some_variable = 0;" > conftest.$ac_ext
- (eval echo "\"\$as_me:3906: $ac_compile\"" >&5)
+ (eval echo "\"\$as_me:3907: $ac_compile\"" >&5)
(eval "$ac_compile" 2>conftest.err)
cat conftest.err >&5
- (eval echo "\"\$as_me:3909: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
+ (eval echo "\"\$as_me:3910: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
(eval "$NM \"conftest.$ac_objext\"" 2>conftest.err > conftest.out)
cat conftest.err >&5
- (eval echo "\"\$as_me:3912: output\"" >&5)
+ (eval echo "\"\$as_me:3913: output\"" >&5)
cat conftest.out >&5
if $GREP 'External.*some_variable' conftest.out > /dev/null; then
lt_cv_nm_interface="MS dumpbin"
@@ -5115,7 +5116,7 @@ ia64-*-hpux*)
;;
*-*-irix6*)
# Find out which ABI we are using.
- echo '#line 5118 "configure"' > conftest.$ac_ext
+ echo '#line 5119 "configure"' > conftest.$ac_ext
if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_compile\""; } >&5
(eval $ac_compile) 2>&5
ac_status=$?
@@ -6640,11 +6641,11 @@ else
-e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
-e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
-e 's:$: $lt_compiler_flag:'`
- (eval echo "\"\$as_me:6643: $lt_compile\"" >&5)
+ (eval echo "\"\$as_me:6644: $lt_compile\"" >&5)
(eval "$lt_compile" 2>conftest.err)
ac_status=$?
cat conftest.err >&5
- echo "$as_me:6647: \$? = $ac_status" >&5
+ echo "$as_me:6648: \$? = $ac_status" >&5
if (exit $ac_status) && test -s "$ac_outfile"; then
# The compiler can only warn and ignore the option if not recognized
# So say no if there are warnings other than the usual output.
@@ -6979,11 +6980,11 @@ else
-e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
-e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
-e 's:$: $lt_compiler_flag:'`
- (eval echo "\"\$as_me:6982: $lt_compile\"" >&5)
+ (eval echo "\"\$as_me:6983: $lt_compile\"" >&5)
(eval "$lt_compile" 2>conftest.err)
ac_status=$?
cat conftest.err >&5
- echo "$as_me:6986: \$? = $ac_status" >&5
+ echo "$as_me:6987: \$? = $ac_status" >&5
if (exit $ac_status) && test -s "$ac_outfile"; then
# The compiler can only warn and ignore the option if not recognized
# So say no if there are warnings other than the usual output.
@@ -7084,11 +7085,11 @@ else
-e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
-e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
-e 's:$: $lt_compiler_flag:'`
- (eval echo "\"\$as_me:7087: $lt_compile\"" >&5)
+ (eval echo "\"\$as_me:7088: $lt_compile\"" >&5)
(eval "$lt_compile" 2>out/conftest.err)
ac_status=$?
cat out/conftest.err >&5
- echo "$as_me:7091: \$? = $ac_status" >&5
+ echo "$as_me:7092: \$? = $ac_status" >&5
if (exit $ac_status) && test -s out/conftest2.$ac_objext
then
# The compiler can only warn and ignore the option if not recognized
@@ -7139,11 +7140,11 @@ else
-e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
-e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
-e 's:$: $lt_compiler_flag:'`
- (eval echo "\"\$as_me:7142: $lt_compile\"" >&5)
+ (eval echo "\"\$as_me:7143: $lt_compile\"" >&5)
(eval "$lt_compile" 2>out/conftest.err)
ac_status=$?
cat out/conftest.err >&5
- echo "$as_me:7146: \$? = $ac_status" >&5
+ echo "$as_me:7147: \$? = $ac_status" >&5
if (exit $ac_status) && test -s out/conftest2.$ac_objext
then
# The compiler can only warn and ignore the option if not recognized
@@ -9519,7 +9520,7 @@ else
lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
lt_status=$lt_dlunknown
cat > conftest.$ac_ext <<_LT_EOF
-#line 9522 "configure"
+#line 9523 "configure"
#include "confdefs.h"
#if HAVE_DLFCN_H
@@ -9615,7 +9616,7 @@ else
lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
lt_status=$lt_dlunknown
cat > conftest.$ac_ext <<_LT_EOF
-#line 9618 "configure"
+#line 9619 "configure"
#include "confdefs.h"
#if HAVE_DLFCN_H
@@ -10378,7 +10379,6 @@ if test "x${TCLLIBDIR+set}" != "xset" ; then
TCLLIBDIR="${TCLLIBDIR}/sqlite3"
fi
-
#########
# Set up an appropriate program prefix
#
@@ -10582,6 +10582,135 @@ fi
fi
##########
+# Which crypto library do we use
+#
+
+# Check whether --with-crypto-lib was given.
+if test "${with_crypto_lib+set}" = set; then :
+ withval=$with_crypto_lib; crypto_lib=$withval
+fi
+
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for crypto library to use" >&5
+$as_echo_n "checking for crypto library to use... " >&6; }
+if test "$crypto_lib" = "none"; then
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: none" >&5
+$as_echo "none" >&6; }
+else
+ if test "$crypto_lib" = "commoncrypto"; then
+ CFLAGS+=" -DSQLCIPHER_CRYPTO_CC"
+ BUILD_CFLAGS+=" -DSQLCIPHER_CRYPTO_CC"
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: commoncrypto" >&5
+$as_echo "commoncrypto" >&6; }
+ else
+ if test "$crypto_lib" = "libtomcrypt"; then
+ CFLAGS+=" -DSQLCIPHER_CRYPTO_LIBTOMCRYPT"
+ BUILD_CFLAGS+=" -DSQLCIPHER_CRYPTO_LIBTOMCRYPT"
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: libtomcrypt" >&5
+$as_echo "libtomcrypt" >&6; }
+ { $as_echo "$as_me:${as_lineno-$LINENO}: checking for register_cipher in -ltomcrypt" >&5
+$as_echo_n "checking for register_cipher in -ltomcrypt... " >&6; }
+if ${ac_cv_lib_tomcrypt_register_cipher+:} false; then :
+ $as_echo_n "(cached) " >&6
+else
+ ac_check_lib_save_LIBS=$LIBS
+LIBS="-ltomcrypt $LIBS"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h. */
+
+/* Override any GCC internal prototype to avoid an error.
+ Use char because int might match the return type of a GCC
+ builtin and then its argument prototype would still apply. */
+#ifdef __cplusplus
+extern "C"
+#endif
+char register_cipher ();
+int
+main ()
+{
+return register_cipher ();
+ ;
+ return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+ ac_cv_lib_tomcrypt_register_cipher=yes
+else
+ ac_cv_lib_tomcrypt_register_cipher=no
+fi
+rm -f core conftest.err conftest.$ac_objext \
+ conftest$ac_exeext conftest.$ac_ext
+LIBS=$ac_check_lib_save_LIBS
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_tomcrypt_register_cipher" >&5
+$as_echo "$ac_cv_lib_tomcrypt_register_cipher" >&6; }
+if test "x$ac_cv_lib_tomcrypt_register_cipher" = xyes; then :
+ cat >>confdefs.h <<_ACEOF
+#define HAVE_LIBTOMCRYPT 1
+_ACEOF
+
+ LIBS="-ltomcrypt $LIBS"
+
+else
+ as_fn_error $? "Library crypto not found. Install libtomcrypt!\"" "$LINENO" 5
+fi
+
+ else
+ CFLAGS+=" -DSQLCIPHER_CRYPTO_OPENSSL"
+ BUILD_CFLAGS+=" -DSQLCIPHER_CRYPTO_OPENSSL"
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: openssl" >&5
+$as_echo "openssl" >&6; }
+ { $as_echo "$as_me:${as_lineno-$LINENO}: checking for HMAC_Init_ex in -lcrypto" >&5
+$as_echo_n "checking for HMAC_Init_ex in -lcrypto... " >&6; }
+if ${ac_cv_lib_crypto_HMAC_Init_ex+:} false; then :
+ $as_echo_n "(cached) " >&6
+else
+ ac_check_lib_save_LIBS=$LIBS
+LIBS="-lcrypto $LIBS"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h. */
+
+/* Override any GCC internal prototype to avoid an error.
+ Use char because int might match the return type of a GCC
+ builtin and then its argument prototype would still apply. */
+#ifdef __cplusplus
+extern "C"
+#endif
+char HMAC_Init_ex ();
+int
+main ()
+{
+return HMAC_Init_ex ();
+ ;
+ return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+ ac_cv_lib_crypto_HMAC_Init_ex=yes
+else
+ ac_cv_lib_crypto_HMAC_Init_ex=no
+fi
+rm -f core conftest.err conftest.$ac_objext \
+ conftest$ac_exeext conftest.$ac_ext
+LIBS=$ac_check_lib_save_LIBS
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_crypto_HMAC_Init_ex" >&5
+$as_echo "$ac_cv_lib_crypto_HMAC_Init_ex" >&6; }
+if test "x$ac_cv_lib_crypto_HMAC_Init_ex" = xyes; then :
+ cat >>confdefs.h <<_ACEOF
+#define HAVE_LIBCRYPTO 1
+_ACEOF
+
+ LIBS="-lcrypto $LIBS"
+
+else
+ as_fn_error $? "Library crypto not found. Install openssl!\"" "$LINENO" 5
+fi
+
+ fi
+ fi
+fi
+
+##########
# Do we want to allow a connection created in one thread to be used
# in another thread. This does not work on many Linux systems (ex: RedHat 9)
# due to bugs in the threading implementations. This is thus off by default.
@@ -10898,7 +11027,6 @@ $as_echo "file not found" >&6; }
-
fi
fi
if test "${use_tcl}" = "no" ; then
@@ -11350,7 +11478,7 @@ ac_config_headers="$ac_config_headers config.h"
# Generate the output files.
#
-ac_config_files="$ac_config_files Makefile sqlite3.pc"
+ac_config_files="$ac_config_files Makefile sqlcipher.pc"
cat >confcache <<\_ACEOF
# This file is a shell script that caches the results of configure
@@ -11870,7 +11998,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
# report actual input values of CONFIG_FILES etc. instead of their
# values after options handling.
ac_log="
-This file was extended by sqlite $as_me 3.7.14.1, which was
+This file was extended by sqlcipher $as_me 3.7.17, which was
generated by GNU Autoconf 2.68. Invocation command line was
CONFIG_FILES = $CONFIG_FILES
@@ -11936,7 +12064,7 @@ _ACEOF
cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`"
ac_cs_version="\\
-sqlite config.status 3.7.14.1
+sqlcipher config.status 3.7.17
configured by $0, generated by GNU Autoconf 2.68,
with options \\"\$ac_cs_config\\"
@@ -12321,7 +12449,7 @@ do
"libtool") CONFIG_COMMANDS="$CONFIG_COMMANDS libtool" ;;
"config.h") CONFIG_HEADERS="$CONFIG_HEADERS config.h" ;;
"Makefile") CONFIG_FILES="$CONFIG_FILES Makefile" ;;
- "sqlite3.pc") CONFIG_FILES="$CONFIG_FILES sqlite3.pc" ;;
+ "sqlcipher.pc") CONFIG_FILES="$CONFIG_FILES sqlcipher.pc" ;;
*) as_fn_error $? "invalid argument: \`$ac_config_target'" "$LINENO" 5;;
esac
diff --git a/configure.ac b/configure.ac
index 882c3cc..f226f14 100644
--- a/configure.ac
+++ b/configure.ac
@@ -87,7 +87,7 @@
# you don't need (for example BLT) by erasing or commenting out
# the corresponding code.
#
-AC_INIT(sqlite, m4_esyscmd([cat VERSION | tr -d '\n']))
+AC_INIT(sqlcipher, m4_esyscmd([cat VERSION | tr -d '\n']))
dnl Make sure the local VERSION file matches this configure script
sqlite_version_sanity_check=`cat $srcdir/VERSION | tr -d '\n'`
@@ -159,7 +159,6 @@ if test "x${TCLLIBDIR+set}" != "xset" ; then
TCLLIBDIR="${TCLLIBDIR}/sqlite3"
fi
-
#########
# Set up an appropriate program prefix
#
@@ -252,6 +251,37 @@ if test "$SQLITE_THREADSAFE" = "1"; then
fi
##########
+# Which crypto library do we use
+#
+AC_ARG_WITH([crypto-lib],
+AC_HELP_STRING([--with-crypto-lib],[Specify which crypto library to use]),
+crypto_lib=$withval)
+AC_MSG_CHECKING([for crypto library to use])
+if test "$crypto_lib" = "none"; then
+ AC_MSG_RESULT([none])
+else
+ if test "$crypto_lib" = "commoncrypto"; then
+ CFLAGS+=" -DSQLCIPHER_CRYPTO_CC"
+ BUILD_CFLAGS+=" -DSQLCIPHER_CRYPTO_CC"
+ AC_MSG_RESULT([commoncrypto])
+ else
+ if test "$crypto_lib" = "libtomcrypt"; then
+ CFLAGS+=" -DSQLCIPHER_CRYPTO_LIBTOMCRYPT"
+ BUILD_CFLAGS+=" -DSQLCIPHER_CRYPTO_LIBTOMCRYPT"
+ AC_MSG_RESULT([libtomcrypt])
+ AC_CHECK_LIB([tomcrypt], [register_cipher], ,
+ AC_MSG_ERROR([Library crypto not found. Install libtomcrypt!"]))
+ else
+ CFLAGS+=" -DSQLCIPHER_CRYPTO_OPENSSL"
+ BUILD_CFLAGS+=" -DSQLCIPHER_CRYPTO_OPENSSL"
+ AC_MSG_RESULT([openssl])
+ AC_CHECK_LIB([crypto], [HMAC_Init_ex], ,
+ AC_MSG_ERROR([Library crypto not found. Install openssl!"]))
+ fi
+ fi
+fi
+
+##########
# Do we want to allow a connection created in one thread to be used
# in another thread. This does not work on many Linux systems (ex: RedHat 9)
# due to bugs in the threading implementations. This is thus off by default.
@@ -501,7 +531,6 @@ if test "${use_tcl}" = "yes" ; then
AC_SUBST(TCL_VERSION)
AC_SUBST(TCL_BIN_DIR)
AC_SUBST(TCL_SRC_DIR)
- AC_SUBST(TCL_LIBS)
AC_SUBST(TCL_INCLUDE_SPEC)
AC_SUBST(TCL_LIB_FILE)
@@ -700,5 +729,5 @@ AC_CONFIG_HEADERS(config.h)
AC_SUBST(BUILD_CFLAGS)
AC_OUTPUT([
Makefile
-sqlite3.pc
+sqlcipher.pc
])
diff --git a/ext/async/README.txt b/ext/async/README.txt
index 05acffe..f62fa2f 100644
--- a/ext/async/README.txt
+++ b/ext/async/README.txt
@@ -1,3 +1,10 @@
+NOTE (2012-11-29):
+
+The functionality implemented by this extension has been superseded
+by WAL-mode. This module is no longer supported or maintained. The
+code is retained for historical reference only.
+
+------------------------------------------------------------------------------
Normally, when SQLite writes to a database file, it waits until the write
operation is finished before returning control to the calling application.
@@ -161,4 +168,3 @@ the database, eliminating the bottleneck.
The functionality required of each of the above functions is described
in comments in sqlite3async.c.
-
diff --git a/ext/async/sqlite3async.c b/ext/async/sqlite3async.c
index 0814da7..4ab39ca 100644
--- a/ext/async/sqlite3async.c
+++ b/ext/async/sqlite3async.c
@@ -1510,6 +1510,7 @@ static void asyncWriterThread(void){
case ASYNC_DELETE:
ASYNC_TRACE(("DELETE %s\n", p->zBuf));
rc = pVfs->xDelete(pVfs, p->zBuf, (int)p->iOffset);
+ if( rc==SQLITE_IOERR_DELETE_NOENT ) rc = SQLITE_OK;
break;
case ASYNC_OPENEXCLUSIVE: {
diff --git a/ext/async/sqlite3async.h b/ext/async/sqlite3async.h
index 143cdc7..5b20d71 100644
--- a/ext/async/sqlite3async.h
+++ b/ext/async/sqlite3async.h
@@ -75,7 +75,7 @@ int sqlite3async_initialize(const char *zParent, int isDefault);
** On win32 platforms, this function also releases the small number of
** critical section and event objects created by sqlite3async_initialize().
*/
-void sqlite3async_shutdown();
+void sqlite3async_shutdown(void);
/*
** This function may only be called when the asynchronous IO VFS is
@@ -94,7 +94,7 @@ void sqlite3async_shutdown();
** If multiple simultaneous calls are made to sqlite3async_run() from two
** or more threads, then the calls are serialized internally.
*/
-void sqlite3async_run();
+void sqlite3async_run(void);
/*
** This function may only be called when the asynchronous IO VFS is
diff --git a/ext/fts1/ft_hash.h b/ext/fts1/ft_hash.h
index 93b6dcf..95871a4 100644
--- a/ext/fts1/ft_hash.h
+++ b/ext/fts1/ft_hash.h
@@ -9,7 +9,7 @@
** May you share freely, never taking more than you give.
**
*************************************************************************
-** This is the header file for the generic hash-table implemenation
+** This is the header file for the generic hash-table implementation
** used in SQLite. We've modified it slightly to serve as a standalone
** hash table implementation for the full-text indexing module.
**
diff --git a/ext/fts1/fts1_hash.h b/ext/fts1/fts1_hash.h
index c31c430..9001152 100644
--- a/ext/fts1/fts1_hash.h
+++ b/ext/fts1/fts1_hash.h
@@ -9,7 +9,7 @@
** May you share freely, never taking more than you give.
**
*************************************************************************
-** This is the header file for the generic hash-table implemenation
+** This is the header file for the generic hash-table implementation
** used in SQLite. We've modified it slightly to serve as a standalone
** hash table implementation for the full-text indexing module.
**
diff --git a/ext/fts2/fts2.c b/ext/fts2/fts2.c
index 93e03cd..f008ce6 100644
--- a/ext/fts2/fts2.c
+++ b/ext/fts2/fts2.c
@@ -6779,7 +6779,7 @@ void sqlite3Fts2IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
int sqlite3Fts2InitHashTable(sqlite3 *, fts2Hash *, const char *);
/*
-** Initialise the fts2 extension. If this extension is built as part
+** Initialize the fts2 extension. If this extension is built as part
** of the sqlite library, then this function is called directly by
** SQLite. If fts2 is built as a dynamically loadable extension, this
** function is called by the sqlite3_extension_init() entry point.
@@ -6797,7 +6797,7 @@ int sqlite3Fts2Init(sqlite3 *db){
sqlite3Fts2IcuTokenizerModule(&pIcu);
#endif
- /* Allocate and initialise the hash-table used to store tokenizers. */
+ /* Allocate and initialize the hash-table used to store tokenizers. */
pHash = sqlite3_malloc(sizeof(fts2Hash));
if( !pHash ){
rc = SQLITE_NOMEM;
diff --git a/ext/fts2/fts2_hash.h b/ext/fts2/fts2_hash.h
index 571aa2c..02936f1 100644
--- a/ext/fts2/fts2_hash.h
+++ b/ext/fts2/fts2_hash.h
@@ -9,7 +9,7 @@
** May you share freely, never taking more than you give.
**
*************************************************************************
-** This is the header file for the generic hash-table implemenation
+** This is the header file for the generic hash-table implementation
** used in SQLite. We've modified it slightly to serve as a standalone
** hash table implementation for the full-text indexing module.
**
diff --git a/ext/fts2/fts2_icu.c b/ext/fts2/fts2_icu.c
index de8e116..2670301 100644
--- a/ext/fts2/fts2_icu.c
+++ b/ext/fts2/fts2_icu.c
@@ -118,7 +118,7 @@ static int icuOpen(
nChar = nInput+1;
pCsr = (IcuCursor *)sqlite3_malloc(
sizeof(IcuCursor) + /* IcuCursor */
- nChar * sizeof(UChar) + /* IcuCursor.aChar[] */
+ ((nChar+3)&~3) * sizeof(UChar) + /* IcuCursor.aChar[] */
(nChar+1) * sizeof(int) /* IcuCursor.aOffset[] */
);
if( !pCsr ){
@@ -126,7 +126,7 @@ static int icuOpen(
}
memset(pCsr, 0, sizeof(IcuCursor));
pCsr->aChar = (UChar *)&pCsr[1];
- pCsr->aOffset = (int *)&pCsr->aChar[nChar];
+ pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3];
pCsr->aOffset[iOut] = iInput;
U8_NEXT(zInput, iInput, nInput, c);
diff --git a/ext/fts2/fts2_tokenizer.c b/ext/fts2/fts2_tokenizer.c
index f8b0663..a93790c 100644
--- a/ext/fts2/fts2_tokenizer.c
+++ b/ext/fts2/fts2_tokenizer.c
@@ -319,7 +319,7 @@ static void intTestFunc(
/*
** Set up SQL objects in database db used to access the contents of
** the hash table pointed to by argument pHash. The hash table must
-** been initialised to use string keys, and to take a private copy
+** been initialized to use string keys, and to take a private copy
** of the key when a value is inserted. i.e. by a call similar to:
**
** sqlite3Fts2HashInit(pHash, FTS2_HASH_STRING, 1);
diff --git a/ext/fts2/fts2_tokenizer.h b/ext/fts2/fts2_tokenizer.h
index 8c256b2..8db2048 100644
--- a/ext/fts2/fts2_tokenizer.h
+++ b/ext/fts2/fts2_tokenizer.h
@@ -70,7 +70,7 @@ struct sqlite3_tokenizer_module {
** This method should return either SQLITE_OK (0), or an SQLite error
** code. If SQLITE_OK is returned, then *ppTokenizer should be set
** to point at the newly created tokenizer structure. The generic
- ** sqlite3_tokenizer.pModule variable should not be initialised by
+ ** sqlite3_tokenizer.pModule variable should not be initialized by
** this callback. The caller will do so.
*/
int (*xCreate)(
diff --git a/ext/fts3/fts3.c b/ext/fts3/fts3.c
index 58414f6..c00a13f 100644
--- a/ext/fts3/fts3.c
+++ b/ext/fts3/fts3.c
@@ -1571,7 +1571,7 @@ static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
}else{
rc = sqlite3_reset(pCsr->pStmt);
if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
- /* If no row was found and no error has occured, then the %_content
+ /* If no row was found and no error has occurred, then the %_content
** table is missing a row that is present in the full-text index.
** The data structures are corrupt. */
rc = FTS_CORRUPT_VTAB;
@@ -2811,7 +2811,7 @@ static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
}
/*
-** This function retreives the doclist for the specified term (or term
+** This function retrieves the doclist for the specified term (or term
** prefix) from the database.
*/
static int fts3TermSelect(
@@ -2975,14 +2975,12 @@ static int fts3FilterMethod(
pCsr->iLangid = 0;
if( nVal==2 ) pCsr->iLangid = sqlite3_value_int(apVal[1]);
+ assert( p->base.zErrMsg==0 );
rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
- p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr
+ p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr,
+ &p->base.zErrMsg
);
if( rc!=SQLITE_OK ){
- if( rc==SQLITE_ERROR ){
- static const char *zErr = "malformed MATCH expression: [%s]";
- p->base.zErrMsg = sqlite3_mprintf(zErr, zQuery);
- }
return rc;
}
@@ -3562,7 +3560,7 @@ void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
#endif
/*
-** Initialise the fts3 extension. If this extension is built as part
+** Initialize the fts3 extension. If this extension is built as part
** of the sqlite library, then this function is called directly by
** SQLite. If fts3 is built as a dynamically loadable extension, this
** function is called by the sqlite3_extension_init() entry point.
@@ -3596,7 +3594,7 @@ int sqlite3Fts3Init(sqlite3 *db){
sqlite3Fts3SimpleTokenizerModule(&pSimple);
sqlite3Fts3PorterTokenizerModule(&pPorter);
- /* Allocate and initialise the hash-table used to store tokenizers. */
+ /* Allocate and initialize the hash-table used to store tokenizers. */
pHash = sqlite3_malloc(sizeof(Fts3Hash));
if( !pHash ){
rc = SQLITE_NOMEM;
@@ -3646,9 +3644,13 @@ int sqlite3Fts3Init(sqlite3 *db){
db, "fts4", &fts3Module, (void *)pHash, 0
);
}
+ if( rc==SQLITE_OK ){
+ rc = sqlite3Fts3InitTok(db, (void *)pHash);
+ }
return rc;
}
+
/* An error has occurred. Delete the hash table and return the error code. */
assert( rc!=SQLITE_OK );
if( pHash ){
@@ -4743,35 +4745,39 @@ static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
nTmp += p->pRight->pPhrase->doclist.nList;
}
nTmp += p->pPhrase->doclist.nList;
- aTmp = sqlite3_malloc(nTmp*2);
- if( !aTmp ){
- *pRc = SQLITE_NOMEM;
+ if( nTmp==0 ){
res = 0;
}else{
- char *aPoslist = p->pPhrase->doclist.pList;
- int nToken = p->pPhrase->nToken;
+ aTmp = sqlite3_malloc(nTmp*2);
+ if( !aTmp ){
+ *pRc = SQLITE_NOMEM;
+ res = 0;
+ }else{
+ char *aPoslist = p->pPhrase->doclist.pList;
+ int nToken = p->pPhrase->nToken;
- for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
- Fts3Phrase *pPhrase = p->pRight->pPhrase;
- int nNear = p->nNear;
- res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
- }
-
- aPoslist = pExpr->pRight->pPhrase->doclist.pList;
- nToken = pExpr->pRight->pPhrase->nToken;
- for(p=pExpr->pLeft; p && res; p=p->pLeft){
- int nNear;
- Fts3Phrase *pPhrase;
- assert( p->pParent && p->pParent->pLeft==p );
- nNear = p->pParent->nNear;
- pPhrase = (
- p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
- );
- res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
+ for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
+ Fts3Phrase *pPhrase = p->pRight->pPhrase;
+ int nNear = p->nNear;
+ res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
+ }
+
+ aPoslist = pExpr->pRight->pPhrase->doclist.pList;
+ nToken = pExpr->pRight->pPhrase->nToken;
+ for(p=pExpr->pLeft; p && res; p=p->pLeft){
+ int nNear;
+ Fts3Phrase *pPhrase;
+ assert( p->pParent && p->pParent->pLeft==p );
+ nNear = p->pParent->nNear;
+ pPhrase = (
+ p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
+ );
+ res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
+ }
}
- }
- sqlite3_free(aTmp);
+ sqlite3_free(aTmp);
+ }
}
return res;
@@ -5191,7 +5197,7 @@ int sqlite3Fts3EvalPhraseStats(
** of the current row.
**
** More specifically, the returned buffer contains 1 varint for each
-** occurence of the phrase in the column, stored using the normal (delta+2)
+** occurrence of the phrase in the column, stored using the normal (delta+2)
** compression and is terminated by either an 0x01 or 0x00 byte. For example,
** if the requested column contains "a b X c d X X" and the position-list
** for 'X' is requested, the buffer returned may contain:
diff --git a/ext/fts3/fts3Int.h b/ext/fts3/fts3Int.h
index 77ca470..b19064c 100644
--- a/ext/fts3/fts3Int.h
+++ b/ext/fts3/fts3Int.h
@@ -524,7 +524,7 @@ void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
/* fts3_expr.c */
int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
- char **, int, int, int, const char *, int, Fts3Expr **
+ char **, int, int, int, const char *, int, Fts3Expr **, char **
);
void sqlite3Fts3ExprFree(Fts3Expr *);
#ifdef SQLITE_TEST
@@ -549,6 +549,9 @@ int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **);
int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
+/* fts3_tokenize_vtab.c */
+int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *);
+
/* fts3_unicode2.c (functions generated by parsing unicode text files) */
#ifdef SQLITE_ENABLE_FTS4_UNICODE61
int sqlite3FtsUnicodeFold(int, int);
diff --git a/ext/fts3/fts3_aux.c b/ext/fts3/fts3_aux.c
index a2bff2e..9b582fc 100644
--- a/ext/fts3/fts3_aux.c
+++ b/ext/fts3/fts3_aux.c
@@ -70,17 +70,26 @@ static int fts3auxConnectMethod(
UNUSED_PARAMETER(pUnused);
- /* The user should specify a single argument - the name of an fts3 table. */
- if( argc!=4 ){
- *pzErr = sqlite3_mprintf(
- "wrong number of arguments to fts4aux constructor"
- );
- return SQLITE_ERROR;
- }
+ /* The user should invoke this in one of two forms:
+ **
+ ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table);
+ ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table);
+ */
+ if( argc!=4 && argc!=5 ) goto bad_args;
zDb = argv[1];
nDb = (int)strlen(zDb);
- zFts3 = argv[3];
+ if( argc==5 ){
+ if( nDb==4 && 0==sqlite3_strnicmp("temp", zDb, 4) ){
+ zDb = argv[3];
+ nDb = (int)strlen(zDb);
+ zFts3 = argv[4];
+ }else{
+ goto bad_args;
+ }
+ }else{
+ zFts3 = argv[3];
+ }
nFts3 = (int)strlen(zFts3);
rc = sqlite3_declare_vtab(db, FTS3_TERMS_SCHEMA);
@@ -103,6 +112,10 @@ static int fts3auxConnectMethod(
*ppVtab = (sqlite3_vtab *)p;
return SQLITE_OK;
+
+ bad_args:
+ *pzErr = sqlite3_mprintf("invalid arguments to fts4aux constructor");
+ return SQLITE_ERROR;
}
/*
diff --git a/ext/fts3/fts3_expr.c b/ext/fts3/fts3_expr.c
index a6e3492..c046d7d 100644
--- a/ext/fts3/fts3_expr.c
+++ b/ext/fts3/fts3_expr.c
@@ -106,7 +106,7 @@ struct ParseContext {
** This function is equivalent to the standard isspace() function.
**
** The standard isspace() can be awkward to use safely, because although it
-** is defined to accept an argument of type int, its behaviour when passed
+** is defined to accept an argument of type int, its behavior when passed
** an integer that falls outside of the range of the unsigned char type
** is undefined (and sometimes, "undefined" means segfault). This wrapper
** is defined to accept an argument of type char, and always returns 0 for
@@ -185,7 +185,7 @@ static int getNextToken(
rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, n, &pCursor);
if( rc==SQLITE_OK ){
const char *zToken;
- int nToken, iStart, iEnd, iPosition;
+ int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
int nByte; /* total space to allocate */
rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
@@ -300,7 +300,7 @@ static int getNextString(
int ii;
for(ii=0; rc==SQLITE_OK; ii++){
const char *zByte;
- int nByte, iBegin, iEnd, iPos;
+ int nByte = 0, iBegin = 0, iEnd = 0, iPos = 0;
rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
if( rc==SQLITE_OK ){
Fts3PhraseToken *pToken;
@@ -640,8 +640,10 @@ static int fts3ExprParse(
}
pNot->eType = FTSQUERY_NOT;
pNot->pRight = p;
+ p->pParent = pNot;
if( pNotBranch ){
pNot->pLeft = pNotBranch;
+ pNotBranch->pParent = pNot;
}
pNotBranch = pNot;
p = pPrev;
@@ -729,6 +731,7 @@ static int fts3ExprParse(
pIter = pIter->pLeft;
}
pIter->pLeft = pRet;
+ pRet->pParent = pIter;
pRet = pNotBranch;
}
}
@@ -746,30 +749,184 @@ exprparse_out:
}
/*
-** Parameters z and n contain a pointer to and length of a buffer containing
-** an fts3 query expression, respectively. This function attempts to parse the
-** query expression and create a tree of Fts3Expr structures representing the
-** parsed expression. If successful, *ppExpr is set to point to the head
-** of the parsed expression tree and SQLITE_OK is returned. If an error
-** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
-** error) is returned and *ppExpr is set to 0.
+** Return SQLITE_ERROR if the maximum depth of the expression tree passed
+** as the only argument is more than nMaxDepth.
+*/
+static int fts3ExprCheckDepth(Fts3Expr *p, int nMaxDepth){
+ int rc = SQLITE_OK;
+ if( p ){
+ if( nMaxDepth<0 ){
+ rc = SQLITE_TOOBIG;
+ }else{
+ rc = fts3ExprCheckDepth(p->pLeft, nMaxDepth-1);
+ if( rc==SQLITE_OK ){
+ rc = fts3ExprCheckDepth(p->pRight, nMaxDepth-1);
+ }
+ }
+ }
+ return rc;
+}
+
+/*
+** This function attempts to transform the expression tree at (*pp) to
+** an equivalent but more balanced form. The tree is modified in place.
+** If successful, SQLITE_OK is returned and (*pp) set to point to the
+** new root expression node.
**
-** If parameter n is a negative number, then z is assumed to point to a
-** nul-terminated string and the length is determined using strlen().
+** nMaxDepth is the maximum allowable depth of the balanced sub-tree.
**
-** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
-** use to normalize query tokens while parsing the expression. The azCol[]
-** array, which is assumed to contain nCol entries, should contain the names
-** of each column in the target fts3 table, in order from left to right.
-** Column names must be nul-terminated strings.
+** Otherwise, if an error occurs, an SQLite error code is returned and
+** expression (*pp) freed.
+*/
+static int fts3ExprBalance(Fts3Expr **pp, int nMaxDepth){
+ int rc = SQLITE_OK; /* Return code */
+ Fts3Expr *pRoot = *pp; /* Initial root node */
+ Fts3Expr *pFree = 0; /* List of free nodes. Linked by pParent. */
+ int eType = pRoot->eType; /* Type of node in this tree */
+
+ if( nMaxDepth==0 ){
+ rc = SQLITE_ERROR;
+ }
+
+ if( rc==SQLITE_OK && (eType==FTSQUERY_AND || eType==FTSQUERY_OR) ){
+ Fts3Expr **apLeaf;
+ apLeaf = (Fts3Expr **)sqlite3_malloc(sizeof(Fts3Expr *) * nMaxDepth);
+ if( 0==apLeaf ){
+ rc = SQLITE_NOMEM;
+ }else{
+ memset(apLeaf, 0, sizeof(Fts3Expr *) * nMaxDepth);
+ }
+
+ if( rc==SQLITE_OK ){
+ int i;
+ Fts3Expr *p;
+
+ /* Set $p to point to the left-most leaf in the tree of eType nodes. */
+ for(p=pRoot; p->eType==eType; p=p->pLeft){
+ assert( p->pParent==0 || p->pParent->pLeft==p );
+ assert( p->pLeft && p->pRight );
+ }
+
+ /* This loop runs once for each leaf in the tree of eType nodes. */
+ while( 1 ){
+ int iLvl;
+ Fts3Expr *pParent = p->pParent; /* Current parent of p */
+
+ assert( pParent==0 || pParent->pLeft==p );
+ p->pParent = 0;
+ if( pParent ){
+ pParent->pLeft = 0;
+ }else{
+ pRoot = 0;
+ }
+ rc = fts3ExprBalance(&p, nMaxDepth-1);
+ if( rc!=SQLITE_OK ) break;
+
+ for(iLvl=0; p && iLvl<nMaxDepth; iLvl++){
+ if( apLeaf[iLvl]==0 ){
+ apLeaf[iLvl] = p;
+ p = 0;
+ }else{
+ assert( pFree );
+ pFree->pLeft = apLeaf[iLvl];
+ pFree->pRight = p;
+ pFree->pLeft->pParent = pFree;
+ pFree->pRight->pParent = pFree;
+
+ p = pFree;
+ pFree = pFree->pParent;
+ p->pParent = 0;
+ apLeaf[iLvl] = 0;
+ }
+ }
+ if( p ){
+ sqlite3Fts3ExprFree(p);
+ rc = SQLITE_TOOBIG;
+ break;
+ }
+
+ /* If that was the last leaf node, break out of the loop */
+ if( pParent==0 ) break;
+
+ /* Set $p to point to the next leaf in the tree of eType nodes */
+ for(p=pParent->pRight; p->eType==eType; p=p->pLeft);
+
+ /* Remove pParent from the original tree. */
+ assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent );
+ pParent->pRight->pParent = pParent->pParent;
+ if( pParent->pParent ){
+ pParent->pParent->pLeft = pParent->pRight;
+ }else{
+ assert( pParent==pRoot );
+ pRoot = pParent->pRight;
+ }
+
+ /* Link pParent into the free node list. It will be used as an
+ ** internal node of the new tree. */
+ pParent->pParent = pFree;
+ pFree = pParent;
+ }
+
+ if( rc==SQLITE_OK ){
+ p = 0;
+ for(i=0; i<nMaxDepth; i++){
+ if( apLeaf[i] ){
+ if( p==0 ){
+ p = apLeaf[i];
+ p->pParent = 0;
+ }else{
+ assert( pFree!=0 );
+ pFree->pRight = p;
+ pFree->pLeft = apLeaf[i];
+ pFree->pLeft->pParent = pFree;
+ pFree->pRight->pParent = pFree;
+
+ p = pFree;
+ pFree = pFree->pParent;
+ p->pParent = 0;
+ }
+ }
+ }
+ pRoot = p;
+ }else{
+ /* An error occurred. Delete the contents of the apLeaf[] array
+ ** and pFree list. Everything else is cleaned up by the call to
+ ** sqlite3Fts3ExprFree(pRoot) below. */
+ Fts3Expr *pDel;
+ for(i=0; i<nMaxDepth; i++){
+ sqlite3Fts3ExprFree(apLeaf[i]);
+ }
+ while( (pDel=pFree)!=0 ){
+ pFree = pDel->pParent;
+ sqlite3_free(pDel);
+ }
+ }
+
+ assert( pFree==0 );
+ sqlite3_free( apLeaf );
+ }
+ }
+
+ if( rc!=SQLITE_OK ){
+ sqlite3Fts3ExprFree(pRoot);
+ pRoot = 0;
+ }
+ *pp = pRoot;
+ return rc;
+}
+
+/*
+** This function is similar to sqlite3Fts3ExprParse(), with the following
+** differences:
**
-** The iDefaultCol parameter should be passed the index of the table column
-** that appears on the left-hand-side of the MATCH operator (the default
-** column to match against for tokens for which a column name is not explicitly
-** specified as part of the query string), or -1 if tokens may by default
-** match any table column.
+** 1. It does not do expression rebalancing.
+** 2. It does not check that the expression does not exceed the
+** maximum allowable depth.
+** 3. Even if it fails, *ppExpr may still be set to point to an
+** expression tree. It should be deleted using sqlite3Fts3ExprFree()
+** in this case.
*/
-int sqlite3Fts3ExprParse(
+static int fts3ExprParseUnbalanced(
sqlite3_tokenizer *pTokenizer, /* Tokenizer module */
int iLangid, /* Language id for tokenizer */
char **azCol, /* Array of column names for fts3 table */
@@ -798,28 +955,116 @@ int sqlite3Fts3ExprParse(
n = (int)strlen(z);
}
rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed);
+ assert( rc==SQLITE_OK || *ppExpr==0 );
/* Check for mismatched parenthesis */
if( rc==SQLITE_OK && sParse.nNest ){
rc = SQLITE_ERROR;
+ }
+
+ return rc;
+}
+
+/*
+** Parameters z and n contain a pointer to and length of a buffer containing
+** an fts3 query expression, respectively. This function attempts to parse the
+** query expression and create a tree of Fts3Expr structures representing the
+** parsed expression. If successful, *ppExpr is set to point to the head
+** of the parsed expression tree and SQLITE_OK is returned. If an error
+** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
+** error) is returned and *ppExpr is set to 0.
+**
+** If parameter n is a negative number, then z is assumed to point to a
+** nul-terminated string and the length is determined using strlen().
+**
+** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
+** use to normalize query tokens while parsing the expression. The azCol[]
+** array, which is assumed to contain nCol entries, should contain the names
+** of each column in the target fts3 table, in order from left to right.
+** Column names must be nul-terminated strings.
+**
+** The iDefaultCol parameter should be passed the index of the table column
+** that appears on the left-hand-side of the MATCH operator (the default
+** column to match against for tokens for which a column name is not explicitly
+** specified as part of the query string), or -1 if tokens may by default
+** match any table column.
+*/
+int sqlite3Fts3ExprParse(
+ sqlite3_tokenizer *pTokenizer, /* Tokenizer module */
+ int iLangid, /* Language id for tokenizer */
+ char **azCol, /* Array of column names for fts3 table */
+ int bFts4, /* True to allow FTS4-only syntax */
+ int nCol, /* Number of entries in azCol[] */
+ int iDefaultCol, /* Default column to query */
+ const char *z, int n, /* Text of MATCH query */
+ Fts3Expr **ppExpr, /* OUT: Parsed query structure */
+ char **pzErr /* OUT: Error message (sqlite3_malloc) */
+){
+ static const int MAX_EXPR_DEPTH = 12;
+ int rc = fts3ExprParseUnbalanced(
+ pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr
+ );
+
+ /* Rebalance the expression. And check that its depth does not exceed
+ ** MAX_EXPR_DEPTH. */
+ if( rc==SQLITE_OK && *ppExpr ){
+ rc = fts3ExprBalance(ppExpr, MAX_EXPR_DEPTH);
+ if( rc==SQLITE_OK ){
+ rc = fts3ExprCheckDepth(*ppExpr, MAX_EXPR_DEPTH);
+ }
+ }
+
+ if( rc!=SQLITE_OK ){
sqlite3Fts3ExprFree(*ppExpr);
*ppExpr = 0;
+ if( rc==SQLITE_TOOBIG ){
+ *pzErr = sqlite3_mprintf(
+ "FTS expression tree is too large (maximum depth %d)", MAX_EXPR_DEPTH
+ );
+ rc = SQLITE_ERROR;
+ }else if( rc==SQLITE_ERROR ){
+ *pzErr = sqlite3_mprintf("malformed MATCH expression: [%s]", z);
+ }
}
return rc;
}
/*
+** Free a single node of an expression tree.
+*/
+static void fts3FreeExprNode(Fts3Expr *p){
+ assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 );
+ sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
+ sqlite3_free(p->aMI);
+ sqlite3_free(p);
+}
+
+/*
** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
+**
+** This function would be simpler if it recursively called itself. But
+** that would mean passing a sufficiently large expression to ExprParse()
+** could cause a stack overflow.
*/
-void sqlite3Fts3ExprFree(Fts3Expr *p){
- if( p ){
- assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 );
- sqlite3Fts3ExprFree(p->pLeft);
- sqlite3Fts3ExprFree(p->pRight);
- sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
- sqlite3_free(p->aMI);
- sqlite3_free(p);
+void sqlite3Fts3ExprFree(Fts3Expr *pDel){
+ Fts3Expr *p;
+ assert( pDel==0 || pDel->pParent==0 );
+ for(p=pDel; p && (p->pLeft||p->pRight); p=(p->pLeft ? p->pLeft : p->pRight)){
+ assert( p->pParent==0 || p==p->pParent->pRight || p==p->pParent->pLeft );
+ }
+ while( p ){
+ Fts3Expr *pParent = p->pParent;
+ fts3FreeExprNode(p);
+ if( pParent && p==pParent->pLeft && pParent->pRight ){
+ p = pParent->pRight;
+ while( p && (p->pLeft || p->pRight) ){
+ assert( p==p->pParent->pRight || p==p->pParent->pLeft );
+ p = (p->pLeft ? p->pLeft : p->pRight);
+ }
+ }else{
+ p = pParent;
+ }
}
}
@@ -871,6 +1116,9 @@ static int queryTestTokenizer(
** the returned expression text and then freed using sqlite3_free().
*/
static char *exprToString(Fts3Expr *pExpr, char *zBuf){
+ if( pExpr==0 ){
+ return sqlite3_mprintf("");
+ }
switch( pExpr->eType ){
case FTSQUERY_PHRASE: {
Fts3Phrase *pPhrase = pExpr->pPhrase;
@@ -978,10 +1226,21 @@ static void fts3ExprTest(
azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
}
- rc = sqlite3Fts3ExprParse(
- pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr
- );
+ if( sqlite3_user_data(context) ){
+ char *zDummy = 0;
+ rc = sqlite3Fts3ExprParse(
+ pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy
+ );
+ assert( rc==SQLITE_OK || pExpr==0 );
+ sqlite3_free(zDummy);
+ }else{
+ rc = fts3ExprParseUnbalanced(
+ pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr
+ );
+ }
+
if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
+ sqlite3Fts3ExprFree(pExpr);
sqlite3_result_error(context, "Error parsing expression", -1);
}else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){
sqlite3_result_error_nomem(context);
@@ -1004,9 +1263,15 @@ exprtest_out:
** with database connection db.
*/
int sqlite3Fts3ExprInitTestInterface(sqlite3* db){
- return sqlite3_create_function(
+ int rc = sqlite3_create_function(
db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0
);
+ if( rc==SQLITE_OK ){
+ rc = sqlite3_create_function(db, "fts3_exprtest_rebalance",
+ -1, SQLITE_UTF8, (void *)1, fts3ExprTest, 0, 0
+ );
+ }
+ return rc;
}
#endif
diff --git a/ext/fts3/fts3_hash.h b/ext/fts3/fts3_hash.h
index 399f515..dc3fcf8 100644
--- a/ext/fts3/fts3_hash.h
+++ b/ext/fts3/fts3_hash.h
@@ -9,7 +9,7 @@
** May you share freely, never taking more than you give.
**
*************************************************************************
-** This is the header file for the generic hash-table implemenation
+** This is the header file for the generic hash-table implementation
** used in SQLite. We've modified it slightly to serve as a standalone
** hash table implementation for the full-text indexing module.
**
diff --git a/ext/fts3/fts3_icu.c b/ext/fts3/fts3_icu.c
index 18b7948..52df8c7 100644
--- a/ext/fts3/fts3_icu.c
+++ b/ext/fts3/fts3_icu.c
@@ -119,7 +119,7 @@ static int icuOpen(
nChar = nInput+1;
pCsr = (IcuCursor *)sqlite3_malloc(
sizeof(IcuCursor) + /* IcuCursor */
- nChar * sizeof(UChar) + /* IcuCursor.aChar[] */
+ ((nChar+3)&~3) * sizeof(UChar) + /* IcuCursor.aChar[] */
(nChar+1) * sizeof(int) /* IcuCursor.aOffset[] */
);
if( !pCsr ){
@@ -127,7 +127,7 @@ static int icuOpen(
}
memset(pCsr, 0, sizeof(IcuCursor));
pCsr->aChar = (UChar *)&pCsr[1];
- pCsr->aOffset = (int *)&pCsr->aChar[nChar];
+ pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3];
pCsr->aOffset[iOut] = iInput;
U8_NEXT(zInput, iInput, nInput, c);
diff --git a/ext/fts3/fts3_snippet.c b/ext/fts3/fts3_snippet.c
index 6fce3d0..d54a787 100644
--- a/ext/fts3/fts3_snippet.c
+++ b/ext/fts3/fts3_snippet.c
@@ -389,9 +389,9 @@ static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
** is the snippet with the highest score, where scores are calculated
** by adding:
**
-** (a) +1 point for each occurence of a matchable phrase in the snippet.
+** (a) +1 point for each occurrence of a matchable phrase in the snippet.
**
-** (b) +1000 points for the first occurence of each matchable phrase in
+** (b) +1000 points for the first occurrence of each matchable phrase in
** the snippet for which the corresponding mCovered bit is not set.
**
** The selected snippet parameters are stored in structure *pFragment before
@@ -576,7 +576,7 @@ static int fts3SnippetShift(
return rc;
}
while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){
- const char *ZDUMMY; int DUMMY1, DUMMY2, DUMMY3;
+ const char *ZDUMMY; int DUMMY1 = 0, DUMMY2 = 0, DUMMY3 = 0;
rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
}
pMod->xClose(pC);
@@ -620,8 +620,6 @@ static int fts3SnippetText(
int iCol = pFragment->iCol+1; /* Query column to extract text from */
sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor open on zDoc/nDoc */
- const char *ZDUMMY; /* Dummy argument used with tokenizer */
- int DUMMY1; /* Dummy argument used with tokenizer */
zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
if( zDoc==0 ){
@@ -640,10 +638,23 @@ static int fts3SnippetText(
}
while( rc==SQLITE_OK ){
- int iBegin; /* Offset in zDoc of start of token */
- int iFin; /* Offset in zDoc of end of token */
- int isHighlight; /* True for highlighted terms */
-
+ const char *ZDUMMY; /* Dummy argument used with tokenizer */
+ int DUMMY1 = -1; /* Dummy argument used with tokenizer */
+ int iBegin = 0; /* Offset in zDoc of start of token */
+ int iFin = 0; /* Offset in zDoc of end of token */
+ int isHighlight = 0; /* True for highlighted terms */
+
+ /* Variable DUMMY1 is initialized to a negative value above. Elsewhere
+ ** in the FTS code the variable that the third argument to xNext points to
+ ** is initialized to zero before the first (*but not necessarily
+ ** subsequent*) call to xNext(). This is done for a particular application
+ ** that needs to know whether or not the tokenizer is being used for
+ ** snippet generation or for some other purpose.
+ **
+ ** Extreme care is required when writing code to depend on this
+ ** initialization. It is not a documented part of the tokenizer interface.
+ ** If a tokenizer is used directly by any code outside of FTS, this
+ ** convention might not be respected. */
rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
if( rc!=SQLITE_OK ){
if( rc==SQLITE_DONE ){
@@ -1333,8 +1344,6 @@ void sqlite3Fts3Offsets(
){
Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
- const char *ZDUMMY; /* Dummy argument used with xNext() */
- int NDUMMY; /* Dummy argument used with xNext() */
int rc; /* Return Code */
int nToken; /* Number of tokens in query */
int iCol; /* Column currently being processed */
@@ -1367,9 +1376,11 @@ void sqlite3Fts3Offsets(
*/
for(iCol=0; iCol<pTab->nColumn; iCol++){
sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
- int iStart;
- int iEnd;
- int iCurrent;
+ const char *ZDUMMY; /* Dummy argument used with xNext() */
+ int NDUMMY = 0; /* Dummy argument used with xNext() */
+ int iStart = 0;
+ int iEnd = 0;
+ int iCurrent = 0;
const char *zDoc;
int nDoc;
diff --git a/ext/fts3/fts3_test.c b/ext/fts3/fts3_test.c
index 4da0b8f..75ec6bd 100644
--- a/ext/fts3/fts3_test.c
+++ b/ext/fts3/fts3_test.c
@@ -267,7 +267,7 @@ static int fts3_near_match_cmd(
**
** Whether or not the arguments are present, this command returns a list of
** two integers - the initial chunksize and threshold when the command is
-** invoked. This can be used to restore the default behaviour after running
+** invoked. This can be used to restore the default behavior after running
** tests. For example:
**
** # Override incr-load settings for testing:
diff --git a/ext/fts3/fts3_tokenize_vtab.c b/ext/fts3/fts3_tokenize_vtab.c
new file mode 100644
index 0000000..364852e
--- /dev/null
+++ b/ext/fts3/fts3_tokenize_vtab.c
@@ -0,0 +1,454 @@
+/*
+** 2013 Apr 22
+**
+** 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 file contains code for the "fts3tokenize" virtual table module.
+** An fts3tokenize virtual table is created as follows:
+**
+** CREATE VIRTUAL TABLE <tbl> USING fts3tokenize(
+** <tokenizer-name>, <arg-1>, ...
+** );
+**
+** The table created has the following schema:
+**
+** CREATE TABLE <tbl>(input, token, start, end, position)
+**
+** When queried, the query must include a WHERE clause of type:
+**
+** input = <string>
+**
+** The virtual table module tokenizes this <string>, using the FTS3
+** tokenizer specified by the arguments to the CREATE VIRTUAL TABLE
+** statement and returns one row for each token in the result. With
+** fields set as follows:
+**
+** input: Always set to a copy of <string>
+** token: A token from the input.
+** start: Byte offset of the token within the input <string>.
+** end: Byte offset of the byte immediately following the end of the
+** token within the input string.
+** pos: Token offset of token within input.
+**
+*/
+#include "fts3Int.h"
+#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
+
+#include <string.h>
+#include <assert.h>
+
+typedef struct Fts3tokTable Fts3tokTable;
+typedef struct Fts3tokCursor Fts3tokCursor;
+
+/*
+** Virtual table structure.
+*/
+struct Fts3tokTable {
+ sqlite3_vtab base; /* Base class used by SQLite core */
+ const sqlite3_tokenizer_module *pMod;
+ sqlite3_tokenizer *pTok;
+};
+
+/*
+** Virtual table cursor structure.
+*/
+struct Fts3tokCursor {
+ sqlite3_vtab_cursor base; /* Base class used by SQLite core */
+ char *zInput; /* Input string */
+ sqlite3_tokenizer_cursor *pCsr; /* Cursor to iterate through zInput */
+ int iRowid; /* Current 'rowid' value */
+ const char *zToken; /* Current 'token' value */
+ int nToken; /* Size of zToken in bytes */
+ int iStart; /* Current 'start' value */
+ int iEnd; /* Current 'end' value */
+ int iPos; /* Current 'pos' value */
+};
+
+/*
+** Query FTS for the tokenizer implementation named zName.
+*/
+static int fts3tokQueryTokenizer(
+ Fts3Hash *pHash,
+ const char *zName,
+ const sqlite3_tokenizer_module **pp,
+ char **pzErr
+){
+ sqlite3_tokenizer_module *p;
+ int nName = (int)strlen(zName);
+
+ p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
+ if( !p ){
+ *pzErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
+ return SQLITE_ERROR;
+ }
+
+ *pp = p;
+ return SQLITE_OK;
+}
+
+/*
+** The second argument, argv[], is an array of pointers to nul-terminated
+** strings. This function makes a copy of the array and strings into a
+** single block of memory. It then dequotes any of the strings that appear
+** to be quoted.
+**
+** If successful, output parameter *pazDequote is set to point at the
+** array of dequoted strings and SQLITE_OK is returned. The caller is
+** responsible for eventually calling sqlite3_free() to free the array
+** in this case. Or, if an error occurs, an SQLite error code is returned.
+** The final value of *pazDequote is undefined in this case.
+*/
+static int fts3tokDequoteArray(
+ int argc, /* Number of elements in argv[] */
+ const char * const *argv, /* Input array */
+ char ***pazDequote /* Output array */
+){
+ int rc = SQLITE_OK; /* Return code */
+ if( argc==0 ){
+ *pazDequote = 0;
+ }else{
+ int i;
+ int nByte = 0;
+ char **azDequote;
+
+ for(i=0; i<argc; i++){
+ nByte += (int)(strlen(argv[i]) + 1);
+ }
+
+ *pazDequote = azDequote = sqlite3_malloc(sizeof(char *)*argc + nByte);
+ if( azDequote==0 ){
+ rc = SQLITE_NOMEM;
+ }else{
+ char *pSpace = (char *)&azDequote[argc];
+ for(i=0; i<argc; i++){
+ int n = (int)strlen(argv[i]);
+ azDequote[i] = pSpace;
+ memcpy(pSpace, argv[i], n+1);
+ sqlite3Fts3Dequote(pSpace);
+ pSpace += (n+1);
+ }
+ }
+ }
+
+ return rc;
+}
+
+/*
+** Schema of the tokenizer table.
+*/
+#define FTS3_TOK_SCHEMA "CREATE TABLE x(input, token, start, end, position)"
+
+/*
+** This function does all the work for both the xConnect and xCreate methods.
+** These tables have no persistent representation of their own, so xConnect
+** and xCreate are identical operations.
+**
+** argv[0]: module name
+** argv[1]: database name
+** argv[2]: table name
+** argv[3]: first argument (tokenizer name)
+*/
+static int fts3tokConnectMethod(
+ sqlite3 *db, /* Database connection */
+ void *pHash, /* Hash table of tokenizers */
+ int argc, /* Number of elements in argv array */
+ const char * const *argv, /* xCreate/xConnect argument array */
+ sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
+ char **pzErr /* OUT: sqlite3_malloc'd error message */
+){
+ Fts3tokTable *pTab;
+ const sqlite3_tokenizer_module *pMod = 0;
+ sqlite3_tokenizer *pTok = 0;
+ int rc;
+ char **azDequote = 0;
+ int nDequote;
+
+ rc = sqlite3_declare_vtab(db, FTS3_TOK_SCHEMA);
+ if( rc!=SQLITE_OK ) return rc;
+
+ nDequote = argc-3;
+ rc = fts3tokDequoteArray(nDequote, &argv[3], &azDequote);
+
+ if( rc==SQLITE_OK ){
+ const char *zModule;
+ if( nDequote<1 ){
+ zModule = "simple";
+ }else{
+ zModule = azDequote[0];
+ }
+ rc = fts3tokQueryTokenizer((Fts3Hash*)pHash, zModule, &pMod, pzErr);
+ }
+
+ assert( (rc==SQLITE_OK)==(pMod!=0) );
+ if( rc==SQLITE_OK ){
+ const char * const *azArg = (const char * const *)&azDequote[1];
+ rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok);
+ }
+
+ if( rc==SQLITE_OK ){
+ pTab = (Fts3tokTable *)sqlite3_malloc(sizeof(Fts3tokTable));
+ if( pTab==0 ){
+ rc = SQLITE_NOMEM;
+ }
+ }
+
+ if( rc==SQLITE_OK ){
+ memset(pTab, 0, sizeof(Fts3tokTable));
+ pTab->pMod = pMod;
+ pTab->pTok = pTok;
+ *ppVtab = &pTab->base;
+ }else{
+ if( pTok ){
+ pMod->xDestroy(pTok);
+ }
+ }
+
+ sqlite3_free(azDequote);
+ return rc;
+}
+
+/*
+** This function does the work for both the xDisconnect and xDestroy methods.
+** These tables have no persistent representation of their own, so xDisconnect
+** and xDestroy are identical operations.
+*/
+static int fts3tokDisconnectMethod(sqlite3_vtab *pVtab){
+ Fts3tokTable *pTab = (Fts3tokTable *)pVtab;
+
+ pTab->pMod->xDestroy(pTab->pTok);
+ sqlite3_free(pTab);
+ return SQLITE_OK;
+}
+
+/*
+** xBestIndex - Analyze a WHERE and ORDER BY clause.
+*/
+static int fts3tokBestIndexMethod(
+ sqlite3_vtab *pVTab,
+ sqlite3_index_info *pInfo
+){
+ int i;
+ UNUSED_PARAMETER(pVTab);
+
+ for(i=0; i<pInfo->nConstraint; i++){
+ if( pInfo->aConstraint[i].usable
+ && pInfo->aConstraint[i].iColumn==0
+ && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ
+ ){
+ pInfo->idxNum = 1;
+ pInfo->aConstraintUsage[i].argvIndex = 1;
+ pInfo->aConstraintUsage[i].omit = 1;
+ pInfo->estimatedCost = 1;
+ return SQLITE_OK;
+ }
+ }
+
+ pInfo->idxNum = 0;
+ assert( pInfo->estimatedCost>1000000.0 );
+
+ return SQLITE_OK;
+}
+
+/*
+** xOpen - Open a cursor.
+*/
+static int fts3tokOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
+ Fts3tokCursor *pCsr;
+ UNUSED_PARAMETER(pVTab);
+
+ pCsr = (Fts3tokCursor *)sqlite3_malloc(sizeof(Fts3tokCursor));
+ if( pCsr==0 ){
+ return SQLITE_NOMEM;
+ }
+ memset(pCsr, 0, sizeof(Fts3tokCursor));
+
+ *ppCsr = (sqlite3_vtab_cursor *)pCsr;
+ return SQLITE_OK;
+}
+
+/*
+** Reset the tokenizer cursor passed as the only argument. As if it had
+** just been returned by fts3tokOpenMethod().
+*/
+static void fts3tokResetCursor(Fts3tokCursor *pCsr){
+ if( pCsr->pCsr ){
+ Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab);
+ pTab->pMod->xClose(pCsr->pCsr);
+ pCsr->pCsr = 0;
+ }
+ sqlite3_free(pCsr->zInput);
+ pCsr->zInput = 0;
+ pCsr->zToken = 0;
+ pCsr->nToken = 0;
+ pCsr->iStart = 0;
+ pCsr->iEnd = 0;
+ pCsr->iPos = 0;
+ pCsr->iRowid = 0;
+}
+
+/*
+** xClose - Close a cursor.
+*/
+static int fts3tokCloseMethod(sqlite3_vtab_cursor *pCursor){
+ Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
+
+ fts3tokResetCursor(pCsr);
+ sqlite3_free(pCsr);
+ return SQLITE_OK;
+}
+
+/*
+** xNext - Advance the cursor to the next row, if any.
+*/
+static int fts3tokNextMethod(sqlite3_vtab_cursor *pCursor){
+ Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
+ Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
+ int rc; /* Return code */
+
+ pCsr->iRowid++;
+ rc = pTab->pMod->xNext(pCsr->pCsr,
+ &pCsr->zToken, &pCsr->nToken,
+ &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos
+ );
+
+ if( rc!=SQLITE_OK ){
+ fts3tokResetCursor(pCsr);
+ if( rc==SQLITE_DONE ) rc = SQLITE_OK;
+ }
+
+ return rc;
+}
+
+/*
+** xFilter - Initialize a cursor to point at the start of its data.
+*/
+static int fts3tokFilterMethod(
+ sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
+ int idxNum, /* Strategy index */
+ const char *idxStr, /* Unused */
+ int nVal, /* Number of elements in apVal */
+ sqlite3_value **apVal /* Arguments for the indexing scheme */
+){
+ int rc = SQLITE_ERROR;
+ Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
+ Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
+ UNUSED_PARAMETER(idxStr);
+ UNUSED_PARAMETER(nVal);
+
+ fts3tokResetCursor(pCsr);
+ if( idxNum==1 ){
+ const char *zByte = (const char *)sqlite3_value_text(apVal[0]);
+ int nByte = sqlite3_value_bytes(apVal[0]);
+ pCsr->zInput = sqlite3_malloc(nByte+1);
+ if( pCsr->zInput==0 ){
+ rc = SQLITE_NOMEM;
+ }else{
+ memcpy(pCsr->zInput, zByte, nByte);
+ pCsr->zInput[nByte] = 0;
+ rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr);
+ if( rc==SQLITE_OK ){
+ pCsr->pCsr->pTokenizer = pTab->pTok;
+ }
+ }
+ }
+
+ if( rc!=SQLITE_OK ) return rc;
+ return fts3tokNextMethod(pCursor);
+}
+
+/*
+** xEof - Return true if the cursor is at EOF, or false otherwise.
+*/
+static int fts3tokEofMethod(sqlite3_vtab_cursor *pCursor){
+ Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
+ return (pCsr->zToken==0);
+}
+
+/*
+** xColumn - Return a column value.
+*/
+static int fts3tokColumnMethod(
+ sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
+ sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
+ int iCol /* Index of column to read value from */
+){
+ Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
+
+ /* CREATE TABLE x(input, token, start, end, position) */
+ switch( iCol ){
+ case 0:
+ sqlite3_result_text(pCtx, pCsr->zInput, -1, SQLITE_TRANSIENT);
+ break;
+ case 1:
+ sqlite3_result_text(pCtx, pCsr->zToken, pCsr->nToken, SQLITE_TRANSIENT);
+ break;
+ case 2:
+ sqlite3_result_int(pCtx, pCsr->iStart);
+ break;
+ case 3:
+ sqlite3_result_int(pCtx, pCsr->iEnd);
+ break;
+ default:
+ assert( iCol==4 );
+ sqlite3_result_int(pCtx, pCsr->iPos);
+ break;
+ }
+ return SQLITE_OK;
+}
+
+/*
+** xRowid - Return the current rowid for the cursor.
+*/
+static int fts3tokRowidMethod(
+ sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
+ sqlite_int64 *pRowid /* OUT: Rowid value */
+){
+ Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
+ *pRowid = (sqlite3_int64)pCsr->iRowid;
+ return SQLITE_OK;
+}
+
+/*
+** Register the fts3tok module with database connection db. Return SQLITE_OK
+** if successful or an error code if sqlite3_create_module() fails.
+*/
+int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash){
+ static const sqlite3_module fts3tok_module = {
+ 0, /* iVersion */
+ fts3tokConnectMethod, /* xCreate */
+ fts3tokConnectMethod, /* xConnect */
+ fts3tokBestIndexMethod, /* xBestIndex */
+ fts3tokDisconnectMethod, /* xDisconnect */
+ fts3tokDisconnectMethod, /* xDestroy */
+ fts3tokOpenMethod, /* xOpen */
+ fts3tokCloseMethod, /* xClose */
+ fts3tokFilterMethod, /* xFilter */
+ fts3tokNextMethod, /* xNext */
+ fts3tokEofMethod, /* xEof */
+ fts3tokColumnMethod, /* xColumn */
+ fts3tokRowidMethod, /* xRowid */
+ 0, /* xUpdate */
+ 0, /* xBegin */
+ 0, /* xSync */
+ 0, /* xCommit */
+ 0, /* xRollback */
+ 0, /* xFindFunction */
+ 0, /* xRename */
+ 0, /* xSavepoint */
+ 0, /* xRelease */
+ 0 /* xRollbackTo */
+ };
+ int rc; /* Return code */
+
+ rc = sqlite3_create_module(db, "fts3tokenize", &fts3tok_module, (void*)pHash);
+ return rc;
+}
+
+#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
diff --git a/ext/fts3/fts3_tokenizer.c b/ext/fts3/fts3_tokenizer.c
index 4a7a175..04f8446 100644
--- a/ext/fts3/fts3_tokenizer.c
+++ b/ext/fts3/fts3_tokenizer.c
@@ -251,10 +251,10 @@ static void testFunc(
const char *azArg[64];
const char *zToken;
- int nToken;
- int iStart;
- int iEnd;
- int iPos;
+ int nToken = 0;
+ int iStart = 0;
+ int iEnd = 0;
+ int iPos = 0;
int i;
Tcl_Obj *pRet;
@@ -428,7 +428,7 @@ static void intTestFunc(
/*
** Set up SQL objects in database db used to access the contents of
** the hash table pointed to by argument pHash. The hash table must
-** been initialised to use string keys, and to take a private copy
+** been initialized to use string keys, and to take a private copy
** of the key when a value is inserted. i.e. by a call similar to:
**
** sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
diff --git a/ext/fts3/fts3_tokenizer.h b/ext/fts3/fts3_tokenizer.h
index c91c7ed..4a40b2b 100644
--- a/ext/fts3/fts3_tokenizer.h
+++ b/ext/fts3/fts3_tokenizer.h
@@ -70,7 +70,7 @@ struct sqlite3_tokenizer_module {
** This method should return either SQLITE_OK (0), or an SQLite error
** code. If SQLITE_OK is returned, then *ppTokenizer should be set
** to point at the newly created tokenizer structure. The generic
- ** sqlite3_tokenizer.pModule variable should not be initialised by
+ ** sqlite3_tokenizer.pModule variable should not be initialized by
** this callback. The caller will do so.
*/
int (*xCreate)(
diff --git a/ext/fts3/fts3_unicode.c b/ext/fts3/fts3_unicode.c
index 79941ed..188358e 100644
--- a/ext/fts3/fts3_unicode.c
+++ b/ext/fts3/fts3_unicode.c
@@ -125,7 +125,7 @@ static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
**
** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic()
** identifies as a diacritic) occurs in the zIn/nIn string it is ignored.
-** It is not possible to change the behaviour of the tokenizer with respect
+** It is not possible to change the behavior of the tokenizer with respect
** to these codepoints.
*/
static int unicodeAddExceptions(
diff --git a/ext/fts3/fts3_write.c b/ext/fts3/fts3_write.c
index c9f1743..269d1dd 100644
--- a/ext/fts3/fts3_write.c
+++ b/ext/fts3/fts3_write.c
@@ -776,16 +776,16 @@ static int fts3PendingTermsAdd(
int iLangid, /* Language id to use */
const char *zText, /* Text of document to be inserted */
int iCol, /* Column into which text is being inserted */
- u32 *pnWord /* OUT: Number of tokens inserted */
+ u32 *pnWord /* IN/OUT: Incr. by number tokens inserted */
){
int rc;
- int iStart;
- int iEnd;
- int iPos;
+ int iStart = 0;
+ int iEnd = 0;
+ int iPos = 0;
int nWord = 0;
char const *zToken;
- int nToken;
+ int nToken = 0;
sqlite3_tokenizer *pTokenizer = p->pTokenizer;
sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
@@ -840,7 +840,7 @@ static int fts3PendingTermsAdd(
}
pModule->xClose(pCsr);
- *pnWord = nWord;
+ *pnWord += nWord;
return (rc==SQLITE_DONE ? SQLITE_OK : rc);
}
@@ -1044,11 +1044,13 @@ static void fts3DeleteTerms(
int *pRC, /* Result code */
Fts3Table *p, /* The FTS table to delete from */
sqlite3_value *pRowid, /* The docid to be deleted */
- u32 *aSz /* Sizes of deleted document written here */
+ u32 *aSz, /* Sizes of deleted document written here */
+ int *pbFound /* OUT: Set to true if row really does exist */
){
int rc;
sqlite3_stmt *pSelect;
+ assert( *pbFound==0 );
if( *pRC ) return;
rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, &pRowid);
if( rc==SQLITE_OK ){
@@ -1066,6 +1068,7 @@ static void fts3DeleteTerms(
*pRC = rc;
return;
}
+ *pbFound = 1;
}
rc = sqlite3_reset(pSelect);
}else{
@@ -1479,6 +1482,7 @@ static int fts3SegReaderNextDocid(
*pnOffsetList = (int)(p - pReader->pOffsetList - 1);
}
+ /* List may have been edited in place by fts3EvalNearTrim() */
while( p<pEnd && *p==0 ) p++;
/* If there are no more entries in the doclist, set pOffsetList to
@@ -2494,9 +2498,13 @@ static int fts3DeleteSegdir(
**
** If there are no entries in the input position list for column iCol, then
** *pnList is set to zero before returning.
+**
+** If parameter bZero is non-zero, then any part of the input list following
+** the end of the output list is zeroed before returning.
*/
static void fts3ColumnFilter(
int iCol, /* Column to filter on */
+ int bZero, /* Zero out anything following *ppList */
char **ppList, /* IN/OUT: Pointer to position list */
int *pnList /* IN/OUT: Size of buffer *ppList in bytes */
){
@@ -2525,6 +2533,9 @@ static void fts3ColumnFilter(
p += sqlite3Fts3GetVarint32(p, &iCurrent);
}
+ if( bZero && &pList[nList]!=pEnd ){
+ memset(&pList[nList], 0, pEnd - &pList[nList]);
+ }
*ppList = pList;
*pnList = nList;
}
@@ -2598,19 +2609,19 @@ int sqlite3Fts3MsrIncrNext(
if( rc!=SQLITE_OK ) return rc;
fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
+ if( nList>0 && fts3SegReaderIsPending(apSegment[0]) ){
+ rc = fts3MsrBufferData(pMsr, pList, nList+1);
+ if( rc!=SQLITE_OK ) return rc;
+ assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
+ pList = pMsr->aBuffer;
+ }
+
if( pMsr->iColFilter>=0 ){
- fts3ColumnFilter(pMsr->iColFilter, &pList, &nList);
+ fts3ColumnFilter(pMsr->iColFilter, 1, &pList, &nList);
}
if( nList>0 ){
- if( fts3SegReaderIsPending(apSegment[0]) ){
- rc = fts3MsrBufferData(pMsr, pList, nList+1);
- if( rc!=SQLITE_OK ) return rc;
- *paPoslist = pMsr->aBuffer;
- assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
- }else{
- *paPoslist = pList;
- }
+ *paPoslist = pList;
*piDocid = iDocid;
*pnPoslist = nList;
break;
@@ -2853,7 +2864,7 @@ int sqlite3Fts3SegReaderStep(
}
if( isColFilter ){
- fts3ColumnFilter(pFilter->iCol, &pList, &nList);
+ fts3ColumnFilter(pFilter->iCol, 0, &pList, &nList);
}
if( !isIgnoreEmpty || nList>0 ){
@@ -3290,7 +3301,7 @@ static int fts3DoRebuild(Fts3Table *p){
int iCol;
int iLangid = langidFromSelect(p, pStmt);
rc = fts3PendingTermsDocid(p, iLangid, sqlite3_column_int64(pStmt, 0));
- aSz[p->nColumn] = 0;
+ memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
rc = fts3PendingTermsAdd(p, iLangid, z, iCol, &aSz[iCol]);
@@ -4934,9 +4945,9 @@ static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){
rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText, &pT);
while( rc==SQLITE_OK ){
char const *zToken; /* Buffer containing token */
- int nToken; /* Number of bytes in token */
- int iDum1, iDum2; /* Dummy variables */
- int iPos; /* Position of token in zText */
+ int nToken = 0; /* Number of bytes in token */
+ int iDum1 = 0, iDum2 = 0; /* Dummy variables */
+ int iPos = 0; /* Position of token in zText */
rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
if( rc==SQLITE_OK ){
@@ -5103,9 +5114,9 @@ int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
rc = sqlite3Fts3OpenTokenizer(pT, pCsr->iLangid, zText, -1, &pTC);
while( rc==SQLITE_OK ){
char const *zToken; /* Buffer containing token */
- int nToken; /* Number of bytes in token */
- int iDum1, iDum2; /* Dummy variables */
- int iPos; /* Position of token in zText */
+ int nToken = 0; /* Number of bytes in token */
+ int iDum1 = 0, iDum2 = 0; /* Dummy variables */
+ int iPos = 0; /* Position of token in zText */
rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
@@ -5194,28 +5205,32 @@ int sqlite3Fts3DeferToken(
static int fts3DeleteByRowid(
Fts3Table *p,
sqlite3_value *pRowid,
- int *pnDoc,
+ int *pnChng, /* IN/OUT: Decrement if row is deleted */
u32 *aSzDel
){
- int isEmpty = 0;
- int rc = fts3IsEmpty(p, pRowid, &isEmpty);
- if( rc==SQLITE_OK ){
- if( isEmpty ){
- /* Deleting this row means the whole table is empty. In this case
- ** delete the contents of all three tables and throw away any
- ** data in the pendingTerms hash table. */
- rc = fts3DeleteAll(p, 1);
- *pnDoc = *pnDoc - 1;
- }else{
- fts3DeleteTerms(&rc, p, pRowid, aSzDel);
- if( p->zContentTbl==0 ){
- fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
- if( sqlite3_changes(p->db) ) *pnDoc = *pnDoc - 1;
+ int rc = SQLITE_OK; /* Return code */
+ int bFound = 0; /* True if *pRowid really is in the table */
+
+ fts3DeleteTerms(&rc, p, pRowid, aSzDel, &bFound);
+ if( bFound && rc==SQLITE_OK ){
+ int isEmpty = 0; /* Deleting *pRowid leaves the table empty */
+ rc = fts3IsEmpty(p, pRowid, &isEmpty);
+ if( rc==SQLITE_OK ){
+ if( isEmpty ){
+ /* Deleting this row means the whole table is empty. In this case
+ ** delete the contents of all three tables and throw away any
+ ** data in the pendingTerms hash table. */
+ rc = fts3DeleteAll(p, 1);
+ *pnChng = 0;
+ memset(aSzDel, 0, sizeof(u32) * (p->nColumn+1) * 2);
}else{
- *pnDoc = *pnDoc - 1;
- }
- if( p->bHasDocsize ){
- fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
+ *pnChng = *pnChng - 1;
+ if( p->zContentTbl==0 ){
+ fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
+ }
+ if( p->bHasDocsize ){
+ fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
+ }
}
}
}
@@ -5246,7 +5261,7 @@ int sqlite3Fts3UpdateMethod(
int rc = SQLITE_OK; /* Return Code */
int isRemove = 0; /* True for an UPDATE or DELETE */
u32 *aSzIns = 0; /* Sizes of inserted documents */
- u32 *aSzDel; /* Sizes of deleted documents */
+ u32 *aSzDel = 0; /* Sizes of deleted documents */
int nChng = 0; /* Net change in number of documents */
int bInsertDone = 0;
@@ -5274,13 +5289,13 @@ int sqlite3Fts3UpdateMethod(
}
/* Allocate space to hold the change in document sizes */
- aSzIns = sqlite3_malloc( sizeof(aSzIns[0])*(p->nColumn+1)*2 );
- if( aSzIns==0 ){
+ aSzDel = sqlite3_malloc( sizeof(aSzDel[0])*(p->nColumn+1)*2 );
+ if( aSzDel==0 ){
rc = SQLITE_NOMEM;
goto update_out;
}
- aSzDel = &aSzIns[p->nColumn+1];
- memset(aSzIns, 0, sizeof(aSzIns[0])*(p->nColumn+1)*2);
+ aSzIns = &aSzDel[p->nColumn+1];
+ memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2);
/* If this is an INSERT operation, or an UPDATE that modifies the rowid
** value, then this operation requires constraint handling.
@@ -5365,7 +5380,7 @@ int sqlite3Fts3UpdateMethod(
}
update_out:
- sqlite3_free(aSzIns);
+ sqlite3_free(aSzDel);
sqlite3Fts3SegmentsClose(p);
return rc;
}
diff --git a/ext/icu/README.txt b/ext/icu/README.txt
index c5cadb5..d744f74 100644
--- a/ext/icu/README.txt
+++ b/ext/icu/README.txt
@@ -98,7 +98,7 @@ SQLite. Documentation follows.
<string> REGEXP <re-pattern>
This extension uses the ICU defaults for regular expression matching
- behaviour. Specifically, this means that:
+ behavior. Specifically, this means that:
* Matching is case-sensitive,
* Regular expression comments are not allowed within patterns, and
diff --git a/ext/misc/amatch.c b/ext/misc/amatch.c
new file mode 100644
index 0000000..b613080
--- /dev/null
+++ b/ext/misc/amatch.c
@@ -0,0 +1,1483 @@
+/*
+** 2013-03-14
+**
+** 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 file contains code for a demonstration virtual table that finds
+** "approximate matches" - strings from a finite set that are nearly the
+** same as a single input string. The virtual table is called "amatch".
+**
+** A amatch virtual table is created like this:
+**
+** CREATE VIRTUAL TABLE f USING approximate_match(
+** vocabulary_table=<tablename>, -- V
+** vocabulary_word=<columnname>, -- W
+** vocabulary_language=<columnname>, -- L
+** edit_distances=<edit-cost-table>
+** );
+**
+** When it is created, the new amatch table must be supplied with the
+** the name of a table V and columns V.W and V.L such that
+**
+** SELECT W FROM V WHERE L=$language
+**
+** returns the allowed vocabulary for the match. If the "vocabulary_language"
+** or L columnname is left unspecified or is an empty string, then no
+** filtering of the vocabulary by language is performed.
+**
+** For efficiency, it is essential that the vocabulary table be indexed:
+**
+** CREATE vocab_index ON V(W)
+**
+** A separate edit-cost-table provides scoring information that defines
+** what it means for one string to be "close" to another.
+**
+** The edit-cost-table must contain exactly four columns (more precisely,
+** the statement "SELECT * FROM <edit-cost-table>" must return records
+** that consist of four columns). It does not matter what the columns are
+** named.
+**
+** Each row in the edit-cost-table represents a single character
+** transformation going from user input to the vocabulary. The leftmost
+** column of the row (column 0) contains an integer identifier of the
+** language to which the transformation rule belongs (see "MULTIPLE LANGUAGES"
+** below). The second column of the row (column 1) contains the input
+** character or characters - the characters of user input. The third
+** column contains characters as they appear in the vocabulary table.
+** And the fourth column contains the integer cost of making the
+** transformation. For example:
+**
+** CREATE TABLE f_data(iLang, cFrom, cTo, Cost);
+** INSERT INTO f_data(iLang, cFrom, cTo, Cost) VALUES(0, '', 'a', 100);
+** INSERT INTO f_data(iLang, cFrom, cTo, Cost) VALUES(0, 'b', '', 87);
+** INSERT INTO f_data(iLang, cFrom, cTo, Cost) VALUES(0, 'o', 'oe', 38);
+** INSERT INTO f_data(iLang, cFrom, cTo, Cost) VALUES(0, 'oe', 'o', 40);
+**
+** The first row inserted into the edit-cost-table by the SQL script
+** above indicates that the cost of having an extra 'a' in the vocabulary
+** table that is missing in the user input 100. (All costs are integers.
+** Overall cost must not exceed 16777216.) The second INSERT statement
+** creates a rule saying that the cost of having a single letter 'b' in
+** user input which is missing in the vocabulary table is 87. The third
+** INSERT statement mean that the cost of matching an 'o' in user input
+** against an 'oe' in the vocabulary table is 38. And so forth.
+**
+** The following rules are special:
+**
+** INSERT INTO f_data(iLang, cFrom, cTo, Cost) VALUES(0, '?', '', 97);
+** INSERT INTO f_data(iLang, cFrom, cTo, Cost) VALUES(0, '', '?', 98);
+** INSERT INTO f_data(iLang, cFrom, cTo, Cost) VALUES(0, '?', '?', 99);
+**
+** The '?' to '' rule is the cost of having any single character in the input
+** that is not found in the vocabular. The '' to '?' rule is the cost of
+** having a character in the vocabulary table that is missing from input.
+** And the '?' to '?' rule is the cost of doing an arbitrary character
+** substitution. These three generic rules apply across all languages.
+** In other words, the iLang field is ignored for the generic substitution
+** rules. If more than one cost is given for a generic substitution rule,
+** then the lowest cost is used.
+**
+** Once it has been created, the amatch virtual table can be queried
+** as follows:
+**
+** SELECT word, distance FROM f
+** WHERE word MATCH 'abcdefg'
+** AND distance<200;
+**
+** This query outputs the strings contained in the T(F) field that
+** are close to "abcdefg" and in order of increasing distance. No string
+** is output more than once. If there are multiple ways to transform the
+** target string ("abcdefg") into a string in the vocabulary table then
+** the lowest cost transform is the one that is returned. In this example,
+** the search is limited to strings with a total distance of less than 200.
+**
+** For efficiency, it is important to put tight bounds on the distance.
+** The time and memory space needed to perform this query is exponential
+** in the maximum distance. A good rule of thumb is to limit the distance
+** to no more than 1.5 or 2 times the maximum cost of any rule in the
+** edit-cost-table.
+**
+** The amatch is a read-only table. Any attempt to DELETE, INSERT, or
+** UPDATE on a amatch table will throw an error.
+**
+** It is important to put some kind of a limit on the amatch output. This
+** can be either in the form of a LIMIT clause at the end of the query,
+** or better, a "distance<NNN" constraint where NNN is some number. The
+** running time and memory requirement is exponential in the value of NNN
+** so you want to make sure that NNN is not too big. A value of NNN that
+** is about twice the average transformation cost seems to give good results.
+**
+** The amatch table can be useful for tasks such as spelling correction.
+** Suppose all allowed words are in table vocabulary(w). Then one would create
+** an amatch virtual table like this:
+**
+** CREATE VIRTUAL TABLE ex1 USING amatch(
+** vocabtable=vocabulary,
+** vocabcolumn=w,
+** edit_distances=ec1
+** );
+**
+** Then given an input word $word, look up close spellings this way:
+**
+** SELECT word, distance FROM ex1
+** WHERE word MATCH $word AND distance<200;
+**
+** MULTIPLE LANGUAGES
+**
+** Normally, the "iLang" value associated with all character transformations
+** in the edit-cost-table is zero. However, if required, the amatch
+** virtual table allows multiple languages to be defined. Each query uses
+** only a single iLang value. This allows, for example, a single
+** amatch table to support multiple languages.
+**
+** By default, only the rules with iLang=0 are used. To specify an
+** alternative language, a "language = ?" expression must be added to the
+** WHERE clause of a SELECT, where ? is the integer identifier of the desired
+** language. For example:
+**
+** SELECT word, distance FROM ex1
+** WHERE word MATCH $word
+** AND distance<=200
+** AND language=1 -- Specify use language 1 instead of 0
+**
+** If no "language = ?" constraint is specified in the WHERE clause, language
+** 0 is used.
+**
+** LIMITS
+**
+** The maximum language number is 2147483647. The maximum length of either
+** of the strings in the second or third column of the amatch data table
+** is 50 bytes. The maximum cost on a rule is 1000.
+*/
+#include "sqlite3ext.h"
+SQLITE_EXTENSION_INIT1
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <stdio.h>
+#include <ctype.h>
+
+#ifndef SQLITE_OMIT_VIRTUALTABLE
+
+/*
+** Forward declaration of objects used by this implementation
+*/
+typedef struct amatch_vtab amatch_vtab;
+typedef struct amatch_cursor amatch_cursor;
+typedef struct amatch_rule amatch_rule;
+typedef struct amatch_word amatch_word;
+typedef struct amatch_avl amatch_avl;
+
+
+/*****************************************************************************
+** AVL Tree implementation
+*/
+/*
+** Objects that want to be members of the AVL tree should embedded an
+** instance of this structure.
+*/
+struct amatch_avl {
+ amatch_word *pWord; /* Points to the object being stored in the tree */
+ char *zKey; /* Key. zero-terminated string. Must be unique */
+ amatch_avl *pBefore; /* Other elements less than zKey */
+ amatch_avl *pAfter; /* Other elements greater than zKey */
+ amatch_avl *pUp; /* Parent element */
+ short int height; /* Height of this node. Leaf==1 */
+ short int imbalance; /* Height difference between pBefore and pAfter */
+};
+
+/* Recompute the amatch_avl.height and amatch_avl.imbalance fields for p.
+** Assume that the children of p have correct heights.
+*/
+static void amatchAvlRecomputeHeight(amatch_avl *p){
+ short int hBefore = p->pBefore ? p->pBefore->height : 0;
+ short int hAfter = p->pAfter ? p->pAfter->height : 0;
+ p->imbalance = hBefore - hAfter; /* -: pAfter higher. +: pBefore higher */
+ p->height = (hBefore>hAfter ? hBefore : hAfter)+1;
+}
+
+/*
+** P B
+** / \ / \
+** B Z ==> X P
+** / \ / \
+** X Y Y Z
+**
+*/
+static amatch_avl *amatchAvlRotateBefore(amatch_avl *pP){
+ amatch_avl *pB = pP->pBefore;
+ amatch_avl *pY = pB->pAfter;
+ pB->pUp = pP->pUp;
+ pB->pAfter = pP;
+ pP->pUp = pB;
+ pP->pBefore = pY;
+ if( pY ) pY->pUp = pP;
+ amatchAvlRecomputeHeight(pP);
+ amatchAvlRecomputeHeight(pB);
+ return pB;
+}
+
+/*
+** P A
+** / \ / \
+** X A ==> P Z
+** / \ / \
+** Y Z X Y
+**
+*/
+static amatch_avl *amatchAvlRotateAfter(amatch_avl *pP){
+ amatch_avl *pA = pP->pAfter;
+ amatch_avl *pY = pA->pBefore;
+ pA->pUp = pP->pUp;
+ pA->pBefore = pP;
+ pP->pUp = pA;
+ pP->pAfter = pY;
+ if( pY ) pY->pUp = pP;
+ amatchAvlRecomputeHeight(pP);
+ amatchAvlRecomputeHeight(pA);
+ return pA;
+}
+
+/*
+** Return a pointer to the pBefore or pAfter pointer in the parent
+** of p that points to p. Or if p is the root node, return pp.
+*/
+static amatch_avl **amatchAvlFromPtr(amatch_avl *p, amatch_avl **pp){
+ amatch_avl *pUp = p->pUp;
+ if( pUp==0 ) return pp;
+ if( pUp->pAfter==p ) return &pUp->pAfter;
+ return &pUp->pBefore;
+}
+
+/*
+** Rebalance all nodes starting with p and working up to the root.
+** Return the new root.
+*/
+static amatch_avl *amatchAvlBalance(amatch_avl *p){
+ amatch_avl *pTop = p;
+ amatch_avl **pp;
+ while( p ){
+ amatchAvlRecomputeHeight(p);
+ if( p->imbalance>=2 ){
+ amatch_avl *pB = p->pBefore;
+ if( pB->imbalance<0 ) p->pBefore = amatchAvlRotateAfter(pB);
+ pp = amatchAvlFromPtr(p,&p);
+ p = *pp = amatchAvlRotateBefore(p);
+ }else if( p->imbalance<=(-2) ){
+ amatch_avl *pA = p->pAfter;
+ if( pA->imbalance>0 ) p->pAfter = amatchAvlRotateBefore(pA);
+ pp = amatchAvlFromPtr(p,&p);
+ p = *pp = amatchAvlRotateAfter(p);
+ }
+ pTop = p;
+ p = p->pUp;
+ }
+ return pTop;
+}
+
+/* Search the tree rooted at p for an entry with zKey. Return a pointer
+** to the entry or return NULL.
+*/
+static amatch_avl *amatchAvlSearch(amatch_avl *p, const char *zKey){
+ int c;
+ while( p && (c = strcmp(zKey, p->zKey))!=0 ){
+ p = (c<0) ? p->pBefore : p->pAfter;
+ }
+ return p;
+}
+
+/* Find the first node (the one with the smallest key).
+*/
+static amatch_avl *amatchAvlFirst(amatch_avl *p){
+ if( p ) while( p->pBefore ) p = p->pBefore;
+ return p;
+}
+
+#if 0 /* NOT USED */
+/* Return the node with the next larger key after p.
+*/
+static amatch_avl *amatchAvlNext(amatch_avl *p){
+ amatch_avl *pPrev = 0;
+ while( p && p->pAfter==pPrev ){
+ pPrev = p;
+ p = p->pUp;
+ }
+ if( p && pPrev==0 ){
+ p = amatchAvlFirst(p->pAfter);
+ }
+ return p;
+}
+#endif
+
+#if 0 /* NOT USED */
+/* Verify AVL tree integrity
+*/
+static int amatchAvlIntegrity(amatch_avl *pHead){
+ amatch_avl *p;
+ if( pHead==0 ) return 1;
+ if( (p = pHead->pBefore)!=0 ){
+ assert( p->pUp==pHead );
+ assert( amatchAvlIntegrity(p) );
+ assert( strcmp(p->zKey, pHead->zKey)<0 );
+ while( p->pAfter ) p = p->pAfter;
+ assert( strcmp(p->zKey, pHead->zKey)<0 );
+ }
+ if( (p = pHead->pAfter)!=0 ){
+ assert( p->pUp==pHead );
+ assert( amatchAvlIntegrity(p) );
+ assert( strcmp(p->zKey, pHead->zKey)>0 );
+ p = amatchAvlFirst(p);
+ assert( strcmp(p->zKey, pHead->zKey)>0 );
+ }
+ return 1;
+}
+static int amatchAvlIntegrity2(amatch_avl *pHead){
+ amatch_avl *p, *pNext;
+ for(p=amatchAvlFirst(pHead); p; p=pNext){
+ pNext = amatchAvlNext(p);
+ if( pNext==0 ) break;
+ assert( strcmp(p->zKey, pNext->zKey)<0 );
+ }
+ return 1;
+}
+#endif
+
+/* Insert a new node pNew. Return NULL on success. If the key is not
+** unique, then do not perform the insert but instead leave pNew unchanged
+** and return a pointer to an existing node with the same key.
+*/
+static amatch_avl *amatchAvlInsert(amatch_avl **ppHead, amatch_avl *pNew){
+ int c;
+ amatch_avl *p = *ppHead;
+ if( p==0 ){
+ p = pNew;
+ pNew->pUp = 0;
+ }else{
+ while( p ){
+ c = strcmp(pNew->zKey, p->zKey);
+ if( c<0 ){
+ if( p->pBefore ){
+ p = p->pBefore;
+ }else{
+ p->pBefore = pNew;
+ pNew->pUp = p;
+ break;
+ }
+ }else if( c>0 ){
+ if( p->pAfter ){
+ p = p->pAfter;
+ }else{
+ p->pAfter = pNew;
+ pNew->pUp = p;
+ break;
+ }
+ }else{
+ return p;
+ }
+ }
+ }
+ pNew->pBefore = 0;
+ pNew->pAfter = 0;
+ pNew->height = 1;
+ pNew->imbalance = 0;
+ *ppHead = amatchAvlBalance(p);
+ /* assert( amatchAvlIntegrity(*ppHead) ); */
+ /* assert( amatchAvlIntegrity2(*ppHead) ); */
+ return 0;
+}
+
+/* Remove node pOld from the tree. pOld must be an element of the tree or
+** the AVL tree will become corrupt.
+*/
+static void amatchAvlRemove(amatch_avl **ppHead, amatch_avl *pOld){
+ amatch_avl **ppParent;
+ amatch_avl *pBalance;
+ /* assert( amatchAvlSearch(*ppHead, pOld->zKey)==pOld ); */
+ ppParent = amatchAvlFromPtr(pOld, ppHead);
+ if( pOld->pBefore==0 && pOld->pAfter==0 ){
+ *ppParent = 0;
+ pBalance = pOld->pUp;
+ }else if( pOld->pBefore && pOld->pAfter ){
+ amatch_avl *pX, *pY;
+ pX = amatchAvlFirst(pOld->pAfter);
+ *amatchAvlFromPtr(pX, 0) = pX->pAfter;
+ if( pX->pAfter ) pX->pAfter->pUp = pX->pUp;
+ pBalance = pX->pUp;
+ pX->pAfter = pOld->pAfter;
+ if( pX->pAfter ){
+ pX->pAfter->pUp = pX;
+ }else{
+ assert( pBalance==pOld );
+ pBalance = pX;
+ }
+ pX->pBefore = pY = pOld->pBefore;
+ if( pY ) pY->pUp = pX;
+ pX->pUp = pOld->pUp;
+ *ppParent = pX;
+ }else if( pOld->pBefore==0 ){
+ *ppParent = pBalance = pOld->pAfter;
+ pBalance->pUp = pOld->pUp;
+ }else if( pOld->pAfter==0 ){
+ *ppParent = pBalance = pOld->pBefore;
+ pBalance->pUp = pOld->pUp;
+ }
+ *ppHead = amatchAvlBalance(pBalance);
+ pOld->pUp = 0;
+ pOld->pBefore = 0;
+ pOld->pAfter = 0;
+ /* assert( amatchAvlIntegrity(*ppHead) ); */
+ /* assert( amatchAvlIntegrity2(*ppHead) ); */
+}
+/*
+** End of the AVL Tree implementation
+******************************************************************************/
+
+
+/*
+** Various types.
+**
+** amatch_cost is the "cost" of an edit operation.
+**
+** amatch_len is the length of a matching string.
+**
+** amatch_langid is an ruleset identifier.
+*/
+typedef int amatch_cost;
+typedef signed char amatch_len;
+typedef int amatch_langid;
+
+/*
+** Limits
+*/
+#define AMATCH_MX_LENGTH 50 /* Maximum length of a rule string */
+#define AMATCH_MX_LANGID 2147483647 /* Maximum rule ID */
+#define AMATCH_MX_COST 1000 /* Maximum single-rule cost */
+
+/*
+** A match or partial match
+*/
+struct amatch_word {
+ amatch_word *pNext; /* Next on a list of all amatch_words */
+ amatch_avl sCost; /* Linkage of this node into the cost tree */
+ amatch_avl sWord; /* Linkage of this node into the word tree */
+ amatch_cost rCost; /* Cost of the match so far */
+ int iSeq; /* Sequence number */
+ char zCost[10]; /* Cost key (text rendering of rCost) */
+ short int nMatch; /* Input characters matched */
+ char zWord[4]; /* Text of the word. Extra space appended as needed */
+};
+
+/*
+** Each transformation rule is stored as an instance of this object.
+** All rules are kept on a linked list sorted by rCost.
+*/
+struct amatch_rule {
+ amatch_rule *pNext; /* Next rule in order of increasing rCost */
+ char *zFrom; /* Transform from (a string from user input) */
+ amatch_cost rCost; /* Cost of this transformation */
+ amatch_langid iLang; /* The langauge to which this rule belongs */
+ amatch_len nFrom, nTo; /* Length of the zFrom and zTo strings */
+ char zTo[4]; /* Tranform to V.W value (extra space appended) */
+};
+
+/*
+** A amatch virtual-table object
+*/
+struct amatch_vtab {
+ sqlite3_vtab base; /* Base class - must be first */
+ char *zClassName; /* Name of this class. Default: "amatch" */
+ char *zDb; /* Name of database. (ex: "main") */
+ char *zSelf; /* Name of this virtual table */
+ char *zCostTab; /* Name of edit-cost-table */
+ char *zVocabTab; /* Name of vocabulary table */
+ char *zVocabWord; /* Name of vocabulary table word column */
+ char *zVocabLang; /* Name of vocabulary table language column */
+ amatch_rule *pRule; /* All active rules in this amatch */
+ amatch_cost rIns; /* Generic insertion cost '' -> ? */
+ amatch_cost rDel; /* Generic deletion cost ? -> '' */
+ amatch_cost rSub; /* Generic substitution cost ? -> ? */
+ sqlite3 *db; /* The database connection */
+ sqlite3_stmt *pVCheck; /* Query to check zVocabTab */
+ int nCursor; /* Number of active cursors */
+};
+
+/* A amatch cursor object */
+struct amatch_cursor {
+ sqlite3_vtab_cursor base; /* Base class - must be first */
+ sqlite3_int64 iRowid; /* The rowid of the current word */
+ amatch_langid iLang; /* Use this language ID */
+ amatch_cost rLimit; /* Maximum cost of any term */
+ int nBuf; /* Space allocated for zBuf */
+ int oomErr; /* True following an OOM error */
+ int nWord; /* Number of amatch_word objects */
+ char *zBuf; /* Temp-use buffer space */
+ char *zInput; /* Input word to match against */
+ amatch_vtab *pVtab; /* The virtual table this cursor belongs to */
+ amatch_word *pAllWords; /* List of all amatch_word objects */
+ amatch_word *pCurrent; /* Most recent solution */
+ amatch_avl *pCost; /* amatch_word objects keyed by iCost */
+ amatch_avl *pWord; /* amatch_word objects keyed by zWord */
+};
+
+/*
+** The two input rule lists are both sorted in order of increasing
+** cost. Merge them together into a single list, sorted by cost, and
+** return a pointer to the head of that list.
+*/
+static amatch_rule *amatchMergeRules(amatch_rule *pA, amatch_rule *pB){
+ amatch_rule head;
+ amatch_rule *pTail;
+
+ pTail = &head;
+ while( pA && pB ){
+ if( pA->rCost<=pB->rCost ){
+ pTail->pNext = pA;
+ pTail = pA;
+ pA = pA->pNext;
+ }else{
+ pTail->pNext = pB;
+ pTail = pB;
+ pB = pB->pNext;
+ }
+ }
+ if( pA==0 ){
+ pTail->pNext = pB;
+ }else{
+ pTail->pNext = pA;
+ }
+ return head.pNext;
+}
+
+/*
+** Statement pStmt currently points to a row in the amatch data table. This
+** function allocates and populates a amatch_rule structure according to
+** the content of the row.
+**
+** If successful, *ppRule is set to point to the new object and SQLITE_OK
+** is returned. Otherwise, *ppRule is zeroed, *pzErr may be set to point
+** to an error message and an SQLite error code returned.
+*/
+static int amatchLoadOneRule(
+ amatch_vtab *p, /* Fuzzer virtual table handle */
+ sqlite3_stmt *pStmt, /* Base rule on statements current row */
+ amatch_rule **ppRule, /* OUT: New rule object */
+ char **pzErr /* OUT: Error message */
+){
+ sqlite3_int64 iLang = sqlite3_column_int64(pStmt, 0);
+ const char *zFrom = (const char *)sqlite3_column_text(pStmt, 1);
+ const char *zTo = (const char *)sqlite3_column_text(pStmt, 2);
+ amatch_cost rCost = sqlite3_column_int(pStmt, 3);
+
+ int rc = SQLITE_OK; /* Return code */
+ int nFrom; /* Size of string zFrom, in bytes */
+ int nTo; /* Size of string zTo, in bytes */
+ amatch_rule *pRule = 0; /* New rule object to return */
+
+ if( zFrom==0 ) zFrom = "";
+ if( zTo==0 ) zTo = "";
+ nFrom = (int)strlen(zFrom);
+ nTo = (int)strlen(zTo);
+
+ /* Silently ignore null transformations */
+ if( strcmp(zFrom, zTo)==0 ){
+ if( zFrom[0]=='?' && zFrom[1]==0 ){
+ if( p->rSub==0 || p->rSub>rCost ) p->rSub = rCost;
+ }
+ *ppRule = 0;
+ return SQLITE_OK;
+ }
+
+ if( rCost<=0 || rCost>AMATCH_MX_COST ){
+ *pzErr = sqlite3_mprintf("%s: cost must be between 1 and %d",
+ p->zClassName, AMATCH_MX_COST
+ );
+ rc = SQLITE_ERROR;
+ }else
+ if( nFrom>AMATCH_MX_LENGTH || nTo>AMATCH_MX_LENGTH ){
+ *pzErr = sqlite3_mprintf("%s: maximum string length is %d",
+ p->zClassName, AMATCH_MX_LENGTH
+ );
+ rc = SQLITE_ERROR;
+ }else
+ if( iLang<0 || iLang>AMATCH_MX_LANGID ){
+ *pzErr = sqlite3_mprintf("%s: iLang must be between 0 and %d",
+ p->zClassName, AMATCH_MX_LANGID
+ );
+ rc = SQLITE_ERROR;
+ }else
+ if( strcmp(zFrom,"")==0 && strcmp(zTo,"?")==0 ){
+ if( p->rIns==0 || p->rIns>rCost ) p->rIns = rCost;
+ }else
+ if( strcmp(zFrom,"?")==0 && strcmp(zTo,"")==0 ){
+ if( p->rDel==0 || p->rDel>rCost ) p->rDel = rCost;
+ }else
+ {
+ pRule = sqlite3_malloc( sizeof(*pRule) + nFrom + nTo );
+ if( pRule==0 ){
+ rc = SQLITE_NOMEM;
+ }else{
+ memset(pRule, 0, sizeof(*pRule));
+ pRule->zFrom = &pRule->zTo[nTo+1];
+ pRule->nFrom = nFrom;
+ memcpy(pRule->zFrom, zFrom, nFrom+1);
+ memcpy(pRule->zTo, zTo, nTo+1);
+ pRule->nTo = nTo;
+ pRule->rCost = rCost;
+ pRule->iLang = (int)iLang;
+ }
+ }
+
+ *ppRule = pRule;
+ return rc;
+}
+
+/*
+** Free all the content in the edit-cost-table
+*/
+static void amatchFreeRules(amatch_vtab *p){
+ while( p->pRule ){
+ amatch_rule *pRule = p->pRule;
+ p->pRule = pRule->pNext;
+ sqlite3_free(pRule);
+ }
+ p->pRule = 0;
+}
+
+/*
+** Load the content of the amatch data table into memory.
+*/
+static int amatchLoadRules(
+ sqlite3 *db, /* Database handle */
+ amatch_vtab *p, /* Virtual amatch table to configure */
+ char **pzErr /* OUT: Error message */
+){
+ int rc = SQLITE_OK; /* Return code */
+ char *zSql; /* SELECT used to read from rules table */
+ amatch_rule *pHead = 0;
+
+ zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", p->zDb, p->zCostTab);
+ if( zSql==0 ){
+ rc = SQLITE_NOMEM;
+ }else{
+ int rc2; /* finalize() return code */
+ sqlite3_stmt *pStmt = 0;
+ rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
+ if( rc!=SQLITE_OK ){
+ *pzErr = sqlite3_mprintf("%s: %s", p->zClassName, sqlite3_errmsg(db));
+ }else if( sqlite3_column_count(pStmt)!=4 ){
+ *pzErr = sqlite3_mprintf("%s: %s has %d columns, expected 4",
+ p->zClassName, p->zCostTab, sqlite3_column_count(pStmt)
+ );
+ rc = SQLITE_ERROR;
+ }else{
+ while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
+ amatch_rule *pRule = 0;
+ rc = amatchLoadOneRule(p, pStmt, &pRule, pzErr);
+ if( pRule ){
+ pRule->pNext = pHead;
+ pHead = pRule;
+ }
+ }
+ }
+ rc2 = sqlite3_finalize(pStmt);
+ if( rc==SQLITE_OK ) rc = rc2;
+ }
+ sqlite3_free(zSql);
+
+ /* All rules are now in a singly linked list starting at pHead. This
+ ** block sorts them by cost and then sets amatch_vtab.pRule to point to
+ ** point to the head of the sorted list.
+ */
+ if( rc==SQLITE_OK ){
+ unsigned int i;
+ amatch_rule *pX;
+ amatch_rule *a[15];
+ for(i=0; i<sizeof(a)/sizeof(a[0]); i++) a[i] = 0;
+ while( (pX = pHead)!=0 ){
+ pHead = pX->pNext;
+ pX->pNext = 0;
+ for(i=0; a[i] && i<sizeof(a)/sizeof(a[0])-1; i++){
+ pX = amatchMergeRules(a[i], pX);
+ a[i] = 0;
+ }
+ a[i] = amatchMergeRules(a[i], pX);
+ }
+ for(pX=a[0], i=1; i<sizeof(a)/sizeof(a[0]); i++){
+ pX = amatchMergeRules(a[i], pX);
+ }
+ p->pRule = amatchMergeRules(p->pRule, pX);
+ }else{
+ /* An error has occurred. Setting p->pRule to point to the head of the
+ ** allocated list ensures that the list will be cleaned up in this case.
+ */
+ assert( p->pRule==0 );
+ p->pRule = pHead;
+ }
+
+ return rc;
+}
+
+/*
+** This function converts an SQL quoted string into an unquoted string
+** and returns a pointer to a buffer allocated using sqlite3_malloc()
+** containing the result. The caller should eventually free this buffer
+** using sqlite3_free.
+**
+** Examples:
+**
+** "abc" becomes abc
+** 'xyz' becomes xyz
+** [pqr] becomes pqr
+** `mno` becomes mno
+*/
+static char *amatchDequote(const char *zIn){
+ int nIn; /* Size of input string, in bytes */
+ char *zOut; /* Output (dequoted) string */
+
+ nIn = (int)strlen(zIn);
+ zOut = sqlite3_malloc(nIn+1);
+ if( zOut ){
+ char q = zIn[0]; /* Quote character (if any ) */
+
+ if( q!='[' && q!= '\'' && q!='"' && q!='`' ){
+ memcpy(zOut, zIn, nIn+1);
+ }else{
+ int iOut = 0; /* Index of next byte to write to output */
+ int iIn; /* Index of next byte to read from input */
+
+ if( q=='[' ) q = ']';
+ for(iIn=1; iIn<nIn; iIn++){
+ if( zIn[iIn]==q ) iIn++;
+ zOut[iOut++] = zIn[iIn];
+ }
+ }
+ assert( (int)strlen(zOut)<=nIn );
+ }
+ return zOut;
+}
+
+/*
+** Deallocate the pVCheck prepared statement.
+*/
+static void amatchVCheckClear(amatch_vtab *p){
+ if( p->pVCheck ){
+ sqlite3_finalize(p->pVCheck);
+ p->pVCheck = 0;
+ }
+}
+
+/*
+** Deallocate an amatch_vtab object
+*/
+static void amatchFree(amatch_vtab *p){
+ if( p ){
+ amatchFreeRules(p);
+ amatchVCheckClear(p);
+ sqlite3_free(p->zClassName);
+ sqlite3_free(p->zDb);
+ sqlite3_free(p->zCostTab);
+ sqlite3_free(p->zVocabTab);
+ sqlite3_free(p->zVocabWord);
+ sqlite3_free(p->zVocabLang);
+ memset(p, 0, sizeof(*p));
+ sqlite3_free(p);
+ }
+}
+
+/*
+** xDisconnect/xDestroy method for the amatch module.
+*/
+static int amatchDisconnect(sqlite3_vtab *pVtab){
+ amatch_vtab *p = (amatch_vtab*)pVtab;
+ assert( p->nCursor==0 );
+ amatchFree(p);
+ return SQLITE_OK;
+}
+
+/*
+** Check to see if the argument is of the form:
+**
+** KEY = VALUE
+**
+** If it is, return a pointer to the first character of VALUE.
+** If not, return NULL. Spaces around the = are ignored.
+*/
+static const char *amatchValueOfKey(const char *zKey, const char *zStr){
+ int nKey = (int)strlen(zKey);
+ int nStr = (int)strlen(zStr);
+ int i;
+ if( nStr<nKey+1 ) return 0;
+ if( memcmp(zStr, zKey, nKey)!=0 ) return 0;
+ for(i=nKey; isspace(zStr[i]); i++){}
+ if( zStr[i]!='=' ) return 0;
+ i++;
+ while( isspace(zStr[i]) ){ i++; }
+ return zStr+i;
+}
+
+/*
+** xConnect/xCreate method for the amatch module. Arguments are:
+**
+** argv[0] -> module name ("approximate_match")
+** argv[1] -> database name
+** argv[2] -> table name
+** argv[3...] -> arguments
+*/
+static int amatchConnect(
+ sqlite3 *db,
+ void *pAux,
+ int argc, const char *const*argv,
+ sqlite3_vtab **ppVtab,
+ char **pzErr
+){
+ int rc = SQLITE_OK; /* Return code */
+ amatch_vtab *pNew = 0; /* New virtual table */
+ const char *zModule = argv[0];
+ const char *zDb = argv[1];
+ const char *zVal;
+ int i;
+
+ (void)pAux;
+ *ppVtab = 0;
+ pNew = sqlite3_malloc( sizeof(*pNew) );
+ if( pNew==0 ) return SQLITE_NOMEM;
+ rc = SQLITE_NOMEM;
+ memset(pNew, 0, sizeof(*pNew));
+ pNew->db = db;
+ pNew->zClassName = sqlite3_mprintf("%s", zModule);
+ if( pNew->zClassName==0 ) goto amatchConnectError;
+ pNew->zDb = sqlite3_mprintf("%s", zDb);
+ if( pNew->zDb==0 ) goto amatchConnectError;
+ pNew->zSelf = sqlite3_mprintf("%s", argv[2]);
+ if( pNew->zSelf==0 ) goto amatchConnectError;
+ for(i=3; i<argc; i++){
+ zVal = amatchValueOfKey("vocabulary_table", argv[i]);
+ if( zVal ){
+ sqlite3_free(pNew->zVocabTab);
+ pNew->zVocabTab = amatchDequote(zVal);
+ if( pNew->zVocabTab==0 ) goto amatchConnectError;
+ continue;
+ }
+ zVal = amatchValueOfKey("vocabulary_word", argv[i]);
+ if( zVal ){
+ sqlite3_free(pNew->zVocabWord);
+ pNew->zVocabWord = amatchDequote(zVal);
+ if( pNew->zVocabWord==0 ) goto amatchConnectError;
+ continue;
+ }
+ zVal = amatchValueOfKey("vocabulary_language", argv[i]);
+ if( zVal ){
+ sqlite3_free(pNew->zVocabLang);
+ pNew->zVocabLang = amatchDequote(zVal);
+ if( pNew->zVocabLang==0 ) goto amatchConnectError;
+ continue;
+ }
+ zVal = amatchValueOfKey("edit_distances", argv[i]);
+ if( zVal ){
+ sqlite3_free(pNew->zCostTab);
+ pNew->zCostTab = amatchDequote(zVal);
+ if( pNew->zCostTab==0 ) goto amatchConnectError;
+ continue;
+ }
+ *pzErr = sqlite3_mprintf("unrecognized argument: [%s]\n", argv[i]);
+ amatchFree(pNew);
+ *ppVtab = 0;
+ return SQLITE_ERROR;
+ }
+ rc = SQLITE_OK;
+ if( pNew->zCostTab==0 ){
+ *pzErr = sqlite3_mprintf("no edit_distances table specified");
+ rc = SQLITE_ERROR;
+ }else{
+ rc = amatchLoadRules(db, pNew, pzErr);
+ }
+ if( rc==SQLITE_OK ){
+ rc = sqlite3_declare_vtab(db,
+ "CREATE TABLE x(word,distance,language,"
+ "command HIDDEN,nword HIDDEN)"
+ );
+#define AMATCH_COL_WORD 0
+#define AMATCH_COL_DISTANCE 1
+#define AMATCH_COL_LANGUAGE 2
+#define AMATCH_COL_COMMAND 3
+#define AMATCH_COL_NWORD 4
+ }
+ if( rc!=SQLITE_OK ){
+ amatchFree(pNew);
+ }
+ *ppVtab = &pNew->base;
+ return rc;
+
+amatchConnectError:
+ amatchFree(pNew);
+ return rc;
+}
+
+/*
+** Open a new amatch cursor.
+*/
+static int amatchOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
+ amatch_vtab *p = (amatch_vtab*)pVTab;
+ amatch_cursor *pCur;
+ pCur = sqlite3_malloc( sizeof(*pCur) );
+ if( pCur==0 ) return SQLITE_NOMEM;
+ memset(pCur, 0, sizeof(*pCur));
+ pCur->pVtab = p;
+ *ppCursor = &pCur->base;
+ p->nCursor++;
+ return SQLITE_OK;
+}
+
+/*
+** Free up all the memory allocated by a cursor. Set it rLimit to 0
+** to indicate that it is at EOF.
+*/
+static void amatchClearCursor(amatch_cursor *pCur){
+ amatch_word *pWord, *pNextWord;
+ for(pWord=pCur->pAllWords; pWord; pWord=pNextWord){
+ pNextWord = pWord->pNext;
+ sqlite3_free(pWord);
+ }
+ pCur->pAllWords = 0;
+ sqlite3_free(pCur->zInput);
+ pCur->zInput = 0;
+ pCur->pCost = 0;
+ pCur->pWord = 0;
+ pCur->pCurrent = 0;
+ pCur->rLimit = 1000000;
+ pCur->iLang = 0;
+ pCur->nWord = 0;
+}
+
+/*
+** Close a amatch cursor.
+*/
+static int amatchClose(sqlite3_vtab_cursor *cur){
+ amatch_cursor *pCur = (amatch_cursor *)cur;
+ amatchClearCursor(pCur);
+ pCur->pVtab->nCursor--;
+ sqlite3_free(pCur);
+ return SQLITE_OK;
+}
+
+/*
+** Render a 24-bit unsigned integer as a 4-byte base-64 number.
+*/
+static void amatchEncodeInt(int x, char *z){
+ static const char a[] =
+ "0123456789"
+ "ABCDEFGHIJ"
+ "KLMNOPQRST"
+ "UVWXYZ^abc"
+ "defghijklm"
+ "nopqrstuvw"
+ "xyz~";
+ z[0] = a[(x>>18)&0x3f];
+ z[1] = a[(x>>12)&0x3f];
+ z[2] = a[(x>>6)&0x3f];
+ z[3] = a[x&0x3f];
+}
+
+/*
+** Write the zCost[] field for a amatch_word object
+*/
+static void amatchWriteCost(amatch_word *pWord){
+ amatchEncodeInt(pWord->rCost, pWord->zCost);
+ amatchEncodeInt(pWord->iSeq, pWord->zCost+4);
+ pWord->zCost[8] = 0;
+}
+
+/*
+** Add a new amatch_word object to the queue.
+**
+** If a prior amatch_word object with the same zWord, and nMatch
+** already exists, update its rCost (if the new rCost is less) but
+** otherwise leave it unchanged. Do not add a duplicate.
+**
+** Do nothing if the cost exceeds threshold.
+*/
+static void amatchAddWord(
+ amatch_cursor *pCur,
+ amatch_cost rCost,
+ int nMatch,
+ const char *zWordBase,
+ const char *zWordTail
+){
+ amatch_word *pWord;
+ amatch_avl *pNode;
+ amatch_avl *pOther;
+ int nBase, nTail;
+ char zBuf[4];
+
+ if( rCost>pCur->rLimit ){
+ return;
+ }
+ nBase = (int)strlen(zWordBase);
+ nTail = (int)strlen(zWordTail);
+ if( nBase+nTail+3>pCur->nBuf ){
+ pCur->nBuf = nBase+nTail+100;
+ pCur->zBuf = sqlite3_realloc(pCur->zBuf, pCur->nBuf);
+ if( pCur->zBuf==0 ){
+ pCur->nBuf = 0;
+ return;
+ }
+ }
+ amatchEncodeInt(nMatch, zBuf);
+ memcpy(pCur->zBuf, zBuf+2, 2);
+ memcpy(pCur->zBuf+2, zWordBase, nBase);
+ memcpy(pCur->zBuf+2+nBase, zWordTail, nTail+1);
+ pNode = amatchAvlSearch(pCur->pWord, pCur->zBuf);
+ if( pNode ){
+ pWord = pNode->pWord;
+ if( pWord->rCost>rCost ){
+#ifdef AMATCH_TRACE_1
+ printf("UPDATE [%s][%.*s^%s] %d (\"%s\" \"%s\")\n",
+ pWord->zWord+2, pWord->nMatch, pCur->zInput, pCur->zInput,
+ pWord->rCost, pWord->zWord, pWord->zCost);
+#endif
+ amatchAvlRemove(&pCur->pCost, &pWord->sCost);
+ pWord->rCost = rCost;
+ amatchWriteCost(pWord);
+#ifdef AMATCH_TRACE_1
+ printf(" ---> %d (\"%s\" \"%s\")\n",
+ pWord->rCost, pWord->zWord, pWord->zCost);
+#endif
+ pOther = amatchAvlInsert(&pCur->pCost, &pWord->sCost);
+ assert( pOther==0 ); (void)pOther;
+ }
+ return;
+ }
+ pWord = sqlite3_malloc( sizeof(*pWord) + nBase + nTail - 1 );
+ if( pWord==0 ) return;
+ memset(pWord, 0, sizeof(*pWord));
+ pWord->rCost = rCost;
+ pWord->iSeq = pCur->nWord++;
+ amatchWriteCost(pWord);
+ pWord->nMatch = nMatch;
+ pWord->pNext = pCur->pAllWords;
+ pCur->pAllWords = pWord;
+ pWord->sCost.zKey = pWord->zCost;
+ pWord->sCost.pWord = pWord;
+ pOther = amatchAvlInsert(&pCur->pCost, &pWord->sCost);
+ assert( pOther==0 ); (void)pOther;
+ pWord->sWord.zKey = pWord->zWord;
+ pWord->sWord.pWord = pWord;
+ strcpy(pWord->zWord, pCur->zBuf);
+ pOther = amatchAvlInsert(&pCur->pWord, &pWord->sWord);
+ assert( pOther==0 ); (void)pOther;
+#ifdef AMATCH_TRACE_1
+ printf("INSERT [%s][%.*s^%s] %d (\"%s\" \"%s\")\n", pWord->zWord+2,
+ pWord->nMatch, pCur->zInput, pCur->zInput+pWord->nMatch, rCost,
+ pWord->zWord, pWord->zCost);
+#endif
+}
+
+/*
+** Advance a cursor to its next row of output
+*/
+static int amatchNext(sqlite3_vtab_cursor *cur){
+ amatch_cursor *pCur = (amatch_cursor*)cur;
+ amatch_word *pWord = 0;
+ amatch_avl *pNode;
+ int isMatch = 0;
+ amatch_vtab *p = pCur->pVtab;
+ int nWord;
+ int rc;
+ int i;
+ const char *zW;
+ amatch_rule *pRule;
+ char *zBuf = 0;
+ char nBuf = 0;
+ char zNext[8];
+ char zNextIn[8];
+ int nNextIn;
+
+ if( p->pVCheck==0 ){
+ char *zSql;
+ if( p->zVocabLang && p->zVocabLang[0] ){
+ zSql = sqlite3_mprintf(
+ "SELECT \"%s\" FROM \"%s\"",
+ " WHERE \"%w\">=?1 AND \"%w\"=?2"
+ " ORDER BY 1",
+ p->zVocabWord, p->zVocabTab,
+ p->zVocabWord, p->zVocabLang
+ );
+ }else{
+ zSql = sqlite3_mprintf(
+ "SELECT \"%s\" FROM \"%s\""
+ " WHERE \"%w\">=?1"
+ " ORDER BY 1",
+ p->zVocabWord, p->zVocabTab,
+ p->zVocabWord
+ );
+ }
+ rc = sqlite3_prepare_v2(p->db, zSql, -1, &p->pVCheck, 0);
+ sqlite3_free(zSql);
+ if( rc ) return rc;
+ }
+ sqlite3_bind_int(p->pVCheck, 2, pCur->iLang);
+
+ do{
+ pNode = amatchAvlFirst(pCur->pCost);
+ if( pNode==0 ){
+ pWord = 0;
+ break;
+ }
+ pWord = pNode->pWord;
+ amatchAvlRemove(&pCur->pCost, &pWord->sCost);
+
+#ifdef AMATCH_TRACE_1
+ printf("PROCESS [%s][%.*s^%s] %d (\"%s\" \"%s\")\n",
+ pWord->zWord+2, pWord->nMatch, pCur->zInput, pCur->zInput+pWord->nMatch,
+ pWord->rCost, pWord->zWord, pWord->zCost);
+#endif
+ nWord = (int)strlen(pWord->zWord+2);
+ if( nWord+20>nBuf ){
+ nBuf = nWord+100;
+ zBuf = sqlite3_realloc(zBuf, nBuf);
+ if( zBuf==0 ) return SQLITE_NOMEM;
+ }
+ strcpy(zBuf, pWord->zWord+2);
+ zNext[0] = 0;
+ zNextIn[0] = pCur->zInput[pWord->nMatch];
+ if( zNextIn[0] ){
+ for(i=1; i<=4 && (pCur->zInput[pWord->nMatch+i]&0xc0)==0x80; i++){
+ zNextIn[i] = pCur->zInput[pWord->nMatch+i];
+ }
+ zNextIn[i] = 0;
+ nNextIn = i;
+ }else{
+ nNextIn = 0;
+ }
+
+ if( zNextIn[0] && zNextIn[0]!='*' ){
+ sqlite3_reset(p->pVCheck);
+ strcat(zBuf, zNextIn);
+ sqlite3_bind_text(p->pVCheck, 1, zBuf, nWord+nNextIn, SQLITE_STATIC);
+ rc = sqlite3_step(p->pVCheck);
+ if( rc==SQLITE_ROW ){
+ zW = (const char*)sqlite3_column_text(p->pVCheck, 0);
+ if( strncmp(zBuf, zW, nWord+nNextIn)==0 ){
+ amatchAddWord(pCur, pWord->rCost, pWord->nMatch+nNextIn, zBuf, "");
+ }
+ }
+ zBuf[nWord] = 0;
+ }
+
+ while( 1 ){
+ strcpy(zBuf+nWord, zNext);
+ sqlite3_reset(p->pVCheck);
+ sqlite3_bind_text(p->pVCheck, 1, zBuf, -1, SQLITE_TRANSIENT);
+ rc = sqlite3_step(p->pVCheck);
+ if( rc!=SQLITE_ROW ) break;
+ zW = (const char*)sqlite3_column_text(p->pVCheck, 0);
+ strcpy(zBuf+nWord, zNext);
+ if( strncmp(zW, zBuf, nWord)!=0 ) break;
+ if( (zNextIn[0]=='*' && zNextIn[1]==0)
+ || (zNextIn[0]==0 && zW[nWord]==0)
+ ){
+ isMatch = 1;
+ zNextIn[0] = 0;
+ nNextIn = 0;
+ break;
+ }
+ zNext[0] = zW[nWord];
+ for(i=1; i<=4 && (zW[nWord+i]&0xc0)==0x80; i++){
+ zNext[i] = zW[nWord+i];
+ }
+ zNext[i] = 0;
+ zBuf[nWord] = 0;
+ if( p->rIns>0 ){
+ amatchAddWord(pCur, pWord->rCost+p->rIns, pWord->nMatch,
+ zBuf, zNext);
+ }
+ if( p->rSub>0 ){
+ amatchAddWord(pCur, pWord->rCost+p->rSub, pWord->nMatch+nNextIn,
+ zBuf, zNext);
+ }
+ if( p->rIns<0 && p->rSub<0 ) break;
+ zNext[i-1]++; /* FIX ME */
+ }
+ sqlite3_reset(p->pVCheck);
+
+ if( p->rDel>0 ){
+ zBuf[nWord] = 0;
+ amatchAddWord(pCur, pWord->rCost+p->rDel, pWord->nMatch+nNextIn,
+ zBuf, "");
+ }
+
+ for(pRule=p->pRule; pRule; pRule=pRule->pNext){
+ if( pRule->iLang!=pCur->iLang ) continue;
+ if( strncmp(pRule->zFrom, pCur->zInput+pWord->nMatch, pRule->nFrom)==0 ){
+ amatchAddWord(pCur, pWord->rCost+pRule->rCost,
+ pWord->nMatch+pRule->nFrom, pWord->zWord+2, pRule->zTo);
+ }
+ }
+ }while( !isMatch );
+ pCur->pCurrent = pWord;
+ sqlite3_free(zBuf);
+ return SQLITE_OK;
+}
+
+/*
+** Called to "rewind" a cursor back to the beginning so that
+** it starts its output over again. Always called at least once
+** prior to any amatchColumn, amatchRowid, or amatchEof call.
+*/
+static int amatchFilter(
+ sqlite3_vtab_cursor *pVtabCursor,
+ int idxNum, const char *idxStr,
+ int argc, sqlite3_value **argv
+){
+ amatch_cursor *pCur = (amatch_cursor *)pVtabCursor;
+ const char *zWord = "*";
+ int idx;
+
+ amatchClearCursor(pCur);
+ idx = 0;
+ if( idxNum & 1 ){
+ zWord = (const char*)sqlite3_value_text(argv[0]);
+ idx++;
+ }
+ if( idxNum & 2 ){
+ pCur->rLimit = (amatch_cost)sqlite3_value_int(argv[idx]);
+ idx++;
+ }
+ if( idxNum & 4 ){
+ pCur->iLang = (amatch_cost)sqlite3_value_int(argv[idx]);
+ idx++;
+ }
+ pCur->zInput = sqlite3_mprintf("%s", zWord);
+ if( pCur->zInput==0 ) return SQLITE_NOMEM;
+ amatchAddWord(pCur, 0, 0, "", "");
+ amatchNext(pVtabCursor);
+
+ return SQLITE_OK;
+}
+
+/*
+** Only the word and distance columns have values. All other columns
+** return NULL
+*/
+static int amatchColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
+ amatch_cursor *pCur = (amatch_cursor*)cur;
+ switch( i ){
+ case AMATCH_COL_WORD: {
+ sqlite3_result_text(ctx, pCur->pCurrent->zWord+2, -1, SQLITE_STATIC);
+ break;
+ }
+ case AMATCH_COL_DISTANCE: {
+ sqlite3_result_int(ctx, pCur->pCurrent->rCost);
+ break;
+ }
+ case AMATCH_COL_LANGUAGE: {
+ sqlite3_result_int(ctx, pCur->iLang);
+ break;
+ }
+ case AMATCH_COL_NWORD: {
+ sqlite3_result_int(ctx, pCur->nWord);
+ break;
+ }
+ default: {
+ sqlite3_result_null(ctx);
+ break;
+ }
+ }
+ return SQLITE_OK;
+}
+
+/*
+** The rowid.
+*/
+static int amatchRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
+ amatch_cursor *pCur = (amatch_cursor*)cur;
+ *pRowid = pCur->iRowid;
+ return SQLITE_OK;
+}
+
+/*
+** EOF indicator
+*/
+static int amatchEof(sqlite3_vtab_cursor *cur){
+ amatch_cursor *pCur = (amatch_cursor*)cur;
+ return pCur->pCurrent==0;
+}
+
+/*
+** Search for terms of these forms:
+**
+** (A) word MATCH $str
+** (B1) distance < $value
+** (B2) distance <= $value
+** (C) language == $language
+**
+** The distance< and distance<= are both treated as distance<=.
+** The query plan number is a bit vector:
+**
+** bit 1: Term of the form (A) found
+** bit 2: Term like (B1) or (B2) found
+** bit 3: Term like (C) found
+**
+** If bit-1 is set, $str is always in filter.argv[0]. If bit-2 is set
+** then $value is in filter.argv[0] if bit-1 is clear and is in
+** filter.argv[1] if bit-1 is set. If bit-3 is set, then $ruleid is
+** in filter.argv[0] if bit-1 and bit-2 are both zero, is in
+** filter.argv[1] if exactly one of bit-1 and bit-2 are set, and is in
+** filter.argv[2] if both bit-1 and bit-2 are set.
+*/
+static int amatchBestIndex(
+ sqlite3_vtab *tab,
+ sqlite3_index_info *pIdxInfo
+){
+ int iPlan = 0;
+ int iDistTerm = -1;
+ int iLangTerm = -1;
+ int i;
+ const struct sqlite3_index_constraint *pConstraint;
+
+ (void)tab;
+ pConstraint = pIdxInfo->aConstraint;
+ for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
+ if( pConstraint->usable==0 ) continue;
+ if( (iPlan & 1)==0
+ && pConstraint->iColumn==0
+ && pConstraint->op==SQLITE_INDEX_CONSTRAINT_MATCH
+ ){
+ iPlan |= 1;
+ pIdxInfo->aConstraintUsage[i].argvIndex = 1;
+ pIdxInfo->aConstraintUsage[i].omit = 1;
+ }
+ if( (iPlan & 2)==0
+ && pConstraint->iColumn==1
+ && (pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT
+ || pConstraint->op==SQLITE_INDEX_CONSTRAINT_LE)
+ ){
+ iPlan |= 2;
+ iDistTerm = i;
+ }
+ if( (iPlan & 4)==0
+ && pConstraint->iColumn==2
+ && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ
+ ){
+ iPlan |= 4;
+ pIdxInfo->aConstraintUsage[i].omit = 1;
+ iLangTerm = i;
+ }
+ }
+ if( iPlan & 2 ){
+ pIdxInfo->aConstraintUsage[iDistTerm].argvIndex = 1+((iPlan&1)!=0);
+ }
+ if( iPlan & 4 ){
+ int idx = 1;
+ if( iPlan & 1 ) idx++;
+ if( iPlan & 2 ) idx++;
+ pIdxInfo->aConstraintUsage[iLangTerm].argvIndex = idx;
+ }
+ pIdxInfo->idxNum = iPlan;
+ if( pIdxInfo->nOrderBy==1
+ && pIdxInfo->aOrderBy[0].iColumn==1
+ && pIdxInfo->aOrderBy[0].desc==0
+ ){
+ pIdxInfo->orderByConsumed = 1;
+ }
+ pIdxInfo->estimatedCost = (double)10000;
+
+ return SQLITE_OK;
+}
+
+/*
+** The xUpdate() method.
+**
+** This implementation disallows DELETE and UPDATE. The only thing
+** allowed is INSERT into the "command" column.
+*/
+static int amatchUpdate(
+ sqlite3_vtab *pVTab,
+ int argc,
+ sqlite3_value **argv,
+ sqlite_int64 *pRowid
+){
+ amatch_vtab *p = (amatch_vtab*)pVTab;
+ const unsigned char *zCmd;
+ (void)pRowid;
+ if( argc==1 ){
+ pVTab->zErrMsg = sqlite3_mprintf("DELETE from %s is not allowed",
+ p->zSelf);
+ return SQLITE_ERROR;
+ }
+ if( sqlite3_value_type(argv[0])!=SQLITE_NULL ){
+ pVTab->zErrMsg = sqlite3_mprintf("UPDATE of %s is not allowed",
+ p->zSelf);
+ return SQLITE_ERROR;
+ }
+ if( sqlite3_value_type(argv[2+AMATCH_COL_WORD])!=SQLITE_NULL
+ || sqlite3_value_type(argv[2+AMATCH_COL_DISTANCE])!=SQLITE_NULL
+ || sqlite3_value_type(argv[2+AMATCH_COL_LANGUAGE])!=SQLITE_NULL
+ ){
+ pVTab->zErrMsg = sqlite3_mprintf(
+ "INSERT INTO %s allowed for column [command] only", p->zSelf);
+ return SQLITE_ERROR;
+ }
+ zCmd = sqlite3_value_text(argv[2+AMATCH_COL_COMMAND]);
+ if( zCmd==0 ) return SQLITE_OK;
+
+ return SQLITE_OK;
+}
+
+/*
+** A virtual table module that implements the "approximate_match".
+*/
+static sqlite3_module amatchModule = {
+ 0, /* iVersion */
+ amatchConnect, /* xCreate */
+ amatchConnect, /* xConnect */
+ amatchBestIndex, /* xBestIndex */
+ amatchDisconnect, /* xDisconnect */
+ amatchDisconnect, /* xDestroy */
+ amatchOpen, /* xOpen - open a cursor */
+ amatchClose, /* xClose - close a cursor */
+ amatchFilter, /* xFilter - configure scan constraints */
+ amatchNext, /* xNext - advance a cursor */
+ amatchEof, /* xEof - check for end of scan */
+ amatchColumn, /* xColumn - read data */
+ amatchRowid, /* xRowid - read data */
+ amatchUpdate, /* xUpdate */
+ 0, /* xBegin */
+ 0, /* xSync */
+ 0, /* xCommit */
+ 0, /* xRollback */
+ 0, /* xFindMethod */
+ 0, /* xRename */
+ 0, /* xSavepoint */
+ 0, /* xRelease */
+ 0 /* xRollbackTo */
+};
+
+#endif /* SQLITE_OMIT_VIRTUALTABLE */
+
+/*
+** Register the amatch virtual table
+*/
+#ifdef _WIN32
+__declspec(dllexport)
+#endif
+int sqlite3_amatch_init(
+ sqlite3 *db,
+ char **pzErrMsg,
+ const sqlite3_api_routines *pApi
+){
+ int rc = SQLITE_OK;
+ SQLITE_EXTENSION_INIT2(pApi);
+ (void)pzErrMsg; /* Not used */
+#ifndef SQLITE_OMIT_VIRTUALTABLE
+ rc = sqlite3_create_module(db, "approximate_match", &amatchModule, 0);
+#endif /* SQLITE_OMIT_VIRTUALTABLE */
+ return rc;
+}
diff --git a/ext/misc/closure.c b/ext/misc/closure.c
new file mode 100644
index 0000000..213b763
--- /dev/null
+++ b/ext/misc/closure.c
@@ -0,0 +1,948 @@
+/*
+** 2013-04-16
+**
+** 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 file contains code for a virtual table that finds the transitive
+** closure of a parent/child relationship in a real table. The virtual
+** table is called "transitive_closure".
+**
+** A transitive_closure virtual table is created like this:
+**
+** CREATE VIRTUAL TABLE x USING transitive_closure(
+** tablename=<tablename>, -- T
+** idcolumn=<columnname>, -- X
+** parentcolumn=<columnname> -- P
+** );
+**
+** When it is created, the new transitive_closure table may be supplied
+** with default values for the name of a table T and columns T.X and T.P.
+** The T.X and T.P columns must contain integers. The ideal case is for
+** T.X to be the INTEGER PRIMARY KEY. The T.P column should reference
+** the T.X column. The row referenced by T.P is the parent of the current row.
+**
+** The tablename, idcolumn, and parentcolumn supplied by the CREATE VIRTUAL
+** TABLE statement may be overridden in individual queries by including
+** terms like tablename='newtable', idcolumn='id2', or
+** parentcolumn='parent3' in the WHERE clause of the query.
+**
+** For efficiency, it is essential that there be an index on the P column:
+**
+** CREATE Tidx1 ON T(P)
+**
+** Suppose a specific instance of the closure table is as follows:
+**
+** CREATE VIRTUAL TABLE ct1 USING transitive_closure(
+** tablename='group',
+** idcolumn='groupId',
+** parentcolumn='parentId'
+** );
+**
+** Such an instance of the transitive_closure virtual table would be
+** appropriate for walking a tree defined using a table like this, for example:
+**
+** CREATE TABLE group(
+** groupId INTEGER PRIMARY KEY,
+** parentId INTEGER REFERENCES group
+** );
+** CREATE INDEX group_idx1 ON group(parentId);
+**
+** The group table above would presumably have other application-specific
+** fields. The key point here is that rows of the group table form a
+** tree. The purpose of the ct1 virtual table is to easily extract
+** branches of that tree.
+**
+** Once it has been created, the ct1 virtual table can be queried
+** as follows:
+**
+** SELECT * FROM element
+** WHERE element.groupId IN (SELECT id FROM ct1 WHERE root=?1);
+**
+** The above query will return all elements that are part of group ?1
+** or children of group ?1 or grand-children of ?1 and so forth for all
+** descendents of group ?1. The same query can be formulated as a join:
+**
+** SELECT element.* FROM element, ct1
+** WHERE element.groupid=ct1.id
+** AND ct1.root=?1;
+**
+** The depth of the transitive_closure (the number of generations of
+** parent/child relations to follow) can be limited by setting "depth"
+** column in the WHERE clause. So, for example, the following query
+** finds only children and grandchildren but no further descendents:
+**
+** SELECT element.* FROM element, ct1
+** WHERE element.groupid=ct1.id
+** AND ct1.root=?1
+** AND ct1.depth<=2;
+**
+** The "ct1.depth<=2" term could be a strict equality "ct1.depth=2" in
+** order to find only the grandchildren of ?1, not ?1 itself or the
+** children of ?1.
+**
+** The root=?1 term must be supplied in WHERE clause or else the query
+** of the ct1 virtual table will return an empty set. The tablename,
+** idcolumn, and parentcolumn attributes can be overridden in the WHERE
+** clause if desired. So, for example, the ct1 table could be repurposed
+** to find ancestors rather than descendents by inverting the roles of
+** the idcolumn and parentcolumn:
+**
+** SELECT element.* FROM element, ct1
+** WHERE element.groupid=ct1.id
+** AND ct1.root=?1
+** AND ct1.idcolumn='parentId'
+** AND ct1.parentcolumn='groupId';
+**
+** Multiple calls to ct1 could be combined. For example, the following
+** query finds all elements that "cousins" of groupId ?1. That is to say
+** elements where the groupId is a grandchild of the grandparent of ?1.
+** (This definition of "cousins" also includes siblings and self.)
+**
+** SELECT element.* FROM element, ct1
+** WHERE element.groupId=ct1.id
+** AND ct1.depth=2
+** AND ct1.root IN (SELECT id FROM ct1
+** WHERE root=?1
+** AND depth=2
+** AND idcolumn='parentId'
+** AND parentcolumn='groupId');
+**
+** In our example, the group.groupId column is unique and thus the
+** subquery will return exactly one row. For that reason, the IN
+** operator could be replaced by "=" to get the same result. But
+** in the general case where the idcolumn is not unique, an IN operator
+** would be required for this kind of query.
+**
+** Note that because the tablename, idcolumn, and parentcolumn can
+** all be specified in the query, it is possible for an application
+** to define a single transitive_closure virtual table for use on lots
+** of different hierarchy tables. One might say:
+**
+** CREATE VIRTUAL TABLE temp.closure USING transitive_closure;
+**
+** As each database connection is being opened. Then the application
+** would always have a "closure" virtual table handy to use for querying.
+**
+** SELECT element.* FROM element, closure
+** WHERE element.groupid=ct1.id
+** AND closure.root=?1
+** AND closure.tablename='group'
+** AND closure.idname='groupId'
+** AND closure.parentname='parentId';
+**
+** See the documentation at http://www.sqlite.org/loadext.html for information
+** on how to compile and use loadable extensions such as this one.
+*/
+#include "sqlite3ext.h"
+SQLITE_EXTENSION_INIT1
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <stdio.h>
+#include <ctype.h>
+
+#ifndef SQLITE_OMIT_VIRTUALTABLE
+
+/*
+** Forward declaration of objects used by this implementation
+*/
+typedef struct closure_vtab closure_vtab;
+typedef struct closure_cursor closure_cursor;
+typedef struct closure_queue closure_queue;
+typedef struct closure_avl closure_avl;
+
+/*****************************************************************************
+** AVL Tree implementation
+*/
+/*
+** Objects that want to be members of the AVL tree should embedded an
+** instance of this structure.
+*/
+struct closure_avl {
+ sqlite3_int64 id; /* Id of this entry in the table */
+ int iGeneration; /* Which generation is this entry part of */
+ closure_avl *pList; /* A linked list of nodes */
+ closure_avl *pBefore; /* Other elements less than id */
+ closure_avl *pAfter; /* Other elements greater than id */
+ closure_avl *pUp; /* Parent element */
+ short int height; /* Height of this node. Leaf==1 */
+ short int imbalance; /* Height difference between pBefore and pAfter */
+};
+
+/* Recompute the closure_avl.height and closure_avl.imbalance fields for p.
+** Assume that the children of p have correct heights.
+*/
+static void closureAvlRecomputeHeight(closure_avl *p){
+ short int hBefore = p->pBefore ? p->pBefore->height : 0;
+ short int hAfter = p->pAfter ? p->pAfter->height : 0;
+ p->imbalance = hBefore - hAfter; /* -: pAfter higher. +: pBefore higher */
+ p->height = (hBefore>hAfter ? hBefore : hAfter)+1;
+}
+
+/*
+** P B
+** / \ / \
+** B Z ==> X P
+** / \ / \
+** X Y Y Z
+**
+*/
+static closure_avl *closureAvlRotateBefore(closure_avl *pP){
+ closure_avl *pB = pP->pBefore;
+ closure_avl *pY = pB->pAfter;
+ pB->pUp = pP->pUp;
+ pB->pAfter = pP;
+ pP->pUp = pB;
+ pP->pBefore = pY;
+ if( pY ) pY->pUp = pP;
+ closureAvlRecomputeHeight(pP);
+ closureAvlRecomputeHeight(pB);
+ return pB;
+}
+
+/*
+** P A
+** / \ / \
+** X A ==> P Z
+** / \ / \
+** Y Z X Y
+**
+*/
+static closure_avl *closureAvlRotateAfter(closure_avl *pP){
+ closure_avl *pA = pP->pAfter;
+ closure_avl *pY = pA->pBefore;
+ pA->pUp = pP->pUp;
+ pA->pBefore = pP;
+ pP->pUp = pA;
+ pP->pAfter = pY;
+ if( pY ) pY->pUp = pP;
+ closureAvlRecomputeHeight(pP);
+ closureAvlRecomputeHeight(pA);
+ return pA;
+}
+
+/*
+** Return a pointer to the pBefore or pAfter pointer in the parent
+** of p that points to p. Or if p is the root node, return pp.
+*/
+static closure_avl **closureAvlFromPtr(closure_avl *p, closure_avl **pp){
+ closure_avl *pUp = p->pUp;
+ if( pUp==0 ) return pp;
+ if( pUp->pAfter==p ) return &pUp->pAfter;
+ return &pUp->pBefore;
+}
+
+/*
+** Rebalance all nodes starting with p and working up to the root.
+** Return the new root.
+*/
+static closure_avl *closureAvlBalance(closure_avl *p){
+ closure_avl *pTop = p;
+ closure_avl **pp;
+ while( p ){
+ closureAvlRecomputeHeight(p);
+ if( p->imbalance>=2 ){
+ closure_avl *pB = p->pBefore;
+ if( pB->imbalance<0 ) p->pBefore = closureAvlRotateAfter(pB);
+ pp = closureAvlFromPtr(p,&p);
+ p = *pp = closureAvlRotateBefore(p);
+ }else if( p->imbalance<=(-2) ){
+ closure_avl *pA = p->pAfter;
+ if( pA->imbalance>0 ) p->pAfter = closureAvlRotateBefore(pA);
+ pp = closureAvlFromPtr(p,&p);
+ p = *pp = closureAvlRotateAfter(p);
+ }
+ pTop = p;
+ p = p->pUp;
+ }
+ return pTop;
+}
+
+/* Search the tree rooted at p for an entry with id. Return a pointer
+** to the entry or return NULL.
+*/
+static closure_avl *closureAvlSearch(closure_avl *p, sqlite3_int64 id){
+ while( p && id!=p->id ){
+ p = (id<p->id) ? p->pBefore : p->pAfter;
+ }
+ return p;
+}
+
+/* Find the first node (the one with the smallest key).
+*/
+static closure_avl *closureAvlFirst(closure_avl *p){
+ if( p ) while( p->pBefore ) p = p->pBefore;
+ return p;
+}
+
+/* Return the node with the next larger key after p.
+*/
+closure_avl *closureAvlNext(closure_avl *p){
+ closure_avl *pPrev = 0;
+ while( p && p->pAfter==pPrev ){
+ pPrev = p;
+ p = p->pUp;
+ }
+ if( p && pPrev==0 ){
+ p = closureAvlFirst(p->pAfter);
+ }
+ return p;
+}
+
+/* Insert a new node pNew. Return NULL on success. If the key is not
+** unique, then do not perform the insert but instead leave pNew unchanged
+** and return a pointer to an existing node with the same key.
+*/
+static closure_avl *closureAvlInsert(
+ closure_avl **ppHead, /* Head of the tree */
+ closure_avl *pNew /* New node to be inserted */
+){
+ closure_avl *p = *ppHead;
+ if( p==0 ){
+ p = pNew;
+ pNew->pUp = 0;
+ }else{
+ while( p ){
+ if( pNew->id<p->id ){
+ if( p->pBefore ){
+ p = p->pBefore;
+ }else{
+ p->pBefore = pNew;
+ pNew->pUp = p;
+ break;
+ }
+ }else if( pNew->id>p->id ){
+ if( p->pAfter ){
+ p = p->pAfter;
+ }else{
+ p->pAfter = pNew;
+ pNew->pUp = p;
+ break;
+ }
+ }else{
+ return p;
+ }
+ }
+ }
+ pNew->pBefore = 0;
+ pNew->pAfter = 0;
+ pNew->height = 1;
+ pNew->imbalance = 0;
+ *ppHead = closureAvlBalance(p);
+ return 0;
+}
+
+/* Walk the tree can call xDestroy on each node
+*/
+static void closureAvlDestroy(closure_avl *p, void (*xDestroy)(closure_avl*)){
+ if( p ){
+ closureAvlDestroy(p->pBefore, xDestroy);
+ closureAvlDestroy(p->pAfter, xDestroy);
+ xDestroy(p);
+ }
+}
+/*
+** End of the AVL Tree implementation
+******************************************************************************/
+
+/*
+** A closure virtual-table object
+*/
+struct closure_vtab {
+ sqlite3_vtab base; /* Base class - must be first */
+ char *zDb; /* Name of database. (ex: "main") */
+ char *zSelf; /* Name of this virtual table */
+ char *zTableName; /* Name of table holding parent/child relation */
+ char *zIdColumn; /* Name of ID column of zTableName */
+ char *zParentColumn; /* Name of PARENT column in zTableName */
+ sqlite3 *db; /* The database connection */
+ int nCursor; /* Number of pending cursors */
+};
+
+/* A closure cursor object */
+struct closure_cursor {
+ sqlite3_vtab_cursor base; /* Base class - must be first */
+ closure_vtab *pVtab; /* The virtual table this cursor belongs to */
+ char *zTableName; /* Name of table holding parent/child relation */
+ char *zIdColumn; /* Name of ID column of zTableName */
+ char *zParentColumn; /* Name of PARENT column in zTableName */
+ closure_avl *pCurrent; /* Current element of output */
+ closure_avl *pClosure; /* The complete closure tree */
+};
+
+/* A queue of AVL nodes */
+struct closure_queue {
+ closure_avl *pFirst; /* Oldest node on the queue */
+ closure_avl *pLast; /* Youngest node on the queue */
+};
+
+/*
+** Add a node to the end of the queue
+*/
+static void queuePush(closure_queue *pQueue, closure_avl *pNode){
+ pNode->pList = 0;
+ if( pQueue->pLast ){
+ pQueue->pLast->pList = pNode;
+ }else{
+ pQueue->pFirst = pNode;
+ }
+ pQueue->pLast = pNode;
+}
+
+/*
+** Extract the oldest element (the front element) from the queue.
+*/
+static closure_avl *queuePull(closure_queue *pQueue){
+ closure_avl *p = pQueue->pFirst;
+ if( p ){
+ pQueue->pFirst = p->pList;
+ if( pQueue->pFirst==0 ) pQueue->pLast = 0;
+ }
+ return p;
+}
+
+/*
+** This function converts an SQL quoted string into an unquoted string
+** and returns a pointer to a buffer allocated using sqlite3_malloc()
+** containing the result. The caller should eventually free this buffer
+** using sqlite3_free.
+**
+** Examples:
+**
+** "abc" becomes abc
+** 'xyz' becomes xyz
+** [pqr] becomes pqr
+** `mno` becomes mno
+*/
+static char *closureDequote(const char *zIn){
+ int nIn; /* Size of input string, in bytes */
+ char *zOut; /* Output (dequoted) string */
+
+ nIn = (int)strlen(zIn);
+ zOut = sqlite3_malloc(nIn+1);
+ if( zOut ){
+ char q = zIn[0]; /* Quote character (if any ) */
+
+ if( q!='[' && q!= '\'' && q!='"' && q!='`' ){
+ memcpy(zOut, zIn, nIn+1);
+ }else{
+ int iOut = 0; /* Index of next byte to write to output */
+ int iIn; /* Index of next byte to read from input */
+
+ if( q=='[' ) q = ']';
+ for(iIn=1; iIn<nIn; iIn++){
+ if( zIn[iIn]==q ) iIn++;
+ zOut[iOut++] = zIn[iIn];
+ }
+ }
+ assert( (int)strlen(zOut)<=nIn );
+ }
+ return zOut;
+}
+
+/*
+** Deallocate an closure_vtab object
+*/
+static void closureFree(closure_vtab *p){
+ if( p ){
+ sqlite3_free(p->zDb);
+ sqlite3_free(p->zSelf);
+ sqlite3_free(p->zTableName);
+ sqlite3_free(p->zIdColumn);
+ sqlite3_free(p->zParentColumn);
+ memset(p, 0, sizeof(*p));
+ sqlite3_free(p);
+ }
+}
+
+/*
+** xDisconnect/xDestroy method for the closure module.
+*/
+static int closureDisconnect(sqlite3_vtab *pVtab){
+ closure_vtab *p = (closure_vtab*)pVtab;
+ assert( p->nCursor==0 );
+ closureFree(p);
+ return SQLITE_OK;
+}
+
+/*
+** Check to see if the argument is of the form:
+**
+** KEY = VALUE
+**
+** If it is, return a pointer to the first character of VALUE.
+** If not, return NULL. Spaces around the = are ignored.
+*/
+static const char *closureValueOfKey(const char *zKey, const char *zStr){
+ int nKey = (int)strlen(zKey);
+ int nStr = (int)strlen(zStr);
+ int i;
+ if( nStr<nKey+1 ) return 0;
+ if( memcmp(zStr, zKey, nKey)!=0 ) return 0;
+ for(i=nKey; isspace(zStr[i]); i++){}
+ if( zStr[i]!='=' ) return 0;
+ i++;
+ while( isspace(zStr[i]) ){ i++; }
+ return zStr+i;
+}
+
+/*
+** xConnect/xCreate method for the closure module. Arguments are:
+**
+** argv[0] -> module name ("approximate_match")
+** argv[1] -> database name
+** argv[2] -> table name
+** argv[3...] -> arguments
+*/
+static int closureConnect(
+ sqlite3 *db,
+ void *pAux,
+ int argc, const char *const*argv,
+ sqlite3_vtab **ppVtab,
+ char **pzErr
+){
+ int rc = SQLITE_OK; /* Return code */
+ closure_vtab *pNew = 0; /* New virtual table */
+ const char *zDb = argv[1];
+ const char *zVal;
+ int i;
+
+ (void)pAux;
+ *ppVtab = 0;
+ pNew = sqlite3_malloc( sizeof(*pNew) );
+ if( pNew==0 ) return SQLITE_NOMEM;
+ rc = SQLITE_NOMEM;
+ memset(pNew, 0, sizeof(*pNew));
+ pNew->db = db;
+ pNew->zDb = sqlite3_mprintf("%s", zDb);
+ if( pNew->zDb==0 ) goto closureConnectError;
+ pNew->zSelf = sqlite3_mprintf("%s", argv[2]);
+ if( pNew->zSelf==0 ) goto closureConnectError;
+ for(i=3; i<argc; i++){
+ zVal = closureValueOfKey("tablename", argv[i]);
+ if( zVal ){
+ sqlite3_free(pNew->zTableName);
+ pNew->zTableName = closureDequote(zVal);
+ if( pNew->zTableName==0 ) goto closureConnectError;
+ continue;
+ }
+ zVal = closureValueOfKey("idcolumn", argv[i]);
+ if( zVal ){
+ sqlite3_free(pNew->zIdColumn);
+ pNew->zIdColumn = closureDequote(zVal);
+ if( pNew->zIdColumn==0 ) goto closureConnectError;
+ continue;
+ }
+ zVal = closureValueOfKey("parentcolumn", argv[i]);
+ if( zVal ){
+ sqlite3_free(pNew->zParentColumn);
+ pNew->zParentColumn = closureDequote(zVal);
+ if( pNew->zParentColumn==0 ) goto closureConnectError;
+ continue;
+ }
+ *pzErr = sqlite3_mprintf("unrecognized argument: [%s]\n", argv[i]);
+ closureFree(pNew);
+ *ppVtab = 0;
+ return SQLITE_ERROR;
+ }
+ rc = sqlite3_declare_vtab(db,
+ "CREATE TABLE x(id,depth,root HIDDEN,tablename HIDDEN,"
+ "idcolumn HIDDEN,parentcolumn HIDDEN)"
+ );
+#define CLOSURE_COL_ID 0
+#define CLOSURE_COL_DEPTH 1
+#define CLOSURE_COL_ROOT 2
+#define CLOSURE_COL_TABLENAME 3
+#define CLOSURE_COL_IDCOLUMN 4
+#define CLOSURE_COL_PARENTCOLUMN 5
+ if( rc!=SQLITE_OK ){
+ closureFree(pNew);
+ }
+ *ppVtab = &pNew->base;
+ return rc;
+
+closureConnectError:
+ closureFree(pNew);
+ return rc;
+}
+
+/*
+** Open a new closure cursor.
+*/
+static int closureOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
+ closure_vtab *p = (closure_vtab*)pVTab;
+ closure_cursor *pCur;
+ pCur = sqlite3_malloc( sizeof(*pCur) );
+ if( pCur==0 ) return SQLITE_NOMEM;
+ memset(pCur, 0, sizeof(*pCur));
+ pCur->pVtab = p;
+ *ppCursor = &pCur->base;
+ p->nCursor++;
+ return SQLITE_OK;
+}
+
+/*
+** Free up all the memory allocated by a cursor. Set it rLimit to 0
+** to indicate that it is at EOF.
+*/
+static void closureClearCursor(closure_cursor *pCur){
+ closureAvlDestroy(pCur->pClosure, (void(*)(closure_avl*))sqlite3_free);
+ sqlite3_free(pCur->zTableName);
+ sqlite3_free(pCur->zIdColumn);
+ sqlite3_free(pCur->zParentColumn);
+ pCur->zTableName = 0;
+ pCur->zIdColumn = 0;
+ pCur->zParentColumn = 0;
+ pCur->pCurrent = 0;
+ pCur->pClosure = 0;
+}
+
+/*
+** Close a closure cursor.
+*/
+static int closureClose(sqlite3_vtab_cursor *cur){
+ closure_cursor *pCur = (closure_cursor *)cur;
+ closureClearCursor(pCur);
+ pCur->pVtab->nCursor--;
+ sqlite3_free(pCur);
+ return SQLITE_OK;
+}
+
+/*
+** Advance a cursor to its next row of output
+*/
+static int closureNext(sqlite3_vtab_cursor *cur){
+ closure_cursor *pCur = (closure_cursor*)cur;
+ pCur->pCurrent = closureAvlNext(pCur->pCurrent);
+ return SQLITE_OK;
+}
+
+/*
+** Allocate and insert a node
+*/
+static int closureInsertNode(
+ closure_queue *pQueue, /* Add new node to this queue */
+ closure_cursor *pCur, /* The cursor into which to add the node */
+ sqlite3_int64 id, /* The node ID */
+ int iGeneration /* The generation number for this node */
+){
+ closure_avl *pNew = sqlite3_malloc( sizeof(*pNew) );
+ if( pNew==0 ) return SQLITE_NOMEM;
+ memset(pNew, 0, sizeof(*pNew));
+ pNew->id = id;
+ pNew->iGeneration = iGeneration;
+ closureAvlInsert(&pCur->pClosure, pNew);
+ queuePush(pQueue, pNew);
+ return SQLITE_OK;
+}
+
+/*
+** Called to "rewind" a cursor back to the beginning so that
+** it starts its output over again. Always called at least once
+** prior to any closureColumn, closureRowid, or closureEof call.
+**
+** This routine actually computes the closure.
+**
+** See the comment at the beginning of closureBestIndex() for a
+** description of the meaning of idxNum. The idxStr parameter is
+** not used.
+*/
+static int closureFilter(
+ sqlite3_vtab_cursor *pVtabCursor,
+ int idxNum, const char *idxStr,
+ int argc, sqlite3_value **argv
+){
+ closure_cursor *pCur = (closure_cursor *)pVtabCursor;
+ closure_vtab *pVtab = pCur->pVtab;
+ sqlite3_int64 iRoot;
+ int mxGen = 999999999;
+ char *zSql;
+ sqlite3_stmt *pStmt;
+ closure_avl *pAvl;
+ int rc = SQLITE_OK;
+ const char *zTableName = pVtab->zTableName;
+ const char *zIdColumn = pVtab->zIdColumn;
+ const char *zParentColumn = pVtab->zParentColumn;
+ closure_queue sQueue;
+
+ (void)idxStr; /* Unused parameter */
+ (void)argc; /* Unused parameter */
+ closureClearCursor(pCur);
+ memset(&sQueue, 0, sizeof(sQueue));
+ if( (idxNum & 1)==0 ){
+ /* No root=$root in the WHERE clause. Return an empty set */
+ return SQLITE_OK;
+ }
+ iRoot = sqlite3_value_int64(argv[0]);
+ if( (idxNum & 0x000f0)!=0 ){
+ mxGen = sqlite3_value_int(argv[(idxNum>>4)&0x0f]);
+ if( (idxNum & 0x00002)!=0 ) mxGen--;
+ }
+ if( (idxNum & 0x00f00)!=0 ){
+ zTableName = (const char*)sqlite3_value_text(argv[(idxNum>>8)&0x0f]);
+ pCur->zTableName = sqlite3_mprintf("%s", zTableName);
+ }
+ if( (idxNum & 0x0f000)!=0 ){
+ zIdColumn = (const char*)sqlite3_value_text(argv[(idxNum>>12)&0x0f]);
+ pCur->zIdColumn = sqlite3_mprintf("%s", zIdColumn);
+ }
+ if( (idxNum & 0x0f0000)!=0 ){
+ zParentColumn = (const char*)sqlite3_value_text(argv[(idxNum>>16)&0x0f]);
+ pCur->zParentColumn = sqlite3_mprintf("%s", zParentColumn);
+ }
+
+ zSql = sqlite3_mprintf(
+ "SELECT \"%w\".\"%w\" FROM \"%w\" WHERE \"%w\".\"%w\"=?1",
+ zTableName, zIdColumn, zTableName, zTableName, zParentColumn);
+ if( zSql==0 ){
+ return SQLITE_NOMEM;
+ }else{
+ rc = sqlite3_prepare_v2(pVtab->db, zSql, -1, &pStmt, 0);
+ sqlite3_free(zSql);
+ if( rc ){
+ sqlite3_free(pVtab->base.zErrMsg);
+ pVtab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pVtab->db));
+ return rc;
+ }
+ }
+ if( rc==SQLITE_OK ){
+ rc = closureInsertNode(&sQueue, pCur, iRoot, 0);
+ }
+ while( (pAvl = queuePull(&sQueue))!=0 ){
+ if( pAvl->iGeneration>=mxGen ) continue;
+ sqlite3_bind_int64(pStmt, 1, pAvl->id);
+ while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
+ if( sqlite3_column_type(pStmt,0)==SQLITE_INTEGER ){
+ sqlite3_int64 iNew = sqlite3_column_int64(pStmt, 0);
+ if( closureAvlSearch(pCur->pClosure, iNew)==0 ){
+ rc = closureInsertNode(&sQueue, pCur, iNew, pAvl->iGeneration+1);
+ }
+ }
+ }
+ sqlite3_reset(pStmt);
+ }
+ sqlite3_finalize(pStmt);
+ if( rc==SQLITE_OK ){
+ pCur->pCurrent = closureAvlFirst(pCur->pClosure);
+ }
+
+ return rc;
+}
+
+/*
+** Only the word and distance columns have values. All other columns
+** return NULL
+*/
+static int closureColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
+ closure_cursor *pCur = (closure_cursor*)cur;
+ switch( i ){
+ case CLOSURE_COL_ID: {
+ sqlite3_result_int64(ctx, pCur->pCurrent->id);
+ break;
+ }
+ case CLOSURE_COL_DEPTH: {
+ sqlite3_result_int(ctx, pCur->pCurrent->iGeneration);
+ break;
+ }
+ case CLOSURE_COL_ROOT: {
+ sqlite3_result_null(ctx);
+ break;
+ }
+ case CLOSURE_COL_TABLENAME: {
+ sqlite3_result_text(ctx,
+ pCur->zTableName ? pCur->zTableName : pCur->pVtab->zTableName,
+ -1, SQLITE_TRANSIENT);
+ break;
+ }
+ case CLOSURE_COL_IDCOLUMN: {
+ sqlite3_result_text(ctx,
+ pCur->zIdColumn ? pCur->zIdColumn : pCur->pVtab->zIdColumn,
+ -1, SQLITE_TRANSIENT);
+ break;
+ }
+ case CLOSURE_COL_PARENTCOLUMN: {
+ sqlite3_result_text(ctx,
+ pCur->zParentColumn ? pCur->zParentColumn : pCur->pVtab->zParentColumn,
+ -1, SQLITE_TRANSIENT);
+ break;
+ }
+ }
+ return SQLITE_OK;
+}
+
+/*
+** The rowid. For the closure table, this is the same as the "id" column.
+*/
+static int closureRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
+ closure_cursor *pCur = (closure_cursor*)cur;
+ *pRowid = pCur->pCurrent->id;
+ return SQLITE_OK;
+}
+
+/*
+** EOF indicator
+*/
+static int closureEof(sqlite3_vtab_cursor *cur){
+ closure_cursor *pCur = (closure_cursor*)cur;
+ return pCur->pCurrent==0;
+}
+
+/*
+** Search for terms of these forms:
+**
+** (A) root = $root
+** (B1) depth < $depth
+** (B2) depth <= $depth
+** (B3) depth = $depth
+** (C) tablename = $tablename
+** (D) idcolumn = $idcolumn
+** (E) parentcolumn = $parentcolumn
+**
+**
+**
+** idxNum meaning
+** ---------- ------------------------------------------------------
+** 0x00000001 Term of the form (A) found
+** 0x00000002 The term of bit-2 is like (B1)
+** 0x000000f0 Index in filter.argv[] of $depth. 0 if not used.
+** 0x00000f00 Index in filter.argv[] of $tablename. 0 if not used.
+** 0x0000f000 Index in filter.argv[] of $idcolumn. 0 if not used
+** 0x000f0000 Index in filter.argv[] of $parentcolumn. 0 if not used.
+**
+** There must be a term of type (A). If there is not, then the index type
+** is 0 and the query will return an empty set.
+*/
+static int closureBestIndex(
+ sqlite3_vtab *pTab, /* The virtual table */
+ sqlite3_index_info *pIdxInfo /* Information about the query */
+){
+ int iPlan = 0;
+ int i;
+ int idx = 1;
+ const struct sqlite3_index_constraint *pConstraint;
+ closure_vtab *pVtab = (closure_vtab*)pTab;
+
+ pConstraint = pIdxInfo->aConstraint;
+ for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
+ if( pConstraint->usable==0 ) continue;
+ if( (iPlan & 1)==0
+ && pConstraint->iColumn==CLOSURE_COL_ROOT
+ && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ
+ ){
+ iPlan |= 1;
+ pIdxInfo->aConstraintUsage[i].argvIndex = 1;
+ pIdxInfo->aConstraintUsage[i].omit = 1;
+ }
+ if( (iPlan & 0x0000f0)==0
+ && pConstraint->iColumn==CLOSURE_COL_DEPTH
+ && (pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT
+ || pConstraint->op==SQLITE_INDEX_CONSTRAINT_LE
+ || pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ)
+ ){
+ iPlan |= idx<<4;
+ pIdxInfo->aConstraintUsage[i].argvIndex = ++idx;
+ if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT ) iPlan |= 0x000002;
+ }
+ if( (iPlan & 0x000f00)==0
+ && pConstraint->iColumn==CLOSURE_COL_TABLENAME
+ && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ
+ ){
+ iPlan |= idx<<8;
+ pIdxInfo->aConstraintUsage[i].argvIndex = ++idx;
+ pIdxInfo->aConstraintUsage[i].omit = 1;
+ }
+ if( (iPlan & 0x00f000)==0
+ && pConstraint->iColumn==CLOSURE_COL_IDCOLUMN
+ && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ
+ ){
+ iPlan |= idx<<12;
+ pIdxInfo->aConstraintUsage[i].argvIndex = ++idx;
+ pIdxInfo->aConstraintUsage[i].omit = 1;
+ }
+ if( (iPlan & 0x0f0000)==0
+ && pConstraint->iColumn==CLOSURE_COL_PARENTCOLUMN
+ && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ
+ ){
+ iPlan |= idx<<16;
+ pIdxInfo->aConstraintUsage[i].argvIndex = ++idx;
+ pIdxInfo->aConstraintUsage[i].omit = 1;
+ }
+ }
+ if( (pVtab->zTableName==0 && (iPlan & 0x000f00)==0)
+ || (pVtab->zIdColumn==0 && (iPlan & 0x00f000)==0)
+ || (pVtab->zParentColumn==0 && (iPlan & 0x0f0000)==0)
+ ){
+ /* All of tablename, idcolumn, and parentcolumn must be specified
+ ** in either the CREATE VIRTUAL TABLE or in the WHERE clause constraints
+ ** or else the result is an empty set. */
+ iPlan = 0;
+ }
+ pIdxInfo->idxNum = iPlan;
+ if( pIdxInfo->nOrderBy==1
+ && pIdxInfo->aOrderBy[0].iColumn==CLOSURE_COL_ID
+ && pIdxInfo->aOrderBy[0].desc==0
+ ){
+ pIdxInfo->orderByConsumed = 1;
+ }
+ pIdxInfo->estimatedCost = (double)10000;
+
+ return SQLITE_OK;
+}
+
+/*
+** A virtual table module that implements the "approximate_match".
+*/
+static sqlite3_module closureModule = {
+ 0, /* iVersion */
+ closureConnect, /* xCreate */
+ closureConnect, /* xConnect */
+ closureBestIndex, /* xBestIndex */
+ closureDisconnect, /* xDisconnect */
+ closureDisconnect, /* xDestroy */
+ closureOpen, /* xOpen - open a cursor */
+ closureClose, /* xClose - close a cursor */
+ closureFilter, /* xFilter - configure scan constraints */
+ closureNext, /* xNext - advance a cursor */
+ closureEof, /* xEof - check for end of scan */
+ closureColumn, /* xColumn - read data */
+ closureRowid, /* xRowid - read data */
+ 0, /* xUpdate */
+ 0, /* xBegin */
+ 0, /* xSync */
+ 0, /* xCommit */
+ 0, /* xRollback */
+ 0, /* xFindMethod */
+ 0, /* xRename */
+ 0, /* xSavepoint */
+ 0, /* xRelease */
+ 0 /* xRollbackTo */
+};
+
+#endif /* SQLITE_OMIT_VIRTUALTABLE */
+
+/*
+** Register the closure virtual table
+*/
+#ifdef _WIN32
+__declspec(dllexport)
+#endif
+int sqlite3_closure_init(
+ sqlite3 *db,
+ char **pzErrMsg,
+ const sqlite3_api_routines *pApi
+){
+ int rc = SQLITE_OK;
+ SQLITE_EXTENSION_INIT2(pApi);
+ (void)pzErrMsg;
+#ifndef SQLITE_OMIT_VIRTUALTABLE
+ rc = sqlite3_create_module(db, "transitive_closure", &closureModule, 0);
+#endif /* SQLITE_OMIT_VIRTUALTABLE */
+ return rc;
+}
diff --git a/src/test_fuzzer.c b/ext/misc/fuzzer.c
index 10496f2..642b8f9 100644
--- a/src/test_fuzzer.c
+++ b/ext/misc/fuzzer.c
@@ -141,13 +141,14 @@
** of the strings in the second or third column of the fuzzer data table
** is 50 bytes. The maximum cost on a rule is 1000.
*/
+#include "sqlite3ext.h"
+SQLITE_EXTENSION_INIT1
/* If SQLITE_DEBUG is not defined, disable assert statements. */
#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
# define NDEBUG
#endif
-#include "sqlite3.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
@@ -1155,61 +1156,18 @@ static sqlite3_module fuzzerModule = {
#endif /* SQLITE_OMIT_VIRTUALTABLE */
-/*
-** Register the fuzzer virtual table
-*/
-int fuzzer_register(sqlite3 *db){
+#ifdef _WIN32
+__declspec(dllexport)
+#endif
+int sqlite3_fuzzer_init(
+ sqlite3 *db,
+ char **pzErrMsg,
+ const sqlite3_api_routines *pApi
+){
int rc = SQLITE_OK;
+ SQLITE_EXTENSION_INIT2(pApi);
#ifndef SQLITE_OMIT_VIRTUALTABLE
rc = sqlite3_create_module(db, "fuzzer", &fuzzerModule, 0);
#endif
return rc;
}
-
-#ifdef SQLITE_TEST
-#include <tcl.h>
-/*
-** Decode a pointer to an sqlite3 object.
-*/
-extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
-
-/*
-** Register the echo virtual table module.
-*/
-static int register_fuzzer_module(
- ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
- Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
- int objc, /* Number of arguments */
- Tcl_Obj *CONST objv[] /* Command arguments */
-){
- sqlite3 *db;
- if( objc!=2 ){
- Tcl_WrongNumArgs(interp, 1, objv, "DB");
- return TCL_ERROR;
- }
- getDbPointer(interp, Tcl_GetString(objv[1]), &db);
- fuzzer_register(db);
- return TCL_OK;
-}
-
-
-/*
-** Register commands with the TCL interpreter.
-*/
-int Sqlitetestfuzzer_Init(Tcl_Interp *interp){
- static struct {
- char *zName;
- Tcl_ObjCmdProc *xProc;
- void *clientData;
- } aObjCmd[] = {
- { "register_fuzzer_module", register_fuzzer_module, 0 },
- };
- int i;
- for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
- Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
- aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
- }
- return TCL_OK;
-}
-
-#endif /* SQLITE_TEST */
diff --git a/ext/misc/ieee754.c b/ext/misc/ieee754.c
new file mode 100644
index 0000000..436b11e
--- /dev/null
+++ b/ext/misc/ieee754.c
@@ -0,0 +1,131 @@
+/*
+** 2013-04-17
+**
+** 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 SQLite extension implements functions for the exact display
+** and input of IEEE754 Binary64 floating-point numbers.
+**
+** ieee754(X)
+** ieee754(Y,Z)
+**
+** In the first form, the value X should be a floating-point number.
+** The function will return a string of the form 'ieee754(Y,Z)' where
+** Y and Z are integers such that X==Y*pow(w.0,Z).
+**
+** In the second form, Y and Z are integers which are the mantissa and
+** base-2 exponent of a new floating point number. The function returns
+** a floating-point value equal to Y*pow(2.0,Z).
+**
+** Examples:
+**
+** ieee754(2.0) -> 'ieee754(2,0)'
+** ieee754(45.25) -> 'ieee754(181,-2)'
+** ieee754(2, 0) -> 2.0
+** ieee754(181, -2) -> 45.25
+*/
+#include "sqlite3ext.h"
+SQLITE_EXTENSION_INIT1
+#include <assert.h>
+#include <string.h>
+
+/*
+** Implementation of the ieee754() function
+*/
+static void ieee754func(
+ sqlite3_context *context,
+ int argc,
+ sqlite3_value **argv
+){
+ if( argc==1 ){
+ sqlite3_int64 m, a;
+ double r;
+ int e;
+ int isNeg;
+ char zResult[100];
+ assert( sizeof(m)==sizeof(r) );
+ if( sqlite3_value_type(argv[0])!=SQLITE_FLOAT ) return;
+ r = sqlite3_value_double(argv[0]);
+ if( r<0.0 ){
+ isNeg = 1;
+ r = -r;
+ }else{
+ isNeg = 0;
+ }
+ memcpy(&a,&r,sizeof(a));
+ if( a==0 ){
+ e = 0;
+ m = 0;
+ }else{
+ e = a>>52;
+ m = a & ((((sqlite3_int64)1)<<52)-1);
+ m |= ((sqlite3_int64)1)<<52;
+ while( e<1075 && m>0 && (m&1)==0 ){
+ m >>= 1;
+ e++;
+ }
+ if( isNeg ) m = -m;
+ }
+ sqlite3_snprintf(sizeof(zResult), zResult, "ieee754(%lld,%d)",
+ m, e-1075);
+ sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT);
+ }else if( argc==2 ){
+ sqlite3_int64 m, e, a;
+ double r;
+ int isNeg = 0;
+ m = sqlite3_value_int64(argv[0]);
+ e = sqlite3_value_int64(argv[1]);
+ if( m<0 ){
+ isNeg = 1;
+ m = -m;
+ if( m<0 ) return;
+ }else if( m==0 && e>1000 && e<1000 ){
+ sqlite3_result_double(context, 0.0);
+ return;
+ }
+ while( (m>>32)&0xffe00000 ){
+ m >>= 1;
+ e++;
+ }
+ while( ((m>>32)&0xfff00000)==0 ){
+ m <<= 1;
+ e--;
+ }
+ e += 1075;
+ if( e<0 ) e = m = 0;
+ if( e>0x7ff ) m = 0;
+ a = m & ((((sqlite3_int64)1)<<52)-1);
+ a |= e<<52;
+ if( isNeg ) a |= ((sqlite3_int64)1)<<63;
+ memcpy(&r, &a, sizeof(r));
+ sqlite3_result_double(context, r);
+ }
+}
+
+
+#ifdef _WIN32
+__declspec(dllexport)
+#endif
+int sqlite3_ieee_init(
+ sqlite3 *db,
+ char **pzErrMsg,
+ const sqlite3_api_routines *pApi
+){
+ int rc = SQLITE_OK;
+ SQLITE_EXTENSION_INIT2(pApi);
+ (void)pzErrMsg; /* Unused parameter */
+ rc = sqlite3_create_function(db, "ieee754", 1, SQLITE_UTF8, 0,
+ ieee754func, 0, 0);
+ if( rc==SQLITE_OK ){
+ rc = sqlite3_create_function(db, "ieee754", 2, SQLITE_UTF8, 0,
+ ieee754func, 0, 0);
+ }
+ return rc;
+}
diff --git a/ext/misc/nextchar.c b/ext/misc/nextchar.c
new file mode 100644
index 0000000..e063043
--- /dev/null
+++ b/ext/misc/nextchar.c
@@ -0,0 +1,265 @@
+/*
+** 2013-02-28
+**
+** 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 file contains code to implement the next_char(A,T,F,W) SQL function.
+**
+** The next_char(A,T,F,H) function finds all valid "next" characters for
+** string A given the vocabulary in T.F. The T.F field should be indexed.
+** If the W value exists and is a non-empty string, then it is an SQL
+** expression that limits the entries in T.F that will be considered.
+**
+** For example, suppose an application has a dictionary like this:
+**
+** CREATE TABLE dictionary(word TEXT UNIQUE);
+**
+** Further suppose that for user keypad entry, it is desired to disable
+** (gray out) keys that are not valid as the next character. If the
+** the user has previously entered (say) 'cha' then to find all allowed
+** next characters (and thereby determine when keys should not be grayed
+** out) run the following query:
+**
+** SELECT next_char('cha','dictionary','word');
+*/
+#include "sqlite3ext.h"
+SQLITE_EXTENSION_INIT1
+#include <string.h>
+
+/*
+** A structure to hold context of the next_char() computation across
+** nested function calls.
+*/
+typedef struct nextCharContext nextCharContext;
+struct nextCharContext {
+ sqlite3 *db; /* Database connection */
+ sqlite3_stmt *pStmt; /* Prepared statement used to query */
+ const unsigned char *zPrefix; /* Prefix to scan */
+ int nPrefix; /* Size of zPrefix in bytes */
+ int nAlloc; /* Space allocated to aResult */
+ int nUsed; /* Space used in aResult */
+ unsigned int *aResult; /* Array of next characters */
+ int mallocFailed; /* True if malloc fails */
+ int otherError; /* True for any other failure */
+};
+
+/*
+** Append a result character if the character is not already in the
+** result.
+*/
+static void nextCharAppend(nextCharContext *p, unsigned c){
+ int i;
+ for(i=0; i<p->nUsed; i++){
+ if( p->aResult[i]==c ) return;
+ }
+ if( p->nUsed+1 > p->nAlloc ){
+ unsigned int *aNew;
+ int n = p->nAlloc*2 + 30;
+ aNew = sqlite3_realloc(p->aResult, n*sizeof(unsigned int));
+ if( aNew==0 ){
+ p->mallocFailed = 1;
+ return;
+ }else{
+ p->aResult = aNew;
+ p->nAlloc = n;
+ }
+ }
+ p->aResult[p->nUsed++] = c;
+}
+
+/*
+** Write a character into z[] as UTF8. Return the number of bytes needed
+** to hold the character
+*/
+static int writeUtf8(unsigned char *z, unsigned c){
+ if( c<0x00080 ){
+ z[0] = (unsigned char)(c&0xff);
+ return 1;
+ }
+ if( c<0x00800 ){
+ z[0] = 0xC0 + (unsigned char)((c>>6)&0x1F);
+ z[1] = 0x80 + (unsigned char)(c & 0x3F);
+ return 2;
+ }
+ if( c<0x10000 ){
+ z[0] = 0xE0 + (unsigned char)((c>>12)&0x0F);
+ z[1] = 0x80 + (unsigned char)((c>>6) & 0x3F);
+ z[2] = 0x80 + (unsigned char)(c & 0x3F);
+ return 3;
+ }
+ z[0] = 0xF0 + (unsigned char)((c>>18) & 0x07);
+ z[1] = 0x80 + (unsigned char)((c>>12) & 0x3F);
+ z[2] = 0x80 + (unsigned char)((c>>6) & 0x3F);
+ z[3] = 0x80 + (unsigned char)(c & 0x3F);
+ return 4;
+}
+
+/*
+** Read a UTF8 character out of z[] and write it into *pOut. Return
+** the number of bytes in z[] that were used to construct the character.
+*/
+static int readUtf8(const unsigned char *z, unsigned *pOut){
+ static const unsigned char validBits[] = {
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
+ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+ 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
+ };
+ unsigned c = z[0];
+ if( c<0xc0 ){
+ *pOut = c;
+ return 1;
+ }else{
+ int n = 1;
+ c = validBits[c-0xc0];
+ while( (z[n] & 0xc0)==0x80 ){
+ c = (c<<6) + (0x3f & z[n++]);
+ }
+ if( c<0x80 || (c&0xFFFFF800)==0xD800 || (c&0xFFFFFFFE)==0xFFFE ){
+ c = 0xFFFD;
+ }
+ *pOut = c;
+ return n;
+ }
+}
+
+/*
+** The nextCharContext structure has been set up. Add all "next" characters
+** to the result set.
+*/
+static void findNextChars(nextCharContext *p){
+ unsigned cPrev = 0;
+ unsigned char zPrev[8];
+ int n, rc;
+
+ for(;;){
+ sqlite3_bind_text(p->pStmt, 1, (char*)p->zPrefix, p->nPrefix,
+ SQLITE_STATIC);
+ n = writeUtf8(zPrev, cPrev+1);
+ sqlite3_bind_text(p->pStmt, 2, (char*)zPrev, n, SQLITE_STATIC);
+ rc = sqlite3_step(p->pStmt);
+ if( rc==SQLITE_DONE ){
+ sqlite3_reset(p->pStmt);
+ return;
+ }else if( rc!=SQLITE_ROW ){
+ p->otherError = rc;
+ return;
+ }else{
+ const unsigned char *zOut = sqlite3_column_text(p->pStmt, 0);
+ unsigned cNext;
+ n = readUtf8(zOut+p->nPrefix, &cNext);
+ sqlite3_reset(p->pStmt);
+ nextCharAppend(p, cNext);
+ cPrev = cNext;
+ if( p->mallocFailed ) return;
+ }
+ }
+}
+
+
+/*
+** next_character(A,T,F,W)
+**
+** Return a string composted of all next possible characters after
+** A for elements of T.F. If W is supplied, then it is an SQL expression
+** that limits the elements in T.F that are considered.
+*/
+static void nextCharFunc(
+ sqlite3_context *context,
+ int argc,
+ sqlite3_value **argv
+){
+ nextCharContext c;
+ const unsigned char *zTable = sqlite3_value_text(argv[1]);
+ const unsigned char *zField = sqlite3_value_text(argv[2]);
+ const unsigned char *zWhere;
+ char *zSql;
+ int rc;
+
+ memset(&c, 0, sizeof(c));
+ c.db = sqlite3_context_db_handle(context);
+ c.zPrefix = sqlite3_value_text(argv[0]);
+ c.nPrefix = sqlite3_value_bytes(argv[0]);
+ if( zTable==0 || zField==0 || c.zPrefix==0 ) return;
+ if( argc<4
+ || (zWhere = sqlite3_value_text(argv[3]))==0
+ || zWhere[0]==0
+ ){
+ zSql = sqlite3_mprintf(
+ "SELECT \"%w\" FROM \"%w\""
+ " WHERE \"%w\">=(?1 || ?2)"
+ " AND \"%w\"<=(?1 || char(1114111))" /* 1114111 == 0x10ffff */
+ " ORDER BY 1 ASC LIMIT 1",
+ zField, zTable, zField, zField);
+ }else{
+ zSql = sqlite3_mprintf(
+ "SELECT \"%w\" FROM \"%w\""
+ " WHERE \"%w\">=(?1 || ?2)"
+ " AND \"%w\"<=(?1 || char(1114111))" /* 1114111 == 0x10ffff */
+ " AND (%s)"
+ " ORDER BY 1 ASC LIMIT 1",
+ zField, zTable, zField, zField, zWhere);
+ }
+ if( zSql==0 ){
+ sqlite3_result_error_nomem(context);
+ return;
+ }
+
+ rc = sqlite3_prepare_v2(c.db, zSql, -1, &c.pStmt, 0);
+ sqlite3_free(zSql);
+ if( rc ){
+ sqlite3_result_error(context, sqlite3_errmsg(c.db), -1);
+ return;
+ }
+ findNextChars(&c);
+ if( c.mallocFailed ){
+ sqlite3_result_error_nomem(context);
+ }else{
+ unsigned char *pRes;
+ pRes = sqlite3_malloc( c.nUsed*4 + 1 );
+ if( pRes==0 ){
+ sqlite3_result_error_nomem(context);
+ }else{
+ int i;
+ int n = 0;
+ for(i=0; i<c.nUsed; i++){
+ n += writeUtf8(pRes+n, c.aResult[i]);
+ }
+ pRes[n] = 0;
+ sqlite3_result_text(context, (const char*)pRes, n, sqlite3_free);
+ }
+ }
+ sqlite3_finalize(c.pStmt);
+ sqlite3_free(c.aResult);
+}
+
+#ifdef _WIN32
+__declspec(dllexport)
+#endif
+int sqlite3_nextchar_init(
+ sqlite3 *db,
+ char **pzErrMsg,
+ const sqlite3_api_routines *pApi
+){
+ int rc = SQLITE_OK;
+ SQLITE_EXTENSION_INIT2(pApi);
+ (void)pzErrMsg; /* Unused parameter */
+ rc = sqlite3_create_function(db, "next_char", 3, SQLITE_UTF8, 0,
+ nextCharFunc, 0, 0);
+ if( rc==SQLITE_OK ){
+ rc = sqlite3_create_function(db, "next_char", 4, SQLITE_UTF8, 0,
+ nextCharFunc, 0, 0);
+ }
+ return rc;
+}
diff --git a/ext/misc/regexp.c b/ext/misc/regexp.c
new file mode 100644
index 0000000..16fa7d0
--- /dev/null
+++ b/ext/misc/regexp.c
@@ -0,0 +1,756 @@
+/*
+** 2012-11-13
+**
+** 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.
+**
+******************************************************************************
+**
+** The code in this file implements a compact but reasonably
+** efficient regular-expression matcher for posix extended regular
+** expressions against UTF8 text.
+**
+** This file is an SQLite extension. It registers a single function
+** named "regexp(A,B)" where A is the regular expression and B is the
+** string to be matched. By registering this function, SQLite will also
+** then implement the "B regexp A" operator. Note that with the function
+** the regular expression comes first, but with the operator it comes
+** second.
+**
+** The following regular expression syntax is supported:
+**
+** X* zero or more occurrences of X
+** X+ one or more occurrences of X
+** X? zero or one occurrences of X
+** X{p,q} between p and q occurrences of X
+** (X) match X
+** X|Y X or Y
+** ^X X occurring at the beginning of the string
+** X$ X occurring at the end of the string
+** . Match any single character
+** \c Character c where c is one of \{}()[]|*+?.
+** \c C-language escapes for c in afnrtv. ex: \t or \n
+** \uXXXX Where XXXX is exactly 4 hex digits, unicode value XXXX
+** \xXX Where XX is exactly 2 hex digits, unicode value XX
+** [abc] Any single character from the set abc
+** [^abc] Any single character not in the set abc
+** [a-z] Any single character in the range a-z
+** [^a-z] Any single character not in the range a-z
+** \b Word boundary
+** \w Word character. [A-Za-z0-9_]
+** \W Non-word character
+** \d Digit
+** \D Non-digit
+** \s Whitespace character
+** \S Non-whitespace character
+**
+** A nondeterministic finite automaton (NFA) is used for matching, so the
+** performance is bounded by O(N*M) where N is the size of the regular
+** expression and M is the size of the input string. The matcher never
+** exhibits exponential behavior. Note that the X{p,q} operator expands
+** to p copies of X following by q-p copies of X? and that the size of the
+** regular expression in the O(N*M) performance bound is computed after
+** this expansion.
+*/
+#include <string.h>
+#include <stdlib.h>
+#include "sqlite3ext.h"
+SQLITE_EXTENSION_INIT1
+
+/*
+** The following #defines change the names of some functions implemented in
+** this file to prevent name collisions with C-library functions of the
+** same name.
+*/
+#define re_match sqlite3re_match
+#define re_compile sqlite3re_compile
+#define re_free sqlite3re_free
+
+/* The end-of-input character */
+#define RE_EOF 0 /* End of input */
+
+/* The NFA is implemented as sequence of opcodes taken from the following
+** set. Each opcode has a single integer argument.
+*/
+#define RE_OP_MATCH 1 /* Match the one character in the argument */
+#define RE_OP_ANY 2 /* Match any one character. (Implements ".") */
+#define RE_OP_ANYSTAR 3 /* Special optimized version of .* */
+#define RE_OP_FORK 4 /* Continue to both next and opcode at iArg */
+#define RE_OP_GOTO 5 /* Jump to opcode at iArg */
+#define RE_OP_ACCEPT 6 /* Halt and indicate a successful match */
+#define RE_OP_CC_INC 7 /* Beginning of a [...] character class */
+#define RE_OP_CC_EXC 8 /* Beginning of a [^...] character class */
+#define RE_OP_CC_VALUE 9 /* Single value in a character class */
+#define RE_OP_CC_RANGE 10 /* Range of values in a character class */
+#define RE_OP_WORD 11 /* Perl word character [A-Za-z0-9_] */
+#define RE_OP_NOTWORD 12 /* Not a perl word character */
+#define RE_OP_DIGIT 13 /* digit: [0-9] */
+#define RE_OP_NOTDIGIT 14 /* Not a digit */
+#define RE_OP_SPACE 15 /* space: [ \t\n\r\v\f] */
+#define RE_OP_NOTSPACE 16 /* Not a digit */
+#define RE_OP_BOUNDARY 17 /* Boundary between word and non-word */
+
+/* Each opcode is a "state" in the NFA */
+typedef unsigned short ReStateNumber;
+
+/* Because this is an NFA and not a DFA, multiple states can be active at
+** once. An instance of the following object records all active states in
+** the NFA. The implementation is optimized for the common case where the
+** number of actives states is small.
+*/
+typedef struct ReStateSet {
+ unsigned nState; /* Number of current states */
+ ReStateNumber *aState; /* Current states */
+} ReStateSet;
+
+/* An input string read one character at a time.
+*/
+typedef struct ReInput ReInput;
+struct ReInput {
+ const unsigned char *z; /* All text */
+ int i; /* Next byte to read */
+ int mx; /* EOF when i>=mx */
+};
+
+/* A compiled NFA (or an NFA that is in the process of being compiled) is
+** an instance of the following object.
+*/
+typedef struct ReCompiled ReCompiled;
+struct ReCompiled {
+ ReInput sIn; /* Regular expression text */
+ const char *zErr; /* Error message to return */
+ char *aOp; /* Operators for the virtual machine */
+ int *aArg; /* Arguments to each operator */
+ unsigned (*xNextChar)(ReInput*); /* Next character function */
+ unsigned char zInit[12]; /* Initial text to match */
+ int nInit; /* Number of characters in zInit */
+ unsigned nState; /* Number of entries in aOp[] and aArg[] */
+ unsigned nAlloc; /* Slots allocated for aOp[] and aArg[] */
+};
+
+/* Add a state to the given state set if it is not already there */
+static void re_add_state(ReStateSet *pSet, int newState){
+ unsigned i;
+ for(i=0; i<pSet->nState; i++) if( pSet->aState[i]==newState ) return;
+ pSet->aState[pSet->nState++] = newState;
+}
+
+/* Extract the next unicode character from *pzIn and return it. Advance
+** *pzIn to the first byte past the end of the character returned. To
+** be clear: this routine converts utf8 to unicode. This routine is
+** optimized for the common case where the next character is a single byte.
+*/
+static unsigned re_next_char(ReInput *p){
+ unsigned c;
+ if( p->i>=p->mx ) return 0;
+ c = p->z[p->i++];
+ if( c>=0x80 ){
+ if( (c&0xe0)==0xc0 && p->i<p->mx && (p->z[p->i]&0xc0)==0x80 ){
+ c = (c&0x1f)<<6 | (p->z[p->i++]&0x3f);
+ if( c<0x80 ) c = 0xfffd;
+ }else if( (c&0xf0)==0xe0 && p->i+1<p->mx && (p->z[p->i]&0xc0)==0x80
+ && (p->z[p->i+1]&0xc0)==0x80 ){
+ c = (c&0x0f)<<12 | ((p->z[p->i]&0x3f)<<6) | (p->z[p->i+1]&0x3f);
+ p->i += 2;
+ if( c<=0x3ff || (c>=0xd800 && c<=0xdfff) ) c = 0xfffd;
+ }else if( (c&0xf8)==0xf0 && p->i+3<p->mx && (p->z[p->i]&0xc0)==0x80
+ && (p->z[p->i+1]&0xc0)==0x80 && (p->z[p->i+2]&0xc0)==0x80 ){
+ c = (c&0x07)<<18 | ((p->z[p->i]&0x3f)<<12) | ((p->z[p->i+1]&0x3f)<<6)
+ | (p->z[p->i+2]&0x3f);
+ p->i += 3;
+ if( c<=0xffff || c>0x10ffff ) c = 0xfffd;
+ }else{
+ c = 0xfffd;
+ }
+ }
+ return c;
+}
+static unsigned re_next_char_nocase(ReInput *p){
+ unsigned c = re_next_char(p);
+ if( c>='A' && c<='Z' ) c += 'a' - 'A';
+ return c;
+}
+
+/* Return true if c is a perl "word" character: [A-Za-z0-9_] */
+static int re_word_char(int c){
+ return (c>='0' && c<='9') || (c>='a' && c<='z')
+ || (c>='A' && c<='Z') || c=='_';
+}
+
+/* Return true if c is a "digit" character: [0-9] */
+static int re_digit_char(int c){
+ return (c>='0' && c<='9');
+}
+
+/* Return true if c is a perl "space" character: [ \t\r\n\v\f] */
+static int re_space_char(int c){
+ return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
+}
+
+/* Run a compiled regular expression on the zero-terminated input
+** string zIn[]. Return true on a match and false if there is no match.
+*/
+static int re_match(ReCompiled *pRe, const unsigned char *zIn, int nIn){
+ ReStateSet aStateSet[2], *pThis, *pNext;
+ ReStateNumber aSpace[100];
+ ReStateNumber *pToFree;
+ unsigned int i = 0;
+ unsigned int iSwap = 0;
+ int c = RE_EOF+1;
+ int cPrev = 0;
+ int rc = 0;
+ ReInput in;
+
+ in.z = zIn;
+ in.i = 0;
+ in.mx = nIn>=0 ? nIn : (int)strlen((char const*)zIn);
+
+ /* Look for the initial prefix match, if there is one. */
+ if( pRe->nInit ){
+ unsigned char x = pRe->zInit[0];
+ while( in.i+pRe->nInit<=in.mx
+ && (zIn[in.i]!=x ||
+ strncmp((const char*)zIn+in.i, (const char*)pRe->zInit, pRe->nInit)!=0)
+ ){
+ in.i++;
+ }
+ if( in.i+pRe->nInit>in.mx ) return 0;
+ }
+
+ if( pRe->nState<=(sizeof(aSpace)/(sizeof(aSpace[0])*2)) ){
+ pToFree = 0;
+ aStateSet[0].aState = aSpace;
+ }else{
+ pToFree = sqlite3_malloc( sizeof(ReStateNumber)*2*pRe->nState );
+ if( pToFree==0 ) return -1;
+ aStateSet[0].aState = pToFree;
+ }
+ aStateSet[1].aState = &aStateSet[0].aState[pRe->nState];
+ pNext = &aStateSet[1];
+ pNext->nState = 0;
+ re_add_state(pNext, 0);
+ while( c!=RE_EOF && pNext->nState>0 ){
+ cPrev = c;
+ c = pRe->xNextChar(&in);
+ pThis = pNext;
+ pNext = &aStateSet[iSwap];
+ iSwap = 1 - iSwap;
+ pNext->nState = 0;
+ for(i=0; i<pThis->nState; i++){
+ int x = pThis->aState[i];
+ switch( pRe->aOp[x] ){
+ case RE_OP_MATCH: {
+ if( pRe->aArg[x]==c ) re_add_state(pNext, x+1);
+ break;
+ }
+ case RE_OP_ANY: {
+ re_add_state(pNext, x+1);
+ break;
+ }
+ case RE_OP_WORD: {
+ if( re_word_char(c) ) re_add_state(pNext, x+1);
+ break;
+ }
+ case RE_OP_NOTWORD: {
+ if( !re_word_char(c) ) re_add_state(pNext, x+1);
+ break;
+ }
+ case RE_OP_DIGIT: {
+ if( re_digit_char(c) ) re_add_state(pNext, x+1);
+ break;
+ }
+ case RE_OP_NOTDIGIT: {
+ if( !re_digit_char(c) ) re_add_state(pNext, x+1);
+ break;
+ }
+ case RE_OP_SPACE: {
+ if( re_space_char(c) ) re_add_state(pNext, x+1);
+ break;
+ }
+ case RE_OP_NOTSPACE: {
+ if( !re_space_char(c) ) re_add_state(pNext, x+1);
+ break;
+ }
+ case RE_OP_BOUNDARY: {
+ if( re_word_char(c)!=re_word_char(cPrev) ) re_add_state(pThis, x+1);
+ break;
+ }
+ case RE_OP_ANYSTAR: {
+ re_add_state(pNext, x);
+ re_add_state(pThis, x+1);
+ break;
+ }
+ case RE_OP_FORK: {
+ re_add_state(pThis, x+pRe->aArg[x]);
+ re_add_state(pThis, x+1);
+ break;
+ }
+ case RE_OP_GOTO: {
+ re_add_state(pThis, x+pRe->aArg[x]);
+ break;
+ }
+ case RE_OP_ACCEPT: {
+ rc = 1;
+ goto re_match_end;
+ }
+ case RE_OP_CC_INC:
+ case RE_OP_CC_EXC: {
+ int j = 1;
+ int n = pRe->aArg[x];
+ int hit = 0;
+ for(j=1; j>0 && j<n; j++){
+ if( pRe->aOp[x+j]==RE_OP_CC_VALUE ){
+ if( pRe->aArg[x+j]==c ){
+ hit = 1;
+ j = -1;
+ }
+ }else{
+ if( pRe->aArg[x+j]<=c && pRe->aArg[x+j+1]>=c ){
+ hit = 1;
+ j = -1;
+ }else{
+ j++;
+ }
+ }
+ }
+ if( pRe->aOp[x]==RE_OP_CC_EXC ) hit = !hit;
+ if( hit ) re_add_state(pNext, x+n);
+ break;
+ }
+ }
+ }
+ }
+ for(i=0; i<pNext->nState; i++){
+ if( pRe->aOp[pNext->aState[i]]==RE_OP_ACCEPT ){ rc = 1; break; }
+ }
+re_match_end:
+ sqlite3_free(pToFree);
+ return rc;
+}
+
+/* Resize the opcode and argument arrays for an RE under construction.
+*/
+static int re_resize(ReCompiled *p, int N){
+ char *aOp;
+ int *aArg;
+ aOp = sqlite3_realloc(p->aOp, N*sizeof(p->aOp[0]));
+ if( aOp==0 ) return 1;
+ p->aOp = aOp;
+ aArg = sqlite3_realloc(p->aArg, N*sizeof(p->aArg[0]));
+ if( aArg==0 ) return 1;
+ p->aArg = aArg;
+ p->nAlloc = N;
+ return 0;
+}
+
+/* Insert a new opcode and argument into an RE under construction. The
+** insertion point is just prior to existing opcode iBefore.
+*/
+static int re_insert(ReCompiled *p, int iBefore, int op, int arg){
+ int i;
+ if( p->nAlloc<=p->nState && re_resize(p, p->nAlloc*2) ) return 0;
+ for(i=p->nState; i>iBefore; i--){
+ p->aOp[i] = p->aOp[i-1];
+ p->aArg[i] = p->aArg[i-1];
+ }
+ p->nState++;
+ p->aOp[iBefore] = op;
+ p->aArg[iBefore] = arg;
+ return iBefore;
+}
+
+/* Append a new opcode and argument to the end of the RE under construction.
+*/
+static int re_append(ReCompiled *p, int op, int arg){
+ return re_insert(p, p->nState, op, arg);
+}
+
+/* Make a copy of N opcodes starting at iStart onto the end of the RE
+** under construction.
+*/
+static void re_copy(ReCompiled *p, int iStart, int N){
+ if( p->nState+N>=p->nAlloc && re_resize(p, p->nAlloc*2+N) ) return;
+ memcpy(&p->aOp[p->nState], &p->aOp[iStart], N*sizeof(p->aOp[0]));
+ memcpy(&p->aArg[p->nState], &p->aArg[iStart], N*sizeof(p->aArg[0]));
+ p->nState += N;
+}
+
+/* Return true if c is a hexadecimal digit character: [0-9a-fA-F]
+** If c is a hex digit, also set *pV = (*pV)*16 + valueof(c). If
+** c is not a hex digit *pV is unchanged.
+*/
+static int re_hex(int c, int *pV){
+ if( c>='0' && c<='9' ){
+ c -= '0';
+ }else if( c>='a' && c<='f' ){
+ c -= 'a' - 10;
+ }else if( c>='A' && c<='F' ){
+ c -= 'A' - 10;
+ }else{
+ return 0;
+ }
+ *pV = (*pV)*16 + (c & 0xff);
+ return 1;
+}
+
+/* A backslash character has been seen, read the next character and
+** return its interpretation.
+*/
+static unsigned re_esc_char(ReCompiled *p){
+ static const char zEsc[] = "afnrtv\\()*.+?[$^{|}]";
+ static const char zTrans[] = "\a\f\n\r\t\v";
+ int i, v = 0;
+ char c;
+ if( p->sIn.i>=p->sIn.mx ) return 0;
+ c = p->sIn.z[p->sIn.i];
+ if( c=='u' && p->sIn.i+4<p->sIn.mx ){
+ const unsigned char *zIn = p->sIn.z + p->sIn.i;
+ if( re_hex(zIn[1],&v)
+ && re_hex(zIn[2],&v)
+ && re_hex(zIn[3],&v)
+ && re_hex(zIn[4],&v)
+ ){
+ p->sIn.i += 5;
+ return v;
+ }
+ }
+ if( c=='x' && p->sIn.i+2<p->sIn.mx ){
+ const unsigned char *zIn = p->sIn.z + p->sIn.i;
+ if( re_hex(zIn[1],&v)
+ && re_hex(zIn[2],&v)
+ ){
+ p->sIn.i += 3;
+ return v;
+ }
+ }
+ for(i=0; zEsc[i] && zEsc[i]!=c; i++){}
+ if( zEsc[i] ){
+ if( i<6 ) c = zTrans[i];
+ p->sIn.i++;
+ }else{
+ p->zErr = "unknown \\ escape";
+ }
+ return c;
+}
+
+/* Forward declaration */
+static const char *re_subcompile_string(ReCompiled*);
+
+/* Peek at the next byte of input */
+static unsigned char rePeek(ReCompiled *p){
+ return p->sIn.i<p->sIn.mx ? p->sIn.z[p->sIn.i] : 0;
+}
+
+/* Compile RE text into a sequence of opcodes. Continue up to the
+** first unmatched ")" character, then return. If an error is found,
+** return a pointer to the error message string.
+*/
+static const char *re_subcompile_re(ReCompiled *p){
+ const char *zErr;
+ int iStart, iEnd, iGoto;
+ iStart = p->nState;
+ zErr = re_subcompile_string(p);
+ if( zErr ) return zErr;
+ while( rePeek(p)=='|' ){
+ iEnd = p->nState;
+ re_insert(p, iStart, RE_OP_FORK, iEnd + 2 - iStart);
+ iGoto = re_append(p, RE_OP_GOTO, 0);
+ p->sIn.i++;
+ zErr = re_subcompile_string(p);
+ if( zErr ) return zErr;
+ p->aArg[iGoto] = p->nState - iGoto;
+ }
+ return 0;
+}
+
+/* Compile an element of regular expression text (anything that can be
+** an operand to the "|" operator). Return NULL on success or a pointer
+** to the error message if there is a problem.
+*/
+static const char *re_subcompile_string(ReCompiled *p){
+ int iPrev = -1;
+ int iStart;
+ unsigned c;
+ const char *zErr;
+ while( (c = p->xNextChar(&p->sIn))!=0 ){
+ iStart = p->nState;
+ switch( c ){
+ case '|':
+ case '$':
+ case ')': {
+ p->sIn.i--;
+ return 0;
+ }
+ case '(': {
+ zErr = re_subcompile_re(p);
+ if( zErr ) return zErr;
+ if( rePeek(p)!=')' ) return "unmatched '('";
+ p->sIn.i++;
+ break;
+ }
+ case '.': {
+ if( rePeek(p)=='*' ){
+ re_append(p, RE_OP_ANYSTAR, 0);
+ p->sIn.i++;
+ }else{
+ re_append(p, RE_OP_ANY, 0);
+ }
+ break;
+ }
+ case '*': {
+ if( iPrev<0 ) return "'*' without operand";
+ re_insert(p, iPrev, RE_OP_GOTO, p->nState - iPrev + 1);
+ re_append(p, RE_OP_FORK, iPrev - p->nState + 1);
+ break;
+ }
+ case '+': {
+ if( iPrev<0 ) return "'+' without operand";
+ re_append(p, RE_OP_FORK, iPrev - p->nState);
+ break;
+ }
+ case '?': {
+ if( iPrev<0 ) return "'?' without operand";
+ re_insert(p, iPrev, RE_OP_FORK, p->nState - iPrev+1);
+ break;
+ }
+ case '{': {
+ int m = 0, n = 0;
+ int sz, j;
+ if( iPrev<0 ) return "'{m,n}' without operand";
+ while( (c=rePeek(p))>='0' && c<='9' ){ m = m*10 + c - '0'; p->sIn.i++; }
+ n = m;
+ if( c==',' ){
+ p->sIn.i++;
+ n = 0;
+ while( (c=rePeek(p))>='0' && c<='9' ){ n = n*10 + c-'0'; p->sIn.i++; }
+ }
+ if( c!='}' ) return "unmatched '{'";
+ if( n>0 && n<m ) return "n less than m in '{m,n}'";
+ p->sIn.i++;
+ sz = p->nState - iPrev;
+ if( m==0 ){
+ if( n==0 ) return "both m and n are zero in '{m,n}'";
+ re_insert(p, iPrev, RE_OP_FORK, sz+1);
+ n--;
+ }else{
+ for(j=1; j<m; j++) re_copy(p, iPrev, sz);
+ }
+ for(j=m; j<n; j++){
+ re_append(p, RE_OP_FORK, sz+1);
+ re_copy(p, iPrev, sz);
+ }
+ if( n==0 && m>0 ){
+ re_append(p, RE_OP_FORK, -sz);
+ }
+ break;
+ }
+ case '[': {
+ int iFirst = p->nState;
+ if( rePeek(p)=='^' ){
+ re_append(p, RE_OP_CC_EXC, 0);
+ p->sIn.i++;
+ }else{
+ re_append(p, RE_OP_CC_INC, 0);
+ }
+ while( (c = p->xNextChar(&p->sIn))!=0 ){
+ if( c=='[' && rePeek(p)==':' ){
+ return "POSIX character classes not supported";
+ }
+ if( c=='\\' ) c = re_esc_char(p);
+ if( rePeek(p)=='-' ){
+ re_append(p, RE_OP_CC_RANGE, c);
+ p->sIn.i++;
+ c = p->xNextChar(&p->sIn);
+ if( c=='\\' ) c = re_esc_char(p);
+ re_append(p, RE_OP_CC_RANGE, c);
+ }else{
+ re_append(p, RE_OP_CC_VALUE, c);
+ }
+ if( rePeek(p)==']' ){ p->sIn.i++; break; }
+ }
+ if( c==0 ) return "unclosed '['";
+ p->aArg[iFirst] = p->nState - iFirst;
+ break;
+ }
+ case '\\': {
+ int specialOp = 0;
+ switch( rePeek(p) ){
+ case 'b': specialOp = RE_OP_BOUNDARY; break;
+ case 'd': specialOp = RE_OP_DIGIT; break;
+ case 'D': specialOp = RE_OP_NOTDIGIT; break;
+ case 's': specialOp = RE_OP_SPACE; break;
+ case 'S': specialOp = RE_OP_NOTSPACE; break;
+ case 'w': specialOp = RE_OP_WORD; break;
+ case 'W': specialOp = RE_OP_NOTWORD; break;
+ }
+ if( specialOp ){
+ p->sIn.i++;
+ re_append(p, specialOp, 0);
+ }else{
+ c = re_esc_char(p);
+ re_append(p, RE_OP_MATCH, c);
+ }
+ break;
+ }
+ default: {
+ re_append(p, RE_OP_MATCH, c);
+ break;
+ }
+ }
+ iPrev = iStart;
+ }
+ return 0;
+}
+
+/* Free and reclaim all the memory used by a previously compiled
+** regular expression. Applications should invoke this routine once
+** for every call to re_compile() to avoid memory leaks.
+*/
+void re_free(ReCompiled *pRe){
+ if( pRe ){
+ sqlite3_free(pRe->aOp);
+ sqlite3_free(pRe->aArg);
+ sqlite3_free(pRe);
+ }
+}
+
+/*
+** Compile a textual regular expression in zIn[] into a compiled regular
+** expression suitable for us by re_match() and return a pointer to the
+** compiled regular expression in *ppRe. Return NULL on success or an
+** error message if something goes wrong.
+*/
+const char *re_compile(ReCompiled **ppRe, const char *zIn, int noCase){
+ ReCompiled *pRe;
+ const char *zErr;
+ int i, j;
+
+ *ppRe = 0;
+ pRe = sqlite3_malloc( sizeof(*pRe) );
+ if( pRe==0 ){
+ return "out of memory";
+ }
+ memset(pRe, 0, sizeof(*pRe));
+ pRe->xNextChar = noCase ? re_next_char_nocase : re_next_char;
+ if( re_resize(pRe, 30) ){
+ re_free(pRe);
+ return "out of memory";
+ }
+ if( zIn[0]=='^' ){
+ zIn++;
+ }else{
+ re_append(pRe, RE_OP_ANYSTAR, 0);
+ }
+ pRe->sIn.z = (unsigned char*)zIn;
+ pRe->sIn.i = 0;
+ pRe->sIn.mx = (int)strlen(zIn);
+ zErr = re_subcompile_re(pRe);
+ if( zErr ){
+ re_free(pRe);
+ return zErr;
+ }
+ if( rePeek(pRe)=='$' && pRe->sIn.i+1>=pRe->sIn.mx ){
+ re_append(pRe, RE_OP_MATCH, RE_EOF);
+ re_append(pRe, RE_OP_ACCEPT, 0);
+ *ppRe = pRe;
+ }else if( pRe->sIn.i>=pRe->sIn.mx ){
+ re_append(pRe, RE_OP_ACCEPT, 0);
+ *ppRe = pRe;
+ }else{
+ re_free(pRe);
+ return "unrecognized character";
+ }
+
+ /* The following is a performance optimization. If the regex begins with
+ ** ".*" (if the input regex lacks an initial "^") and afterwards there are
+ ** one or more matching characters, enter those matching characters into
+ ** zInit[]. The re_match() routine can then search ahead in the input
+ ** string looking for the initial match without having to run the whole
+ ** regex engine over the string. Do not worry able trying to match
+ ** unicode characters beyond plane 0 - those are very rare and this is
+ ** just an optimization. */
+ if( pRe->aOp[0]==RE_OP_ANYSTAR ){
+ for(j=0, i=1; j<sizeof(pRe->zInit)-2 && pRe->aOp[i]==RE_OP_MATCH; i++){
+ unsigned x = pRe->aArg[i];
+ if( x<=127 ){
+ pRe->zInit[j++] = x;
+ }else if( x<=0xfff ){
+ pRe->zInit[j++] = 0xc0 | (x>>6);
+ pRe->zInit[j++] = 0x80 | (x&0x3f);
+ }else if( x<=0xffff ){
+ pRe->zInit[j++] = 0xd0 | (x>>12);
+ pRe->zInit[j++] = 0x80 | ((x>>6)&0x3f);
+ pRe->zInit[j++] = 0x80 | (x&0x3f);
+ }else{
+ break;
+ }
+ }
+ if( j>0 && pRe->zInit[j-1]==0 ) j--;
+ pRe->nInit = j;
+ }
+ return pRe->zErr;
+}
+
+/*
+** Implementation of the regexp() SQL function. This function implements
+** the build-in REGEXP operator. The first argument to the function is the
+** pattern and the second argument is the string. So, the SQL statements:
+**
+** A REGEXP B
+**
+** is implemented as regexp(B,A).
+*/
+static void re_sql_func(
+ sqlite3_context *context,
+ int argc,
+ sqlite3_value **argv
+){
+ ReCompiled *pRe; /* Compiled regular expression */
+ const char *zPattern; /* The regular expression */
+ const unsigned char *zStr;/* String being searched */
+ const char *zErr; /* Compile error message */
+
+ pRe = sqlite3_get_auxdata(context, 0);
+ if( pRe==0 ){
+ zPattern = (const char*)sqlite3_value_text(argv[0]);
+ if( zPattern==0 ) return;
+ zErr = re_compile(&pRe, zPattern, 0);
+ if( zErr ){
+ re_free(pRe);
+ sqlite3_result_error(context, zErr, -1);
+ return;
+ }
+ if( pRe==0 ){
+ sqlite3_result_error_nomem(context);
+ return;
+ }
+ sqlite3_set_auxdata(context, 0, pRe, (void(*)(void*))re_free);
+ }
+ zStr = (const unsigned char*)sqlite3_value_text(argv[1]);
+ if( zStr!=0 ){
+ sqlite3_result_int(context, re_match(pRe, zStr, -1));
+ }
+}
+
+/*
+** Invoke this routine to register the regexp() function with the
+** SQLite database connection.
+*/
+#ifdef _WIN32
+__declspec(dllexport)
+#endif
+int sqlite3_regexp_init(
+ sqlite3 *db,
+ char **pzErrMsg,
+ const sqlite3_api_routines *pApi
+){
+ int rc = SQLITE_OK;
+ SQLITE_EXTENSION_INIT2(pApi);
+ rc = sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, 0,
+ re_sql_func, 0, 0);
+ return rc;
+}
diff --git a/ext/misc/rot13.c b/ext/misc/rot13.c
new file mode 100644
index 0000000..68fdf60
--- /dev/null
+++ b/ext/misc/rot13.c
@@ -0,0 +1,114 @@
+/*
+** 2013-05-15
+**
+** 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 SQLite extension implements a rot13() function and a rot13
+** collating sequence.
+*/
+#include "sqlite3ext.h"
+SQLITE_EXTENSION_INIT1
+#include <assert.h>
+#include <string.h>
+
+/*
+** Perform rot13 encoding on a single ASCII character.
+*/
+static unsigned char rot13(unsigned char c){
+ if( c>='a' && c<='z' ){
+ c += 13;
+ if( c>'z' ) c -= 26;
+ }else if( c>='A' && c<='Z' ){
+ c += 13;
+ if( c>'Z' ) c -= 26;
+ }
+ return c;
+}
+
+/*
+** Implementation of the rot13() function.
+**
+** Rotate ASCII alphabetic characters by 13 character positions.
+** Non-ASCII characters are unchanged. rot13(rot13(X)) should always
+** equal X.
+*/
+static void rot13func(
+ sqlite3_context *context,
+ int argc,
+ sqlite3_value **argv
+){
+ const unsigned char *zIn;
+ int nIn;
+ unsigned char *zOut;
+ char *zToFree = 0;
+ int i;
+ char zTemp[100];
+ assert( argc==1 );
+ if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
+ zIn = (const unsigned char*)sqlite3_value_text(argv[0]);
+ nIn = sqlite3_value_bytes(argv[0]);
+ if( nIn<sizeof(zTemp)-1 ){
+ zOut = zTemp;
+ }else{
+ zOut = zToFree = sqlite3_malloc( nIn+1 );
+ if( zOut==0 ){
+ sqlite3_result_error_nomem(context);
+ return;
+ }
+ }
+ for(i=0; i<nIn; i++) zOut[i] = rot13(zIn[i]);
+ zOut[i] = 0;
+ sqlite3_result_text(context, (char*)zOut, i, SQLITE_TRANSIENT);
+ sqlite3_free(zToFree);
+}
+
+/*
+** Implement the rot13 collating sequence so that if
+**
+** x=y COLLATE rot13
+**
+** Then
+**
+** rot13(x)=rot13(y) COLLATE binary
+*/
+static int rot13CollFunc(
+ void *notUsed,
+ int nKey1, const void *pKey1,
+ int nKey2, const void *pKey2
+){
+ const char *zA = (const char*)pKey1;
+ const char *zB = (const char*)pKey2;
+ int i, x;
+ for(i=0; i<nKey1 && i<nKey2; i++){
+ x = (int)rot13(zA[i]) - (int)rot13(zB[i]);
+ if( x!=0 ) return x;
+ }
+ return nKey1 - nKey2;
+}
+
+
+#ifdef _WIN32
+__declspec(dllexport)
+#endif
+int sqlite3_rot_init(
+ sqlite3 *db,
+ char **pzErrMsg,
+ const sqlite3_api_routines *pApi
+){
+ int rc = SQLITE_OK;
+ SQLITE_EXTENSION_INIT2(pApi);
+ (void)pzErrMsg; /* Unused parameter */
+ rc = sqlite3_create_function(db, "rot13", 1, SQLITE_UTF8, 0,
+ rot13func, 0, 0);
+ if( rc==SQLITE_OK ){
+ rc = sqlite3_create_collation(db, "rot13", SQLITE_UTF8, 0, rot13CollFunc);
+ }
+ return rc;
+}
diff --git a/src/test_spellfix.c b/ext/misc/spellfix.c
index 3f21d73..eb5442e 100644
--- a/src/test_spellfix.c
+++ b/ext/misc/spellfix.c
@@ -12,18 +12,24 @@
**
** This module implements the spellfix1 VIRTUAL TABLE that can be used
** to search a large vocabulary for close matches. See separate
-** documentation files (spellfix1.wiki and editdist3.wiki) for details.
+** documentation (http://www.sqlite.org/spellfix1.html) for details.
*/
-#if SQLITE_CORE
-# include "sqliteInt.h"
-#else
+#include "sqlite3ext.h"
+SQLITE_EXTENSION_INIT1
+
+#ifndef SQLITE_AMALGAMATION
# include <string.h>
# include <stdio.h>
# include <stdlib.h>
-# include "sqlite3ext.h"
- SQLITE_EXTENSION_INIT1
-#endif /* !SQLITE_CORE */
-#include <ctype.h>
+# include <assert.h>
+# define ALWAYS(X) 1
+# define NEVER(X) 0
+ typedef unsigned char u8;
+ typedef unsigned short u16;
+# include <ctype.h>
+#endif
+
+#ifndef SQLITE_OMIT_VIRTUALTABLE
/*
** Character classes for ASCII characters:
@@ -739,22 +745,22 @@ static int utf8Len(unsigned char c, int N){
}
/*
-** Return TRUE (non-zero) of the To side of the given cost matches
+** Return TRUE (non-zero) if the To side of the given cost matches
** the given string.
*/
static int matchTo(EditDist3Cost *p, const char *z, int n){
if( p->nTo>n ) return 0;
- if( memcmp(p->a+p->nFrom, z, p->nTo)!=0 ) return 0;
+ if( strncmp(p->a+p->nFrom, z, p->nTo)!=0 ) return 0;
return 1;
}
/*
-** Return TRUE (non-zero) of the To side of the given cost matches
+** Return TRUE (non-zero) if the From side of the given cost matches
** the given string.
*/
static int matchFrom(EditDist3Cost *p, const char *z, int n){
assert( p->nFrom<=n );
- if( memcmp(p->a, z, p->nFrom)!=0 ) return 0;
+ if( strncmp(p->a, z, p->nFrom)!=0 ) return 0;
return 1;
}
@@ -1947,7 +1953,7 @@ static int spellfix1Init(
);
}
for(i=3; rc==SQLITE_OK && i<argc; i++){
- if( memcmp(argv[i],"edit_cost_table=",16)==0 && pNew->zCostTable==0 ){
+ if( strncmp(argv[i],"edit_cost_table=",16)==0 && pNew->zCostTable==0 ){
pNew->zCostTable = spellfix1Dequote(&argv[i][16]);
if( pNew->zCostTable==0 ) rc = SQLITE_NOMEM;
continue;
@@ -2668,7 +2674,7 @@ static int spellfix1Update(
if( zCmd==0 ){
pVTab->zErrMsg = sqlite3_mprintf("%s.word may not be NULL",
p->zTableName);
- return SQLITE_CONSTRAINT;
+ return SQLITE_CONSTRAINT_NOTNULL;
}
if( strcmp(zCmd,"reset")==0 ){
/* Reset the edit cost table (if there is one). */
@@ -2676,6 +2682,18 @@ static int spellfix1Update(
p->pConfig3 = 0;
return SQLITE_OK;
}
+ if( strncmp(zCmd,"edit_cost_table=",16)==0 ){
+ editDist3ConfigDelete(p->pConfig3);
+ p->pConfig3 = 0;
+ sqlite3_free(p->zCostTable);
+ p->zCostTable = spellfix1Dequote(zCmd+16);
+ if( p->zCostTable==0 ) return SQLITE_NOMEM;
+ if( p->zCostTable[0]==0 || sqlite3_stricmp(p->zCostTable,"null")==0 ){
+ sqlite3_free(p->zCostTable);
+ p->zCostTable = 0;
+ }
+ return SQLITE_OK;
+ }
pVTab->zErrMsg = sqlite3_mprintf("unknown value for %s.command: \"%w\"",
p->zTableName, zCmd);
return SQLITE_ERROR;
@@ -2805,26 +2823,22 @@ static int spellfix1Register(sqlite3 *db){
return rc;
}
-#if SQLITE_CORE || defined(SQLITE_TEST)
-/*
-** Register the spellfix1 virtual table and its associated functions.
-*/
-int sqlite3Spellfix1Register(sqlite3 *db){
- return spellfix1Register(db);
-}
-#endif
+#endif /* SQLITE_OMIT_VIRTUALTABLE */
-
-#if !SQLITE_CORE
/*
** Extension load function.
*/
-int sqlite3_extension_init(
+#ifdef _WIN32
+__declspec(dllexport)
+#endif
+int sqlite3_spellfix_init(
sqlite3 *db,
char **pzErrMsg,
const sqlite3_api_routines *pApi
){
SQLITE_EXTENSION_INIT2(pApi);
+#ifndef SQLITE_OMIT_VIRTUALTABLE
return spellfix1Register(db);
+#endif
+ return SQLITE_OK;
}
-#endif /* !SQLITE_CORE */
diff --git a/src/test_wholenumber.c b/ext/misc/wholenumber.c
index 7c42d01..63369c6 100644
--- a/src/test_wholenumber.c
+++ b/ext/misc/wholenumber.c
@@ -22,7 +22,8 @@
**
** 1 2 3 4 5 6 7 8 9
*/
-#include "sqlite3.h"
+#include "sqlite3ext.h"
+SQLITE_EXTENSION_INIT1
#include <assert.h>
#include <string.h>
@@ -217,7 +218,13 @@ static int wholenumberBestIndex(
){
pIdxInfo->orderByConsumed = 1;
}
- pIdxInfo->estimatedCost = (double)1;
+ if( (idxNum & 12)==0 ){
+ pIdxInfo->estimatedCost = (double)100000000;
+ }else if( (idxNum & 3)==0 ){
+ pIdxInfo->estimatedCost = (double)5;
+ }else{
+ pIdxInfo->estimatedCost = (double)1;
+ }
return SQLITE_OK;
}
@@ -250,62 +257,18 @@ static sqlite3_module wholenumberModule = {
#endif /* SQLITE_OMIT_VIRTUALTABLE */
-
-/*
-** Register the wholenumber virtual table
-*/
-int wholenumber_register(sqlite3 *db){
+#ifdef _WIN32
+__declspec(dllexport)
+#endif
+int sqlite3_wholenumber_init(
+ sqlite3 *db,
+ char **pzErrMsg,
+ const sqlite3_api_routines *pApi
+){
int rc = SQLITE_OK;
+ SQLITE_EXTENSION_INIT2(pApi);
#ifndef SQLITE_OMIT_VIRTUALTABLE
rc = sqlite3_create_module(db, "wholenumber", &wholenumberModule, 0);
#endif
return rc;
}
-
-#ifdef SQLITE_TEST
-#include <tcl.h>
-/*
-** Decode a pointer to an sqlite3 object.
-*/
-extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
-
-/*
-** Register the echo virtual table module.
-*/
-static int register_wholenumber_module(
- ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
- Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
- int objc, /* Number of arguments */
- Tcl_Obj *CONST objv[] /* Command arguments */
-){
- sqlite3 *db;
- if( objc!=2 ){
- Tcl_WrongNumArgs(interp, 1, objv, "DB");
- return TCL_ERROR;
- }
- if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
- wholenumber_register(db);
- return TCL_OK;
-}
-
-
-/*
-** Register commands with the TCL interpreter.
-*/
-int Sqlitetestwholenumber_Init(Tcl_Interp *interp){
- static struct {
- char *zName;
- Tcl_ObjCmdProc *xProc;
- void *clientData;
- } aObjCmd[] = {
- { "register_wholenumber_module", register_wholenumber_module, 0 },
- };
- int i;
- for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
- Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
- aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
- }
- return TCL_OK;
-}
-
-#endif /* SQLITE_TEST */
diff --git a/ext/rtree/rtree.c b/ext/rtree/rtree.c
index 66da481..16a316f 100644
--- a/ext/rtree/rtree.c
+++ b/ext/rtree/rtree.c
@@ -2660,12 +2660,12 @@ static int newRowid(Rtree *pRtree, i64 *piRowid){
*/
static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
int rc; /* Return code */
- RtreeNode *pLeaf; /* Leaf node containing record iDelete */
+ RtreeNode *pLeaf = 0; /* Leaf node containing record iDelete */
int iCell; /* Index of iDelete cell in pLeaf */
RtreeNode *pRoot; /* Root node of rtree structure */
- /* Obtain a reference to the root node to initialise Rtree.iDepth */
+ /* Obtain a reference to the root node to initialize Rtree.iDepth */
rc = nodeAcquire(pRtree, 1, 0, &pRoot);
/* Obtain a reference to the leaf node that contains the entry
@@ -2863,7 +2863,7 @@ static int rtreeUpdate(
*/
if( rc==SQLITE_OK && nData>1 ){
/* Insert the new record into the r-tree */
- RtreeNode *pLeaf;
+ RtreeNode *pLeaf = 0;
/* Figure out the rowid of the new row. */
if( bHaveRowid==0 ){
@@ -3049,7 +3049,8 @@ static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
static int getNodeSize(
sqlite3 *db, /* Database handle */
Rtree *pRtree, /* Rtree handle */
- int isCreate /* True for xCreate, false for xConnect */
+ int isCreate, /* True for xCreate, false for xConnect */
+ char **pzErr /* OUT: Error message, if any */
){
int rc;
char *zSql;
@@ -3062,6 +3063,8 @@ static int getNodeSize(
if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
}
+ }else{
+ *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
}
}else{
zSql = sqlite3_mprintf(
@@ -3069,6 +3072,9 @@ static int getNodeSize(
pRtree->zDb, pRtree->zName
);
rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
+ if( rc!=SQLITE_OK ){
+ *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
+ }
}
sqlite3_free(zSql);
@@ -3132,7 +3138,7 @@ static int rtreeInit(
memcpy(pRtree->zName, argv[2], nName);
/* Figure out the node size to use. */
- rc = getNodeSize(db, pRtree, isCreate);
+ rc = getNodeSize(db, pRtree, isCreate, pzErr);
/* Create/Connect to the underlying relational database schema. If
** that is successful, call sqlite3_declare_vtab() to configure
diff --git a/ext/rtree/rtree1.test b/ext/rtree/rtree1.test
index e3c7d68..275b132 100644
--- a/ext/rtree/rtree1.test
+++ b/ext/rtree/rtree1.test
@@ -17,6 +17,7 @@ if {![info exists testdir]} {
}
source [file join [file dirname [info script]] rtree_util.tcl]
source $testdir/tester.tcl
+set testprefix rtree1
# Test plan:
#
diff --git a/ext/rtree/rtree5.test b/ext/rtree/rtree5.test
index 8990772..8ff90b0 100644
--- a/ext/rtree/rtree5.test
+++ b/ext/rtree/rtree5.test
@@ -61,7 +61,7 @@ do_test rtree5-1.9 {
do_test rtree5-1.10 {
execsql { SELECT (1<<31)-5, (1<<31)-1, -1*(1<<31), -1*(1<<31)+5 }
} {2147483643 2147483647 -2147483648 -2147483643}
-do_test rtree5-1.10 {
+do_test rtree5-1.11 {
execsql {
INSERT INTO t1 VALUES(2, (1<<31)-5, (1<<31)-1, -1*(1<<31), -1*(1<<31)+5)
}
diff --git a/magic.txt b/magic.txt
new file mode 100644
index 0000000..a2ca209
--- /dev/null
+++ b/magic.txt
@@ -0,0 +1,28 @@
+# This file contains suggested magic(5) text for the unix file(1)
+# utility for recognizing SQLite3 databases.
+#
+# When SQLite is used as an application file format, it is desirable to
+# have file(1) recognize the database file as being with the specific
+# application. You can set the application_id for a database file
+# using:
+#
+# PRAGMA application_id = INTEGER;
+#
+# INTEGER can be any signed 32-bit integer. That integer is written as
+# a 4-byte big-endian integer into offset 68 of the database header.
+#
+# The Monotone application used "PRAGMA user_version=1598903374;" to set
+# its identifier long before "PRAGMA application_id" became available.
+# The user_version is very similar to application_id except that it is
+# stored at offset 68 instead of offset 60. The application_id pragma
+# is preferred. The rule using offset 60 for Monotone is for historical
+# compatibility only.
+#
+0 string =SQLite\ format\ 3
+>68 belong =0x0f055111 Fossil repository -
+>68 belong =0x0f055112 Fossil checkout -
+>68 belong =0x0f055113 Fossil global configuration -
+>68 belong =0x42654462 Bentley Systems BeSQLite Database -
+>68 belong =0x42654c6e Bentley Systems Localization File -
+>60 belong =0x5f4d544e Monotone source repository -
+>0 string =SQLite SQLite3 database
diff --git a/main.mk b/main.mk
index 408e609..912ebb4 100644
--- a/main.mk
+++ b/main.mk
@@ -55,6 +55,7 @@ LIBOBJ+= alter.o analyze.o attach.o auth.o \
callback.o complete.o ctime.o date.o delete.o expr.o fault.o fkey.o \
fts3.o fts3_aux.o fts3_expr.o fts3_hash.o fts3_icu.o fts3_porter.o \
fts3_snippet.o fts3_tokenizer.o fts3_tokenizer1.o \
+ fts3_tokenize_vtab.o \
fts3_unicode.o fts3_unicode2.o \
fts3_write.o func.o global.o hash.o \
icu.o insert.o journal.o legacy.o loadext.o \
@@ -197,6 +198,7 @@ SRC += \
$(TOP)/ext/fts3/fts3_tokenizer.h \
$(TOP)/ext/fts3/fts3_tokenizer.c \
$(TOP)/ext/fts3/fts3_tokenizer1.c \
+ $(TOP)/ext/fts3/fts3_tokenize_vtab.c \
$(TOP)/ext/fts3/fts3_unicode.c \
$(TOP)/ext/fts3/fts3_unicode2.c \
$(TOP)/ext/fts3/fts3_write.c
@@ -240,8 +242,8 @@ TESTSRC = \
$(TOP)/src/test_config.c \
$(TOP)/src/test_demovfs.c \
$(TOP)/src/test_devsym.c \
+ $(TOP)/src/test_fs.c \
$(TOP)/src/test_func.c \
- $(TOP)/src/test_fuzzer.c \
$(TOP)/src/test_hexio.c \
$(TOP)/src/test_init.c \
$(TOP)/src/test_intarray.c \
@@ -257,14 +259,27 @@ TESTSRC = \
$(TOP)/src/test_schema.c \
$(TOP)/src/test_server.c \
$(TOP)/src/test_stat.c \
+ $(TOP)/src/test_sqllog.c \
$(TOP)/src/test_superlock.c \
$(TOP)/src/test_syscall.c \
$(TOP)/src/test_tclvar.c \
$(TOP)/src/test_thread.c \
$(TOP)/src/test_vfs.c \
- $(TOP)/src/test_wholenumber.c \
$(TOP)/src/test_wsd.c
+# Extensions to be statically loaded.
+#
+TESTSRC += \
+ $(TOP)/ext/misc/amatch.c \
+ $(TOP)/ext/misc/closure.c \
+ $(TOP)/ext/misc/fuzzer.c \
+ $(TOP)/ext/misc/ieee754.c \
+ $(TOP)/ext/misc/nextchar.c \
+ $(TOP)/ext/misc/regexp.c \
+ $(TOP)/ext/misc/spellfix.c \
+ $(TOP)/ext/misc/wholenumber.c
+
+
#TESTSRC += $(TOP)/ext/fts2/fts2_tokenizer.c
#TESTSRC += $(TOP)/ext/fts3/fts3_tokenizer.c
@@ -278,6 +293,7 @@ TESTSRC2 = \
$(TOP)/src/func.c \
$(TOP)/src/insert.c \
$(TOP)/src/wal.c \
+ $(TOP)/src/main.c \
$(TOP)/src/mem5.c \
$(TOP)/src/os.c \
$(TOP)/src/os_unix.c \
@@ -362,6 +378,10 @@ sqlite3$(EXE): $(TOP)/src/shell.c libsqlite3.a sqlite3.h
$(TOP)/src/shell.c \
libsqlite3.a $(LIBREADLINE) $(TLIBS) $(THREADLIB)
+mptester$(EXE): sqlite3.c $(TOP)/mptest/mptest.c
+ $(TCCX) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \
+ $(TLIBS) $(THREADLIB)
+
sqlite3.o: sqlite3.c
$(TCCX) -c sqlite3.c
@@ -382,6 +402,7 @@ target_source: $(SRC) $(TOP)/tool/vdbe-compress.tcl
sqlite3.c: target_source $(TOP)/tool/mksqlite3c.tcl
tclsh $(TOP)/tool/mksqlite3c.tcl
+ cp tsrc/shell.c tsrc/sqlite3ext.h .
echo '#ifndef USE_SYSTEM_SQLITE' >tclsqlite3.c
cat sqlite3.c >>tclsqlite3.c
echo '#endif /* USE_SYSTEM_SQLITE */' >>tclsqlite3.c
@@ -508,6 +529,9 @@ fts3_tokenizer.o: $(TOP)/ext/fts3/fts3_tokenizer.c $(HDR) $(EXTHDR)
fts3_tokenizer1.o: $(TOP)/ext/fts3/fts3_tokenizer1.c $(HDR) $(EXTHDR)
$(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_tokenizer1.c
+fts3_tokenize_vtab.o: $(TOP)/ext/fts3/fts3_tokenize_vtab.c $(HDR) $(EXTHDR)
+ $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_tokenize_vtab.c
+
fts3_unicode.o: $(TOP)/ext/fts3/fts3_unicode.c $(HDR) $(EXTHDR)
$(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_unicode.c
@@ -546,7 +570,7 @@ TESTFIXTURE_FLAGS += -DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" -DSQLITE_CORE
testfixture$(EXE): $(TESTSRC2) libsqlite3.a $(TESTSRC) $(TOP)/src/tclsqlite.c
$(TCCX) $(TCL_FLAGS) -DTCLSH=1 $(TESTFIXTURE_FLAGS) \
$(TESTSRC) $(TESTSRC2) $(TOP)/src/tclsqlite.c \
- -o testfixture$(EXE) $(LIBTCL) $(THREADLIB) libsqlite3.a
+ -o testfixture$(EXE) $(LIBTCL) libsqlite3.a $(THREADLIB)
amalgamation-testfixture$(EXE): sqlite3.c $(TESTSRC) $(TOP)/src/tclsqlite.c
$(TCCX) $(TCL_FLAGS) -DTCLSH=1 $(TESTFIXTURE_FLAGS) \
@@ -565,6 +589,9 @@ fulltest: testfixture$(EXE) sqlite3$(EXE)
soaktest: testfixture$(EXE) sqlite3$(EXE)
./testfixture$(EXE) $(TOP)/test/all.test -soak=1
+fulltestonly: testfixture$(EXE) sqlite3$(EXE)
+ ./testfixture$(EXE) $(TOP)/test/full.test
+
test: testfixture$(EXE) sqlite3$(EXE)
./testfixture$(EXE) $(TOP)/test/veryquick.test
@@ -615,5 +642,8 @@ clean:
rm -f testfixture testfixture.exe
rm -f threadtest3 threadtest3.exe
rm -f sqlite3.c fts?amal.c tclsqlite3.c
+ rm -f sqlite3rc.h
+ rm -f shell.c sqlite3ext.h
rm -f sqlite3_analyzer sqlite3_analyzer.exe sqlite3_analyzer.c
- rm -f sqlite-output.vsix
+ rm -f sqlite-*-output.vsix
+ rm -f mptester mptester.exe
diff --git a/manifest b/manifest
index 6cd0a46..5b3b906 100644
--- a/manifest
+++ b/manifest
@@ -1,12 +1,12 @@
-C Version\s3.7.14.1
-D 2012-10-04T19:37:12.994
+C Version\s3.7.17
+D 2013-05-20T00:56:22.515
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
-F Makefile.in abd5c10d21d1395f140d9e50ea999df8fa4d6376
+F Makefile.in f6b58b7bdf6535f0f0620c486dd59aa4662c0b4f
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
-F Makefile.msc 2d696f01c228995e98b3b953a08b7bba1d48c130
-F Makefile.vxworks 879f034a64062a364b21000266bbd5bc6e0c19b9
+F Makefile.msc 5dc042f51187414d5886ac6d8308630d484690c4
+F Makefile.vxworks db21ed42a01d5740e656b16f92cb5d8d5e5dd315
F README cd04a36fbc7ea56932a4052d7d0b7f09f27c33d6
-F VERSION 9694dff497fc7e53037a5890f30ab069c26e8ab7
+F VERSION 05c7bd63b96f31cfdef5c766ed91307ac121f5aa
F aclocal.m4 a5c22d164aff7ed549d53a90fa56d56955281f50
F addopcodes.awk 17dc593f791f874d2c23a0f9360850ded0286531
F art/sqlite370.eps aa97a671332b432a54e1d74ff5e8775be34200c2
@@ -15,23 +15,23 @@ F art/sqlite370.jpg d512473dae7e378a67e28ff96a34da7cb331def2
F config.guess 226d9a188c6196f3033ffc651cbc9dcee1a42977
F config.h.in 0921066a13130082764ab4ab6456f7b5bebe56de
F config.sub 9ebe4c3b3dab6431ece34f16828b594fb420da55
-F configure e2c3b28dec7f5d1f72dd7465c87bb37f6af1931f x
-F configure.ac 6e909664785b8184db2179013cd9d574f96ca3a3
+F configure 8bb8bd13d3c918c4c1c73480930e81f955ac298a x
+F configure.ac 81c43d151d0b0e406be056394cc9ff4cb3fd0444
F contrib/sqlitecon.tcl 210a913ad63f9f991070821e599d600bd913e0ad
F doc/lemon.html 334dbf6621b8fb8790297ec1abf3cfa4621709d1
F doc/pager-invariants.txt 27fed9a70ddad2088750c4a2b493b63853da2710
F doc/vfs-shm.txt e101f27ea02a8387ce46a05be2b1a902a021d37a
F ext/README.txt 913a7bd3f4837ab14d7e063304181787658b14e1
-F ext/async/README.txt 0c541f418b14b415212264cbaaf51c924ec62e5b
-F ext/async/sqlite3async.c 733a9f21b1066f44ff07b9c0da973b1e483d1e0c
-F ext/async/sqlite3async.h a21e1252deb14a2c211f0e165c4b9122a8f1f344
+F ext/async/README.txt e12275968f6fde133a80e04387d0e839b0c51f91
+F ext/async/sqlite3async.c b5a3e30f538a9ffe81538b3063b4d5963f9bb422
+F ext/async/sqlite3async.h f489b080af7e72aec0e1ee6f1d98ab6cf2e4dcef
F ext/fts1/README.txt 20ac73b006a70bcfd80069bdaf59214b6cf1db5e
F ext/fts1/ft_hash.c 3927bd880e65329bdc6f506555b228b28924921b
-F ext/fts1/ft_hash.h 1a35e654a235c2c662d3ca0dfc3138ad60b8b7d5
+F ext/fts1/ft_hash.h 06df7bba40dadd19597aa400a875dbc2fed705ea
F ext/fts1/fts1.c 3e7b253e61aab0bb1fea808c7a0ce36c19432acc
F ext/fts1/fts1.h 6060b8f62c1d925ea8356cb1a6598073eb9159a6
F ext/fts1/fts1_hash.c 3196cee866edbebb1c0521e21672e6d599965114
-F ext/fts1/fts1_hash.h 957d378355ed29f672cd5add012ce8b088a5e089
+F ext/fts1/fts1_hash.h e7f0d761353996a8175eda351104acfde23afcb0
F ext/fts1/fts1_porter.c b1c7304b8988ba3f764a147cdd32043b4913ea7b
F ext/fts1/fts1_tokenizer.h fdea722c38a9f82ed921642981234f666e47919c
F ext/fts1/fts1_tokenizer1.c fd00d1fe4dc30dfc5c64cba695ce34f4af20d2fa
@@ -41,55 +41,65 @@ F ext/fts1/simple_tokenizer.c 1844d72f7194c3fd3d7e4173053911bf0661b70d
F ext/fts1/tokenizer.h 0c53421b832366d20d720d21ea3e1f6e66a36ef9
F ext/fts2/README.tokenizers 21e3684ea5a095b55d70f6878b4ce6af5932dfb7
F ext/fts2/README.txt 8c18f41574404623b76917b9da66fcb0ab38328d
-F ext/fts2/fts2.c 4ef7d7ecf590da0dd416ac54612c53a7d4175790
+F ext/fts2/fts2.c b48cc0bb657c0a421f4067b79aa0354bd16a046d
F ext/fts2/fts2.h da5f76c65163301d1068a971fd32f4119e3c95fa
F ext/fts2/fts2_hash.c 2689e42e1107ea67207f725cf69cf8972d00cf93
-F ext/fts2/fts2_hash.h 9a5b1be94664139f93217a0770d7144425cffb3a
-F ext/fts2/fts2_icu.c 1ea9993a39c9783c2e2d7446d055e9d64411dda0
+F ext/fts2/fts2_hash.h 1824b99dfd8d0225facbdb26a2c87289b2e7dcf8
+F ext/fts2/fts2_icu.c 51c5cd3c04954badd329fa738c95fcdb717b5188
F ext/fts2/fts2_porter.c 747056987951f743e955c8479f1df21a565720fe
-F ext/fts2/fts2_tokenizer.c 26e993a00b2bd5b6e73c155597361710b12ffe25
-F ext/fts2/fts2_tokenizer.h a7e46462d935a314b2682287f12f27530a3ee08e
+F ext/fts2/fts2_tokenizer.c a86d08c9634fabfa237c8f379008de2e11248d36
+F ext/fts2/fts2_tokenizer.h 27a1a99ca2d615cf7e142839b8d79e8751b4529e
F ext/fts2/fts2_tokenizer1.c 0123d21078e053bd98fd6186c5c6dc6d67969f2e
F ext/fts2/mkfts2amal.tcl 974d5d438cb3f7c4a652639262f82418c1e4cff0
F ext/fts3/README.content fdc666a70d5257a64fee209f97cf89e0e6e32b51
F ext/fts3/README.syntax a19711dc5458c20734b8e485e75fb1981ec2427a
F ext/fts3/README.tokenizers e0a8b81383ea60d0334d274fadf305ea14a8c314
F ext/fts3/README.txt 8c18f41574404623b76917b9da66fcb0ab38328d
-F ext/fts3/fts3.c ab90126ee0163539d21d0618d22afa2eb645f7e2
+F ext/fts3/fts3.c 4bc160e6ff9ab5456b600f389f8941485ea5082f
F ext/fts3/fts3.h 3a10a0af180d502cecc50df77b1b22df142817fe
-F ext/fts3/fts3Int.h 1e58825246b56259267382d2f9902774c049460a
-F ext/fts3/fts3_aux.c 5205182bd8f372782597888156404766edf5781e
-F ext/fts3/fts3_expr.c dbc7ba4c3a6061adde0f38ed8e9b349568299551
+F ext/fts3/fts3Int.h 0b167bed9e63151635620a4f639bc62ac6012cba
+F ext/fts3/fts3_aux.c b02632f6dd0e375ce97870206d914ea6d8df5ccd
+F ext/fts3/fts3_expr.c 193d6fc156d744ab548a2ed06c31869e54dac739
F ext/fts3/fts3_hash.c 8dd2d06b66c72c628c2732555a32bc0943114914
-F ext/fts3/fts3_hash.h 8331fb2206c609f9fc4c4735b9ab5ad6137c88ec
-F ext/fts3/fts3_icu.c b85eca4a52e5ec11b94392de5167974c11906d4a
+F ext/fts3/fts3_hash.h 39cf6874dc239d6b4e30479b1975fe5b22a3caaf
+F ext/fts3/fts3_icu.c e319e108661147bcca8dd511cd562f33a1ba81b5
F ext/fts3/fts3_porter.c a465b49fcb8249a755792f87516eff182efa42b3
-F ext/fts3/fts3_snippet.c bf67520ae9d2352a65368ed101729ff701c08808
+F ext/fts3/fts3_snippet.c 5fcfcafff46a2a3a63b8e59fcb51987d01c74695
F ext/fts3/fts3_term.c a521f75132f9a495bdca1bdd45949b3191c52763
-F ext/fts3/fts3_test.c 348f7d08cae05285794e23dc4fe8b8fdf66e264a
-F ext/fts3/fts3_tokenizer.c e94a8b901066031437ccfe4769fc76370257cede
-F ext/fts3/fts3_tokenizer.h 66dec98e365854b6cd2d54f1a96bb6d428fc5a68
+F ext/fts3/fts3_test.c f9a1a1702db1bfad3e2d0064746eeb808f125489
+F ext/fts3/fts3_tokenize_vtab.c 011170fe9eba5ff062f1a31d3188e00267716706
+F ext/fts3/fts3_tokenizer.c bbdc731bc91338050675c6d1da9ab82147391e16
+F ext/fts3/fts3_tokenizer.h 64c6ef6c5272c51ebe60fc607a896e84288fcbc3
F ext/fts3/fts3_tokenizer1.c 5c98225a53705e5ee34824087478cf477bdb7004
-F ext/fts3/fts3_unicode.c 49e36e6ba59f79e6bd6a8bfe434570fe48d20559
+F ext/fts3/fts3_unicode.c 92391b4b4fb043564c6539ea9b8661e3bcba47b9
F ext/fts3/fts3_unicode2.c a863f05f758af36777dffc2facc898bc73fec896
-F ext/fts3/fts3_write.c c30c49f3debb9497a07f15cc4c042815e35474ef
+F ext/fts3/fts3_write.c d92c6cbe07363791cfe8b62b4dee67e6f8afc9e2
F ext/fts3/fts3speed.tcl b54caf6a18d38174f1a6e84219950d85e98bb1e9
F ext/fts3/mkfts3amal.tcl 252ecb7fe6467854f2aa237bf2c390b74e71f100
F ext/fts3/tool/fts3view.c 6cfc5b67a5f0e09c0d698f9fd012c784bfaa9197
F ext/fts3/unicode/CaseFolding.txt 8c678ca52ecc95e16bc7afc2dbf6fc9ffa05db8c
F ext/fts3/unicode/UnicodeData.txt cd07314edb62d49fde34debdaf92fa2aa69011e7
F ext/fts3/unicode/mkunicode.tcl 7a9bc018e2962abb79563c5a39fe581fcbf2f675
-F ext/icu/README.txt bf8461d8cdc6b8f514c080e4e10dc3b2bbdfefa9
+F ext/icu/README.txt d9fbbad0c2f647c3fdf715fc9fd64af53aedfc43
F ext/icu/icu.c eb9ae1d79046bd7871aa97ee6da51eb770134b5a
F ext/icu/sqliteicu.h 728867a802baa5a96de7495e9689a8e01715ef37
+F ext/misc/amatch.c eae8454cd9dcb287b2a3ec2e65a865a4ac5f0d06
+F ext/misc/closure.c 40788c54c59190a1f52f6492a260d8894a246fe9
+F ext/misc/fuzzer.c 51bd96960b6b077d41d6f3cedefbcb57f29efaa2
+F ext/misc/ieee754.c 2565ce373d842977efe0922dc50b8a41b3289556
+F ext/misc/nextchar.c 1131e2b36116ffc6fe6b2e3464bfdace27978b1e
+F ext/misc/regexp.c c25c65fe775f5d9801fb8573e36ebe73f2c0c2e0
+F ext/misc/rot13.c 1ac6f95f99b575907b9b09c81a349114cf9be45a
+F ext/misc/spellfix.c 6d7ce6105a4b7729f6c44ccdf1ab7e80d9707c02
+F ext/misc/wholenumber.c 784b12543d60702ebdd47da936e278aa03076212
F ext/rtree/README 6315c0d73ebf0ec40dedb5aa0e942bc8b54e3761
-F ext/rtree/rtree.c d17aecb7a92762efa7b1f5d5fd7c88fd77d70827
+F ext/rtree/rtree.c 757abea591d4ff67c0ff4e8f9776aeda86b18c14
F ext/rtree/rtree.h 834dbcb82dc85b2481cde6a07cdadfddc99e9b9e
-F ext/rtree/rtree1.test e474a2b5eff231496dbd073fe67e5fbaf7f444c9
+F ext/rtree/rtree1.test cf679265ecafff494a768ac9c2f43a70915a6290
F ext/rtree/rtree2.test acbb3a4ce0f4fbc2c304d2b4b784cfa161856bba
F ext/rtree/rtree3.test a494da55c30ee0bc9b01a91c80c81b387b22d2dc
F ext/rtree/rtree4.test c8fe384f60ebd49540a5fecc990041bf452eb6e0
-F ext/rtree/rtree5.test 9a229678a00f40e6aedb40cb3a07ec5444af892c
+F ext/rtree/rtree5.test 6a510494f12454bf57ef28f45bc7764ea279431e
F ext/rtree/rtree6.test 3ff9113b4a872fa935309e3511cd9b7cdb4d2472
F ext/rtree/rtree7.test 1fa710b9e6bf997a0c1a537b81be7bb6fded1971
F ext/rtree/rtree8.test 9772e16da71e17e02bdebf0a5188590f289ab37d
@@ -103,158 +113,162 @@ F ext/rtree/tkt3363.test 142ab96eded44a3615ec79fba98c7bde7d0f96de
F ext/rtree/viewrtree.tcl eea6224b3553599ae665b239bd827e182b466024
F install-sh 9d4de14ab9fb0facae2f48780b874848cbf2f895 x
F ltmain.sh 3ff0879076df340d2e23ae905484d8c15d5fdea8
-F main.mk 72026405046ed5b1f0368943b89c0aa29ad558b6
+F magic.txt 3f820e18c43504b25da40ff4b4cdb66dc4c4907e
+F main.mk a8ebdf910e2cc10db1f9f54ec316f637458e8001
F mkdll.sh 7d09b23c05d56532e9d44a50868eb4b12ff4f74a
F mkextu.sh 416f9b7089d80e5590a29692c9d9280a10dbad9f
F mkextw.sh 4123480947681d9b434a5e7b1ee08135abe409ac
F mkopcodec.awk f6fccee29e68493bfd90a2e0466ede5fa94dd2fc
F mkopcodeh.awk 29b84656502eee5f444c3147f331ee686956ab0e
F mkso.sh fd21c06b063bb16a5d25deea1752c2da6ac3ed83
-F publish.sh 313c5b2425f2cf5e547db7549a9796acc4508f22
-F publish_osx.sh 2ad2ee7d50632dff99949edc9c162dbb052f7534
+F mptest/config01.test 3c6adcbc50b991866855f1977ff172eb6d901271
+F mptest/config02.test 4415dfe36c48785f751e16e32c20b077c28ae504
+F mptest/crash01.test cce8e306d8596d5a2e497e27112dae1f6e5e3538
+F mptest/crash02.subtest f4ef05adcd15d60e5d2bd654204f2c008b519df8
+F mptest/mptest.c 499a74af4be293b7c1c7c3d40f332b67227dd739
+F mptest/multiwrite01.test 499ad0310da8dff8e8f98d2e272fc2a8aa741b2e
F spec.template 86a4a43b99ebb3e75e6b9a735d5fd293a24e90ca
F sqlite.pc.in 42b7bf0d02e08b9e77734a47798d1a55a9e0716b
F sqlite3.1 6be1ad09113570e1fc8dcaff84c9b0b337db5ffc
F sqlite3.pc.in ae6f59a76e862f5c561eb32a380228a02afc3cad
-F src/alter.c 149cc80d9257971b0bff34e58fb2263e01998289
-F src/analyze.c 7553068d21e32a57fc33ab6b2393fc8c1ba41410
-F src/attach.c 577bf5675b0c50495fc28549f2fcbdb1bac71143
+F src/alter.c f8db986c03eb0bfb221523fc9bbb9d0b70de3168
+F src/analyze.c d5f895810e8ff9737c9ec7b76abc3dcff5860335
+F src/attach.c 1816f5a9eea8d2010fc2b22b44f0f63eb3a62704
F src/auth.c 523da7fb4979469955d822ff9298352d6b31de34
-F src/backup.c 5b31b24d6814b11de763debf342c8cd0a15a4910
-F src/bitvec.c 26675fe8e431dc555e6f2d0e11e651d172234aa1
+F src/backup.c b266767351ae2d847716c56fcb2a1fea7c761c03
+F src/bitvec.c 19a4ba637bd85f8f63fc8c9bae5ade9fb05ec1cb
F src/btmutex.c 976f45a12e37293e32cae0281b15a21d48a8aaa7
-F src/btree.c 97edf88abd2b66f31886ba977d2b3b2a21d81d4c
-F src/btree.h 4aee02e879211bfcfd3f551769578d2e940ab6c2
-F src/btreeInt.h 4e5c2bd0f9b36b2a815a6d84f771a61a65830621
-F src/build.c a3b700afd475e6387da59be6f2e86161e80d6d87
-F src/callback.c 0cb4228cdcd827dcc5def98fb099edcc9142dbcd
+F src/btree.c fcfbe61a311e54224b23527bbf7586ce320e7b40
+F src/btree.h 6fa8a3ff2483d0bb64a9f0105a8cedeac9e00cca
+F src/btreeInt.h eecc84f02375b2bb7a44abbcbbe3747dde73edb2
+F src/build.c 92ef9483189389828966153c5950f2e5a49c1182
+F src/callback.c d7e46f40c3cf53c43550b7da7a1d0479910b62cc
F src/complete.c dc1d136c0feee03c2f7550bafc0d29075e36deac
-F src/ctime.c 500d019da966631ad957c37705642be87524463b
+F src/ctime.c 4262c227bc91cecc61ae37ed3a40f08069cfa267
F src/date.c 067a81c9942c497aafd2c260e13add8a7d0c7dd4
-F src/delete.c 335f36750dc6ac88d580aa36a6487459be9889de
-F src/expr.c 217840a107dcc1e5dbb57cea311daad04bedbb9a
+F src/delete.c aeabdabeeeaa0584127f291baa9617153d334778
+F src/expr.c e40d198a719aba1d2514f6e1541f9154f976ceb6
F src/fault.c 160a0c015b6c2629d3899ed2daf63d75754a32bb
-F src/fkey.c 9c77d842dc9961d92a06a65abb80c64ef1750296
-F src/func.c 18dfedfb857e100b05755a1b12e88b389f957879
-F src/global.c 4cfdca5cb0edd33c4d021baec4ede958cb2c793b
-F src/hash.c a4031441741932da9e7a65bee2b36b5d0e81c073
-F src/hash.h 2894c932d84d9f892d4b4023a75e501f83050970
+F src/fkey.c e16942bd5c8a868ac53287886464a5ed0e72b179
+F src/func.c d3fdcff9274bc161152e67ed3f626841c247f4b9
+F src/global.c 5caf4deab621abb45b4c607aad1bd21c20aac759
+F src/hash.c ac3470bbf1ca4ae4e306a8ecb0fdf1731810ffe4
+F src/hash.h 8890a25af81fb85a9ad7790d32eedab4b994da22
F src/hwtime.h d32741c8f4df852c7d959236615444e2b1063b08
-F src/insert.c b090d0a9fb9ff2dbdeaf66aedccf98cd13b1af60
-F src/journal.c 552839e54d1bf76fb8f7abe51868b66acacf6a0e
-F src/legacy.c a199d7683d60cef73089e892409113e69c23a99f
-F src/lempar.c 0ee69fca0be54cd93939df98d2aca4ca46f44416
-F src/loadext.c f20382fbaeec832438a1ba7797bee3d3c8a6d51d
-F src/main.c 02255cf1da50956c5427c469abddb15bccc4ba09
+F src/insert.c f7cb141e8ce257cb6b15c497f09e4e23d6055599
+F src/journal.c b4124532212b6952f42eb2c12fa3c25701d8ba8d
+F src/legacy.c 0df0b1550b9cc1f58229644735e317ac89131f12
+F src/lempar.c cdf0a000315332fc9b50b62f3b5e22e080a0952b
+F src/loadext.c c48f7f3f170e502fe0cc20748e03c6e0b5a016c2
+F src/main.c c6419ef57392b1aa0cf6ed9c80dfc06b153fe299
F src/malloc.c fe085aa851b666b7c375c1ff957643dc20a04bf6
F src/mem0.c 6a55ebe57c46ca1a7d98da93aaa07f99f1059645
F src/mem1.c 437c7c4af964895d4650f29881df63535caaa1fa
F src/mem2.c e307323e86b5da1853d7111b68fd6b84ad6f09cf
F src/mem3.c 61c9d47b792908c532ca3a62b999cf21795c6534
F src/mem5.c c2c63b7067570b00bf33d751c39af24182316f7f
-F src/memjournal.c 0ebce851677a7ac035ba1512a7e65851b34530c6
+F src/memjournal.c 41a598445c8f249bd9b26ecae700c60dcf34cbf3
F src/mutex.c d3b66a569368015e0fcb1ac15f81c119f504d3bc
F src/mutex.h 5bc526e19dccc412b7ff04642f6fdad3fdfdabea
F src/mutex_noop.c 7682796b7d8d39bf1c138248858efcd10c9e1553
F src/mutex_unix.c c3a4e00f96ba068a8dbef34084465979aaf369cc
F src/mutex_w32.c 32a9b3841e2d757355f0012b860b1bc5e01eafa0
F src/notify.c 976dd0f6171d4588e89e874fcc765e92914b6d30
-F src/os.c e1acdc09ff3ac2412945cca9766e2dcf4675f31c
-F src/os.h 027491c77d2404c0a678bb3fb06286f331eb9b57
+F src/os.c b4ad71336fd96f97776f75587cd9e8218288f5be
+F src/os.h 4a46270a64e9193af4a0aaa3bc2c66dc07c29b3f
F src/os_common.h 92815ed65f805560b66166e3583470ff94478f04
-F src/os_unix.c 69b2fe66316524eebf5f1ce85c1fdfe2952307e9
-F src/os_win.c 5dec8fe85ee547152075c020db72aec4382f0d0a
-F src/pager.c 5665fa9ecec51f11dabdfd8eefefa89391856007
-F src/pager.h 8b8c9bc065a3c66769df8724dfdf492ee1aab3c5
-F src/parse.y f29df90bd3adc64b33114ab1de9fb7768fcf2099
+F src/os_unix.c 75ce49309b8352c7173ce1ef6fc9e8d1f6daab10
+F src/os_win.c 32b8adca3e989565713ff74098b3cb2cb25d6e59
+F src/pager.c 49e23f9898113ddfe90942bdf1c1ef57955d0921
+F src/pager.h 5cb78b8e1adfd5451e600be7719f5a99d87ac3b1
+F src/parse.y 9708365594eea519cdc8504dee425c0a41c79502
F src/pcache.c f8043b433a57aba85384a531e3937a804432a346
-F src/pcache.h 1b5dcc3dc8103d03e625b177023ee67764fa6b7c
+F src/pcache.h a5e4f5d9f5d592051d91212c5949517971ae6222
F src/pcache1.c 9fd22671c270b35131ef480bbc00392b8b5f8ab9
-F src/pragma.c 97f9357f0e7e5fb46a2519f14539550aa07db49f
-F src/prepare.c 33291b83cca285718048d219c67b8298501fa3a5
+F src/pragma.c 8779308bc1ea1901c4bc94dfe9a83d436f73f52c
+F src/prepare.c 743e484233c51109666d402f470523553b41797c
F src/printf.c 4a9f882f1c1787a8b494a2987765acf9d97ac21f
F src/random.c cd4a67b3953b88019f8cd4ccd81394a8ddfaba50
-F src/resolve.c 9e28280ec98035f31900fdd1db01f86f68ca6c32
+F src/resolve.c 89f9003e8316ee3a172795459efc2a0274e1d5a8
F src/rowset.c 64655f1a627c9c212d9ab497899e7424a34222e0
-F src/select.c f843c872a97baa1594c2cc3d4c003409a7bd03af
-F src/shell.c 87953c5d9c73d9494db97d1607e2e2280418f261
-F src/sqlite.h.in c447d35212736c4c77d86bc2d00f6cf4d4c12131
+F src/select.c a4641882279becc200f2680f55f3e89d4e7c7f78
+F src/shell.c 2109d54f67c815a100abd7dc6a6e25eddb3b97eb
+F src/sqlite.h.in 5a5a22a9b192d81a9e5dee00274e3a0484c4afb1
F src/sqlite3.rc fea433eb0a59f4c9393c8e6d76a6e2596b1fe0c0
-F src/sqlite3ext.h 6904f4aadf976f95241311fbffb00823075d9477
-F src/sqliteInt.h 053e03a532beb909ead2df0721db67cdb4c48ae8
+F src/sqlite3ext.h d936f797812c28b81b26ed18345baf8db28a21a5
+F src/sqliteInt.h 4cc782c9a89b3ddd663e7f68af3fa9e5af596f8b
F src/sqliteLimit.h 164b0e6749d31e0daa1a4589a169d31c0dec7b3d
-F src/status.c 35939e7e03abf1b7577ce311f48f682c40de3208
+F src/status.c bedc37ec1a6bb9399944024d63f4c769971955a9
F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e
-F src/tclsqlite.c d20022c647aa7a871d7b4038c4ec971cafe39744
-F src/test1.c 3d70f7c5987f186884cfebbfa7151a7d3d67d86e
-F src/test2.c 4178056dd1e7d70f954ad8a1e3edb71a2a784daf
-F src/test3.c 3c3c2407fa6ec7a19e24ae23f7cb439d0275a60d
-F src/test4.c bf9fa9bece01de08e6f5e02314e4af5c13590dfa
+F src/tclsqlite.c 2ecec9937e69bc17560ad886da35195daa7261b8
+F src/test1.c 045d45a4f7eeb5d963f8fc150339f1bad279011a
+F src/test2.c 7355101c085304b90024f2261e056cdff13c6c35
+F src/test3.c 1c0e5d6f080b8e33c1ce8b3078e7013fdbcd560c
+F src/test4.c 9b32d22f5f150abe23c1830e2057c4037c45b3df
F src/test5.c a6d1ac55ac054d0b2b8f37b5e655b6c92645a013
-F src/test6.c 417e1e214734393c24a8ee80b41485a9c4169123
-F src/test7.c 2e0781754905c8adc3268d8f0967e7633af58843
-F src/test8.c 8bcce65e5ee027fbfd7da41d28371aabbfd369ff
+F src/test6.c a437f76f9874d2563352a7e6cd0d43217663c220
+F src/test7.c 126b886b53f0358b92aba9b81d3fcbfbe9a93cd6
+F src/test8.c 7ee77ea522ae34aa691dfe407139dec80d4fc039
F src/test9.c bea1e8cf52aa93695487badedd6e1886c321ea60
-F src/test_async.c 0612a752896fad42d55c3999a5122af10dcf22ad
-F src/test_autoext.c 30e7bd98ab6d70a62bb9ba572e4c7df347fe645e
-F src/test_backup.c c129c91127e9b46e335715ae2e75756e25ba27de
+F src/test_async.c 21e11293a2f72080eda70e1124e9102044531cd8
+F src/test_autoext.c 5c95b5d435eaa09d6c0e7d90371c5ca8cd567701
+F src/test_backup.c 3875e899222b651e18b662f86e0e50daa946344e
F src/test_btree.c 5b89601dcb42a33ba8b820a6b763cc9cb48bac16
-F src/test_config.c 09781397ccc24268cb895be0d4c21b4aad651486
+F src/test_config.c 95bb33e9dcaa340a296c0bf0e0ba3d1a1c8004c0
F src/test_demovfs.c 20a4975127993f4959890016ae9ce5535a880094
F src/test_devsym.c e7498904e72ba7491d142d5c83b476c4e76993bc
+F src/test_fs.c 8f786bfd0ad48030cf2a06fb1f050e9c60a150d7
F src/test_func.c 3a8dd37c08ab43b76d38eea2836e34a3897bf170
-F src/test_fuzzer.c 1d26aa965120420bc14807da29d4d4541bfa6148
F src/test_hexio.c abfdecb6fa58c354623978efceb088ca18e379cd
F src/test_init.c 3cbad7ce525aec925f8fda2192d576d47f0d478a
-F src/test_intarray.c d879bbf8e4ce085ab966d1f3c896a7c8b4f5fc99
-F src/test_intarray.h 489edb9068bb926583445cb02589344961054207
+F src/test_intarray.c 87847c71c3c36889c0bcc9c4baf9d31881665d61
+F src/test_intarray.h b999bb18d090b8d9d9c49d36ec37ef8f341fe169
F src/test_journal.c f5c0a05b7b3d5930db769b5ee6c3766dc2221a64
F src/test_loadext.c df586c27176e3c2cb2e099c78da67bf14379a56e
-F src/test_malloc.c 3f5903a1528fd32fe4c472a3bd0259128d8faaef
-F src/test_multiplex.c ac0fbc1748e5b86a41a1d7a84654fae0d53a881d
-F src/test_multiplex.h e99c571bc4968b7a9363b661481f3934bfead61d
-F src/test_mutex.c a6bd7b9cf6e19d989e31392b06ac8d189f0d573e
+F src/test_malloc.c 2855429b8232107b3296409be2a8eb68345290c2
+F src/test_multiplex.c 5d691eeb6cb6aa7888da28eba5e62a9a857d3c0f
+F src/test_multiplex.h 9b63b95f07acedee425fdfe49a47197c9bf5f9d8
+F src/test_mutex.c 293042d623ebba969160f471a82aa1551626454f
F src/test_onefile.c 0396f220561f3b4eedc450cef26d40c593c69a25
F src/test_osinst.c 90a845c8183013d80eccb1f29e8805608516edba
F src/test_pcache.c a5cd24730cb43c5b18629043314548c9169abb00
-F src/test_quota.c 8ab295092c70903ca6f3209fa4c75f5cb6c1bf8e
+F src/test_quota.c 30c64f0ef84734f2231a686df41ed882b0c59bc0
F src/test_quota.h 8761e463b25e75ebc078bd67d70e39b9c817a0cb
-F src/test_rtree.c aba603c949766c4193f1068b91c787f57274e0d9
+F src/test_rtree.c 1d764c352b5348bb2869ff5f54aff8eadcb96041
F src/test_schema.c 8c06ef9ddb240c7a0fcd31bc221a6a2aade58bf0
F src/test_server.c 2f99eb2837dfa06a4aacf24af24c6affdf66a84f
-F src/test_spellfix.c 76dd8d3111d2f5354c374f71fa23b752bd0b029c
+F src/test_sqllog.c c1c1bbedbcaf82b93d83e4f9dd990e62476a680e
F src/test_stat.c d1569c7a4839f13e80187e2c26b2ab4da2d03935
F src/test_superlock.c 2b97936ca127d13962c3605dbc9a4ef269c424cd
-F src/test_syscall.c a992d8c80ea91fbf21fb2dd570db40e77dd7e6ae
+F src/test_syscall.c 16dbe79fb320fadb5acd7a0a59f49e52ab2d2091
F src/test_tclvar.c f4dc67d5f780707210d6bb0eb6016a431c04c7fa
-F src/test_thread.c e286f2173563f2a1747c24bcda6b9d030bf4f4e4
-F src/test_vfs.c c6260ef238c1142c8f8bd402db02216afd182ae3
-F src/test_vfstrace.c f60e12754e65c05386aab59db8d2ae086314138d
-F src/test_wholenumber.c 3d2b9ed1505c40ad5c5ca2ad16ae7a289d6cc251
+F src/test_thread.c 1e133a40b50e9c035b00174035b846e7eef481cb
+F src/test_vfs.c 8e6087a8b3dcc260282074b0efba14b76311120c
+F src/test_vfstrace.c 34b544e80ba7fb77be15395a609c669df2e660a2
F src/test_wsd.c 41cadfd9d97fe8e3e4e44f61a4a8ccd6f7ca8fe9
F src/tokenize.c 1e86210d3976717a19238ea7b047fac481fe8c12
-F src/trigger.c 3f258307040173aff383eb23fb74c44fe829078c
-F src/update.c 28d2d098b43a2c70dae399896ea8a02f622410ef
-F src/utf.c 890c67dcfcc7a74623c95baac7535aadfe265e84
-F src/util.c 0af2e515dc0dabacec931bca39525f6c3f1c5455
-F src/vacuum.c 587a52bb8833d7ac15af8916f25437e2575028bd
-F src/vdbe.c 9c524bded348fd0a53adc19f2d7cad76ba3442b2
-F src/vdbe.h 18f581cac1f4339ec3299f3e0cc6e11aec654cdb
-F src/vdbeInt.h 986b6b11a13c517337355009e5438703ba5b0a40
-F src/vdbeapi.c 88ea823bbcb4320f5a6607f39cd7c2d3cc4c26b1
-F src/vdbeaux.c 9c293fd3040211687e83d5d27bef2382933146c2
-F src/vdbeblob.c 32f2a4899d67f69634ea4dd93e3f651936d732cb
-F src/vdbemem.c cb55e84b8e2c15704968ee05f0fae25883299b74
-F src/vdbesort.c 0dc1b274dcb4d4c8e71b0b2b15261f286caba39b
-F src/vdbetrace.c 8bd5da325fc90f28464335e4cc4ad1407fe30835
-F src/vtab.c d2c54fd22aa83eb34fc6f7cd9b097f2fc2b1e9de
-F src/wal.c 5acb3e7bbd31f10ba39acad9ce6b399055337a9d
-F src/wal.h 29c197540b19044e6cd73487017e5e47a1d3dac6
-F src/walker.c 3d75ba73de15e0f8cd0737643badbeb0e002f07b
-F src/where.c e74f9ed463b0153e7f2a0e5f291c2be16ae76b35
-F test/8_3_names.test 631ea964a3edb091cf73c3b540f6bcfdb36ce823
+F src/trigger.c cd95ac64efa60e39faf9b5597443192ff27a22fa
+F src/update.c 4c0c6864c4349ba292042e984a56d15985b57f4e
+F src/utf.c 8d819e2e5104a430fc2005f018db14347c95a38f
+F src/util.c f566b5138099a2df8533b190d0dcc74b7dfbe0c9
+F src/vacuum.c ddf21cc9577c4cb459d08bee9863a78ec000c5bb
+F src/vdbe.c 5f0047130f80c7fd0bc41bc51a653b5542c4fbd5
+F src/vdbe.h b52887278cb173e66188da84dfab216bea61119d
+F src/vdbeInt.h c1e830268b75f04a2901dd895b51a637a26c7045
+F src/vdbeapi.c 085cf9bf169b859a6c8fa43791702bac805cb7aa
+F src/vdbeaux.c ecb43014bcd3019e5aa2b5561e5c3a447f007a08
+F src/vdbeblob.c 5dc79627775bd9a9b494dd956e26297946417d69
+F src/vdbemem.c 833005f1cbbf447289f1973dba2a0c2228c7b8ab
+F src/vdbesort.c 4fad64071ae120c25f39dcac572d716b9cadeb7f
+F src/vdbetrace.c 18cc59cb475e6115129bfde224367d13a35a7d13
+F src/vtab.c b05e5f1f4902461ba9f5fc49bb7eb7c3a0741a83
+F src/wal.c 436bfceb141b9423c45119e68e444358ee0ed35d
+F src/wal.h df01efe09c5cb8c8e391ff1715cca294f89668a4
+F src/walker.c 4fa43583d0a84b48f93b1e88f11adf2065be4e73
+F src/where.c 5c4cbc1e5205d8d534c483fa4495c81513b45dea
+F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2
F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2
-F test/aggnested.test 0be144b453e0622a085fae8665c32f5676708e00
+F test/aggnested.test 45c0201e28045ad38a530b5a144b73cd4aa2cfd6
F test/alias.test 4529fbc152f190268a15f9384a5651bbbabc9d87
F test/all.test 52fc8dee494092031a556911d404ca30a749a30b
F test/alter.test 57d96ec9b320bd07af77567034488dcb6642c748
@@ -266,8 +280,8 @@ F test/analyze.test f8ab7d15858b4093b06caf5e57e2a5ff7104bdae
F test/analyze3.test c3c7f6c3951900c188cf94b2d5ee3246d6b3ff89
F test/analyze4.test 757b37875cf9bb528d46f74497bc789c88365045
F test/analyze5.test 713354664c5ff1853ab2cbcb740f0cf5cb7c802e
-F test/analyze6.test bd3625806a5ee6f7bef72d06295bd319f0290af2
-F test/analyze7.test d3587aa5af75c9048d031b94fceca2534fa75d1d
+F test/analyze6.test aa8dae5066bbed35c5f45a507fb87f2d342f2c99
+F test/analyze7.test bd09e89264c664d8d8d2450b6866dcdfae080a13
F test/analyze8.test 4ca170de2ba30ccb1af2c0406803db72262f9691
F test/async.test 1d0e056ba1bb9729283a0f22718d3a25e82c277b
F test/async2.test c0a9bd20816d7d6a2ceca7b8c03d3d69c28ffb8b
@@ -280,25 +294,26 @@ F test/attach2.test e54436ed956d3d88bdee61221da59bf3935a0966
F test/attach3.test d89ccfe4fe6e2b5e368d480fcdfe4b496c54cf4e
F test/attach4.test 53bf502f17647c6d6c5add46dda6bac8b6f4665c
F test/attachmalloc.test 3a4bfca9545bfe906a8d2e622de10fbac5b711b0
-F test/auth.test 304e82f31592820d3bde26ab6b75deaa123e1a6f
-F test/auth2.test 270baddc8b9c273682760cffba6739d907bd2882
+F test/auth.test 4a4c3b034fff7750513520defa910f376c96ab49
+F test/auth2.test a2a371aa6df15f8b0c8109b33d3d7f0f73e4c9aa
F test/auth3.test a4755e6a2a2fea547ffe63c874eb569e60a28eb5
F test/autoinc.test bd30d372d00045252f6c2e41b5f41455e1975acf
-F test/autoindex1.test 058d0b331ae6840a61bbee910d8cbae27bfd5991
-F test/autovacuum.test fcaf4616ae5bb18098db1cb36262565e5c841c3c
+F test/autoindex1.test f88146c4c889ea0afbb620e49d83b5fbf5ee4d06
+F test/autovacuum.test 9f22a7733f39c56ef6a5665d10145ac25d8cb574
F test/autovacuum_ioerr2.test 8a367b224183ad801e0e24dcb7d1501f45f244b4
F test/avtrans.test 0252654f4295ddda3b2cce0e894812259e655a85
-F test/backcompat.test bccbc64769d9c755ad65ee7c2f7336b86e3cc0c8
+F test/backcompat.test ecd841f3a3bfb81518721879cc56a760670e3198
F test/backup.test c9cdd23a495864b9edf75a9fa66f5cb7e10fcf62
F test/backup2.test 34986ef926ea522911a51dfdb2f8e99b7b75ebcf
-F test/backup_ioerr.test 40d208bc9224b666ee3ed423f49bc9062a36a9d0
+F test/backup4.test 4d90389daeb781fe718816a4fc836ad1b06791d8
+F test/backup_ioerr.test 4c3c7147cee85b024ecf6e150e090c32fdbb5135
F test/backup_malloc.test 7162d604ec2b4683c4b3799a48657fb8b5e2d450
F test/badutf.test d5360fc31f643d37a973ab0d8b4fb85799c3169f
F test/badutf2.test f5bc7f2d280670ecd79b9cf4f0f1760c607fe51f
F test/bc_common.tcl 5c8689cc6d2fb44b7c0968ae4f85eb26d50022fa
F test/between.test 16b1776c6323faadb097a52d673e8e3d8be7d070
-F test/bigfile.test 8f88b5ef065e31c615c49d725ede94155fbe9609
-F test/bigfile2.test 8a3c242c3c3481e7cde5a6ef2a66fdc367a095f7
+F test/bigfile.test aa74f4e5db51c8e54a1d9de9fa65d01d1eb20b59
+F test/bigfile2.test 7c79f1ef0c6c2c2bc1e7bd895596fab32bfb4796
F test/bigrow.test f0aeb7573dcb8caaafea76454be3ade29b7fc747
F test/bind.test 3c7b320969000c441a70952b0b15938fbb66237c
F test/bindxfer.test efecd12c580c14df5f4ad3b3e83c667744a4f7e0
@@ -312,22 +327,25 @@ F test/boundary3.tcl 8901d6a503d0bf64251dd81cc74e5ad3add4b119
F test/boundary3.test 56ef82096b4329aca2be74fa1e2b0f762ea0eb45
F test/boundary4.tcl 0bb4b1a94f4fc5ae59b79b9a2b7a140c405e2983
F test/boundary4.test 89e02fa66397b8a325d5eb102b5806f961f8ec4b
+F test/btreefault.test f52c593513bda80a506c848325c73c840590884d
F test/busy.test 76b4887f8b9160ba903c1ac22e8ff406ad6ae2f0
-F test/cache.test f64136b0893c293d0b910ed057b3b711249099a7
-F test/capi2.test 835d4cee9f542ea50fa8d01f3fe6de80b0627360
+F test/cache.test 13bc046b26210471ca6f2889aceb1ea52dc717de
+F test/capi2.test e8b18cc61090b6e5e388f54d6b125d711d1b265a
F test/capi3.test 56ab450125ead38846cbae7e5b6a216686c3cffa
F test/capi3b.test efb2b9cfd127efa84433cd7a2d72ce0454ae0dc4
F test/capi3c.test 93d24621c9ff84da9da060f30431e0453db1cdb0
F test/capi3d.test 17b57ca28be3e37e14c2ba8f787d292d84b724a1
F test/capi3e.test f7408dda65c92b9056199fdc180f893015f83dde
F test/cast.test 4c275cbdc8202d6f9c54a3596701719868ac7dc3
-F test/check.test 193f47ed43a8d29aca12b30cd30ceb105fbe710d
+F test/check.test 2eb93611139a7dfaed3be80067c7dc5ceb5fb287
+F test/close.test e37610d60e9c9b9979a981f3f50071d7dff28616
+F test/closure01.test dbb28f1ea9eeaf0a53ec5bc0fed352e479def8c7
F test/coalesce.test cee0dccb9fbd2d494b77234bccf9dc6c6786eb91
-F test/collate1.test e3eaa48c21e150814be1a7b852d2a8af24458d04
+F test/collate1.test fd02c4d8afc71879c4bb952586389961a21fb0ce
F test/collate2.test 04cebe4a033be319d6ddbb3bbc69464e01700b49
-F test/collate3.test d28d2cfab2c3a3d4628ae4b2b7afc9965daa3b4c
-F test/collate4.test 3d3f123f83fd8ccda6f48d617e44e661b9870c7d
-F test/collate5.test 67f1d3e848e230ff4802815a79acb0a8b5e69bd7
+F test/collate3.test 79558a286362cb9ed603c6fa543f1cda7f563f0f
+F test/collate4.test 031f7265c13308b724ba3c49f41cc04612bd92b1
+F test/collate5.test 65d928034d30d2d263a80f6359f7549ee1598ec6
F test/collate6.test 8be65a182abaac8011a622131486dafb8076e907
F test/collate7.test 8ec29d98f3ee4ccebce6e16ce3863fb6b8c7b868
F test/collate8.test df26649cfcbddf109c04122b340301616d3a88f6
@@ -335,7 +353,7 @@ F test/collate9.test 3adcc799229545940df2f25308dd1ad65869145a
F test/collateA.test b8218ab90d1fa5c59dcf156efabb1b2599c580d6
F test/colmeta.test 087c42997754b8c648819832241daf724f813322
F test/colname.test 08948a4809d22817e0e5de89c7c0a8bd90cb551b
-F test/conflict.test cabc41f7616675df71b4fddabca3bd5d9221915a
+F test/conflict.test 0b3922d2304a14a47e3ccd61bbd6824327af659b
F test/corrupt.test 4aabd06cff3fe759e3e658bcc17b71789710665e
F test/corrupt2.test 9c0ab4becd50e9050bc1ebb8675456a4e5587bf0
F test/corrupt3.test 889d7cdb811800303aa722d7813fe8a4299cf726
@@ -348,17 +366,18 @@ F test/corrupt9.test 959179e68dc0b7b99f424cf3e0381c86dcdd0112
F test/corruptA.test fafa652aa585753be4f6b62ff0bb250266eaf7ce
F test/corruptB.test 20d4a20cbed23958888c3e8995b424a47223d647
F test/corruptC.test 62a767fe64acb1975f58cc6171192839c783edbb
-F test/corruptD.test 99b1999dbfa7cc04aaeac9d695a2445d4e7c7458
-F test/corruptE.test 1b9eb20a8711251ce57b44a257e241085b39b52d
+F test/corruptD.test 3b09903a2e2fe07ecafe775fea94177f8a4bb34f
+F test/corruptE.test d3a3d7e864a95978195741744dda4abfd8286018
F test/corruptF.test 984b1706c9c0e4248141b056c21124612628d12e
F test/count.test 454e1ce985c94d13efeac405ce54439f49336163
+F test/coveridxscan.test cdb47d01acc4a634a34fd25abe85189e0d0f1e62
F test/crash.test fb9dc4a02dcba30d4aa5c2c226f98b220b2b959f
F test/crash2.test 5b14d4eb58b880e231361d3b609b216acda86651
F test/crash3.test 8f5de9d32ab9ab95475a9efe7f47a940aa889418
F test/crash4.test fe2821baf37168dc59dd733dcf7dba2a401487bc
-F test/crash5.test 13b9ca94e048194bca070e26160ce76b406c56be
+F test/crash5.test 05dd3aa9dbb751a22d5cdaf22a9c49b6667aa219
F test/crash6.test 4c56f1e40d0291e1110790a99807aa875b1647ba
-F test/crash7.test 6c6a369af266af2ef50ab34df8f94d719065e2c1
+F test/crash7.test 1a194c4900a255258cf94b7fcbfd29536db572df
F test/crash8.test 38767cb504bbe491de6be4a7006b154973a2309f
F test/crashtest1.c 09c1c7d728ccf4feb9e481671e29dda5669bbcc2
F test/createtab.test b5de160630b209c4b8925bdcbbaf48cc90b67fe8
@@ -366,52 +385,53 @@ F test/cse.test 277350a26264495e86b1785f34d2d0c8600e021c
F test/ctime.test 7bd009071e242aac4f18521581536b652b789a47
F test/date.test f3228180c87bbe5d39c9397bf001c0095c3821b9
F test/dbstatus.test 207e5b63fcb7b9c3bb8e1fdf38ebd4654ad0e54b
-F test/dbstatus2.test b1de8250fde1f3474d6b86f0e89de38d84794f56
+F test/dbstatus2.test 10418e62b3db5dca070f0c3eef3ea13946f339c2
F test/default.test 6faf23ccb300114924353007795aa9a8ec0aa9dc
F test/delete.test a065b05d2ebf60fd16639c579a4adfb7c381c701
F test/delete2.test 3a03f2cca1f9a67ec469915cb8babd6485db43fa
F test/delete3.test 555e84a00a99230b7d049d477a324a631126a6ab
F test/descidx1.test 533dcbda614b0463b0ea029527fd27e5a9ab2d66
F test/descidx2.test 9f1a0c83fd57f8667c82310ca21b30a350888b5d
-F test/descidx3.test fe720e8b37d59f4cef808b0bf4e1b391c2e56b6f
+F test/descidx3.test 09ddbe3f5295f482d2f8b687cf6db8bad7acd9a2
F test/diskfull.test 106391384780753ea6896b7b4f005d10e9866b6e
-F test/distinct.test da36612d05b9ed17e0425d4bfd7ab978d28a7e46
+F test/distinct.test 84da1414b2e6887fffd5ed571311b344c5b082ce
F test/distinctagg.test 1a6ef9c87a58669438fc771450d7a72577417376
-F test/e_createtable.test 48598b15e8fe6554d301e7b65a10c9851f177e84
+F test/e_createtable.test d4e17024b1831e7480f5736cf4e02516a5c90927
F test/e_delete.test 89aa84d3d1bd284a0689ede04bce10226a5aeaa5
F test/e_droptrigger.test afd5c4d27dec607f5997a66bf7e2498a082cb235
F test/e_dropview.test 583411e470458c5d76148542cfb5a5fa84c8f93e
F test/e_expr.test 5489424d3d9a452ac3701cdf4b680ae31a157894
-F test/e_fkey.test 057eed81a41a2b21b1790032f4e8aaba0b2b0e17
+F test/e_fkey.test 79a985d95340c6072a884359426ce95cf33e656a
F test/e_fts3.test 5c02288842e4f941896fd44afdef564dd5fc1459
-F test/e_insert.test c6ac239a97cb16dfbd0c16496f8cd871b4068c0c
+F test/e_insert.test d5331cc95e101af2508159fc98b6801631659ffe
F test/e_reindex.test dfedfc32c5a282b0596c6537cbcd4217fbb1a216
F test/e_resolve.test dcce9308fb13b934ce29591105d031d3e14fbba6
-F test/e_select.test f5d4b81205701deacfae42051ae200969c41d2c0
+F test/e_select.test d5af998a402740d8f0488158d22075df2b6f88fa
F test/e_select2.test 5c3d3da19c7b3e90ae444579db2b70098599ab92
F test/e_update.test 161d5dc6a3ed9dd08f5264d13e20735d7a89f00c
-F test/e_uri.test 9e190ca799d9190eec6e43f2aadf1d10c06a57a3
+F test/e_uri.test 8f2f56b29456a3f846276fa4e0993d4ef8a15b79
F test/e_vacuum.test 331da289ae186656cf5f2eb27f577a89c0c221af
F test/enc.test e54531cd6bf941ee6760be041dff19a104c7acea
-F test/enc2.test 796c59832e2b9a52842f382ffda8f3e989db03ad
+F test/enc2.test 83437a79ba1545a55fb549309175c683fb334473
F test/enc3.test 90683ad0e6ea587b9d5542ca93568af9a9858c40
F test/enc4.test c8f1ce3618508fd0909945beb8b8831feef2c020
-F test/eqp.test 6a389bba6ea113fd5179515001be788a38d53ec7
-F test/errmsg.test 3bb606db9d040cc6854459f8f5e5a2bcd9b7fd2a
+F test/eqp.test 46aa946dd55c90635327898275d3e533d23a9845
+F test/errmsg.test 050717f1c6a5685de9c79f5f9f6b83d7c592f73a
F test/eval.test bc269c365ba877554948441e91ad5373f9f91be3
F test/exclusive.test a1b324cb21834a490cd052d409d34789cfef57cb
-F test/exclusive2.test 372be98f6de44dd78734e364b7b626ea211761a6
+F test/exclusive2.test 881193eccec225cfed9d7f744b65e57f26adee25
F test/exec.test e949714dc127eaa5ecc7d723efec1ec27118fdd7
F test/exists.test 8f7b27b61c2fbe5822f0a1f899c715d14e416e30
F test/expr.test 67c9fd6f8f829e239dc8b0f4a08a73c08b09196d
F test/fallocate.test b5d34437bd7ab01d41b1464b8117aefd4d32160e
-F test/filectrl.test f0327bd804d9c7bd048fa7a151c5eab8e27df42b
-F test/filefmt.test ffa17b5aebc3eb4b1e3be1ccb5ee906ffbd97f6e
+F test/filectrl.test 14fa712e42c4cb791e09dfd58a6a03efb47ef13a
+F test/filefmt.test dbee33e57818249cdffbbb7b13464635217beff1
F test/fkey1.test 01c7de578e11747e720c2d9aeef27f239853c4da
-F test/fkey2.test 080969fe219b3b082b0e097ac18c6af2e5b0631f
+F test/fkey2.test 06e0b4cc9e1b3271ae2ae6feeb19755468432111
F test/fkey3.test 5ec899d12b13bcf1e9ef40eff7fb692fdb91392e
-F test/fkey4.test c6c8f9f9be885f95c85c7bceb26f243ad906fd49
-F test/fkey_malloc.test c3a12acd053c976de09036498eef09b83afa4a80
+F test/fkey4.test 86446017011273aad8f9a99c1a65019e7bd9ca9d
+F test/fkey5.test 0bf64f2d19ad80433ca0b24edbf604a18b353d5f
+F test/fkey_malloc.test bb74c9cb8f8fceed03b58f8a7ef2df98520bbd51
F test/format4.test 1f0cac8ff3895e9359ed87e41aaabee982a812eb
F test/fts-9fd058691.test 78b887e30ae6816df0e1fed6259de4b5a64ad33c
F test/fts1a.test 46090311f85da51bb33bd5ce84f7948359c6d8d7
@@ -458,7 +478,7 @@ F test/fts3ae.test ce32a13b34b0260928e4213b4481acf801533bda
F test/fts3af.test d394978c534eabf22dd0837e718b913fd66b499c
F test/fts3ag.test 0b7d303f61ae5d620c4efb5e825713ea34ff9441
F test/fts3ah.test dc9f66c32c296f1bc8bcc4535126bddfeca62894
-F test/fts3ai.test d29cee6ed653e30de478066881cec8aa766531b2
+F test/fts3ai.test 24058fdc6e9e5102c1fd8459591b114b6a85d285
F test/fts3aj.test 0ed71e1dd9b03b843a857dc3eb9b15630e0104fc
F test/fts3ak.test bd14deafe9d1586e8e9bf032411026ac4f8c925d
F test/fts3al.test 07d64326e79bbdbab20ee87fc3328fbf01641c9f
@@ -467,11 +487,11 @@ F test/fts3an.test a49ccadc07a2f7d646ec1b81bc09da2d85a85b18
F test/fts3ao.test e7b80272efcced57d1d087a9da5c690dd7c21fd9
F test/fts3atoken.test fb398ab50aa232489e2a17f9b29d7ad3a3885f36
F test/fts3auto.test 74315a7377403a57ba82a652a33704197fe1e4be
-F test/fts3aux1.test 0b02743955d56fc0d4d66236a26177bd1b726de0
+F test/fts3aux1.test 03cec2dea379931c219dd4406817485caa69d1d8
F test/fts3b.test e93bbb653e52afde110ad53bbd793f14fe7a8984
F test/fts3c.test fc723a9cf10b397fdfc2b32e73c53c8b1ec02958
F test/fts3comp1.test a0f5b16a2df44dd0b15751787130af2183167c0c
-F test/fts3conf.test 8e65ea56f88ced6cdd2252bdddb1a8327ae5af7e
+F test/fts3conf.test ee8500c86dd58ec075e8831a1e216a79989436de
F test/fts3corrupt.test 7b0f91780ca36118d73324ec803187208ad33b32
F test/fts3corrupt2.test 6d96efae2f8a6af3eeaf283aba437e6d0e5447ba
F test/fts3cov.test e0fb00d8b715ddae4a94c305992dfc3ef70353d7
@@ -482,12 +502,13 @@ F test/fts3drop.test 1b906e293d6773812587b3dc458cb9e8f3f0c297
F test/fts3e.test 1f6c6ac9cc8b772ca256e6b22aaeed50c9350851
F test/fts3expr.test 5e745b2b6348499d9ef8d59015de3182072c564c
F test/fts3expr2.test 18da930352e5693eaa163a3eacf96233b7290d1a
+F test/fts3expr3.test 1bfb762b53a794f990f3dffaae8bbea5736422f7
F test/fts3fault.test cb72dccb0a3b9f730f16c5240f3fcb9303eb1660
F test/fts3fault2.test 3198eef2804deea7cac8403e771d9cbcb752d887
F test/fts3first.test dbdedd20914c8d539aa3206c9b34a23775644641
F test/fts3malloc.test b86ea33db9e8c58c0c2f8027a9fcadaf6a1568be
-F test/fts3matchinfo.test 15edde2c4d373d60449658176af7164d622a62f0
-F test/fts3near.test 2e318ee434d32babd27c167142e2b94ddbab4844
+F test/fts3matchinfo.test ecb08f586d027eb03941bcfcded6cb9d8ccb3a66
+F test/fts3near.test 12895557870b0f9af7cc0be81a0171abb2d12f12
F test/fts3prefix.test b36d4f00b128a51e7b386cc013a874246d9d7dc1
F test/fts3prefix2.test 477ca96e67f60745b7ac931cfa6e9b080c562da5
F test/fts3query.test ef79d31fdb355d094baec1c1b24b60439a1fb8a2
@@ -495,15 +516,18 @@ F test/fts3rnd.test 1320d8826a845e38a96e769562bf83d7a92a15d0
F test/fts3shared.test 8bb266521d7c5495c0ae522bb4d376ad5387d4a2
F test/fts3snippet.test 8e956051221a34c7daeb504f023cb54d5fa5a8b2
F test/fts3sort.test 95be0b19d7e41c44b29014f13ea8bddd495fd659
+F test/fts3tok1.test 4d9e7401679dc71f6b2f76416309b923210bfdbe
+F test/fts3tok_err.test 41e5413139c2ef536ffadfcd1584ee50b1665ec0
F test/fts4aa.test 95f448fb02c4a976968b08d1b4ce134e720946ae
F test/fts4check.test 66fa274cab2b615f2fb338b257713aba8fad88a8
-F test/fts4content.test 17b2360f7d1a9a7e5aa8022783f5c5731b6dfd4f
+F test/fts4content.test 6efc53b4fd03cab167e6998d2b0b7d4b7d419ee6
F test/fts4langid.test 24a6e41063b416bbdf371ff6b4476fa41c194aa7
F test/fts4merge.test c424309743fdd203f8e56a1f1cd7872cd66cc0ee
F test/fts4merge2.test 5faa558d1b672f82b847d2a337465fa745e46891
F test/fts4merge3.test aab02a09f50fe6baaddc2e159c3eabc116d45fc7
-F test/fts4unicode.test aad033abdcfa0f87ce5f56468f59fdf2a0acbcef
-F test/func.test 0d89043dab9a8853358d14c68e028ee0093bf066
+F test/fts4unicode.test 25ccad45896f8e50f6a694cff738a35f798cdb40
+F test/full.test 6b3c8fb43c6beab6b95438c1675374b95fab245d
+F test/func.test b0fc34fdc36897769651975a2b0a606312753643
F test/func2.test 772d66227e4e6684b86053302e2d74a2500e1e0f
F test/func3.test 001021e5b88bd02a3b365a5c5fd8f6f49d39744a
F test/fuzz-oss1.test 4912e528ec9cf2f42134456933659d371c9e0d74
@@ -512,15 +536,16 @@ F test/fuzz2.test 207d0f9d06db3eaf47a6b7bfc835b8e2fc397167
F test/fuzz3.test aec64345184d1662bd30e6a17851ff659d596dc5
F test/fuzz_common.tcl a87dfbb88c2a6b08a38e9a070dabd129e617b45b
F test/fuzz_malloc.test 328f70aaca63adf29b4c6f06505ed0cf57ca7c26
-F test/fuzzer1.test 69cf1036b92fd3b8e1fd65bef4d7ee3f085c28fb
-F test/fuzzerfault.test ff2282c81797b6a355f0748d8b54c7287c5d2b25
-F test/hook.test 5f3749de6462a6b87b4209b74adf7df5ac2df639
+F test/fuzzer1.test 41bd5aa6ae0cf18d06342a4476e3cad98604ae48
+F test/fuzzerfault.test 8792cd77fd5bce765b05d0c8e01b9edcf8af8536
+F test/hook.test 45cb22b940c3cc0af616ba7430f666e245711a48
F test/icu.test 70df4faca133254c042d02ae342c0a141f2663f4
F test/in.test 5941096407d8c133b9eff15bd3e666624b6cbde3
F test/in2.test 5d4c61d17493c832f7d2d32bef785119e87bde75
F test/in3.test 3cbf58c87f4052cee3a58b37b6389777505aa0c0
F test/in4.test 64f3cc1acde1b9161ccdd8e5bde3daefdb5b2617
-F test/incrblob.test 26fde912a1e0aff158b3a84ef3b265f046aad3be
+F test/in5.test 99f9a40af01711b06d2d614ecfe96129f334fba3
+F test/incrblob.test e81846d214f3637622620fbde7cd526781cfe328
F test/incrblob2.test edc3a96e557bd61fb39acc8d2edd43371fbbaa19
F test/incrblob3.test aedbb35ea1b6450c33b98f2b6ed98e5020be8dc7
F test/incrblob4.test 09be37d3dd996a31ea6993bba7837ece549414a8
@@ -528,12 +553,13 @@ F test/incrblob_err.test d2562d2771ebffd4b3af89ef64c140dd44371597
F test/incrblobfault.test 917c0292224c64a56ef7215fd633a3a82f805be0
F test/incrvacuum.test d2a6ddf5e429720b5fe502766af747915ccf6c32
F test/incrvacuum2.test 379eeb8740b0ef60c372c439ad4cbea20b34bb9b
-F test/incrvacuum_ioerr.test 22f208d01c528403240e05beecc41dc98ed01637
+F test/incrvacuum3.test 2ffa9e4a23f072bd7902b9ae6471f8822a6522a7
+F test/incrvacuum_ioerr.test 6ae2f783424e47a0033304808fe27789cf93e635
F test/index.test b5429732b3b983fa810e3ac867d7ca85dae35097
F test/index2.test ee83c6b5e3173a3d7137140d945d9a5d4fdfb9d6
F test/index3.test 423a25c789fc8cc51aaf2a4370bbdde2d9e9eed7
F test/index4.test 2983216eb8c86ee62d9ed7cb206b5cc3331c0026
-F test/index5.test edc8c64ca78bee140c21ce3836820fadf47906bb
+F test/index5.test fc07c14193c0430814e7a08b5da46888ee795c33
F test/indexedby.test be501e381b82b2f8ab406309ba7aac46e221f4ad
F test/indexfault.test 31d4ab9a7d2f6e9616933eb079722362a883eb1d
F test/init.test 15c823093fdabbf7b531fe22cf037134d09587a7
@@ -542,15 +568,17 @@ F test/insert2.test 4f3a04d168c728ed5ec2c88842e772606c7ce435
F test/insert3.test 1b7db95a03ad9c5013fdf7d6722b6cd66ee55e30
F test/insert4.test 87f6798f31d60c4e177622fcc3663367e6ecbd90
F test/insert5.test 394f96728d1258f406fe5f5aeb0aaf29487c39a6
+F test/instr.test a34e1d46a9eefb098a7167ef0e730a4a3d82fba0
F test/intarray.test 066b7d7ac38d25bf96f87f1b017bfc687551cdd4
-F test/interrupt.test 42e7cf98646fd9cb4a3b131a93ed3c50b9e149f1
-F test/intpkey.test 537669fd535f62632ca64828e435b9e54e8d677f
-F test/io.test 36d251507d72e92b965fb2f0801c2f0b56335bcf
+F test/interrupt.test dfe9a67a94b0b2d8f70545ba1a6cca10780d71cc
+F test/intpkey.test 7af30f6ae852d8d1c2b70e4bf1551946742e92d8
+F test/io.test ecf44cc81664ad54d8253e2d88fc705b6554abe3
F test/ioerr.test 40bb2cfcab63fb6aa7424cd97812a84bc16b5fb8
F test/ioerr2.test 9d71166f8466eda510f1af6137bdabaa82b5408d
F test/ioerr3.test d3cec5e1a11ad6d27527d0d38573fbff14c71bdd
F test/ioerr4.test f130fe9e71008577b342b8874d52984bd04ede2c
F test/ioerr5.test 2edfa4fb0f896f733071303b42224df8bedd9da4
+F test/ioerr6.test cf25523b921d1a6a0e5b536cd4acb3c3d979ea52
F test/join.test 8d63cc4d230a7affafa4b6ab0b97c49b8ccb365c
F test/join2.test f2171c265e57ee298a27e57e7051d22962f9f324
F test/join3.test 6f0c774ff1ba0489e6c88a3e77b9d3528fb4fda0
@@ -566,12 +594,12 @@ F test/jrnlmode3.test 556b447a05be0e0963f4311e95ab1632b11c9eaa
F test/keyword1.test a2400977a2e4fde43bf33754c2929fda34dbca05
F test/lastinsert.test 474d519c68cb79d07ecae56a763aa7f322c72f51
F test/laststmtchanges.test ae613f53819206b3222771828d024154d51db200
-F test/like.test 7b4aaa4a8192fdec90e0a905984c92a688c51e48
+F test/like.test 0e5412f4dac4a849f613e1ef8b529d56a6e31d08
F test/like2.test 3b2ee13149ba4a8a60b59756f4e5d345573852da
-F test/limit.test 2db7b3b34fb925b8e847d583d2eb67531d0ce67e
-F test/loadext.test 2b5e249c51c986a5aff1f0950cf7ba30976c8f22
+F test/limit.test cc0ab63385239b63c72452b0e93700bf5e8f0b99
+F test/loadext.test 92e6dfefd1229c3ef4aaabd87419efd8fa57a7a5
F test/loadext2.test 0bcaeb4d81cd5b6e883fdfea3c1bdbe1f173cbca
-F test/lock.test db74fdf5a73bad29ab3d862ea78bf1068972cc1d
+F test/lock.test 87af515b0c4cf928576d0f89946d67d7c265dfb4
F test/lock2.test 5242d8ac4e2d59c403aebff606af449b455aceff
F test/lock3.test f271375930711ae044080f4fe6d6eda930870d00
F test/lock4.test e175ae13865bc87680607563bafba21f31a26f12
@@ -582,8 +610,8 @@ F test/lock_common.tcl 0c270b121d40959fa2f3add382200c27045b3d95
F test/lookaside.test 93f07bac140c5bb1d49f3892d2684decafdc7af2
F test/main.test 39c4bb8a157f57298ed1659d6df89d9f35aaf2c8
F test/make-where7.tcl 05c16b5d4f5d6512881dfec560cb793915932ef9
-F test/malloc.test bc745155ff4252d4f35ec8316625b0dfe2abc659
-F test/malloc3.test de8eca0c3e748878845fdca3663ec4b642073caf
+F test/malloc.test fd368e31fe98d4779ed80442f311ed9f03bcd1f7
+F test/malloc3.test e3b32c724b5a124b57cb0ed177f675249ad0c66a
F test/malloc4.test 957337613002b7058a85116493a262f679f3a261
F test/malloc5.test a577cbbcc1594c7763b9b3515b3633555751c7f0
F test/malloc6.test 2f039d9821927eacae43e1831f815e157659a151
@@ -597,20 +625,20 @@ F test/mallocC.test 3dffe16532f109293ce1ccecd0c31dca55ef08c4
F test/mallocD.test f78c295e8e18ea3029e65ca08278690e00c22100
F test/mallocE.test db1ed69d7eded1b080952e2a7c37f364ad241b08
F test/mallocF.test 2d5c590ebc2fc7f0dcebdf5aa8498b9aed69107e
-F test/mallocG.test 4584d0d8ddb8009f16ca0c8bab1fa37f6358efa2
+F test/mallocG.test 0ff91b65c50bdaba680fb75d87fe4ad35bb7934f
F test/mallocH.test 79b65aed612c9b3ed2dcdaa727c85895fd1bfbdb
F test/mallocI.test a88c2b9627c8506bf4703d8397420043a786cdb6
F test/mallocJ.test b5d1839da331d96223e5f458856f8ffe1366f62e
F test/mallocK.test d79968641d1b70d88f6c01bdb9a7eb4a55582cc9
-F test/malloc_common.tcl 2930895b0962823ec679853e67e58dd6d8198b3c
+F test/malloc_common.tcl 9a98856549bfb3fab205edbc1317216edc52e70d
F test/manydb.test 28385ae2087967aa05c38624cec7d96ec74feb3e
F test/mem5.test c6460fba403c5703141348cd90de1c294188c68f
-F test/memdb.test 708a028d6d373e5b3842e4bdc8ba80998c9a4da6
+F test/memdb.test db5260330676de007be8530d6ecc7c9ab2b06ad3
F test/memleak.test 10b9c6c57e19fc68c32941495e9ba1c50123f6e2
F test/memsubsys1.test a8f9e37567453a5d1d9d37ec102d4d88ab6be33f
F test/memsubsys2.test 3a1c1a9de48e5726faa85108b02459fae8cb9ee9
-F test/minmax.test 722d80816f7e096bf2c04f4111f1a6c1ba65453d
-F test/minmax2.test 33504c01a03bd99226144e4b03f7631a274d66e0
+F test/minmax.test 42fbad0e81afaa6e0de41c960329f2b2c3526efd
+F test/minmax2.test b44bae787fc7b227597b01b0ca5575c7cb54d3bc
F test/minmax3.test cc1e8b010136db0d01a6f2a29ba5a9f321034354
F test/minmax4.test 536a3360470633a177e42fbc19660d146b51daef
F test/misc1.test 889b40722442380a2f6575f30831b32b2372d70e
@@ -619,8 +647,10 @@ F test/misc3.test fe55130a43e444ee75e2156ff75dc96e964b5738
F test/misc4.test 9c078510fbfff05a9869a0b6d8b86a623ad2c4f6
F test/misc5.test 528468b26d03303b1f047146e5eefc941b9069f5
F test/misc6.test 953cc693924d88e6117aeba16f46f0bf5abede91
-F test/misc7.test f00dad9a004da659330013e6f21819d018b683d3
+F test/misc7.test dd82ec9250b89178b96cd28b2aca70639d21e5b3
F test/misuse.test ba4fb5d1a6101d1c171ea38b3c613d0661c83054
+F test/mmap1.test 93d167b328255cbe6679fe1e1a23be1b1197d07b
+F test/mmap2.test 9d6dd9ddb4ad2379f29cc78f38ce1e63ed418022
F test/multiplex.test e08cc7177bd6d85990ee1d71100bb6c684c02256
F test/multiplex2.test 580ca5817c7edbe4cc68fa150609c9473393003a
F test/multiplex3.test d228f59eac91839a977eac19f21d053f03e4d101
@@ -628,24 +658,29 @@ F test/mutex1.test 78b2b9bb320e51d156c4efdb71b99b051e7a4b41
F test/mutex2.test bfeaeac2e73095b2ac32285d2756e3a65e681660
F test/nan.test e9648b9d007c7045242af35e11a984d4b169443a
F test/notify1.test 669b2b743618efdc18ca4b02f45423d5d2304abf
-F test/notify2.test 9503e51b9a272a5405c205ad61b7623d5a9ca489
+F test/notify2.test ce23eb522c9e1fff6443f96376fe67872202061c
F test/notify3.test a86259abbfb923aa27d30f0fc038c88e5251488a
-F test/notnull.test cc7c78340328e6112a13c3e311a9ab3127114347
+F test/notnull.test 2afad748d18fd66d01f66463de73b3e2501fb226
F test/null.test a8b09b8ed87852742343b33441a9240022108993
+F test/numcast.test 5d126f7f581432e86a90d1e35cac625164aec4a1
F test/openv2.test 0d3040974bf402e19b7df4b783e447289d7ab394
+F test/orderby1.test f33968647da5c546528fe4d2bf86c6a6a2e5a7ae
+F test/orderby2.test bc11009f7cd99d96b1b11e57b199b00633eb5b04
+F test/orderby3.test 8619d06a3debdcd80a27c0fdea5c40b468854b99
+F test/orderby4.test 4d39bfbaaa3ae64d026ca2ff166353d2edca4ba4
F test/oserror.test 50417780d0e0d7cd23cf12a8277bb44024765df3
-F test/pager1.test 2163c6ef119f497a71a84137c957c63763e640ab
-F test/pager2.test 745b911dde3d1f24ae0870bd433dfa83d7c658c1
+F test/pager1.test 30e63afd425fea12285e9ec5fa1fd000808031f1
+F test/pager2.test 67b8f40ae98112bcdba1f2b2d03ea83266418c71
F test/pager3.test 3856d9c80839be0668efee1b74811b1b7f7fc95f
-F test/pagerfault.test 452f2cc23e3bfcfa935f4442aec1da4fe1dc0442
+F test/pagerfault.test 8483e65d33d5636e5b7656204bb274d826e728d9
F test/pagerfault2.test 1f79ea40d1133b2683a2f811b00f2399f7ec2401
F test/pagerfault3.test f16e2efcb5fc9996d1356f7cbc44c998318ae1d7
-F test/pageropt.test 9191867ed19a2b3db6c42d1b36b6fbc657cd1ab0
+F test/pageropt.test 6b8f6a123a5572c195ad4ae40f2987007923bbd6
F test/pagesize.test 1dd51367e752e742f58e861e65ed7390603827a0
F test/pcache.test 065aa286e722ab24f2e51792c1f093bf60656b16
F test/pcache2.test a83efe2dec0d392f814bfc998def1d1833942025
-F test/permutations.test 1a8ac849b659445a0b3883caf42fa2c2a289f4a1
-F test/pragma.test a62f73293b0f0d79b0c87f8dd32d46fe53b0bd17
+F test/permutations.test d997a947ab8aabb15f763d50a030b3c11e8ef1b6
+F test/pragma.test 5e7de6c32a5d764f09437d2025f07e4917b9e178
F test/pragma2.test 3a55f82b954242c642f8342b17dffc8b47472947
F test/printf.test ec9870c4dce8686a37818e0bf1aba6e6a1863552
F test/progress.test 5b075c3c790c7b2a61419bc199db87aaf48b8301
@@ -658,9 +693,11 @@ F test/quote.test 215897dbe8de1a6f701265836d6601cc6ed103e6
F test/randexpr1.tcl 40dec52119ed3a2b8b2a773bce24b63a3a746459
F test/randexpr1.test eda062a97e60f9c38ae8d806b03b0ddf23d796df
F test/rdonly.test c267d050a1d9a6a321de502b737daf28821a518d
+F test/regexp1.test 497ea812f264d12b6198d6e50a76be4a1973a9d8
F test/reindex.test 44edd3966b474468b823d481eafef0c305022254
F test/releasetest.mk 2eced2f9ae701fd0a29e714a241760503ccba25a
-F test/releasetest.tcl e48fd8e0e8abad89f30e08620790533ae4e02010
+F test/releasetest.tcl 06d289d8255794073a58d2850742f627924545ce
+F test/resolver01.test d1b487fc567bdb70b5dd09ccaaa877ddc61a233e
F test/rollback.test a1b4784b864331eae8b2a98c189efa2a8b11ff07
F test/rowhash.test 0bc1d31415e4575d10cacf31e1a66b5cc0f8be81
F test/rowid.test f777404492adb0e00868fd706a3721328fd3af48
@@ -688,9 +725,11 @@ F test/select6.test e76bd10a56988f15726c097a5d5a7966fe82d3b2
F test/select7.test dad6f00f0d49728a879d6eb6451d4752db0b0abe
F test/select8.test 391de11bdd52339c30580dabbbbe97e3e9a3c79d
F test/select9.test c0ca3cd87a8ebb04de2cb1402c77df55d911a0ea
-F test/selectA.test 06d1032fa9009314c95394f2ca2e60d9f7ae8532
+F test/selectA.test 99cf21df033b93033ea4f34aba14a500f48f04fe
F test/selectB.test 954e4e49cf1f896d61794e440669e03a27ceea25
F test/selectC.test 871fb55d884d3de5943c4057ebd22c2459e71977
+F test/selectD.test b0f02a04ef7737decb24e08be2c39b9664b43394
+F test/selectE.test fc02a1eb04c8eb537091482644b7d778ae8759b7
F test/server1.test 46803bd3fe8b99b30dbc5ff38ffc756f5c13a118
F test/shared.test 1da9dbad400cee0d93f252ccf76e1ae007a63746
F test/shared2.test 03eb4a8d372e290107d34b6ce1809919a698e879
@@ -699,9 +738,11 @@ F test/shared4.test 72d90821e8d2fc918a08f16d32880868d8ee8e9d
F test/shared6.test 866bb4982c45ce216c61ded5e8fde4e7e2f3ffa9
F test/shared7.test 960760bc8d03e1419e70dea69cf41db62853616e
F test/shared8.test b27befbefbe7f4517f1d6b7ff8f64a41ec74165d
-F test/shared_err.test 91e26ec4f3fbe07951967955585137e2f18993de
+F test/shared9.test 5f2a8f79b4d6c7d107a01ffa1ed05ae7e6333e21
+F test/sharedA.test 0cdf1a76dfa00e6beee66af5b534b1e8df2720f5
+F test/shared_err.test 0079c05c97d88cfa03989b7c20a8b266983087aa
F test/sharedlock.test ffa0a3c4ac192145b310f1254f8afca4d553eabf
-F test/shell1.test 9895ee3013742a02e5afd8d77793729967ffd195
+F test/shell1.test 4a2f57952719972c6f862134463f8712e953c038
F test/shell2.test 037d6ad16e873354195d30bb2dc4b5321788154a
F test/shell3.test 9196c42772d575685e722c92b4b39053c6ebba59
F test/shell4.test aa4eef8118b412d1a01477a53426ece169ea86a9
@@ -714,32 +755,32 @@ F test/softheap1.test c16709a16ad79fa43b32929b2e623d1d117ccf53
F test/sort.test 0e4456e729e5a92a625907c63dcdedfbe72c5dc5
F test/speed1.test f2974a91d79f58507ada01864c0e323093065452
F test/speed1p.explain d841e650a04728b39e6740296b852dccdca9b2cb
-F test/speed1p.test c4a469f29f135f4d76c55b1f2a52f36e209466cc
+F test/speed1p.test b180e98609c7677382cf618c0ec9b69f789033a8
F test/speed2.test 53177056baf6556dcbdcf032bbdfc41c1aa74ded
F test/speed3.test d32043614c08c53eafdc80f33191d5bd9b920523
F test/speed4.test abc0ad3399dcf9703abed2fff8705e4f8e416715
F test/speed4p.explain 6b5f104ebeb34a038b2f714150f51d01143e59aa
F test/speed4p.test 0e51908951677de5a969b723e03a27a1c45db38b
-F test/spellfix.test 2953e9da0e46dab5f83059ef6bfdebca66e13418
+F test/spellfix.test bea537caf587df30d430c2c6a8fe9f64b8712834
F test/sqllimits1.test b1aae27cc98eceb845e7f7adf918561256e31298
-F test/stat.test 08e8185b3fd5b010c90d7ad82b9dd4ea1cbf14b0
+F test/stat.test be8d477306006ec696bc86757cfb34bec79447ce
F test/stmt.test 25d64e3dbf9a3ce89558667d7f39d966fe2a71b9
-F test/subquery.test d4aea23ac267463d4aa604bf937c3992347b20f7
-F test/subquery2.test edcad5c118f0531c2e21bf16a09bbb105252d4cd
+F test/subquery.test 869562de9e8c5d8147e0451a2ce5b58cf55ce389
+F test/subquery2.test 91e1e364072aeff431d1f9689b15147e421d88c7
F test/subselect.test d24fd8757daf97dafd2e889c73ea4c4272dcf4e4
F test/substr.test 18f57c4ca8a598805c4d64e304c418734d843c1a
F test/superlock.test 1cde669f68d2dd37d6c9bd35eee1d95491ae3fc2
F test/sync.test a34cd43e98b7fb84eabbf38f7ed8f7349b3f3d85
-F test/syscall.test bea9bf329bff733c791310244617c2a76974e64a
-F test/sysfault.test c79441d88d23696fbec7b147dba98d42a04f523f
+F test/syscall.test a653783d985108c4912cc64d341ffbbb55ad2806
+F test/sysfault.test fa776e60bf46bdd3ae69f0b73e46ee3977a58ae6
F test/table.test a59d985ca366e39b17b175f387f9d5db5a18d4e2
F test/tableapi.test 2674633fa95d80da917571ebdd759a14d9819126
-F test/tclsqlite.test 952b772830bd380c9b5f58c03fb374189be53828
+F test/tclsqlite.test 37a61c2da7e3bfe3b8c1a2867199f6b860df5d43
F test/tempdb.test 19d0f66e2e3eeffd68661a11c83ba5e6ace9128c
-F test/temptable.test 51edd31c65ed1560dd600b1796e8325df96318e2
+F test/temptable.test d2c9b87a54147161bcd1822e30c1d1cd891e5b30
F test/temptrigger.test 26670ed7a39cf2296a7f0a9e0a1d7bdb7abe936d
-F test/tester.tcl 2665f64c9ce71944b4d41269114e658fb81bda05
-F test/thread001.test 7cc2ce08f9cde95964736d11e91f9ab610f82f91
+F test/tester.tcl 3b08771e6d601612fe62d13787db0e50aac4cf7b
+F test/thread001.test 9f22fd3525a307ff42a326b6bc7b0465be1745a5
F test/thread002.test e630504f8a06c00bf8bbe68528774dd96aeb2e58
F test/thread003.test ee4c9efc3b86a6a2767516a37bd64251272560a7
F test/thread004.test f51dfc3936184aaf73ee85f315224baad272a87f
@@ -753,36 +794,40 @@ F test/threadtest3.c 0ed13e09690f6204d7455fac3b0e8ece490f6eef
F test/tkt-02a8e81d44.test 6c80d9c7514e2a42d4918bf87bf6bc54f379110c
F test/tkt-26ff0c2d1e.test 888324e751512972c6e0d1a09df740d8f5aaf660
F test/tkt-2a5629202f.test 1ab32e084e9fc3d36be6dee2617530846a0eb0b6
-F test/tkt-2d1a5c67d.test b028a811049eb472cb2d3a43fc8ce4f6894eebda
+F test/tkt-2d1a5c67d.test d371279946622698ab393ff88cad9f5f6d82960b
F test/tkt-2ea2425d34.test 1cf13e6f75d149b3209a0cb32927a82d3d79fb28
-F test/tkt-31338dca7e.test 1f714c14b6682c5db715e0bda347926a3456f7a9
+F test/tkt-31338dca7e.test 6fb8807851964da0d24e942f2e19c7c705b9fb58
F test/tkt-313723c356.test c47f8a9330523e6f35698bf4489bcb29609b53ac
-F test/tkt-385a5b56b9.test 8eb87c4bbcc3fd4f33d73719de7e9d64973fa196
+F test/tkt-385a5b56b9.test 7782a382912a51f09f1d1a1442bca1e75f9c549b
F test/tkt-38cb5df375.test f3cc8671f1eb604d4ae9cf886ed4366bec656678
F test/tkt-3998683a16.test 6d1d04d551ed1704eb3396ca87bb9ccc8c5c1eb7
F test/tkt-3a77c9714e.test 32bb28afa8c63fc76e972e996193139b63551ed9
F test/tkt-3fe897352e.test 10de1a67bd5c66b238a4c96abe55531b37bb4f00
F test/tkt-4a03edc4c8.test 2865e4edbc075b954daa82f8da7cc973033ec76e
+F test/tkt-4dd95f6943.test 3d0ce415d2ee15d3d564121960016b9c7be79407
F test/tkt-54844eea3f.test a12b851128f46a695e4e378cca67409b9b8f5894
-F test/tkt-5d863f876e.test 884072c2de496ddbb90c387c9ebc0d4f44a91b8e
+F test/tkt-5d863f876e.test c9f36ca503fa154a3655f92a69d2c30da1747bfa
F test/tkt-5e10420e8d.test 904d1687b3c06d43e5b3555bbcf6802e7c0ffd84
F test/tkt-5ee23731f.test 9db6e1d7209dc0794948b260d6f82b2b1de83a9f
+F test/tkt-6bfb98dfc0.test 24780633627b5cfc0635a5500c2389ebfb563336
F test/tkt-752e1646fc.test ea78d88d14fe9866bdd991c634483334639e13bf
-F test/tkt-78e04e52ea.test ab52f0c1e2de6e46c910f4cc16b086bba05952b7
+F test/tkt-78e04e52ea.test 703e0bfb23d543edf0426a97e3bbd0ca346508ec
+F test/tkt-7a31705a7e6.test 5a7889fdb095ffbe1622413e0145de1637d421bd
F test/tkt-7bbfb7d442.test dfa5c8097a8c353ae40705d6cddeb1f99c18b81a
-F test/tkt-80ba201079.test 9eb040d81c404f56838a6af93593f42790def63f
+F test/tkt-80ba201079.test 105a721e6aad0ae3c5946d7615d1e4d03f6145b8
F test/tkt-80e031a00f.test 9a154173461a4dbe2de49cda73963e04842d52f7
F test/tkt-8454a207b9.test c583a9f814a82a2b5ba95207f55001c9f0cd816c
F test/tkt-91e2e8ba6f.test 08c4f94ae07696b05c9b822da0b4e5337a2f54c5
F test/tkt-94c04eaadb.test fa9c71192f7e2ea2d51bf078bc34e8da6088bf71
F test/tkt-9d68c883.test 458f7d82a523d7644b54b497c986378a7d8c8b67
+F test/tkt-a7b7803e.test 159ef554234fa1f9fb318c751b284bd1cf858da4
F test/tkt-b1d3a2e531.test 610ef582413171b379652663111b1f996d9f8f78
F test/tkt-b351d95f9.test d14a503c414c5c58fdde3e80f9a3cfef986498c0
F test/tkt-b72787b1.test a95e8cdad0b98af1853ac7f0afd4ab27b77bf5f3
F test/tkt-bd484a090c.test 60460bf946f79a79712b71f202eda501ca99b898
F test/tkt-bdc6bbbb38.test fc38bb09bdd440e3513a1f5f98fc60a075182d7d
F test/tkt-c48d99d690.test bed446e3513ae10eec1b86fdd186ef750226c408
-F test/tkt-cbd054fa6b.test bd9fb546f63bc0c79d1776978d059fa51c5b1c63
+F test/tkt-cbd054fa6b.test 9c27ed07b333eed458e5d4543f91ecdcf05aeb19
F test/tkt-d11f09d36e.test fb44f7961aa6d4b632fb7b9768239832210b5fc7
F test/tkt-d635236375.test 9d37e988b47d87505bc9445be0ca447002df5d09
F test/tkt-d82e3f3721.test bcc0dfba658d15bab30fd4a9320c9e35d214ce30
@@ -792,6 +837,7 @@ F test/tkt-f7b4edec.test d998a08ff2b18b7f62edce8e3044317c45efe6c7
F test/tkt-f973c7ac31.test 1da0ed15ec2c7749fb5ce2828cd69d07153ad9f4
F test/tkt-fa7bf5ec.test 9102dfea58aa371d78969da735f9392c57e2e035
F test/tkt-fc62af4523.test 72825d3febdedcd5593a27989fc05accdbfc2bb4
+F test/tkt-fc7bd6358f.test 634bb4af7d661e82d6b61b80c86727bad698e08f
F test/tkt1435.test f8c52c41de6e5ca02f1845f3a46e18e25cadac00
F test/tkt1443.test bacc311da5c96a227bf8c167e77a30c99f8e8368
F test/tkt1444.test a9d72f9e942708bd82dde6c707da61c489e213e9
@@ -814,7 +860,7 @@ F test/tkt2285.test cca17be61cf600b397188e77e7143844d2b977e9
F test/tkt2332.test fc955609b958ca86dfa102832243370a0cc84070
F test/tkt2339.test 73bd17818924cd2ac442e5fd9916b58565739450
F test/tkt2391.test ab7a11be7402da8b51a5be603425367aa0684567
-F test/tkt2409.test 464d55beb32e3b12ce2b4bbf9857d063c4c34297
+F test/tkt2409.test be0d60e7d283f639dccea4b0b5e1cd3a4851fb5b
F test/tkt2450.test 77ed94863f2049c1420288ddfea2d41e5e0971d6
F test/tkt2565.test 8be666e927cb207aae88188f31c331870878b650
F test/tkt2640.test 28134f5d1e05658ef182520cf0b680fa3de5211b
@@ -823,7 +869,7 @@ F test/tkt2686.test 6ee01c9b9e9c48f6d3a1fdd553b1cc4258f903d6
F test/tkt2767.test 569000d842678f9cf2db7e0d1b27cbc9011381b0
F test/tkt2817.test f31839e01f4243cff7399ef654d3af3558cb8d8d
F test/tkt2820.test 39940276b3436d125deb7d8ebeee053e4cf13213
-F test/tkt2822.test a2b27a58df62d1b2e712f91dbe42ad3b7e0e77cc
+F test/tkt2822.test c3589494fba643e039bcf0bfde7554ff6028406d
F test/tkt2832.test a9b0b74a02dca166a04d9e37739c414b10929caa
F test/tkt2854.test e432965db29e27e16f539b2ba7f502eb2ccc49af
F test/tkt2920.test a8737380e4ae6424e00c0273dc12775704efbebf
@@ -841,12 +887,12 @@ F test/tkt3357.test 77c37c6482b526fe89941ce951c22d011f5922ed
F test/tkt3419.test 1bbf36d7ea03b638c15804251287c2391f5c1f6b
F test/tkt3424.test 61f831bd2b071bd128fa5d00fbda57e656ca5812
F test/tkt3442.test 0adb70e9fe9cb750a702065a68ad647409dbc158
-F test/tkt3457.test eb68bb3b19c8677cff06c639ff15d206dbf17fd6
+F test/tkt3457.test 44e980fe5334753dcc27b94fa4deabc485a92f74
F test/tkt3461.test 228ea328a5a21e8663f80ee3d212a6ad92549a19
F test/tkt3493.test 1686cbde85f8721fc1bdc0ee72f2ef2f63139218
F test/tkt3508.test d75704db9501625c7f7deec119fcaf1696aefb7d
F test/tkt3522.test 22ce2ebbcb04a6be56c0977d405c207967318fd6
-F test/tkt3527.test 9e8f28a706c772d5a7cd1020c946fab6c74e3ae0
+F test/tkt3527.test 1a6a48441b560bdc53aec581a868eb576234874d
F test/tkt3541.test 5dc257bde9bc833ab9cc6844bf170b998dbb950a
F test/tkt3554.test f599967f279077bace39220cbe76085c7b423725
F test/tkt3581.test 1966b7193f1e3f14951cce8c66907ae69454e9a3
@@ -856,7 +902,7 @@ F test/tkt3718.test 3b59dcb5c4e7754dacd91e7fd353a61492cc402a
F test/tkt3731.test 0c5f4cbffe102d43c3b2188af91a9e36348f974b
F test/tkt3757.test 10cd679a88675c880533083fc79ac04324525595
F test/tkt3761.test b95ea9c98f21cf91325f18a984887e62caceab33
-F test/tkt3762.test 2a9f3b03df44ec49ec0cfa8d5da6574c2a7853df
+F test/tkt3762.test 4d439ff7abdc8d9323150269d182c37c2d514576
F test/tkt3773.test 7bca904d2a647a6a4a291bd86d7fd7c73855b789
F test/tkt3791.test a6624b9a80b216a26cf473607f42f3e51898c267
F test/tkt3793.test d90ffd75c52413908d15e1c44fc2ea9c80fcc449
@@ -881,26 +927,27 @@ F test/trace2.test c1dc104a8d11a347c870cfea6235e3fc6f6cb06d
F test/trans.test 6e1b4c6a42dba31bd65f8fa5e61a2708e08ddde6
F test/trans2.test d5337e61de45e66b1fcbf9db833fa8c82e624b22
F test/trans3.test 373ac5183cc56be69f48ae44090e7f672939f732
-F test/trigger1.test de42feb7cd442787d38185ae74f5a1d7afa400cb
+F test/transitive1.test d04aa9023e425d6f2d4aa61dd81ee9e102f89062
+F test/trigger1.test dc47573ac79ffe0ee3eecaa517d70d8dacbccd03
F test/trigger2.test 834187beafd1db383af0c659cfa49b0576832816
-F test/trigger3.test d2c60d8be271c355d61727411e753181e877230a
+F test/trigger3.test aa640bb2bbb03edd5ff69c055117ea088f121945
F test/trigger4.test 74700b76ebf3947b2f7a92405141eb2cf2a5d359
F test/trigger5.test 619391a3e9fc194081d22cefd830d811e7badf83
F test/trigger6.test 0e411654f122552da6590f0b4e6f781048a4a9b9
F test/trigger7.test b39e6dee1debe0ff9c2ef66326668f149f07c9c4
F test/trigger8.test 30cb0530bd7c4728055420e3f739aa00412eafa4
F test/trigger9.test 5b0789f1c5c4600961f8e68511b825b87be53e31
-F test/triggerA.test e0aaba16d3547193d36bbd82a1b0ed75e9c88d40
+F test/triggerA.test fe5597f47ee21bacb4936dc827994ed94161e332
F test/triggerB.test 56780c031b454abac2340dbb3b71ac5c56c3d7fe
-F test/triggerC.test 4d4bdaf0230c206b50d350330107ef9802bc2d4f
+F test/triggerC.test a7b4367392c755bc5fd5fff88011753e6b6afe90
F test/triggerD.test 8e7f3921a92a5797d472732108109e44575fa650
F test/tt3_checkpoint.c 415eccce672d681b297485fc20f44cdf0eac93af
F test/types.test bf816ce73c7dfcfe26b700c19f97ef4050d194ff
F test/types2.test 3555aacf8ed8dc883356e59efc314707e6247a84
F test/types3.test 99e009491a54f4dc02c06bdbc0c5eea56ae3e25a
-F test/unique.test 083c7fff74695bcc27a71d75699deba3595bc9c2
+F test/unique.test cadb172bbd5a2e83cd644d186ccd602085e54edc
F test/unixexcl.test a9870e46cc6f8390a494513d4f2bf55b5a8b3e46
-F test/unordered.test f53095cee37851bf30130fa1bf299a8845e837bb
+F test/unordered.test 93dce7b6c97a817a4fe26980c484605a4511f614
F test/update.test 8bc86fd7ef1a00014f76dc6a6a7c974df4aef172
F test/uri.test 63e03df051620a18f794b4f4adcdefb3c23b6751
F test/utf16align.test 54cd35a27c005a9b6e7815d887718780b6a462ae
@@ -910,8 +957,8 @@ F test/vacuum3.test 77ecdd54592b45a0bcb133339f99f1ae0ae94d0d
F test/vacuum4.test d3f8ecff345f166911568f397d2432c16d2867d9
F test/varint.test ab7b110089a08b9926ed7390e7e97bdefeb74102
F test/veryquick.test 7701bb609fe8bf6535514e8b849a309e8f00573b
-F test/view.test b182a67ec43f490b156b5a710827a341be83dd17
-F test/vtab1.test 10fb9e656fe4b318cd82ff1616a340acc01aac4b
+F test/view.test 4057630287bfa5955628fe90a13d4c225d1c7352
+F test/vtab1.test 4403f987860ebddef1ce2de6db7216421035339d
F test/vtab2.test 7bcffc050da5c68f4f312e49e443063e2d391c0d
F test/vtab3.test baad99fd27217f5d6db10660522e0b7192446de1
F test/vtab4.test 942f8b8280b3ea8a41dae20e7822d065ca1cb275
@@ -929,14 +976,15 @@ F test/vtabF.test fd5ad376f5a34fe0891df1f3cddb4fe7c3eb077e
F test/vtab_alter.test 9e374885248f69e251bdaacf480b04a197f125e5
F test/vtab_err.test 0d4d8eb4def1d053ac7c5050df3024fd47a3fbd8
F test/vtab_shared.test 82f463886e18d7f8395a4b6167c91815efe54839
-F test/wal.test a040047d7f2b9f34bc4d597964e5e7c09609c635
+F test/wal.test 3a6ebdf0287b38b5537c07c517b30dda9aaac317
F test/wal2.test d4b470f13c87f6d8268b004380afa04c3c67cb90
F test/wal3.test b22eb662bcbc148c5f6d956eaf94b047f7afe9c0
F test/wal4.test 4744e155cd6299c6bd99d3eab1c82f77db9cdb3c
-F test/wal5.test f58ed4b8b542f71c7441da12fbd769d99b362437
+F test/wal5.test 8f888b50f66b78821e61ed0e233ded5de378224b
F test/wal6.test 2e3bc767d9c2ce35c47106148d43fcbd072a93b3
F test/wal7.test 2ae8f427d240099cc4b2dfef63cff44e2a68a1bd
-F test/wal8.test 5ab217d21f7e5e86af2933a4ffd0d8357cc2c0bd
+F test/wal8.test b3ee739fe8f7586aaebdc2367f477ebcf3e3b034
+F test/wal9.test 378e76a9ad09cd9bee06c172ad3547b0129a6750
F test/wal_common.tcl a98f17fba96206122eff624db0ab13ec377be4fe
F test/walbak.test b9f68e39646375c2b877be906babcc15d38b4877
F test/walbig.test f437473a16cfb314867c6b5d1dbcd519e73e3434
@@ -944,7 +992,7 @@ F test/walcksum.test f5447800a157c9e2234fbb8e80243f0813941bde
F test/walcrash.test 4457436593be8c136f9148487c7dccd5e9013af2
F test/walcrash2.test 019d60b89d96c1937adb2b30b850ac7e86e5a142
F test/walcrash3.test 595e44c6197f0d0aa509fc135be2fd0209d11a2c
-F test/walfault.test 97394d8de82a99f7abf1c12ed229640607fd0ad2
+F test/walfault.test 54ad6e849c727f4da463964b9eb8c8e8e155cf82
F test/walhook.test ed00a40ba7255da22d6b66433ab61fab16a63483
F test/walmode.test 4022fe03ae6e830583672caa101f046438a0473c
F test/walnoshm.test 84ca10c544632a756467336b7c3b864d493ee496
@@ -953,26 +1001,28 @@ F test/walro.test a31deb621033442a76c3a61e44929250d06f81b1
F test/walshared.test 6dda2293880c300baf5d791c307f653094585761
F test/walslow.test e7be6d9888f83aa5d3d3c7c08aa9b5c28b93609a
F test/walthread.test de8dbaf6d9e41481c460ba31ca61e163d7348f8e
-F test/where.test 4c9f69987ed2aa0173fa930f2b41ab9879478cd8
-F test/where2.test 43d4becaf5a5df854e6c21d624a1cb84c6904554
+F test/where.test 15ac8611c9439a2c5f4a6ac10cfe4c1ec9854c24
+F test/where2.test 399b3178289925a0aa976b3d60ef139740540ecd
F test/where3.test 667e75642102c97a00bf9b23d3cb267db321d006
F test/where4.test e9b9e2f2f98f00379e6031db6a6fca29bae782a2
F test/where5.test fdf66f96d29a064b63eb543e28da4dfdccd81ad2
F test/where6.test 5da5a98cec820d488e82708301b96cb8c18a258b
F test/where7.test 5c566388f0cc318b0032ce860f4ac5548e3c265a
-F test/where8.test a6c740fd286d7883e274e17b6230a9d672a7ab1f
+F test/where8.test d6a283eb7348a8967d44e2a753f117ab0d21d4f3
F test/where8m.test da346596e19d54f0aba35ebade032a7c47d79739
-F test/where9.test ae98dc22ef9b6f2bc81e9f164e41b38faa9bda06
+F test/where9.test 1b4387c6eacc9a32b28b4d837c27f857c785d0d8
F test/whereA.test 24c234263c8fe358f079d5e57d884fb569d2da0a
F test/whereB.test 0def95db3bdec220a731c7e4bec5930327c1d8c5
F test/whereC.test 13ff5ec0dba407c0e0c075980c75b3275a6774e5
-F test/whereD.test 11945e79899a97958d87d1e5ac6ae5abd827356b
+F test/whereD.test 3f3ee93825c94804f1fc91eef2de0d365981759a
+F test/whereE.test 7bd34945797efef15819368479bacc34215e4e1d
+F test/whereF.test a0e296643cabe5278379bc1a0aa158cf3c54a1c9
F test/wherelimit.test 5e9fd41e79bb2b2d588ed999d641d9c965619b31
-F test/win32lock.test b2a539e85ae6b2d78475e016a9636b4451dc7fb9
+F test/win32lock.test 7a6bd73a5dcdee39b5bb93e92395e1773a194361
F test/zeroblob.test caaecfb4f908f7bc086ed238668049f96774d688
-F test/zerodamage.test 0de750389990b1078bab203c712dc3fefd1d8b82
-F tool/build-all-msvc.bat 1a18aa39983ae7354d834bc55a850a54fc007576 x
-F tool/build-shell.sh b64a481901fc9ffe5ca8812a2a9255b6cfb77381
+F test/zerodamage.test 209d7ed441f44cc5299e4ebffbef06fd5aabfefd
+F tool/build-all-msvc.bat 74fb6e5cca66ebdb6c9bbafb2f8b802f08146d38 x
+F tool/build-shell.sh 950f47c6174f1eea171319438b93ba67ff5bf367
F tool/checkSpacing.c 810e51703529a204fc4e1eb060e9ab663e3c06d2
F tool/diffdb.c 7524b1b5df217c20cd0431f6789851a4e0cb191b
F tool/extract.c 054069d81b095fbdc189a6f5d4466e40380505e2
@@ -986,38 +1036,39 @@ F tool/mkkeywordhash.c bb52064aa614e1426445e4b2b9b00eeecd23cc79
F tool/mkopts.tcl 66ac10d240cc6e86abd37dc908d50382f84ff46e
F tool/mkspeedsql.tcl a1a334d288f7adfe6e996f2e712becf076745c97
F tool/mksqlite3c-noext.tcl 8bce31074e4cbe631bb7676526a048335f4c9f02
-F tool/mksqlite3c.tcl 589c7f44e990be1b8443cfe4808dce392b0327fa
-F tool/mksqlite3h.tcl 78013ad79a5e492e5f764f3c7a8ef834255061f8
+F tool/mksqlite3c.tcl a61fe62a2895ca6458c463fccf1211ca1c000fcf
+F tool/mksqlite3h.tcl ba24038056f51fde07c0079c41885ab85e2cff12
F tool/mksqlite3internalh.tcl 3dca7bb5374cee003379b8cbac73714f610ef795
-F tool/mkvsix.tcl 19b2ab9ea16445953a76568a5bbe4cb864f92dfe
+F tool/mkvsix.tcl 0be7f7a591f1e83f9199cb82911b66668ca484c9
F tool/offsets.c fe4262fdfa378e8f5499a42136d17bf3b98f6091
F tool/omittest.tcl 4665982e95a6e5c1bd806cf7bc3dea95be422d77
F tool/opcodeDoc.awk b3a2a3d5d3075b8bd90b7afe24283efdd586659c
F tool/restore_jrnl.tcl 6957a34f8f1f0f8285e07536225ec3b292a9024a
F tool/rollback-test.c 9fc98427d1e23e84429d7e6d07d9094fbdec65a5
-F tool/showdb.c 2e28d8e499b016485672e9a7ac65dacc0d28ff69
+F tool/showdb.c 525ecc443578647703051308ad50a93de6ba2c4b
F tool/showjournal.c b62cecaab86a4053d944c276bb5232e4d17ece02
-F tool/showwal.c f09e5a80a293919290ec85a6a37c85a5ddcf37d9
+F tool/showwal.c 3f7f7da5ec0cba51b1449a75f700493377da57b5
F tool/soak1.tcl 8d407956e1a45b485a8e072470a3e629a27037fe
F tool/space_used.tcl f714c41a59e326b8b9042f415b628b561bafa06b
-F tool/spaceanal.tcl e42273000686a4afbf6a5e5d7fb12be65e92afb1
+F tool/spaceanal.tcl 76f583a246a0b027f423252339e711f13198932e
F tool/speedtest.tcl 06c76698485ccf597b9e7dbb1ac70706eb873355
F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81
F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
F tool/split-sqlite3c.tcl d9be87f1c340285a3e081eb19b4a247981ed290c
+F tool/stack_usage.tcl f8e71b92cdb099a147dad572375595eae55eca43
F tool/symbols-mingw.sh 4dbcea7e74768305384c9fd2ed2b41bbf9f0414d
F tool/symbols.sh fec58532668296d7c7dc48be9c87f75ccdb5814f
F tool/tostr.awk e75472c2f98dd76e06b8c9c1367f4ab07e122d06
-F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
+F tool/vdbe-compress.tcl f12c884766bd14277f4fcedcae07078011717381
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
-F tool/win/sqlite.vsix 67d8a99aceb56384a81b3f30d6c71743146d2cc9
-P 81fd941da62956e32d1c4ffcdb39abecba7a6f3b
-R a82bf185a73888ab1f23d02429bade9e
+F tool/win/sqlite.vsix 97894c2790eda7b5bce3cc79cb2a8ec2fde9b3ac
+P 7a9aa21c3506a10ab9465540e81071b39bca447d
+R c103e20e6479251bb5b95c9b2e375810
T +bgcolor * #d0c0ff
T +sym-release *
-T +sym-version-3.7.14.1 *
+T +sym-version-3.7.17 *
U drh
-Z 002ec98f7f5b76deaac3a726393245f3
+Z 416a23d38a1b2c089b32c7a6efcbc3bc
diff --git a/manifest.uuid b/manifest.uuid
index f808cb0..eb32e70 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-091570e46d04e84b67228e0bdbcd6e1fb60c6bdb
+118a3b35693b134d56ebd780123b7fd6f1497668
diff --git a/publish.sh b/publish.sh
deleted file mode 100644
index 6c4dea1..0000000
--- a/publish.sh
+++ /dev/null
@@ -1,138 +0,0 @@
-#!/bin/sh
-#
-# This script is used to compile SQLite and package everything up
-# so that it is ready to move to the SQLite website.
-#
-
-# Set srcdir to the name of the directory that contains the publish.sh
-# script.
-#
-srcdir=`echo "$0" | sed 's%\(^.*\)/[^/][^/]*$%\1%'`
-
-# Get the makefile.
-#
-cp $srcdir/Makefile.linux-gcc ./Makefile
-chmod +x $srcdir/install-sh
-
-# Get the current version number - needed to help build filenames
-#
-VERS=`cat $srcdir/VERSION`
-VERSW=`sed 's/\./_/g' $srcdir/VERSION`
-echo "VERSIONS: $VERS $VERSW"
-
-# Start by building an sqlite shell for linux.
-#
-make clean
-make sqlite3.c
-CFLAGS="-Os -DSQLITE_ENABLE_FTS3=0 -DSQLITE_ENABLE_RTREE=0"
-CFLAGS="$CFLAGS -DSQLITE_THREADSAFE=0"
-echo '***** '"COMPILING sqlite3-$VERS.bin..."
-gcc $CFLAGS -Itsrc sqlite3.c tsrc/shell.c -o sqlite3 -ldl
-strip sqlite3
-mv sqlite3 sqlite3-$VERS.bin
-gzip sqlite3-$VERS.bin
-chmod 644 sqlite3-$VERS.bin.gz
-mv sqlite3-$VERS.bin.gz doc
-
-# Build the sqlite.so and tclsqlite.so shared libraries
-# under Linux
-#
-TCLDIR=/home/drh/tcltk/846/linux/846linux
-TCLSTUBLIB=$TCLDIR/libtclstub8.4g.a
-CFLAGS="-Os -DSQLITE_ENABLE_FTS3=3 -DSQLITE_ENABLE_RTREE=1"
-CFLAGS="$CFLAGS -DHAVE_LOCALTIME_R=1 -DHAVE_GMTIME_R=1"
-CFLAGS="$CFLAGS -DSQLITE_ENABLE_COLUMN_METADATA=1"
-echo '***** BUILDING shared libraries for linux'
-gcc $CFLAGS -shared tclsqlite3.c $TCLSTUBLIB -o tclsqlite3.so -lpthread
-strip tclsqlite3.so
-chmod 644 tclsqlite3.so
-mv tclsqlite3.so tclsqlite-$VERS.so
-gzip tclsqlite-$VERS.so
-mv tclsqlite-$VERS.so.gz doc
-gcc $CFLAGS -shared sqlite3.c -o sqlite3.so -lpthread
-strip sqlite3.so
-chmod 644 sqlite3.so
-mv sqlite3.so sqlite-$VERS.so
-gzip sqlite-$VERS.so
-mv sqlite-$VERS.so.gz doc
-
-
-# Build the tclsqlite3.dll and sqlite3.dll shared libraries.
-#
-. $srcdir/mkdll.sh
-echo '***** PACKAGING shared libraries for windows'
-echo zip doc/tclsqlite-$VERSW.zip tclsqlite3.dll
-zip doc/tclsqlite-$VERSW.zip tclsqlite3.dll
-echo zip doc/sqlitedll-$VERSW.zip sqlite3.dll sqlite3.def
-zip doc/sqlitedll-$VERSW.zip sqlite3.dll sqlite3.def
-
-# Build the sqlite.exe executable for windows.
-#
-OPTS='-DSTATIC_BUILD=1 -DNDEBUG=1 -DSQLITE_THREADSAFE=0'
-OPTS="$OPTS -DSQLITE_ENABLE_FTS3=1 -DSQLITE_ENABLE_RTREE=1"
-i386-mingw32msvc-gcc -Os $OPTS -Itsrc -I$TCLDIR sqlite3.c tsrc/shell.c \
- -o sqlite3.exe
-zip doc/sqlite-$VERSW.zip sqlite3.exe
-
-# Build a source archive useful for windows.
-#
-make target_source
-cd tsrc
-echo '***** BUILDING preprocessed source archives'
-rm fts[12]* icu*
-rm -f ../doc/sqlite-source-$VERSW.zip
-zip ../doc/sqlite-source-$VERSW.zip *
-cd ..
-cp tsrc/sqlite3.h tsrc/sqlite3ext.h .
-cp tsrc/shell.c .
-pwd
-zip doc/sqlite-amalgamation-$VERSW.zip sqlite3.c sqlite3.h sqlite3ext.h shell.c sqlite3.def
-
-# Construct a tarball of the source tree
-#
-echo '***** BUILDING source archive'
-ORIGIN=`pwd`
-cd $srcdir
-chmod +x configure
-cd ..
-mv sqlite sqlite-$VERS
-EXCLUDE=`find sqlite-$VERS -print | egrep '(www/|art/|doc/|contrib/|_FOSSIL_)' | sed 's,^, --exclude ,'`
-echo "tar czf $ORIGIN/doc/sqlite-$VERS.tar.gz $EXCLUDE sqlite-$VERS"
-tar czf $ORIGIN/doc/sqlite-$VERS.tar.gz $EXCLUDE sqlite-$VERS
-mv sqlite-$VERS sqlite
-cd $ORIGIN
-
-#
-# Build RPMS (binary) and Source RPM
-#
-
-# Make sure we are properly setup to build RPMs
-#
-echo "%HOME %{expand:%%(cd; pwd)}" > $HOME/.rpmmacros
-echo "%_topdir %{HOME}/rpm" >> $HOME/.rpmmacros
-mkdir $HOME/rpm
-mkdir $HOME/rpm/BUILD
-mkdir $HOME/rpm/SOURCES
-mkdir $HOME/rpm/RPMS
-mkdir $HOME/rpm/SRPMS
-mkdir $HOME/rpm/SPECS
-
-# create the spec file from the template
-sed s/SQLITE_VERSION/$VERS/g $srcdir/spec.template > $HOME/rpm/SPECS/sqlite.spec
-
-# copy the source tarball to the rpm directory
-cp doc/sqlite-$VERS.tar.gz $HOME/rpm/SOURCES/.
-
-# build all the rpms
-rpm -ba $HOME/rpm/SPECS/sqlite.spec >& rpm-$vers.log
-
-# copy the RPMs into the build directory.
-mv $HOME/rpm/RPMS/i386/sqlite*-$vers*.rpm doc
-mv $HOME/rpm/SRPMS/sqlite-$vers*.rpm doc
-
-# Build the website
-#
-#cp $srcdir/../historical/* doc
-#make doc
-#cd doc
-#chmod 644 *.gz
diff --git a/publish_osx.sh b/publish_osx.sh
deleted file mode 100644
index 508b623..0000000
--- a/publish_osx.sh
+++ /dev/null
@@ -1,35 +0,0 @@
-#!/bin/sh
-#
-# This script is used to compile SQLite and package everything up
-# so that it is ready to move to the SQLite website.
-#
-
-# Set srcdir to the name of the directory that contains the publish.sh
-# script.
-#
-srcdir=`echo "$0" | sed 's%\(^.*\)/[^/][^/]*$%\1%'`
-
-# Get the makefile.
-#
-cp $srcdir/Makefile.linux-gcc ./Makefile
-chmod +x $srcdir/install-sh
-
-# Get the current version number - needed to help build filenames
-#
-VERS=`cat $srcdir/VERSION`
-VERSW=`sed 's/\./_/g' $srcdir/VERSION`
-echo "VERSIONS: $VERS $VERSW"
-
-# Start by building an sqlite shell for linux.
-#
-make clean
-make sqlite3.c
-CFLAGS="-Os -DSQLITE_ENABLE_FTS3=1 -DSQLITE_THREADSAFE=0"
-NAME=sqlite3-$VERS-osx-x86.bin
-echo '***** '"COMPILING $NAME..."
-gcc $CFLAGS -Itsrc sqlite3.c tsrc/shell.c -o $NAME -ldl
-strip $NAME
-chmod 644 $NAME
-gzip $NAME
-mkdir -p doc
-mv $NAME.gz doc
diff --git a/sqlite3.1 b/sqlcipher.1
index 785995b..3a83046 100644
--- a/sqlite3.1
+++ b/sqlcipher.1
@@ -2,7 +2,7 @@
.\" First parameter, NAME, should be all caps
.\" Second parameter, SECTION, should be 1-8, maybe w/ subsection
.\" other parameters are allowed: see man(7), man(1)
-.TH SQLITE3 1 "Mon Apr 15 23:49:17 2002"
+.TH SQLCIPHER 1 "Mon Apr 15 23:49:17 2002"
.\" Please adjust this date whenever revising the manpage.
.\"
.\" Some roff macros, for reference:
@@ -16,29 +16,29 @@
.\" .sp <n> insert n+1 empty lines
.\" for manpage-specific macros, see man(7)
.SH NAME
-.B sqlite3
-\- A command line interface for SQLite version 3
+.B sqlcipher
+\- A command line interface for SQLCipher version 2
.SH SYNOPSIS
-.B sqlite3
+.B sqlcipher
.RI [ options ]
.RI [ databasefile ]
.RI [ SQL ]
.SH SUMMARY
.PP
-.B sqlite3
-is a terminal-based front-end to the SQLite library that can evaluate
+.B sqlcipher
+is a terminal-based front-end to the SQLCipher library that can evaluate
queries interactively and display the results in multiple formats.
-.B sqlite3
+.B sqlcipher
can also be used within shell scripts and other applications to provide
batch processing features.
.SH DESCRIPTION
To start a
-.B sqlite3
+.B sqlcipher
interactive session, invoke the
-.B sqlite3
+.B sqlcipher
command and optionally provide the name of a database file. If the
database file does not exist, it will be created. If the database file
does exist, it will be opened.
@@ -47,9 +47,9 @@ For example, to create a new database file named "mydata.db", create
a table named "memos" and insert a couple of records into that table:
.sp
$
-.B sqlite3 mydata.db
+.B sqlcipher mydata.db
.br
-SQLite version 3.1.3
+SQLite version 2.0.3
.br
Enter ".help" for instructions
.br
@@ -85,7 +85,7 @@ semi-colons.
For example:
.sp
$
-.B sqlite3 -line mydata.db 'select * from memos where priority > 20;'
+.B sqlcipher -line mydata.db 'select * from memos where priority > 20;'
.br
text = lunch with Christine
.br
@@ -144,7 +144,7 @@ sqlite>
.fi
.SH OPTIONS
-.B sqlite3
+.B sqlcipher
has the following options:
.TP
.BI \-init\ file
@@ -190,7 +190,7 @@ Show help on options and exit.
.SH INIT FILE
-.B sqlite3
+.B sqlcipher
reads an initialization file to set the configuration of the
interactive environment. Throughout initialization, any previously
specified setting can be overridden. The sequence of initialization is
@@ -220,7 +220,7 @@ o If the -init option is present, the specified file is processed.
o All other command line options are processed.
.SH SEE ALSO
-http://www.sqlite.org/
+http://www.sqlcipher.net/
.br
The sqlite-doc package
.SH AUTHOR
diff --git a/sqlite3.pc.in b/sqlcipher.pc.in
index c8d0aa9..c8d0aa9 100644
--- a/sqlite3.pc.in
+++ b/sqlcipher.pc.in
diff --git a/sqlcipher.xcodeproj/project.pbxproj b/sqlcipher.xcodeproj/project.pbxproj
index a65412d..064d843 100644
--- a/sqlcipher.xcodeproj/project.pbxproj
+++ b/sqlcipher.xcodeproj/project.pbxproj
@@ -196,14 +196,12 @@
INSTALL_PATH = /usr/local/lib;
OTHER_CFLAGS = (
"-DSQLITE_HAS_CODEC",
- "-DNDEBUG",
"-DSQLITE_TEMP_STORE=2",
"-DSQLITE_THREADSAFE",
);
"OTHER_CFLAGS[arch=armv6]" = (
"-mno-thumb",
"-DSQLITE_HAS_CODEC",
- "-DNDEBUG",
"-DSQLITE_TEMP_STORE=2",
"-DSQLITE_THREADSAFE",
);
diff --git a/src/alter.c b/src/alter.c
index 7f56ce7..a49d334 100644
--- a/src/alter.c
+++ b/src/alter.c
@@ -414,7 +414,7 @@ void sqlite3AlterRenameTable(
assert( pSrc->nSrc==1 );
assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
- pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
+ pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
if( !pTab ) goto exit_rename_table;
iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
zDb = db->aDb[iDb].zName;
@@ -664,7 +664,7 @@ void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
** If there is a NOT NULL constraint, then the default value for the
** column must not be NULL.
*/
- if( pCol->isPrimKey ){
+ if( pCol->colFlags & COLFLAG_PRIMKEY ){
sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
return;
}
@@ -757,7 +757,7 @@ void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
assert( pParse->pNewTable==0 );
assert( sqlite3BtreeHoldsAllMutexes(db) );
if( db->mallocFailed ) goto exit_begin_add_column;
- pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
+ pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
if( !pTab ) goto exit_begin_add_column;
#ifndef SQLITE_OMIT_VIRTUALTABLE
diff --git a/src/analyze.c b/src/analyze.c
index 632fdc1..9a3e959 100644
--- a/src/analyze.c
+++ b/src/analyze.c
@@ -473,7 +473,7 @@ static void analyzeOneTable(
/* Do not gather statistics on views or virtual tables */
return;
}
- if( memcmp(pTab->zName, "sqlite_", 7)==0 ){
+ if( sqlite3_strnicmp(pTab->zName, "sqlite_", 7)==0 ){
/* Do not gather statistics on system tables */
return;
}
@@ -883,7 +883,7 @@ static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
if( pIndex==0 ) break;
pIndex->aiRowEst[i] = v;
if( *z==' ' ) z++;
- if( memcmp(z, "unordered", 10)==0 ){
+ if( strcmp(z, "unordered")==0 ){
pIndex->bUnordered = 1;
break;
}
diff --git a/src/attach.c b/src/attach.c
index e295c30..b8e1219 100644
--- a/src/attach.c
+++ b/src/attach.c
@@ -109,7 +109,7 @@ static void attachFunc(
}
}
- /* Allocate the new entry in the db->aDb[] array and initialise the schema
+ /* Allocate the new entry in the db->aDb[] array and initialize the schema
** hash tables.
*/
if( db->aDb==db->aDbStatic ){
@@ -126,7 +126,7 @@ static void attachFunc(
/* Open the database file. If the btree is successfully opened, use
** it to obtain the database schema. At this point the schema may
- ** or may not be initialised.
+ ** or may not be initialized.
*/
flags = db->openFlags;
rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
@@ -434,6 +434,7 @@ int sqlite3FixInit(
assert( db->nDb>iDb );
pFix->pParse = pParse;
pFix->zDb = db->aDb[iDb].zName;
+ pFix->pSchema = db->aDb[iDb].pSchema;
pFix->zType = zType;
pFix->pName = pName;
return 1;
@@ -464,14 +465,15 @@ int sqlite3FixSrcList(
if( NEVER(pList==0) ) return 0;
zDb = pFix->zDb;
for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
- if( pItem->zDatabase==0 ){
- pItem->zDatabase = sqlite3DbStrDup(pFix->pParse->db, zDb);
- }else if( sqlite3StrICmp(pItem->zDatabase,zDb)!=0 ){
+ if( pItem->zDatabase && sqlite3StrICmp(pItem->zDatabase, zDb) ){
sqlite3ErrorMsg(pFix->pParse,
"%s %T cannot reference objects in database %s",
pFix->zType, pFix->pName, pItem->zDatabase);
return 1;
}
+ sqlite3DbFree(pFix->pParse->db, pItem->zDatabase);
+ pItem->zDatabase = 0;
+ pItem->pSchema = pFix->pSchema;
#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
diff --git a/src/backup.c b/src/backup.c
index 4881215..252f61c 100644
--- a/src/backup.c
+++ b/src/backup.c
@@ -212,20 +212,28 @@ static int isFatalError(int rc){
** page iSrcPg from the source database. Copy this data into the
** destination database.
*/
-static int backupOnePage(sqlite3_backup *p, Pgno iSrcPg, const u8 *zSrcData){
+static int backupOnePage(
+ sqlite3_backup *p, /* Backup handle */
+ Pgno iSrcPg, /* Source database page to backup */
+ const u8 *zSrcData, /* Source database page data */
+ int bUpdate /* True for an update, false otherwise */
+){
Pager * const pDestPager = sqlite3BtreePager(p->pDest);
const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
const int nCopy = MIN(nSrcPgsz, nDestPgsz);
const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
#ifdef SQLITE_HAS_CODEC
- int nSrcReserve = sqlite3BtreeGetReserve(p->pSrc);
+ /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
+ ** guaranteed that the shared-mutex is held by this thread, handle
+ ** p->pSrc may not actually be the owner. */
+ int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
#endif
-
int rc = SQLITE_OK;
i64 iOff;
+ assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
assert( p->bDestLocked );
assert( !isFatalError(p->rc) );
assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
@@ -282,6 +290,9 @@ static int backupOnePage(sqlite3_backup *p, Pgno iSrcPg, const u8 *zSrcData){
*/
memcpy(zOut, zIn, nCopy);
((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
+ if( iOff==0 && bUpdate==0 ){
+ sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
+ }
}
sqlite3PagerUnref(pDestPg);
}
@@ -386,9 +397,10 @@ int sqlite3_backup_step(sqlite3_backup *p, int nPage){
const Pgno iSrcPg = p->iNext; /* Source page number */
if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
DbPage *pSrcPg; /* Source page object */
- rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
+ rc = sqlite3PagerAcquire(pSrcPager, iSrcPg, &pSrcPg,
+ PAGER_ACQUIRE_READONLY);
if( rc==SQLITE_OK ){
- rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg));
+ rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
sqlite3PagerUnref(pSrcPg);
}
}
@@ -410,7 +422,13 @@ int sqlite3_backup_step(sqlite3_backup *p, int nPage){
** same schema version.
*/
if( rc==SQLITE_DONE ){
- rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
+ if( nSrcPage==0 ){
+ rc = sqlite3BtreeNewDb(p->pDest);
+ nSrcPage = 1;
+ }
+ if( rc==SQLITE_OK || rc==SQLITE_DONE ){
+ rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
+ }
if( rc==SQLITE_OK ){
if( p->pDestDb ){
sqlite3ResetAllSchemasOfConnection(p->pDestDb);
@@ -444,7 +462,7 @@ int sqlite3_backup_step(sqlite3_backup *p, int nPage){
}else{
nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
}
- sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
+ assert( nDestTruncate>0 );
if( pgszSrc<pgszDest ){
/* If the source page-size is smaller than the destination page-size,
@@ -458,22 +476,38 @@ int sqlite3_backup_step(sqlite3_backup *p, int nPage){
*/
const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
+ Pgno iPg;
+ int nDstPage;
i64 iOff;
i64 iEnd;
assert( pFile );
- assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || (
+ assert( nDestTruncate==0
+ || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
&& iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
));
- /* This call ensures that all data required to recreate the original
+ /* This block ensures that all data required to recreate the original
** database has been stored in the journal for pDestPager and the
** journal synced to disk. So at this point we may safely modify
** the database file in any way, knowing that if a power failure
** occurs, the original database will be reconstructed from the
** journal file. */
- rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
+ sqlite3PagerPagecount(pDestPager, &nDstPage);
+ for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){
+ if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
+ DbPage *pPg;
+ rc = sqlite3PagerGet(pDestPager, iPg, &pPg);
+ if( rc==SQLITE_OK ){
+ rc = sqlite3PagerWrite(pPg);
+ sqlite3PagerUnref(pPg);
+ }
+ }
+ }
+ if( rc==SQLITE_OK ){
+ rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
+ }
/* Write the extra pages and truncate the database file as required */
iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
@@ -500,6 +534,7 @@ int sqlite3_backup_step(sqlite3_backup *p, int nPage){
rc = sqlite3PagerSync(pDestPager);
}
}else{
+ sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
}
@@ -628,7 +663,7 @@ void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
int rc;
assert( p->pDestDb );
sqlite3_mutex_enter(p->pDestDb->mutex);
- rc = backupOnePage(p, iPage, aData);
+ rc = backupOnePage(p, iPage, aData, 1);
sqlite3_mutex_leave(p->pDestDb->mutex);
assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
if( rc!=SQLITE_OK ){
diff --git a/src/bitvec.c b/src/bitvec.c
index 8d805a6..52184aa 100644
--- a/src/bitvec.c
+++ b/src/bitvec.c
@@ -72,7 +72,7 @@
/*
** A bitmap is an instance of the following structure.
**
-** This bitmap records the existance of zero or more bits
+** This bitmap records the existence of zero or more bits
** with values between 1 and iSize, inclusive.
**
** There are three possible representations of the bitmap.
diff --git a/src/btree.c b/src/btree.c
index d34d6ee..3ca6058 100644
--- a/src/btree.c
+++ b/src/btree.c
@@ -43,6 +43,25 @@ int sqlite3BtreeTrace=1; /* True to enable tracing */
*/
#define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
+/*
+** Values passed as the 5th argument to allocateBtreePage()
+*/
+#define BTALLOC_ANY 0 /* Allocate any page */
+#define BTALLOC_EXACT 1 /* Allocate exact page if possible */
+#define BTALLOC_LE 2 /* Allocate any page <= the parameter */
+
+/*
+** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not
+** defined, or 0 if it is. For example:
+**
+** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
+*/
+#ifndef SQLITE_OMIT_AUTOVACUUM
+#define IfNotOmitAV(expr) (expr)
+#else
+#define IfNotOmitAV(expr) 0
+#endif
+
#ifndef SQLITE_OMIT_SHARED_CACHE
/*
** A list of BtShared objects that are eligible for participation
@@ -557,6 +576,19 @@ static void btreeClearHasContent(BtShared *pBt){
}
/*
+** Release all of the apPage[] pages for a cursor.
+*/
+static void btreeReleaseAllCursorPages(BtCursor *pCur){
+ int i;
+ for(i=0; i<=pCur->iPage; i++){
+ releasePage(pCur->apPage[i]);
+ pCur->apPage[i] = 0;
+ }
+ pCur->iPage = -1;
+}
+
+
+/*
** Save the current cursor position in the variables BtCursor.nKey
** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
**
@@ -595,12 +627,7 @@ static int saveCursorPosition(BtCursor *pCur){
assert( !pCur->apPage[0]->intKey || !pCur->pKey );
if( rc==SQLITE_OK ){
- int i;
- for(i=0; i<=pCur->iPage; i++){
- releasePage(pCur->apPage[i]);
- pCur->apPage[i] = 0;
- }
- pCur->iPage = -1;
+ btreeReleaseAllCursorPages(pCur);
pCur->eState = CURSOR_REQUIRESEEK;
}
@@ -618,11 +645,15 @@ static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
assert( sqlite3_mutex_held(pBt->mutex) );
assert( pExcept==0 || pExcept->pBt==pBt );
for(p=pBt->pCursor; p; p=p->pNext){
- if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) &&
- p->eState==CURSOR_VALID ){
- int rc = saveCursorPosition(p);
- if( SQLITE_OK!=rc ){
- return rc;
+ if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
+ if( p->eState==CURSOR_VALID ){
+ int rc = saveCursorPosition(p);
+ if( SQLITE_OK!=rc ){
+ return rc;
+ }
+ }else{
+ testcase( p->iPage>0 );
+ btreeReleaseAllCursorPages(p);
}
}
}
@@ -1550,13 +1581,17 @@ static int btreeGetPage(
BtShared *pBt, /* The btree */
Pgno pgno, /* Number of the page to fetch */
MemPage **ppPage, /* Return the page in this parameter */
- int noContent /* Do not load page content if true */
+ int noContent, /* Do not load page content if true */
+ int bReadonly /* True if a read-only (mmap) page is ok */
){
int rc;
DbPage *pDbPage;
+ int flags = (noContent ? PAGER_ACQUIRE_NOCONTENT : 0)
+ | (bReadonly ? PAGER_ACQUIRE_READONLY : 0);
+ assert( noContent==0 || bReadonly==0 );
assert( sqlite3_mutex_held(pBt->mutex) );
- rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
+ rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
if( rc ) return rc;
*ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
return SQLITE_OK;
@@ -1599,9 +1634,10 @@ u32 sqlite3BtreeLastPage(Btree *p){
** may remain unchanged, or it may be set to an invalid value.
*/
static int getAndInitPage(
- BtShared *pBt, /* The database file */
- Pgno pgno, /* Number of the page to get */
- MemPage **ppPage /* Write the page pointer here */
+ BtShared *pBt, /* The database file */
+ Pgno pgno, /* Number of the page to get */
+ MemPage **ppPage, /* Write the page pointer here */
+ int bReadonly /* True if a read-only (mmap) page is ok */
){
int rc;
assert( sqlite3_mutex_held(pBt->mutex) );
@@ -1609,7 +1645,7 @@ static int getAndInitPage(
if( pgno>btreePagecount(pBt) ){
rc = SQLITE_CORRUPT_BKPT;
}else{
- rc = btreeGetPage(pBt, pgno, ppPage, 0);
+ rc = btreeGetPage(pBt, pgno, ppPage, 0, bReadonly);
if( rc==SQLITE_OK ){
rc = btreeInitPage(*ppPage);
if( rc!=SQLITE_OK ){
@@ -1840,6 +1876,7 @@ int sqlite3BtreeOpen(
rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
EXTRA_SIZE, flags, vfsFlags, pageReinit);
if( rc==SQLITE_OK ){
+ sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
}
if( rc!=SQLITE_OK ){
@@ -2107,6 +2144,19 @@ int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
}
/*
+** Change the limit on the amount of the database file that may be
+** memory mapped.
+*/
+int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
+ BtShared *pBt = p->pBt;
+ assert( sqlite3_mutex_held(p->db->mutex) );
+ sqlite3BtreeEnter(p);
+ sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
+ sqlite3BtreeLeave(p);
+ return SQLITE_OK;
+}
+
+/*
** Change the way data is synced to disk in order to increase or decrease
** how well the database resists damage due to OS crashes and power
** failures. Level 1 is the same as asynchronous (no syncs() occur and
@@ -2200,6 +2250,24 @@ int sqlite3BtreeGetPageSize(Btree *p){
return p->pBt->pageSize;
}
+#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG)
+/*
+** This function is similar to sqlite3BtreeGetReserve(), except that it
+** may only be called if it is guaranteed that the b-tree mutex is already
+** held.
+**
+** This is useful in one special case in the backup API code where it is
+** known that the shared b-tree mutex is held, but the mutex on the
+** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
+** were to be called, it might collide with some other operation on the
+** database handle that owns *p, causing undefined behavior.
+*/
+int sqlite3BtreeGetReserveNoMutex(Btree *p){
+ assert( sqlite3_mutex_held(p->pBt->mutex) );
+ return p->pBt->pageSize - p->pBt->usableSize;
+}
+#endif /* SQLITE_HAS_CODEC || SQLITE_DEBUG */
+
#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
/*
** Return the number of bytes of space at the end of every page that
@@ -2313,7 +2381,7 @@ static int lockBtree(BtShared *pBt){
assert( pBt->pPage1==0 );
rc = sqlite3PagerSharedLock(pBt->pPager);
if( rc!=SQLITE_OK ) return rc;
- rc = btreeGetPage(pBt, 1, &pPage1, 0);
+ rc = btreeGetPage(pBt, 1, &pPage1, 0, 0);
if( rc!=SQLITE_OK ) return rc;
/* Do some checking to help insure the file we opened really is
@@ -2449,6 +2517,29 @@ page1_init_failed:
return rc;
}
+#ifndef NDEBUG
+/*
+** Return the number of cursors open on pBt. This is for use
+** in assert() expressions, so it is only compiled if NDEBUG is not
+** defined.
+**
+** Only write cursors are counted if wrOnly is true. If wrOnly is
+** false then all cursors are counted.
+**
+** For the purposes of this routine, a cursor is any cursor that
+** is capable of reading or writing to the databse. Cursors that
+** have been tripped into the CURSOR_FAULT state are not counted.
+*/
+static int countValidCursors(BtShared *pBt, int wrOnly){
+ BtCursor *pCur;
+ int r = 0;
+ for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
+ if( (wrOnly==0 || pCur->wrFlag) && pCur->eState!=CURSOR_FAULT ) r++;
+ }
+ return r;
+}
+#endif
+
/*
** If there are no outstanding cursors and we are not in the middle
** of a transaction but there is a read lock on the database, then
@@ -2459,7 +2550,7 @@ page1_init_failed:
*/
static void unlockBtreeIfUnused(BtShared *pBt){
assert( sqlite3_mutex_held(pBt->mutex) );
- assert( pBt->pCursor==0 || pBt->inTransaction>TRANS_NONE );
+ assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
assert( pBt->pPage1->aData );
assert( sqlite3PagerRefcount(pBt->pPager)==1 );
@@ -2514,6 +2605,20 @@ static int newDatabase(BtShared *pBt){
}
/*
+** Initialize the first page of the database file (creating a database
+** consisting of a single page and no schema objects). Return SQLITE_OK
+** if successful, or an SQLite error code otherwise.
+*/
+int sqlite3BtreeNewDb(Btree *p){
+ int rc;
+ sqlite3BtreeEnter(p);
+ p->pBt->nPage = 0;
+ rc = newDatabase(p->pBt);
+ sqlite3BtreeLeave(p);
+ return rc;
+}
+
+/*
** Attempt to start a new transaction. A write-transaction
** is started if the second argument is nonzero, otherwise a read-
** transaction. If the second argument is 2 or more and exclusive
@@ -2563,6 +2668,7 @@ int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
goto trans_begun;
}
+ assert( IfNotOmitAV(pBt->bDoTruncate)==0 );
/* Write transactions are not possible on a read-only database */
if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
@@ -2857,7 +2963,7 @@ static int relocatePage(
** iPtrPage.
*/
if( eType!=PTRMAP_ROOTPAGE ){
- rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
+ rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0, 0);
if( rc!=SQLITE_OK ){
return rc;
}
@@ -2879,24 +2985,23 @@ static int relocatePage(
static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
/*
-** Perform a single step of an incremental-vacuum. If successful,
-** return SQLITE_OK. If there is no work to do (and therefore no
-** point in calling this function again), return SQLITE_DONE.
+** Perform a single step of an incremental-vacuum. If successful, return
+** SQLITE_OK. If there is no work to do (and therefore no point in
+** calling this function again), return SQLITE_DONE. Or, if an error
+** occurs, return some other error code.
**
-** More specificly, this function attempts to re-organize the
-** database so that the last page of the file currently in use
-** is no longer in use.
+** More specificly, this function attempts to re-organize the database so
+** that the last page of the file currently in use is no longer in use.
**
-** If the nFin parameter is non-zero, this function assumes
-** that the caller will keep calling incrVacuumStep() until
-** it returns SQLITE_DONE or an error, and that nFin is the
-** number of pages the database file will contain after this
-** process is complete. If nFin is zero, it is assumed that
-** incrVacuumStep() will be called a finite amount of times
-** which may or may not empty the freelist. A full autovacuum
-** has nFin>0. A "PRAGMA incremental_vacuum" has nFin==0.
+** Parameter nFin is the number of pages that this database would contain
+** were this function called until it returns SQLITE_DONE.
+**
+** If the bCommit parameter is non-zero, this function assumes that the
+** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE
+** or an error. bCommit is passed true for an auto-vacuum-on-commmit
+** operation, or false for an incremental vacuum.
*/
-static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){
+static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
Pgno nFreeList; /* Number of pages still on the free-list */
int rc;
@@ -2921,15 +3026,15 @@ static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){
}
if( eType==PTRMAP_FREEPAGE ){
- if( nFin==0 ){
+ if( bCommit==0 ){
/* Remove the page from the files free-list. This is not required
- ** if nFin is non-zero. In that case, the free-list will be
+ ** if bCommit is non-zero. In that case, the free-list will be
** truncated to zero after this function returns, so it doesn't
** matter if it still contains some garbage entries.
*/
Pgno iFreePg;
MemPage *pFreePg;
- rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1);
+ rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT);
if( rc!=SQLITE_OK ){
return rc;
}
@@ -2939,34 +3044,37 @@ static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){
} else {
Pgno iFreePg; /* Index of free page to move pLastPg to */
MemPage *pLastPg;
+ u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */
+ Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */
- rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
+ rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0, 0);
if( rc!=SQLITE_OK ){
return rc;
}
- /* If nFin is zero, this loop runs exactly once and page pLastPg
+ /* If bCommit is zero, this loop runs exactly once and page pLastPg
** is swapped with the first free page pulled off the free list.
**
- ** On the other hand, if nFin is greater than zero, then keep
+ ** On the other hand, if bCommit is greater than zero, then keep
** looping until a free-page located within the first nFin pages
** of the file is found.
*/
+ if( bCommit==0 ){
+ eMode = BTALLOC_LE;
+ iNear = nFin;
+ }
do {
MemPage *pFreePg;
- rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0);
+ rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode);
if( rc!=SQLITE_OK ){
releasePage(pLastPg);
return rc;
}
releasePage(pFreePg);
- }while( nFin!=0 && iFreePg>nFin );
+ }while( bCommit && iFreePg>nFin );
assert( iFreePg<iLastPg );
- rc = sqlite3PagerWrite(pLastPg->pDbPage);
- if( rc==SQLITE_OK ){
- rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0);
- }
+ rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit);
releasePage(pLastPg);
if( rc!=SQLITE_OK ){
return rc;
@@ -2974,30 +3082,40 @@ static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){
}
}
- if( nFin==0 ){
- iLastPg--;
- while( iLastPg==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, iLastPg) ){
- if( PTRMAP_ISPAGE(pBt, iLastPg) ){
- MemPage *pPg;
- rc = btreeGetPage(pBt, iLastPg, &pPg, 0);
- if( rc!=SQLITE_OK ){
- return rc;
- }
- rc = sqlite3PagerWrite(pPg->pDbPage);
- releasePage(pPg);
- if( rc!=SQLITE_OK ){
- return rc;
- }
- }
+ if( bCommit==0 ){
+ do {
iLastPg--;
- }
- sqlite3PagerTruncateImage(pBt->pPager, iLastPg);
+ }while( iLastPg==PENDING_BYTE_PAGE(pBt) || PTRMAP_ISPAGE(pBt, iLastPg) );
+ pBt->bDoTruncate = 1;
pBt->nPage = iLastPg;
}
return SQLITE_OK;
}
/*
+** The database opened by the first argument is an auto-vacuum database
+** nOrig pages in size containing nFree free pages. Return the expected
+** size of the database in pages following an auto-vacuum operation.
+*/
+static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){
+ int nEntry; /* Number of entries on one ptrmap page */
+ Pgno nPtrmap; /* Number of PtrMap pages to be freed */
+ Pgno nFin; /* Return value */
+
+ nEntry = pBt->usableSize/5;
+ nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
+ nFin = nOrig - nFree - nPtrmap;
+ if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
+ nFin--;
+ }
+ while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
+ nFin--;
+ }
+
+ return nFin;
+}
+
+/*
** A write-transaction must be opened before calling this function.
** It performs a single unit of work towards an incremental vacuum.
**
@@ -3014,11 +3132,24 @@ int sqlite3BtreeIncrVacuum(Btree *p){
if( !pBt->autoVacuum ){
rc = SQLITE_DONE;
}else{
- invalidateAllOverflowCache(pBt);
- rc = incrVacuumStep(pBt, 0, btreePagecount(pBt));
- if( rc==SQLITE_OK ){
- rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
- put4byte(&pBt->pPage1->aData[28], pBt->nPage);
+ Pgno nOrig = btreePagecount(pBt);
+ Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
+ Pgno nFin = finalDbSize(pBt, nOrig, nFree);
+
+ if( nOrig<nFin ){
+ rc = SQLITE_CORRUPT_BKPT;
+ }else if( nFree>0 ){
+ rc = saveAllCursors(pBt, 0, 0);
+ if( rc==SQLITE_OK ){
+ invalidateAllOverflowCache(pBt);
+ rc = incrVacuumStep(pBt, nFin, nOrig, 0);
+ }
+ if( rc==SQLITE_OK ){
+ rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
+ put4byte(&pBt->pPage1->aData[28], pBt->nPage);
+ }
+ }else{
+ rc = SQLITE_DONE;
}
}
sqlite3BtreeLeave(p);
@@ -3045,9 +3176,7 @@ static int autoVacuumCommit(BtShared *pBt){
if( !pBt->incrVacuum ){
Pgno nFin; /* Number of pages in database after autovacuuming */
Pgno nFree; /* Number of pages on the freelist initially */
- Pgno nPtrmap; /* Number of PtrMap pages to be freed */
Pgno iFree; /* The next page to be freed */
- int nEntry; /* Number of entries on one ptrmap page */
Pgno nOrig; /* Database size before freeing */
nOrig = btreePagecount(pBt);
@@ -3060,26 +3189,20 @@ static int autoVacuumCommit(BtShared *pBt){
}
nFree = get4byte(&pBt->pPage1->aData[36]);
- nEntry = pBt->usableSize/5;
- nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
- nFin = nOrig - nFree - nPtrmap;
- if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
- nFin--;
- }
- while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
- nFin--;
- }
+ nFin = finalDbSize(pBt, nOrig, nFree);
if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
-
+ if( nFin<nOrig ){
+ rc = saveAllCursors(pBt, 0, 0);
+ }
for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
- rc = incrVacuumStep(pBt, nFin, iFree);
+ rc = incrVacuumStep(pBt, nFin, iFree, 1);
}
if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
put4byte(&pBt->pPage1->aData[32], 0);
put4byte(&pBt->pPage1->aData[36], 0);
put4byte(&pBt->pPage1->aData[28], nFin);
- sqlite3PagerTruncateImage(pBt->pPager, nFin);
+ pBt->bDoTruncate = 1;
pBt->nPage = nFin;
}
if( rc!=SQLITE_OK ){
@@ -3087,7 +3210,7 @@ static int autoVacuumCommit(BtShared *pBt){
}
}
- assert( nRef==sqlite3PagerRefcount(pPager) );
+ assert( nRef>=sqlite3PagerRefcount(pPager) );
return rc;
}
@@ -3134,6 +3257,9 @@ int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
return rc;
}
}
+ if( pBt->bDoTruncate ){
+ sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
+ }
#endif
rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
sqlite3BtreeLeave(p);
@@ -3149,7 +3275,9 @@ static void btreeEndTransaction(Btree *p){
BtShared *pBt = p->pBt;
assert( sqlite3BtreeHoldsMutex(p) );
- btreeClearHasContent(pBt);
+#ifndef SQLITE_OMIT_AUTOVACUUM
+ pBt->bDoTruncate = 0;
+#endif
if( p->inTrans>TRANS_NONE && p->db->activeVdbeCnt>1 ){
/* If there are other active statements that belong to this database
** handle, downgrade to a read-only transaction. The other statements
@@ -3224,6 +3352,7 @@ int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
return rc;
}
pBt->inTransaction = TRANS_READ;
+ btreeClearHasContent(pBt);
}
btreeEndTransaction(p);
@@ -3245,27 +3374,6 @@ int sqlite3BtreeCommit(Btree *p){
return rc;
}
-#ifndef NDEBUG
-/*
-** Return the number of write-cursors open on this handle. This is for use
-** in assert() expressions, so it is only compiled if NDEBUG is not
-** defined.
-**
-** For the purposes of this routine, a write-cursor is any cursor that
-** is capable of writing to the databse. That means the cursor was
-** originally opened for writing and the cursor has not be disabled
-** by having its state changed to CURSOR_FAULT.
-*/
-static int countWriteCursors(BtShared *pBt){
- BtCursor *pCur;
- int r = 0;
- for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
- if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++;
- }
- return r;
-}
-#endif
-
/*
** This routine sets the state to CURSOR_FAULT and the error
** code to errCode for every cursor on BtShared that pBtree
@@ -3337,7 +3445,7 @@ int sqlite3BtreeRollback(Btree *p, int tripCode){
/* The rollback may have destroyed the pPage1->aData value. So
** call btreeGetPage() on page 1 again to make
** sure pPage1->aData is set correctly. */
- if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
+ if( btreeGetPage(pBt, 1, &pPage1, 0, 0)==SQLITE_OK ){
int nPage = get4byte(28+(u8*)pPage1->aData);
testcase( nPage==0 );
if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
@@ -3345,8 +3453,9 @@ int sqlite3BtreeRollback(Btree *p, int tripCode){
pBt->nPage = nPage;
releasePage(pPage1);
}
- assert( countWriteCursors(pBt)==0 );
+ assert( countValidCursors(pBt, 1)==0 );
pBt->inTransaction = TRANS_READ;
+ btreeClearHasContent(pBt);
}
btreeEndTransaction(p);
@@ -3771,7 +3880,7 @@ static int getOverflowPage(
assert( next==0 || rc==SQLITE_DONE );
if( rc==SQLITE_OK ){
- rc = btreeGetPage(pBt, ovfl, &pPage, 0);
+ rc = btreeGetPage(pBt, ovfl, &pPage, 0, (ppPage==0));
assert( rc==SQLITE_OK || pPage==0 );
if( rc==SQLITE_OK ){
next = get4byte(pPage->aData);
@@ -3992,7 +4101,9 @@ static int accessPayload(
{
DbPage *pDbPage;
- rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
+ rc = sqlite3PagerAcquire(pBt->pPager, nextPage, &pDbPage,
+ (eOp==0 ? PAGER_ACQUIRE_READONLY : 0)
+ );
if( rc==SQLITE_OK ){
aPayload = sqlite3PagerGetData(pDbPage);
nextPage = get4byte(aPayload);
@@ -4171,10 +4282,11 @@ static int moveToChild(BtCursor *pCur, u32 newPgno){
assert( cursorHoldsMutex(pCur) );
assert( pCur->eState==CURSOR_VALID );
assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
+ assert( pCur->iPage>=0 );
if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
return SQLITE_CORRUPT_BKPT;
}
- rc = getAndInitPage(pBt, newPgno, &pNewPage);
+ rc = getAndInitPage(pBt, newPgno, &pNewPage, (pCur->wrFlag==0));
if( rc ) return rc;
pCur->apPage[i+1] = pNewPage;
pCur->aiIdx[i+1] = 0;
@@ -4291,7 +4403,7 @@ static int moveToRoot(BtCursor *pCur){
pCur->eState = CURSOR_INVALID;
return SQLITE_OK;
}else{
- rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
+ rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0], pCur->wrFlag==0);
if( rc!=SQLITE_OK ){
pCur->eState = CURSOR_INVALID;
return rc;
@@ -4821,21 +4933,23 @@ int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
** an error. *ppPage and *pPgno are undefined in the event of an error.
** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
**
-** If the "nearby" parameter is not 0, then a (feeble) effort is made to
+** If the "nearby" parameter is not 0, then an effort is made to
** locate a page close to the page number "nearby". This can be used in an
** attempt to keep related pages close to each other in the database file,
** which in turn can make database access faster.
**
-** If the "exact" parameter is not 0, and the page-number nearby exists
-** anywhere on the free-list, then it is guarenteed to be returned. This
-** is only used by auto-vacuum databases when allocating a new table.
+** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
+** anywhere on the free-list, then it is guaranteed to be returned. If
+** eMode is BTALLOC_LT then the page returned will be less than or equal
+** to nearby if any such page exists. If eMode is BTALLOC_ANY then there
+** are no restrictions on which page is returned.
*/
static int allocateBtreePage(
- BtShared *pBt,
- MemPage **ppPage,
- Pgno *pPgno,
- Pgno nearby,
- u8 exact
+ BtShared *pBt, /* The btree */
+ MemPage **ppPage, /* Store pointer to the allocated page here */
+ Pgno *pPgno, /* Store the page number here */
+ Pgno nearby, /* Search for a page near this one */
+ u8 eMode /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */
){
MemPage *pPage1;
int rc;
@@ -4846,6 +4960,7 @@ static int allocateBtreePage(
Pgno mxPage; /* Total size of the database file */
assert( sqlite3_mutex_held(pBt->mutex) );
+ assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
pPage1 = pBt->pPage1;
mxPage = btreePagecount(pBt);
n = get4byte(&pPage1->aData[36]);
@@ -4858,21 +4973,24 @@ static int allocateBtreePage(
Pgno iTrunk;
u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
- /* If the 'exact' parameter was true and a query of the pointer-map
+ /* If eMode==BTALLOC_EXACT and a query of the pointer-map
** shows that the page 'nearby' is somewhere on the free-list, then
** the entire-list will be searched for that page.
*/
#ifndef SQLITE_OMIT_AUTOVACUUM
- if( exact && nearby<=mxPage ){
- u8 eType;
- assert( nearby>0 );
- assert( pBt->autoVacuum );
- rc = ptrmapGet(pBt, nearby, &eType, 0);
- if( rc ) return rc;
- if( eType==PTRMAP_FREEPAGE ){
- searchList = 1;
+ if( eMode==BTALLOC_EXACT ){
+ if( nearby<=mxPage ){
+ u8 eType;
+ assert( nearby>0 );
+ assert( pBt->autoVacuum );
+ rc = ptrmapGet(pBt, nearby, &eType, 0);
+ if( rc ) return rc;
+ if( eType==PTRMAP_FREEPAGE ){
+ searchList = 1;
+ }
}
- *pPgno = nearby;
+ }else if( eMode==BTALLOC_LE ){
+ searchList = 1;
}
#endif
@@ -4885,7 +5003,8 @@ static int allocateBtreePage(
/* The code within this loop is run only once if the 'searchList' variable
** is not true. Otherwise, it runs once for each trunk-page on the
- ** free-list until the page 'nearby' is located.
+ ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
+ ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
*/
do {
pPrevTrunk = pTrunk;
@@ -4898,7 +5017,7 @@ static int allocateBtreePage(
if( iTrunk>mxPage ){
rc = SQLITE_CORRUPT_BKPT;
}else{
- rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
+ rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0, 0);
}
if( rc ){
pTrunk = 0;
@@ -4927,11 +5046,13 @@ static int allocateBtreePage(
rc = SQLITE_CORRUPT_BKPT;
goto end_allocate_page;
#ifndef SQLITE_OMIT_AUTOVACUUM
- }else if( searchList && nearby==iTrunk ){
+ }else if( searchList
+ && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE))
+ ){
/* The list is being searched and this trunk page is the page
** to allocate, regardless of whether it has leaves.
*/
- assert( *pPgno==iTrunk );
+ *pPgno = iTrunk;
*ppPage = pTrunk;
searchList = 0;
rc = sqlite3PagerWrite(pTrunk->pDbPage);
@@ -4960,7 +5081,7 @@ static int allocateBtreePage(
goto end_allocate_page;
}
testcase( iNewTrunk==mxPage );
- rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
+ rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0, 0);
if( rc!=SQLITE_OK ){
goto end_allocate_page;
}
@@ -4994,14 +5115,24 @@ static int allocateBtreePage(
unsigned char *aData = pTrunk->aData;
if( nearby>0 ){
u32 i;
- int dist;
closest = 0;
- dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
- for(i=1; i<k; i++){
- int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
- if( d2<dist ){
- closest = i;
- dist = d2;
+ if( eMode==BTALLOC_LE ){
+ for(i=0; i<k; i++){
+ iPage = get4byte(&aData[8+i*4]);
+ if( iPage<=nearby ){
+ closest = i;
+ break;
+ }
+ }
+ }else{
+ int dist;
+ dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
+ for(i=1; i<k; i++){
+ int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
+ if( d2<dist ){
+ closest = i;
+ dist = d2;
+ }
}
}
}else{
@@ -5015,7 +5146,9 @@ static int allocateBtreePage(
goto end_allocate_page;
}
testcase( iPage==mxPage );
- if( !searchList || iPage==nearby ){
+ if( !searchList
+ || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE))
+ ){
int noContent;
*pPgno = iPage;
TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
@@ -5028,7 +5161,7 @@ static int allocateBtreePage(
}
put4byte(&aData[4], k-1);
noContent = !btreeGetHasContent(pBt, *pPgno);
- rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
+ rc = btreeGetPage(pBt, *pPgno, ppPage, noContent, 0);
if( rc==SQLITE_OK ){
rc = sqlite3PagerWrite((*ppPage)->pDbPage);
if( rc!=SQLITE_OK ){
@@ -5042,8 +5175,26 @@ static int allocateBtreePage(
pPrevTrunk = 0;
}while( searchList );
}else{
- /* There are no pages on the freelist, so create a new page at the
- ** end of the file */
+ /* There are no pages on the freelist, so append a new page to the
+ ** database image.
+ **
+ ** Normally, new pages allocated by this block can be requested from the
+ ** pager layer with the 'no-content' flag set. This prevents the pager
+ ** from trying to read the pages content from disk. However, if the
+ ** current transaction has already run one or more incremental-vacuum
+ ** steps, then the page we are about to allocate may contain content
+ ** that is required in the event of a rollback. In this case, do
+ ** not set the no-content flag. This causes the pager to load and journal
+ ** the current page content before overwriting it.
+ **
+ ** Note that the pager will not actually attempt to load or journal
+ ** content for any page that really does lie past the end of the database
+ ** file on disk. So the effects of disabling the no-content optimization
+ ** here are confined to those pages that lie between the end of the
+ ** database image and the end of the database file.
+ */
+ int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate));
+
rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
if( rc ) return rc;
pBt->nPage++;
@@ -5058,7 +5209,7 @@ static int allocateBtreePage(
MemPage *pPg = 0;
TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
- rc = btreeGetPage(pBt, pBt->nPage, &pPg, 1);
+ rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent, 0);
if( rc==SQLITE_OK ){
rc = sqlite3PagerWrite(pPg->pDbPage);
releasePage(pPg);
@@ -5072,7 +5223,7 @@ static int allocateBtreePage(
*pPgno = pBt->nPage;
assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
- rc = btreeGetPage(pBt, *pPgno, ppPage, 1);
+ rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent, 0);
if( rc ) return rc;
rc = sqlite3PagerWrite((*ppPage)->pDbPage);
if( rc!=SQLITE_OK ){
@@ -5140,7 +5291,7 @@ static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
/* If the secure_delete option is enabled, then
** always fully overwrite deleted information with zeros.
*/
- if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
+ if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0, 0))!=0) )
|| ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
){
goto freepage_out;
@@ -5167,7 +5318,7 @@ static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
u32 nLeaf; /* Initial number of leaf cells on trunk page */
iTrunk = get4byte(&pPage1->aData[32]);
- rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
+ rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0, 0);
if( rc!=SQLITE_OK ){
goto freepage_out;
}
@@ -5213,7 +5364,7 @@ static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
** first trunk in the free-list is full. Either way, the page being freed
** will become the new first trunk page in the free-list.
*/
- if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
+ if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0, 0)) ){
goto freepage_out;
}
rc = sqlite3PagerWrite(pPage->pDbPage);
@@ -5256,7 +5407,7 @@ static int clearCell(MemPage *pPage, unsigned char *pCell){
return SQLITE_OK; /* No overflow pages. Return without doing anything */
}
if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
- return SQLITE_CORRUPT; /* Cell extends past end of page */
+ return SQLITE_CORRUPT_BKPT; /* Cell extends past end of page */
}
ovflPgno = get4byte(&pCell[info.iOverflow]);
assert( pBt->usableSize > 4 );
@@ -5400,7 +5551,7 @@ static int fillInCell(
** If this is the first overflow page, then write a partial entry
** to the pointer-map. If we write nothing to this pointer-map slot,
** then the optimistic overflow chain processing in clearCell()
- ** may misinterpret the uninitialised values and delete the
+ ** may misinterpret the uninitialized values and delete the
** wrong pages from the database.
*/
if( pBt->autoVacuum && rc==SQLITE_OK ){
@@ -5712,7 +5863,7 @@ static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
assert( pPage->nOverflow==1 );
/* This error condition is now caught prior to reaching this function */
- if( pPage->nCell<=0 ) return SQLITE_CORRUPT_BKPT;
+ if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT;
/* Allocate a new page. This page will become the right-sibling of
** pPage. Make the parent page writable, so that the new divider cell
@@ -6014,7 +6165,7 @@ static int balance_nonroot(
}
pgno = get4byte(pRight);
while( 1 ){
- rc = getAndInitPage(pBt, pgno, &apOld[i]);
+ rc = getAndInitPage(pBt, pgno, &apOld[i], 0);
if( rc ){
memset(apOld, 0, (i+1)*sizeof(MemPage*));
goto balance_cleanup;
@@ -6873,7 +7024,7 @@ int sqlite3BtreeInsert(
insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
- /* If no error has occured and pPage has an overflow cell, call balance()
+ /* If no error has occurred and pPage has an overflow cell, call balance()
** to redistribute the cells within the tree. Since balance() may move
** the cursor, zero the BtCursor.info.nSize and BtCursor.validNKey
** variables.
@@ -7087,7 +7238,7 @@ static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
** be moved to the allocated page (unless the allocated page happens
** to reside at pgnoRoot).
*/
- rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
+ rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT);
if( rc!=SQLITE_OK ){
return rc;
}
@@ -7102,10 +7253,17 @@ static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
u8 eType = 0;
Pgno iPtrPage = 0;
+ /* Save the positions of any open cursors. This is required in
+ ** case they are holding a reference to an xFetch reference
+ ** corresponding to page pgnoRoot. */
+ rc = saveAllCursors(pBt, 0, 0);
releasePage(pPageMove);
+ if( rc!=SQLITE_OK ){
+ return rc;
+ }
/* Move the page currently at pgnoRoot to pgnoMove. */
- rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
+ rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0, 0);
if( rc!=SQLITE_OK ){
return rc;
}
@@ -7126,7 +7284,7 @@ static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
if( rc!=SQLITE_OK ){
return rc;
}
- rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
+ rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0, 0);
if( rc!=SQLITE_OK ){
return rc;
}
@@ -7202,7 +7360,7 @@ static int clearDatabasePage(
return SQLITE_CORRUPT_BKPT;
}
- rc = getAndInitPage(pBt, pgno, &pPage);
+ rc = getAndInitPage(pBt, pgno, &pPage, 0);
if( rc ) return rc;
for(i=0; i<pPage->nCell; i++){
pCell = findCell(pPage, i);
@@ -7304,7 +7462,7 @@ static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
return SQLITE_LOCKED_SHAREDCACHE;
}
- rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
+ rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0, 0);
if( rc ) return rc;
rc = sqlite3BtreeClearTable(p, iTable, 0);
if( rc ){
@@ -7339,7 +7497,7 @@ static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
*/
MemPage *pMove;
releasePage(pPage);
- rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
+ rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0, 0);
if( rc!=SQLITE_OK ){
return rc;
}
@@ -7349,7 +7507,7 @@ static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
return rc;
}
pMove = 0;
- rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
+ rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0, 0);
freePage(pMove, &rc);
releasePage(pMove);
if( rc!=SQLITE_OK ){
@@ -7761,7 +7919,7 @@ static int checkTreePage(
usableSize = pBt->usableSize;
if( iPage==0 ) return 0;
if( checkRef(pCheck, iPage, zParentContext) ) return 0;
- if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
+ if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0, 0))!=0 ){
checkAppendMsg(pCheck, zContext,
"unable to get the page. error code=%d", rc);
return 0;
@@ -7994,7 +8152,7 @@ char *sqlite3BtreeIntegrityCheck(
}
i = PENDING_BYTE_PAGE(pBt);
if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
- sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000);
+ sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
sCheck.errMsg.useMalloc = 2;
/* Check the integrity of the freelist
@@ -8233,6 +8391,17 @@ int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
return SQLITE_ABORT;
}
+ /* Save the positions of all other cursors open on this table. This is
+ ** required in case any of them are holding references to an xFetch
+ ** version of the b-tree page modified by the accessPayload call below.
+ **
+ ** Note that pCsr must be open on a BTREE_INTKEY table and saveCursorPosition()
+ ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
+ ** saveAllCursors can only return SQLITE_OK.
+ */
+ VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
+ assert( rc==SQLITE_OK );
+
/* Check some assumptions:
** (a) the cursor is open for writing,
** (b) there is a read/write transaction open,
diff --git a/src/btree.h b/src/btree.h
index 95897d5..ace0f8c 100644
--- a/src/btree.h
+++ b/src/btree.h
@@ -63,6 +63,7 @@ int sqlite3BtreeOpen(
int sqlite3BtreeClose(Btree*);
int sqlite3BtreeSetCacheSize(Btree*,int);
+int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
int sqlite3BtreeSyncDisabled(Btree*);
int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
@@ -71,6 +72,9 @@ int sqlite3BtreeMaxPageCount(Btree*,int);
u32 sqlite3BtreeLastPage(Btree*);
int sqlite3BtreeSecureDelete(Btree*,int);
int sqlite3BtreeGetReserve(Btree*);
+#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG)
+int sqlite3BtreeGetReserveNoMutex(Btree *p);
+#endif
int sqlite3BtreeSetAutoVacuum(Btree *, int);
int sqlite3BtreeGetAutoVacuum(Btree *);
int sqlite3BtreeBeginTrans(Btree*,int);
@@ -114,6 +118,8 @@ void sqlite3BtreeTripAllCursors(Btree*, int);
void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
+int sqlite3BtreeNewDb(Btree *p);
+
/*
** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
** should be one of the following values. The integer values are assigned
@@ -134,6 +140,7 @@ int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
#define BTREE_TEXT_ENCODING 5
#define BTREE_USER_VERSION 6
#define BTREE_INCR_VACUUM 7
+#define BTREE_APPLICATION_ID 8
/*
** Values that may be OR'd together to form the second argument of an
diff --git a/src/btreeInt.h b/src/btreeInt.h
index b157dec..ce3c549 100644
--- a/src/btreeInt.h
+++ b/src/btreeInt.h
@@ -411,6 +411,7 @@ struct BtShared {
#ifndef SQLITE_OMIT_AUTOVACUUM
u8 autoVacuum; /* True if auto-vacuum is enabled */
u8 incrVacuum; /* True if incr-vacuum is enabled */
+ u8 bDoTruncate; /* True to truncate db on commit */
#endif
u8 inTransaction; /* Transaction state */
u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
diff --git a/src/build.c b/src/build.c
index 25e4740..3c91cdc 100644
--- a/src/build.c
+++ b/src/build.c
@@ -127,6 +127,7 @@ void sqlite3FinishCoding(Parse *pParse){
sqlite3 *db;
Vdbe *v;
+ assert( pParse->pToplevel==0 );
db = pParse->db;
if( db->mallocFailed ) return;
if( pParse->nested ) return;
@@ -320,6 +321,31 @@ Table *sqlite3LocateTable(
}
/*
+** Locate the table identified by *p.
+**
+** This is a wrapper around sqlite3LocateTable(). The difference between
+** sqlite3LocateTable() and this function is that this function restricts
+** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
+** non-NULL if it is part of a view or trigger program definition. See
+** sqlite3FixSrcList() for details.
+*/
+Table *sqlite3LocateTableItem(
+ Parse *pParse,
+ int isView,
+ struct SrcList_item *p
+){
+ const char *zDb;
+ assert( p->pSchema==0 || p->zDatabase==0 );
+ if( p->pSchema ){
+ int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
+ zDb = pParse->db->aDb[iDb].zName;
+ }else{
+ zDb = p->zDatabase;
+ }
+ return sqlite3LocateTable(pParse, isView, p->zName, zDb);
+}
+
+/*
** Locate the in-memory structure that describes
** a particular index given the name of that index
** and the name of the database that contains the index.
@@ -1169,7 +1195,7 @@ void sqlite3AddPrimaryKey(
pTab->tabFlags |= TF_HasPrimaryKey;
if( pList==0 ){
iCol = pTab->nCol - 1;
- pTab->aCol[iCol].isPrimKey = 1;
+ pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
}else{
for(i=0; i<pList->nExpr; i++){
for(iCol=0; iCol<pTab->nCol; iCol++){
@@ -1178,7 +1204,7 @@ void sqlite3AddPrimaryKey(
}
}
if( iCol<pTab->nCol ){
- pTab->aCol[iCol].isPrimKey = 1;
+ pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
}
}
if( pList->nExpr>1 ) iCol = -1;
@@ -1295,10 +1321,7 @@ CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
if( !initbusy && (!pColl || !pColl->xCmp) ){
- pColl = sqlite3GetCollSeq(db, enc, pColl, zName);
- if( !pColl ){
- sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
- }
+ pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
}
return pColl;
@@ -1997,6 +2020,7 @@ static void destroyTable(Parse *pParse, Table *pTab){
return;
}else{
int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
+ assert( iDb>=0 && iDb<pParse->db->nDb );
destroyRootPage(pParse, iLargest, iDb);
iDestroyed = iLargest;
}
@@ -2076,7 +2100,7 @@ void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
/* Drop all SQLITE_MASTER table and index entries that refer to the
** table. The program name loops through the master table and deletes
** every row that refers to a table of the same name as the one being
- ** dropped. Triggers are handled seperately because a trigger can be
+ ** dropped. Triggers are handled separately because a trigger can be
** created in the temp database that refers to a table in another
** database.
*/
@@ -2114,8 +2138,7 @@ void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
assert( pParse->nErr==0 );
assert( pName->nSrc==1 );
if( noErr ) db->suppressErr++;
- pTab = sqlite3LocateTable(pParse, isView,
- pName->a[0].zName, pName->a[0].zDatabase);
+ pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
if( noErr ) db->suppressErr--;
if( pTab==0 ){
@@ -2369,9 +2392,6 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
int tnum; /* Root page of index */
Vdbe *v; /* Generate code into this virtual machine */
KeyInfo *pKey; /* KeyInfo for index */
-#ifdef SQLITE_OMIT_MERGE_SORT
- int regIdxKey; /* Registers containing the index key */
-#endif
int regRecord; /* Register holding assemblied index record */
sqlite3 *db = pParse->db; /* The database connection */
int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
@@ -2399,13 +2419,9 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
(char *)pKey, P4_KEYINFO_HANDOFF);
sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
-#ifndef SQLITE_OMIT_MERGE_SORT
/* Open the sorter cursor if we are to use one. */
iSorter = pParse->nTab++;
sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
-#else
- iSorter = iTab;
-#endif
/* Open the table. Loop through all rows of the table, inserting index
** records into the sorter. */
@@ -2413,7 +2429,6 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
regRecord = sqlite3GetTempReg(pParse);
-#ifndef SQLITE_OMIT_MERGE_SORT
sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
@@ -2424,8 +2439,8 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
addr2 = sqlite3VdbeCurrentAddr(v);
sqlite3VdbeAddOp3(v, OP_SorterCompare, iSorter, j2, regRecord);
- sqlite3HaltConstraint(
- pParse, OE_Abort, "indexed columns are not unique", P4_STATIC
+ sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_UNIQUE,
+ OE_Abort, "indexed columns are not unique", P4_STATIC
);
}else{
addr2 = sqlite3VdbeCurrentAddr(v);
@@ -2433,30 +2448,6 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
-#else
- regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
- addr2 = addr1 + 1;
- if( pIndex->onError!=OE_None ){
- const int regRowid = regIdxKey + pIndex->nColumn;
- const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
- void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
-
- /* The registers accessed by the OP_IsUnique opcode were allocated
- ** using sqlite3GetTempRange() inside of the sqlite3GenerateIndexKey()
- ** call above. Just before that function was freed they were released
- ** (made available to the compiler for reuse) using
- ** sqlite3ReleaseTempRange(). So in some ways having the OP_IsUnique
- ** opcode use the values stored within seems dangerous. However, since
- ** we can be sure that no other temp registers have been allocated
- ** since sqlite3ReleaseTempRange() was called, it is safe to do so.
- */
- sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32);
- sqlite3HaltConstraint(
- pParse, OE_Abort, "indexed columns are not unique", P4_STATIC);
- }
- sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0);
- sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
-#endif
sqlite3ReleaseTempReg(pParse, regRecord);
sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2);
sqlite3VdbeJumpHere(v, addr1);
@@ -2555,9 +2546,9 @@ Index *sqlite3CreateIndex(
** sqlite3FixSrcList can never fail. */
assert(0);
}
- pTab = sqlite3LocateTable(pParse, 0, pTblName->a[0].zName,
- pTblName->a[0].zDatabase);
- if( !pTab || db->mallocFailed ) goto exit_create_index;
+ pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
+ assert( db->mallocFailed==0 || pTab==0 );
+ if( pTab==0 ) goto exit_create_index;
assert( db->aDb[iDb].pSchema==pTab->pSchema );
}else{
assert( pName==0 );
@@ -2571,7 +2562,7 @@ Index *sqlite3CreateIndex(
assert( pTab!=0 );
assert( pParse->nErr==0 );
if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
- && memcmp(&pTab->zName[7],"altertab_",9)!=0 ){
+ && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 ){
sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
goto exit_create_index;
}
@@ -2668,12 +2659,8 @@ Index *sqlite3CreateIndex(
for(i=0; i<pList->nExpr; i++){
Expr *pExpr = pList->a[i].pExpr;
if( pExpr ){
- CollSeq *pColl = pExpr->pColl;
- /* Either pColl!=0 or there was an OOM failure. But if an OOM
- ** failure we have quit before reaching this point. */
- if( ALWAYS(pColl) ){
- nExtra += (1 + sqlite3Strlen30(pColl->zName));
- }
+ assert( pExpr->op==TK_COLLATE );
+ nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
}
}
@@ -2746,14 +2733,10 @@ Index *sqlite3CreateIndex(
goto exit_create_index;
}
pIndex->aiColumn[i] = j;
- /* Justification of the ALWAYS(pListItem->pExpr->pColl): Because of
- ** the way the "idxlist" non-terminal is constructed by the parser,
- ** if pListItem->pExpr is not null then either pListItem->pExpr->pColl
- ** must exist or else there must have been an OOM error. But if there
- ** was an OOM error, we would never reach this point. */
- if( pListItem->pExpr && ALWAYS(pListItem->pExpr->pColl) ){
+ if( pListItem->pExpr ){
int nColl;
- zColl = pListItem->pExpr->pColl->zName;
+ assert( pListItem->pExpr->op==TK_COLLATE );
+ zColl = pListItem->pExpr->u.zToken;
nColl = sqlite3Strlen30(zColl) + 1;
assert( nExtra>=nColl );
memcpy(zExtra, zColl, nColl);
@@ -2762,9 +2745,7 @@ Index *sqlite3CreateIndex(
nExtra -= nColl;
}else{
zColl = pTab->aCol[j].zColl;
- if( !zColl ){
- zColl = "BINARY";
- }
+ if( !zColl ) zColl = "BINARY";
}
if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
goto exit_create_index;
@@ -2820,7 +2801,7 @@ Index *sqlite3CreateIndex(
** However the ON CONFLICT clauses are different. If both this
** constraint and the previous equivalent constraint have explicit
** ON CONFLICT clauses this is an error. Otherwise, use the
- ** explicitly specified behaviour for the index.
+ ** explicitly specified behavior for the index.
*/
if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
sqlite3ErrorMsg(pParse,
@@ -3567,6 +3548,15 @@ int sqlite3OpenTempDatabase(Parse *pParse){
void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
Parse *pToplevel = sqlite3ParseToplevel(pParse);
+#ifndef SQLITE_OMIT_TRIGGER
+ if( pToplevel!=pParse ){
+ /* This branch is taken if a trigger is currently being coded. In this
+ ** case, set cookieGoto to a non-zero value to show that this function
+ ** has been called. This is used by the sqlite3ExprCodeConstants()
+ ** function. */
+ pParse->cookieGoto = -1;
+ }
+#endif
if( pToplevel->cookieGoto==0 ){
Vdbe *v = sqlite3GetVdbe(pToplevel);
if( v==0 ) return; /* This only happens if there was a prior error */
@@ -3664,12 +3654,19 @@ void sqlite3MayAbort(Parse *pParse){
** error. The onError parameter determines which (if any) of the statement
** and/or current transaction is rolled back.
*/
-void sqlite3HaltConstraint(Parse *pParse, int onError, char *p4, int p4type){
+void sqlite3HaltConstraint(
+ Parse *pParse, /* Parsing context */
+ int errCode, /* extended error code */
+ int onError, /* Constraint type */
+ char *p4, /* Error message */
+ int p4type /* P4_STATIC or P4_TRANSIENT */
+){
Vdbe *v = sqlite3GetVdbe(pParse);
+ assert( (errCode&0xff)==SQLITE_CONSTRAINT );
if( onError==OE_Abort ){
sqlite3MayAbort(pParse);
}
- sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, p4, p4type);
+ sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type);
}
/*
diff --git a/src/callback.c b/src/callback.c
index a515e05..d40c65c 100644
--- a/src/callback.c
+++ b/src/callback.c
@@ -75,17 +75,18 @@ static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
**
** The return value is either the collation sequence to be used in database
** db for collation type name zName, length nName, or NULL, if no collation
-** sequence can be found.
+** sequence can be found. If no collation is found, leave an error message.
**
** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
*/
CollSeq *sqlite3GetCollSeq(
- sqlite3* db, /* The database connection */
+ Parse *pParse, /* Parsing context */
u8 enc, /* The desired encoding for the collating sequence */
CollSeq *pColl, /* Collating sequence with native encoding, or NULL */
const char *zName /* Collating sequence name */
){
CollSeq *p;
+ sqlite3 *db = pParse->db;
p = pColl;
if( !p ){
@@ -102,6 +103,9 @@ CollSeq *sqlite3GetCollSeq(
p = 0;
}
assert( !p || p->xCmp );
+ if( p==0 ){
+ sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
+ }
return p;
}
@@ -120,10 +124,8 @@ int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
if( pColl ){
const char *zName = pColl->zName;
sqlite3 *db = pParse->db;
- CollSeq *p = sqlite3GetCollSeq(db, ENC(db), pColl, zName);
+ CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
if( !p ){
- sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
- pParse->nErr++;
return SQLITE_ERROR;
}
assert( p==pColl );
diff --git a/src/crypto.c b/src/crypto.c
index 50213b5..2551e6b 100644
--- a/src/crypto.c
+++ b/src/crypto.c
@@ -1,10 +1,8 @@
/*
** SQLCipher
-** crypto.c developed by Stephen Lombardo (Zetetic LLC)
-** sjlombardo at zetetic dot net
-** http://zetetic.net
+** http://sqlcipher.net
**
-** Copyright (c) 2009, ZETETIC LLC
+** Copyright (c) 2008 - 2013, ZETETIC LLC
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
@@ -30,7 +28,7 @@
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
*/
-/* BEGIN CRYPTO */
+/* BEGIN SQLCIPHER */
#ifdef SQLITE_HAS_CODEC
#include <assert.h>
@@ -38,12 +36,12 @@
#include "btreeInt.h"
#include "crypto.h"
-const char* codec_get_cipher_version() {
+static const char* codec_get_cipher_version() {
return CIPHER_VERSION;
}
/* Generate code to return a string value */
-void codec_vdbe_return_static_string(Parse *pParse, const char *zLabel, const char *value){
+static void codec_vdbe_return_static_string(Parse *pParse, const char *zLabel, const char *value){
Vdbe *v = sqlite3GetVdbe(pParse);
sqlite3VdbeSetNumCols(v, 1);
sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
@@ -69,7 +67,7 @@ static int codec_set_btree_to_codec_pagesize(sqlite3 *db, Db *pDb, codec_ctx *ct
return rc;
}
-int codec_set_pass_key(sqlite3* db, int nDb, const void *zKey, int nKey, int for_ctx) {
+static int codec_set_pass_key(sqlite3* db, int nDb, const void *zKey, int nKey, int for_ctx) {
struct Db *pDb = &db->aDb[nDb];
CODEC_TRACE(("codec_set_pass_key: entered db=%p nDb=%d zKey=%s nKey=%d for_ctx=%d\n", db, nDb, (char *)zKey, nKey, for_ctx));
if(pDb->pBt) {
@@ -91,6 +89,11 @@ int codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLeft, const c
CODEC_TRACE(("codec_pragma: entered db=%p iDb=%d pParse=%p zLeft=%s zRight=%s ctx=%p\n", db, iDb, pParse, zLeft, zRight, ctx));
+ if( sqlite3StrICmp(zLeft, "cipher_provider")==0 && !zRight ){
+ if(ctx) { codec_vdbe_return_static_string(pParse, "cipher_provider",
+ sqlcipher_codec_get_cipher_provider(ctx));
+ }
+ } else
if( sqlite3StrICmp(zLeft, "cipher_version")==0 && !zRight ){
codec_vdbe_return_static_string(pParse, "cipher_version", codec_get_cipher_version());
}else
@@ -291,13 +294,13 @@ int sqlite3CodecAttach(sqlite3* db, int nDb, const void *zKey, int nKey) {
sqlcipher_activate(); /* perform internal initialization for sqlcipher */
+ sqlite3_mutex_enter(db->mutex);
+
/* point the internal codec argument against the contet to be prepared */
rc = sqlcipher_codec_ctx_init(&ctx, pDb, pDb->pBt->pBt->pPager, fd, zKey, nKey);
if(rc != SQLITE_OK) return rc; /* initialization failed, do not attach potentially corrupted context */
- sqlite3_mutex_enter(db->mutex);
-
sqlite3pager_sqlite3PagerSetCodec(sqlite3BtreePager(pDb->pBt), sqlite3Codec, NULL, sqlite3FreeCodecArg, (void *) ctx);
codec_set_btree_to_codec_pagesize(db, pDb, ctx);
@@ -423,6 +426,203 @@ void sqlite3CodecGetKey(sqlite3* db, int nDb, void **zKey, int *nKey) {
}
}
+#ifndef OMIT_EXPORT
+
+/*
+ * Implementation of an "export" function that allows a caller
+ * to duplicate the main database to an attached database. This is intended
+ * as a conveneince for users who need to:
+ *
+ * 1. migrate from an non-encrypted database to an encrypted database
+ * 2. move from an encrypted database to a non-encrypted database
+ * 3. convert beween the various flavors of encrypted databases.
+ *
+ * This implementation is based heavily on the procedure and code used
+ * in vacuum.c, but is exposed as a function that allows export to any
+ * named attached database.
+ */
+
+/*
+** Finalize a prepared statement. If there was an error, store the
+** text of the error message in *pzErrMsg. Return the result code.
+**
+** Based on vacuumFinalize from vacuum.c
+*/
+static int sqlcipher_finalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){
+ int rc;
+ rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
+ if( rc ){
+ sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
+ }
+ return rc;
+}
+
+/*
+** Execute zSql on database db. Return an error code.
+**
+** Based on execSql from vacuum.c
+*/
+static int sqlcipher_execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
+ sqlite3_stmt *pStmt;
+ VVA_ONLY( int rc; )
+ if( !zSql ){
+ return SQLITE_NOMEM;
+ }
+ if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
+ sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
+ return sqlite3_errcode(db);
+ }
+ VVA_ONLY( rc = ) sqlite3_step(pStmt);
+ assert( rc!=SQLITE_ROW );
+ return sqlcipher_finalize(db, pStmt, pzErrMsg);
+}
+
+/*
+** Execute zSql on database db. The statement returns exactly
+** one column. Execute this as SQL on the same database.
+**
+** Based on execExecSql from vacuum.c
+*/
+static int sqlcipher_execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
+ sqlite3_stmt *pStmt;
+ int rc;
+
+ rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
+ if( rc!=SQLITE_OK ) return rc;
+
+ while( SQLITE_ROW==sqlite3_step(pStmt) ){
+ rc = sqlcipher_execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0));
+ if( rc!=SQLITE_OK ){
+ sqlcipher_finalize(db, pStmt, pzErrMsg);
+ return rc;
+ }
+ }
+
+ return sqlcipher_finalize(db, pStmt, pzErrMsg);
+}
+
+/*
+ * copy database and schema from the main database to an attached database
+ *
+ * Based on sqlite3RunVacuum from vacuum.c
+*/
+void sqlcipher_exportFunc(sqlite3_context *context, int argc, sqlite3_value **argv) {
+ sqlite3 *db = sqlite3_context_db_handle(context);
+ const char* attachedDb = (const char*) sqlite3_value_text(argv[0]);
+ int saved_flags; /* Saved value of the db->flags */
+ int saved_nChange; /* Saved value of db->nChange */
+ int saved_nTotalChange; /* Saved value of db->nTotalChange */
+ void (*saved_xTrace)(void*,const char*); /* Saved db->xTrace */
+ int rc = SQLITE_OK; /* Return code from service routines */
+ char *zSql = NULL; /* SQL statements */
+ char *pzErrMsg = NULL;
+
+ saved_flags = db->flags;
+ saved_nChange = db->nChange;
+ saved_nTotalChange = db->nTotalChange;
+ saved_xTrace = db->xTrace;
+ db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin;
+ db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder);
+ db->xTrace = 0;
+
+ /* Query the schema of the main database. Create a mirror schema
+ ** in the temporary database.
+ */
+ zSql = sqlite3_mprintf(
+ "SELECT 'CREATE TABLE %s.' || substr(sql,14) "
+ " FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
+ " AND rootpage>0"
+ , attachedDb);
+ rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql);
+ if( rc!=SQLITE_OK ) goto end_of_export;
+ sqlite3_free(zSql);
+
+ zSql = sqlite3_mprintf(
+ "SELECT 'CREATE INDEX %s.' || substr(sql,14)"
+ " FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %%' "
+ , attachedDb);
+ rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql);
+ if( rc!=SQLITE_OK ) goto end_of_export;
+ sqlite3_free(zSql);
+
+ zSql = sqlite3_mprintf(
+ "SELECT 'CREATE UNIQUE INDEX %s.' || substr(sql,21) "
+ " FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %%'"
+ , attachedDb);
+ rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql);
+ if( rc!=SQLITE_OK ) goto end_of_export;
+ sqlite3_free(zSql);
+
+ /* Loop through the tables in the main database. For each, do
+ ** an "INSERT INTO rekey_db.xxx SELECT * FROM main.xxx;" to copy
+ ** the contents to the temporary database.
+ */
+ zSql = sqlite3_mprintf(
+ "SELECT 'INSERT INTO %s.' || quote(name) "
+ "|| ' SELECT * FROM main.' || quote(name) || ';'"
+ "FROM main.sqlite_master "
+ "WHERE type = 'table' AND name!='sqlite_sequence' "
+ " AND rootpage>0"
+ , attachedDb);
+ rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql);
+ if( rc!=SQLITE_OK ) goto end_of_export;
+ sqlite3_free(zSql);
+
+ /* Copy over the sequence table
+ */
+ zSql = sqlite3_mprintf(
+ "SELECT 'DELETE FROM %s.' || quote(name) || ';' "
+ "FROM %s.sqlite_master WHERE name='sqlite_sequence' "
+ , attachedDb, attachedDb);
+ rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql);
+ if( rc!=SQLITE_OK ) goto end_of_export;
+ sqlite3_free(zSql);
+
+ zSql = sqlite3_mprintf(
+ "SELECT 'INSERT INTO %s.' || quote(name) "
+ "|| ' SELECT * FROM main.' || quote(name) || ';' "
+ "FROM %s.sqlite_master WHERE name=='sqlite_sequence';"
+ , attachedDb, attachedDb);
+ rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql);
+ if( rc!=SQLITE_OK ) goto end_of_export;
+ sqlite3_free(zSql);
+
+ /* Copy the triggers, views, and virtual tables from the main database
+ ** over to the temporary database. None of these objects has any
+ ** associated storage, so all we have to do is copy their entries
+ ** from the SQLITE_MASTER table.
+ */
+ zSql = sqlite3_mprintf(
+ "INSERT INTO %s.sqlite_master "
+ " SELECT type, name, tbl_name, rootpage, sql"
+ " FROM main.sqlite_master"
+ " WHERE type='view' OR type='trigger'"
+ " OR (type='table' AND rootpage=0)"
+ , attachedDb);
+ rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execSql(db, &pzErrMsg, zSql);
+ if( rc!=SQLITE_OK ) goto end_of_export;
+ sqlite3_free(zSql);
+
+ zSql = NULL;
+end_of_export:
+ db->flags = saved_flags;
+ db->nChange = saved_nChange;
+ db->nTotalChange = saved_nTotalChange;
+ db->xTrace = saved_xTrace;
+
+ sqlite3_free(zSql);
+
+ if(rc) {
+ if(pzErrMsg != NULL) {
+ sqlite3_result_error(context, pzErrMsg, -1);
+ sqlite3DbFree(db, pzErrMsg);
+ } else {
+ sqlite3_result_error(context, sqlite3ErrStr(rc), -1);
+ }
+ }
+}
+
+#endif
-/* END CRYPTO */
+/* END SQLCIPHER */
#endif
diff --git a/src/crypto.h b/src/crypto.h
index 8520152..a45b57c 100644
--- a/src/crypto.h
+++ b/src/crypto.h
@@ -30,15 +30,21 @@
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
*/
-/* BEGIN CRYPTO */
+/* BEGIN SQLCIPHER */
#ifdef SQLITE_HAS_CODEC
#ifndef CRYPTO_H
#define CRYPTO_H
+#if !defined (SQLCIPHER_CRYPTO_CC) \
+ && !defined (SQLCIPHER_CRYPTO_LIBTOMCRYPT) \
+ && !defined (SQLCIPHER_CRYPTO_OPENSSL)
+#define SQLCIPHER_CRYPTO_OPENSSL
+#endif
+
#define FILE_HEADER_SZ 16
#ifndef CIPHER_VERSION
-#define CIPHER_VERSION "2.1.1"
+#define CIPHER_VERSION "2.2.1"
#endif
#ifndef CIPHER
@@ -82,6 +88,15 @@
#define HMAC_SALT_MASK 0x3a
#endif
+#ifndef CIPHER_MAX_IV_SZ
+#define CIPHER_MAX_IV_SZ 16
+#endif
+
+#ifndef CIPHER_MAX_KEY_SZ
+#define CIPHER_MAX_KEY_SZ 64
+#endif
+
+
#ifdef CODEC_DEBUG
#define CODEC_TRACE(X) {printf X;fflush(stdout);}
#else
@@ -135,16 +150,8 @@ static void cipher_hex2bin(const char *hex, int sz, unsigned char *out){
}
/* extensions defined in crypto_impl.c */
-
typedef struct codec_ctx codec_ctx;
-/* utility functions */
-void* sqlcipher_memset(void *v, unsigned char value, int len);
-int sqlcipher_ismemset(const void *v, unsigned char value, int len);
-int sqlcipher_memcmp(const void *v0, const void *v1, int len);
-int sqlcipher_pseudorandom(void *, int);
-void sqlcipher_free(void *, int);
-
/* activation and initialization */
void sqlcipher_activate();
void sqlcipher_deactivate();
@@ -194,8 +201,7 @@ int sqlcipher_codec_ctx_set_flag(codec_ctx *ctx, unsigned int flag);
int sqlcipher_codec_ctx_unset_flag(codec_ctx *ctx, unsigned int flag);
int sqlcipher_codec_ctx_get_flag(codec_ctx *ctx, unsigned int flag, int for_ctx);
-/* end extensions defined in crypto_impl.c */
-
+const char* sqlcipher_codec_get_cipher_provider(codec_ctx *ctx);
#endif
#endif
-/* END CRYPTO */
+/* END SQLCIPHER */
diff --git a/src/crypto_cc.c b/src/crypto_cc.c
new file mode 100644
index 0000000..cf932f6
--- /dev/null
+++ b/src/crypto_cc.c
@@ -0,0 +1,140 @@
+/*
+** SQLCipher
+** http://sqlcipher.net
+**
+** Copyright (c) 2008 - 2013, ZETETIC LLC
+** All rights reserved.
+**
+** Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in the
+** documentation and/or other materials provided with the distribution.
+** * Neither the name of the ZETETIC LLC nor the
+** names of its contributors may be used to endorse or promote products
+** derived from this software without specific prior written permission.
+**
+** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY
+** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY
+** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+**
+*/
+/* BEGIN SQLCIPHER */
+#ifdef SQLITE_HAS_CODEC
+#ifdef SQLCIPHER_CRYPTO_CC
+#include "crypto.h"
+#include "sqlcipher.h"
+#include <CommonCrypto/CommonCrypto.h>
+#include <Security/SecRandom.h>
+
+/* generate a defined number of random bytes */
+static int sqlcipher_cc_random (void *ctx, void *buffer, int length) {
+ return (SecRandomCopyBytes(kSecRandomDefault, length, (uint8_t *)buffer) == 0) ? SQLITE_OK : SQLITE_ERROR;
+}
+
+static const char* sqlcipher_cc_get_provider_name(void *ctx) {
+ return "commoncrypto";
+}
+
+static int sqlcipher_cc_hmac(void *ctx, unsigned char *hmac_key, int key_sz, unsigned char *in, int in_sz, unsigned char *in2, int in2_sz, unsigned char *out) {
+ CCHmacContext hmac_context;
+ CCHmacInit(&hmac_context, kCCHmacAlgSHA1, hmac_key, key_sz);
+ CCHmacUpdate(&hmac_context, in, in_sz);
+ CCHmacUpdate(&hmac_context, in2, in2_sz);
+ CCHmacFinal(&hmac_context, out);
+ return SQLITE_OK;
+}
+
+static int sqlcipher_cc_kdf(void *ctx, const char *pass, int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key) {
+ CCKeyDerivationPBKDF(kCCPBKDF2, pass, pass_sz, salt, salt_sz, kCCPRFHmacAlgSHA1, workfactor, key, key_sz);
+ return SQLITE_OK;
+}
+
+static int sqlcipher_cc_cipher(void *ctx, int mode, unsigned char *key, int key_sz, unsigned char *iv, unsigned char *in, int in_sz, unsigned char *out) {
+ CCCryptorRef cryptor;
+ size_t tmp_csz, csz;
+ CCOperation op = mode == CIPHER_ENCRYPT ? kCCEncrypt : kCCDecrypt;
+
+ CCCryptorCreate(op, kCCAlgorithmAES128, 0, key, kCCKeySizeAES256, iv, &cryptor);
+ CCCryptorUpdate(cryptor, in, in_sz, out, in_sz, &tmp_csz);
+ csz = tmp_csz;
+ out += tmp_csz;
+ CCCryptorFinal(cryptor, out, in_sz - csz, &tmp_csz);
+ csz += tmp_csz;
+ CCCryptorRelease(cryptor);
+ assert(size == csz);
+
+ return SQLITE_OK;
+}
+
+static int sqlcipher_cc_set_cipher(void *ctx, const char *cipher_name) {
+ return SQLITE_OK;
+}
+
+static const char* sqlcipher_cc_get_cipher(void *ctx) {
+ return "aes-256-cbc";
+}
+
+static int sqlcipher_cc_get_key_sz(void *ctx) {
+ return kCCKeySizeAES256;
+}
+
+static int sqlcipher_cc_get_iv_sz(void *ctx) {
+ return kCCBlockSizeAES128;
+}
+
+static int sqlcipher_cc_get_block_sz(void *ctx) {
+ return kCCBlockSizeAES128;
+}
+
+static int sqlcipher_cc_get_hmac_sz(void *ctx) {
+ return CC_SHA1_DIGEST_LENGTH;
+}
+
+static int sqlcipher_cc_ctx_copy(void *target_ctx, void *source_ctx) {
+ return SQLITE_OK;
+}
+
+static int sqlcipher_cc_ctx_cmp(void *c1, void *c2) {
+ return SQLITE_OK;
+}
+
+static int sqlcipher_cc_ctx_init(void **ctx) {
+ return SQLITE_OK;
+}
+
+static int sqlcipher_cc_ctx_free(void **ctx) {
+ return SQLITE_OK;
+}
+
+int sqlcipher_cc_setup(sqlcipher_provider *p) {
+ p->random = sqlcipher_cc_random;
+ p->get_provider_name = sqlcipher_cc_get_provider_name;
+ p->hmac = sqlcipher_cc_hmac;
+ p->kdf = sqlcipher_cc_kdf;
+ p->cipher = sqlcipher_cc_cipher;
+ p->set_cipher = sqlcipher_cc_set_cipher;
+ p->get_cipher = sqlcipher_cc_get_cipher;
+ p->get_key_sz = sqlcipher_cc_get_key_sz;
+ p->get_iv_sz = sqlcipher_cc_get_iv_sz;
+ p->get_block_sz = sqlcipher_cc_get_block_sz;
+ p->get_hmac_sz = sqlcipher_cc_get_hmac_sz;
+ p->ctx_copy = sqlcipher_cc_ctx_copy;
+ p->ctx_cmp = sqlcipher_cc_ctx_cmp;
+ p->ctx_init = sqlcipher_cc_ctx_init;
+ p->ctx_free = sqlcipher_cc_ctx_free;
+ return SQLITE_OK;
+}
+
+#endif
+#endif
+/* END SQLCIPHER */
diff --git a/src/crypto_impl.c b/src/crypto_impl.c
index f35144d..1e7fa99 100644
--- a/src/crypto_impl.c
+++ b/src/crypto_impl.c
@@ -1,10 +1,8 @@
/*
** SQLCipher
-** crypto_impl.c developed by Stephen Lombardo (Zetetic LLC)
-** sjlombardo at zetetic dot net
-** http://zetetic.net
+** http://sqlcipher.net
**
-** Copyright (c) 2011, ZETETIC LLC
+** Copyright (c) 2008 - 2013, ZETETIC LLC
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
@@ -30,14 +28,12 @@
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
*/
-/* BEGIN CRYPTO */
+/* BEGIN SQLCIPHER */
#ifdef SQLITE_HAS_CODEC
-#include <openssl/rand.h>
-#include <openssl/evp.h>
-#include <openssl/hmac.h>
#include "sqliteInt.h"
#include "btreeInt.h"
+#include "sqlcipher.h"
#include "crypto.h"
#ifndef OMIT_MEMLOCK
#if defined(__unix__) || defined(__APPLE__)
@@ -52,9 +48,6 @@
struct and associated functions are defined here */
typedef struct {
int derive_key;
- EVP_CIPHER *evp_cipher;
- EVP_CIPHER_CTX ectx;
- HMAC_CTX hctx;
int kdf_iter;
int fast_kdf_iter;
int key_sz;
@@ -67,23 +60,15 @@ typedef struct {
unsigned char *key;
unsigned char *hmac_key;
char *pass;
+ sqlcipher_provider *provider;
+ void *provider_ctx;
} cipher_ctx;
-void sqlcipher_cipher_ctx_free(cipher_ctx **);
-int sqlcipher_cipher_ctx_cmp(cipher_ctx *, cipher_ctx *);
-int sqlcipher_cipher_ctx_copy(cipher_ctx *, cipher_ctx *);
-int sqlcipher_cipher_ctx_init(cipher_ctx **);
-int sqlcipher_cipher_ctx_set_pass(cipher_ctx *, const void *, int);
-int sqlcipher_cipher_ctx_key_derive(codec_ctx *, cipher_ctx *);
-
-/* prototype for pager HMAC function */
-int sqlcipher_page_hmac(cipher_ctx *, Pgno, unsigned char *, int, unsigned char *);
-
static unsigned int default_flags = DEFAULT_CIPHER_FLAGS;
static unsigned char hmac_salt_mask = HMAC_SALT_MASK;
-
-static unsigned int openssl_external_init = 0;
-static unsigned int openssl_init_count = 0;
+static unsigned int sqlcipher_activate_count = 0;
+static sqlite3_mutex* sqlcipher_provider_mutex = NULL;
+static sqlcipher_provider *default_provider = NULL;
struct codec_ctx {
int kdf_salt_sz;
@@ -96,49 +81,78 @@ struct codec_ctx {
cipher_ctx *write_ctx;
};
-/* activate and initialize sqlcipher. Most importantly, this will automatically
- intialize OpenSSL's EVP system if it hasn't already be externally. Note that
- this function may be called multiple times as new codecs are intiialized.
- Thus it performs some basic counting to ensure that only the last and final
- sqlcipher_deactivate() will free the EVP structures.
-*/
+int sqlcipher_register_provider(sqlcipher_provider *p) {
+ sqlite3_mutex_enter(sqlcipher_provider_mutex);
+ if(default_provider != NULL && default_provider != p) {
+ /* only free the current registerd provider if it has been initialized
+ and it isn't a pointer to the same provider passed to the function
+ (i.e. protect against a caller calling register twice for the same provider) */
+ sqlcipher_free(default_provider, sizeof(sqlcipher_provider));
+ }
+ default_provider = p;
+ sqlite3_mutex_leave(sqlcipher_provider_mutex);
+ return SQLITE_OK;
+}
+
+/* return a pointer to the currently registered provider. This will
+ allow an application to fetch the current registered provider and
+ make minor changes to it */
+sqlcipher_provider* sqlcipher_get_provider() {
+ return default_provider;
+}
+
void sqlcipher_activate() {
- sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
+ sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
- /* we'll initialize openssl and increment the internal init counter
- but only if it hasn't been initalized outside of SQLCipher by this program
- e.g. on startup */
- if(openssl_init_count == 0 && EVP_get_cipherbyname(CIPHER) != NULL) {
- openssl_external_init = 1;
+ if(sqlcipher_provider_mutex == NULL) {
+ /* allocate a new mutex to guard access to the provider */
+ sqlcipher_provider_mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
}
- if(openssl_external_init == 0) {
- if(openssl_init_count == 0) {
- OpenSSL_add_all_algorithms();
- }
- openssl_init_count++;
- }
- sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
+ /* check to see if there is a provider registered at this point
+ if there no provider registered at this point, register the
+ default provider */
+ if(sqlcipher_get_provider() == NULL) {
+ sqlcipher_provider *p = sqlcipher_malloc(sizeof(sqlcipher_provider));
+#if defined (SQLCIPHER_CRYPTO_CC)
+ extern int sqlcipher_cc_setup(sqlcipher_provider *p);
+ sqlcipher_cc_setup(p);
+#elif defined (SQLCIPHER_CRYPTO_LIBTOMCRYPT)
+ extern int sqlcipher_ltc_setup(sqlcipher_provider *p);
+ sqlcipher_ltc_setup(p);
+#elif defined (SQLCIPHER_CRYPTO_OPENSSL)
+ extern int sqlcipher_openssl_setup(sqlcipher_provider *p);
+ sqlcipher_openssl_setup(p);
+#else
+#error "NO DEFAULT SQLCIPHER CRYPTO PROVIDER DEFINED"
+#endif
+ sqlcipher_register_provider(p);
+ }
+
+ sqlcipher_activate_count++; /* increment activation count */
+
+ sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
}
-/* deactivate SQLCipher, most imporantly decremeting the activation count and
- freeing the EVP structures on the final deactivation to ensure that
- OpenSSL memory is cleaned up */
void sqlcipher_deactivate() {
- sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
- /* If it is initialized externally, then the init counter should never be greater than zero.
- This should prevent SQLCipher from "cleaning up" openssl
- when something else in the program might be using it. */
- if(openssl_external_init == 0) {
- openssl_init_count--;
- /* if the counter reaches zero after it's decremented release EVP memory
- Note: this code will only be reached if OpensSSL_add_all_algorithms()
- is called by SQLCipher internally. */
- if(openssl_init_count == 0) {
- EVP_cleanup();
+ sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
+ sqlcipher_activate_count--;
+ /* if no connections are using sqlcipher, cleanup globals */
+ if(sqlcipher_activate_count < 1) {
+ sqlite3_mutex_enter(sqlcipher_provider_mutex);
+ if(default_provider != NULL) {
+ sqlcipher_free(default_provider, sizeof(sqlcipher_provider));
+ default_provider = NULL;
}
+ sqlite3_mutex_leave(sqlcipher_provider_mutex);
+
+ /* last connection closed, free provider mutex*/
+ sqlite3_mutex_free(sqlcipher_provider_mutex);
+ sqlcipher_provider_mutex = NULL;
+
+ sqlcipher_activate_count = 0; /* reset activation count */
}
- sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
+ sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
}
/* constant time memset using volitile to avoid having the memset
@@ -185,11 +199,6 @@ int sqlcipher_memcmp(const void *v0, const void *v1, int len) {
return (result != 0);
}
-/* generate a defined number of pseudorandom bytes */
-int sqlcipher_random (void *buffer, int length) {
- return RAND_bytes((unsigned char *)buffer, length);
-}
-
/**
* Free and wipe memory. Uses SQLites internal sqlite3_free so that memory
* can be countend and memory leak detection works in the test suite.
@@ -242,14 +251,24 @@ void* sqlcipher_malloc(int sz) {
* returns SQLITE_OK if initialization was successful
* returns SQLITE_NOMEM if an error occured allocating memory
*/
-int sqlcipher_cipher_ctx_init(cipher_ctx **iCtx) {
+static int sqlcipher_cipher_ctx_init(cipher_ctx **iCtx) {
+ int rc;
cipher_ctx *ctx;
*iCtx = (cipher_ctx *) sqlcipher_malloc(sizeof(cipher_ctx));
ctx = *iCtx;
if(ctx == NULL) return SQLITE_NOMEM;
- ctx->key = (unsigned char *) sqlcipher_malloc(EVP_MAX_KEY_LENGTH);
- ctx->hmac_key = (unsigned char *) sqlcipher_malloc(EVP_MAX_KEY_LENGTH);
+ ctx->provider = (sqlcipher_provider *) sqlcipher_malloc(sizeof(sqlcipher_provider));
+ if(ctx->provider == NULL) return SQLITE_NOMEM;
+
+ /* make a copy of the provider to be used for the duration of the context */
+ sqlite3_mutex_enter(sqlcipher_provider_mutex);
+ memcpy(ctx->provider, default_provider, sizeof(sqlcipher_provider));
+ sqlite3_mutex_leave(sqlcipher_provider_mutex);
+
+ if((rc = ctx->provider->ctx_init(&ctx->provider_ctx)) != SQLITE_OK) return rc;
+ ctx->key = (unsigned char *) sqlcipher_malloc(CIPHER_MAX_KEY_SZ);
+ ctx->hmac_key = (unsigned char *) sqlcipher_malloc(CIPHER_MAX_KEY_SZ);
if(ctx->key == NULL) return SQLITE_NOMEM;
if(ctx->hmac_key == NULL) return SQLITE_NOMEM;
@@ -262,9 +281,11 @@ int sqlcipher_cipher_ctx_init(cipher_ctx **iCtx) {
/**
* Free and wipe memory associated with a cipher_ctx
*/
-void sqlcipher_cipher_ctx_free(cipher_ctx **iCtx) {
+static void sqlcipher_cipher_ctx_free(cipher_ctx **iCtx) {
cipher_ctx *ctx = *iCtx;
CODEC_TRACE(("cipher_ctx_free: entered iCtx=%p\n", iCtx));
+ ctx->provider->ctx_free(&ctx->provider_ctx);
+ sqlcipher_free(ctx->provider, sizeof(sqlcipher_provider));
sqlcipher_free(ctx->key, ctx->key_sz);
sqlcipher_free(ctx->hmac_key, ctx->key_sz);
sqlcipher_free(ctx->pass, ctx->pass_sz);
@@ -277,18 +298,18 @@ void sqlcipher_cipher_ctx_free(cipher_ctx **iCtx) {
* returns 0 if all the parameters (except the derived key data) are the same
* returns 1 otherwise
*/
-int sqlcipher_cipher_ctx_cmp(cipher_ctx *c1, cipher_ctx *c2) {
+static int sqlcipher_cipher_ctx_cmp(cipher_ctx *c1, cipher_ctx *c2) {
CODEC_TRACE(("sqlcipher_cipher_ctx_cmp: entered c1=%p c2=%p\n", c1, c2));
if(
- c1->evp_cipher == c2->evp_cipher
- && c1->iv_sz == c2->iv_sz
+ c1->iv_sz == c2->iv_sz
&& c1->kdf_iter == c2->kdf_iter
&& c1->fast_kdf_iter == c2->fast_kdf_iter
&& c1->key_sz == c2->key_sz
&& c1->pass_sz == c2->pass_sz
&& c1->flags == c2->flags
&& c1->hmac_sz == c2->hmac_sz
+ && c1->provider->ctx_cmp(c1->provider_ctx, c2->provider_ctx)
&& (
c1->pass == c2->pass
|| !sqlcipher_memcmp((const unsigned char*)c1->pass,
@@ -307,19 +328,27 @@ int sqlcipher_cipher_ctx_cmp(cipher_ctx *c1, cipher_ctx *c2) {
* returns SQLITE_OK if initialization was successful
* returns SQLITE_NOMEM if an error occured allocating memory
*/
-int sqlcipher_cipher_ctx_copy(cipher_ctx *target, cipher_ctx *source) {
+static int sqlcipher_cipher_ctx_copy(cipher_ctx *target, cipher_ctx *source) {
void *key = target->key;
void *hmac_key = target->hmac_key;
+ void *provider = target->provider;
+ void *provider_ctx = target->provider_ctx;
CODEC_TRACE(("sqlcipher_cipher_ctx_copy: entered target=%p, source=%p\n", target, source));
sqlcipher_free(target->pass, target->pass_sz);
memcpy(target, source, sizeof(cipher_ctx));
target->key = key; //restore pointer to previously allocated key data
- memcpy(target->key, source->key, EVP_MAX_KEY_LENGTH);
+ memcpy(target->key, source->key, CIPHER_MAX_KEY_SZ);
target->hmac_key = hmac_key; //restore pointer to previously allocated hmac key data
- memcpy(target->hmac_key, source->hmac_key, EVP_MAX_KEY_LENGTH);
+ memcpy(target->hmac_key, source->hmac_key, CIPHER_MAX_KEY_SZ);
+
+ target->provider = provider; // restore pointer to previouly allocated provider;
+ memcpy(target->provider, source->provider, sizeof(sqlcipher_provider));
+
+ target->provider_ctx = provider_ctx; // restore pointer to previouly allocated provider context;
+ target->provider->ctx_copy(target->provider_ctx, source->provider_ctx);
target->pass = sqlcipher_malloc(source->pass_sz);
if(target->pass == NULL) return SQLITE_NOMEM;
@@ -336,7 +365,7 @@ int sqlcipher_cipher_ctx_copy(cipher_ctx *target, cipher_ctx *source) {
* returns SQLITE_NOMEM if an error occured allocating memory
* returns SQLITE_ERROR if the key couldn't be set because the pass was null or size was zero
*/
-int sqlcipher_cipher_ctx_set_pass(cipher_ctx *ctx, const void *zKey, int nKey) {
+static int sqlcipher_cipher_ctx_set_pass(cipher_ctx *ctx, const void *zKey, int nKey) {
sqlcipher_free(ctx->pass, ctx->pass_sz);
ctx->pass_sz = nKey;
if(zKey && nKey) {
@@ -366,11 +395,12 @@ int sqlcipher_codec_ctx_set_cipher(codec_ctx *ctx, const char *cipher_name, int
cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx;
int rc;
- c_ctx->evp_cipher = (EVP_CIPHER *) EVP_get_cipherbyname(cipher_name);
- c_ctx->key_sz = EVP_CIPHER_key_length(c_ctx->evp_cipher);
- c_ctx->iv_sz = EVP_CIPHER_iv_length(c_ctx->evp_cipher);
- c_ctx->block_sz = EVP_CIPHER_block_size(c_ctx->evp_cipher);
- c_ctx->hmac_sz = EVP_MD_size(EVP_sha1());
+ c_ctx->provider->set_cipher(c_ctx->provider_ctx, cipher_name);
+
+ c_ctx->key_sz = c_ctx->provider->get_key_sz(c_ctx->provider_ctx);
+ c_ctx->iv_sz = c_ctx->provider->get_iv_sz(c_ctx->provider_ctx);
+ c_ctx->block_sz = c_ctx->provider->get_block_sz(c_ctx->provider_ctx);
+ c_ctx->hmac_sz = c_ctx->provider->get_hmac_sz(c_ctx->provider_ctx);
c_ctx->derive_key = 1;
if(for_ctx == 2)
@@ -382,8 +412,7 @@ int sqlcipher_codec_ctx_set_cipher(codec_ctx *ctx, const char *cipher_name, int
const char* sqlcipher_codec_ctx_get_cipher(codec_ctx *ctx, int for_ctx) {
cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx;
- EVP_CIPHER *evp_cipher = c_ctx->evp_cipher;
- return EVP_CIPHER_name(evp_cipher);
+ return c_ctx->provider->get_cipher(c_ctx->provider_ctx);
}
int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx *ctx, int kdf_iter, int for_ctx) {
@@ -444,7 +473,7 @@ unsigned char sqlcipher_get_hmac_salt_mask() {
/* set the codec flag for whether this individual database should be using hmac */
int sqlcipher_codec_ctx_set_use_hmac(codec_ctx *ctx, int use) {
- int reserve = EVP_MAX_IV_LENGTH; /* base reserve size will be IV only */
+ int reserve = CIPHER_MAX_IV_SZ; /* base reserve size will be IV only */
if(use) reserve += ctx->read_ctx->hmac_sz; /* if reserve will include hmac, update that size */
@@ -568,7 +597,7 @@ int sqlcipher_codec_ctx_init(codec_ctx **iCtx, Db *pDb, Pager *pPager, sqlite3_f
if(fd == NULL || sqlite3OsRead(fd, ctx->kdf_salt, FILE_HEADER_SZ, 0) != SQLITE_OK) {
/* if unable to read the bytes, generate random salt */
- if(sqlcipher_random(ctx->kdf_salt, FILE_HEADER_SZ) != 1) return SQLITE_ERROR;
+ if(ctx->read_ctx->provider->random(ctx->read_ctx->provider_ctx, ctx->kdf_salt, FILE_HEADER_SZ) != SQLITE_OK) return SQLITE_ERROR;
}
if((rc = sqlcipher_codec_ctx_set_cipher(ctx, CIPHER, 0)) != SQLITE_OK) return rc;
@@ -608,7 +637,7 @@ static void sqlcipher_put4byte_le(unsigned char *p, u32 v) {
p[3] = (u8)(v>>24);
}
-int sqlcipher_page_hmac(cipher_ctx *ctx, Pgno pgno, unsigned char *in, int in_sz, unsigned char *out) {
+static int sqlcipher_page_hmac(cipher_ctx *ctx, Pgno pgno, unsigned char *in, int in_sz, unsigned char *out) {
unsigned char pgno_raw[sizeof(pgno)];
/* we may convert page number to consistent representation before calculating MAC for
compatibility across big-endian and little-endian platforms.
@@ -626,16 +655,14 @@ int sqlcipher_page_hmac(cipher_ctx *ctx, Pgno pgno, unsigned char *in, int in_sz
memcpy(pgno_raw, &pgno, sizeof(pgno));
}
- HMAC_CTX_init(&ctx->hctx);
- HMAC_Init_ex(&ctx->hctx, ctx->hmac_key, ctx->key_sz, EVP_sha1(), NULL);
-
/* include the encrypted page data, initialization vector, and page number in HMAC. This will
prevent both tampering with the ciphertext, manipulation of the IV, or resequencing otherwise
valid pages out of order in a database */
- HMAC_Update(&ctx->hctx, in, in_sz);
- HMAC_Update(&ctx->hctx, (const unsigned char*) pgno_raw, sizeof(pgno));
- HMAC_Final(&ctx->hctx, out, NULL);
- HMAC_CTX_cleanup(&ctx->hctx);
+ ctx->provider->hmac(
+ ctx->provider_ctx, ctx->hmac_key,
+ ctx->key_sz, in,
+ in_sz, (unsigned char*) &pgno_raw,
+ sizeof(pgno), out);
return SQLITE_OK;
}
@@ -650,7 +677,7 @@ int sqlcipher_page_hmac(cipher_ctx *ctx, Pgno pgno, unsigned char *in, int in_sz
int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mode, int page_sz, unsigned char *in, unsigned char *out) {
cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx;
unsigned char *iv_in, *iv_out, *hmac_in, *hmac_out, *out_start;
- int tmp_csz, csz, size;
+ int size;
/* calculate some required positions into various buffers */
size = page_sz - c_ctx->reserve_sz; /* adjust size to useable size and memset reserve at end of page */
@@ -675,7 +702,7 @@ int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mode, int
if(mode == CIPHER_ENCRYPT) {
/* start at front of the reserve block, write random data to the end */
- if(sqlcipher_random(iv_out, c_ctx->reserve_sz) != 1) return SQLITE_ERROR;
+ if(c_ctx->provider->random(c_ctx->provider_ctx, iv_out, c_ctx->reserve_sz) != SQLITE_OK) return SQLITE_ERROR;
} else { /* CIPHER_DECRYPT */
memcpy(iv_out, iv_in, c_ctx->iv_sz); /* copy the iv from the input to output buffer */
}
@@ -707,17 +734,8 @@ int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mode, int
}
}
}
-
- EVP_CipherInit(&c_ctx->ectx, c_ctx->evp_cipher, NULL, NULL, mode);
- EVP_CIPHER_CTX_set_padding(&c_ctx->ectx, 0);
- EVP_CipherInit(&c_ctx->ectx, NULL, c_ctx->key, iv_out, mode);
- EVP_CipherUpdate(&c_ctx->ectx, out, &tmp_csz, in, size);
- csz = tmp_csz;
- out += tmp_csz;
- EVP_CipherFinal(&c_ctx->ectx, out, &tmp_csz);
- csz += tmp_csz;
- EVP_CIPHER_CTX_cleanup(&c_ctx->ectx);
- assert(size == csz);
+
+ c_ctx->provider->cipher(c_ctx->provider_ctx, mode, c_ctx->key, c_ctx->key_sz, iv_out, in, size, out);
if((c_ctx->flags & CIPHER_FLAG_HMAC) && (mode == CIPHER_ENCRYPT)) {
sqlcipher_page_hmac(c_ctx, pgno, out_start, size + c_ctx->iv_sz, hmac_out);
@@ -739,7 +757,7 @@ int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mode, int
* returns SQLITE_OK if initialization was successful
* returns SQLITE_ERROR if the key could't be derived (for instance if pass is NULL or pass_sz is 0)
*/
-int sqlcipher_cipher_ctx_key_derive(codec_ctx *ctx, cipher_ctx *c_ctx) {
+static int sqlcipher_cipher_ctx_key_derive(codec_ctx *ctx, cipher_ctx *c_ctx) {
CODEC_TRACE(("codec_key_derive: entered c_ctx->pass=%s, c_ctx->pass_sz=%d \
ctx->kdf_salt=%p ctx->kdf_salt_sz=%d c_ctx->kdf_iter=%d \
ctx->hmac_kdf_salt=%p, c_ctx->fast_kdf_iter=%d c_ctx->key_sz=%d\n",
@@ -755,9 +773,9 @@ int sqlcipher_cipher_ctx_key_derive(codec_ctx *ctx, cipher_ctx *c_ctx) {
cipher_hex2bin(z, n, c_ctx->key);
} else {
CODEC_TRACE(("codec_key_derive: deriving key using full PBKDF2 with %d iterations\n", c_ctx->kdf_iter));
- PKCS5_PBKDF2_HMAC_SHA1( c_ctx->pass, c_ctx->pass_sz,
- ctx->kdf_salt, ctx->kdf_salt_sz,
- c_ctx->kdf_iter, c_ctx->key_sz, c_ctx->key);
+ c_ctx->provider->kdf(c_ctx->provider_ctx, (const char*) c_ctx->pass, c_ctx->pass_sz,
+ ctx->kdf_salt, ctx->kdf_salt_sz, c_ctx->kdf_iter,
+ c_ctx->key_sz, c_ctx->key);
}
@@ -779,9 +797,11 @@ int sqlcipher_cipher_ctx_key_derive(codec_ctx *ctx, cipher_ctx *c_ctx) {
CODEC_TRACE(("codec_key_derive: deriving hmac key from encryption key using PBKDF2 with %d iterations\n",
c_ctx->fast_kdf_iter));
- PKCS5_PBKDF2_HMAC_SHA1( (const char*)c_ctx->key, c_ctx->key_sz,
- ctx->hmac_kdf_salt, ctx->kdf_salt_sz,
- c_ctx->fast_kdf_iter, c_ctx->key_sz, c_ctx->hmac_key);
+
+
+ c_ctx->provider->kdf(c_ctx->provider_ctx, (const char*)c_ctx->key, c_ctx->key_sz,
+ ctx->hmac_kdf_salt, ctx->kdf_salt_sz, c_ctx->fast_kdf_iter,
+ c_ctx->key_sz, c_ctx->hmac_key);
}
c_ctx->derive_key = 0;
@@ -815,202 +835,9 @@ int sqlcipher_codec_key_copy(codec_ctx *ctx, int source) {
}
}
-
-#ifndef OMIT_EXPORT
-
-/*
- * Implementation of an "export" function that allows a caller
- * to duplicate the main database to an attached database. This is intended
- * as a conveneince for users who need to:
- *
- * 1. migrate from an non-encrypted database to an encrypted database
- * 2. move from an encrypted database to a non-encrypted database
- * 3. convert beween the various flavors of encrypted databases.
- *
- * This implementation is based heavily on the procedure and code used
- * in vacuum.c, but is exposed as a function that allows export to any
- * named attached database.
- */
-
-/*
-** Finalize a prepared statement. If there was an error, store the
-** text of the error message in *pzErrMsg. Return the result code.
-**
-** Based on vacuumFinalize from vacuum.c
-*/
-static int sqlcipher_finalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){
- int rc;
- rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
- if( rc ){
- sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
- }
- return rc;
-}
-
-/*
-** Execute zSql on database db. Return an error code.
-**
-** Based on execSql from vacuum.c
-*/
-static int sqlcipher_execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
- sqlite3_stmt *pStmt;
- VVA_ONLY( int rc; )
- if( !zSql ){
- return SQLITE_NOMEM;
- }
- if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
- sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
- return sqlite3_errcode(db);
- }
- VVA_ONLY( rc = ) sqlite3_step(pStmt);
- assert( rc!=SQLITE_ROW );
- return sqlcipher_finalize(db, pStmt, pzErrMsg);
+const char* sqlcipher_codec_get_cipher_provider(codec_ctx *ctx) {
+ return ctx->read_ctx->provider->get_provider_name(ctx->read_ctx);
}
-/*
-** Execute zSql on database db. The statement returns exactly
-** one column. Execute this as SQL on the same database.
-**
-** Based on execExecSql from vacuum.c
-*/
-static int sqlcipher_execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
- sqlite3_stmt *pStmt;
- int rc;
-
- rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
- if( rc!=SQLITE_OK ) return rc;
-
- while( SQLITE_ROW==sqlite3_step(pStmt) ){
- rc = sqlcipher_execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0));
- if( rc!=SQLITE_OK ){
- sqlcipher_finalize(db, pStmt, pzErrMsg);
- return rc;
- }
- }
-
- return sqlcipher_finalize(db, pStmt, pzErrMsg);
-}
-
-/*
- * copy database and schema from the main database to an attached database
- *
- * Based on sqlite3RunVacuum from vacuum.c
-*/
-void sqlcipher_exportFunc(sqlite3_context *context, int argc, sqlite3_value **argv) {
- sqlite3 *db = sqlite3_context_db_handle(context);
- const char* attachedDb = (const char*) sqlite3_value_text(argv[0]);
- int saved_flags; /* Saved value of the db->flags */
- int saved_nChange; /* Saved value of db->nChange */
- int saved_nTotalChange; /* Saved value of db->nTotalChange */
- void (*saved_xTrace)(void*,const char*); /* Saved db->xTrace */
- int rc = SQLITE_OK; /* Return code from service routines */
- char *zSql = NULL; /* SQL statements */
- char *pzErrMsg = NULL;
-
- saved_flags = db->flags;
- saved_nChange = db->nChange;
- saved_nTotalChange = db->nTotalChange;
- saved_xTrace = db->xTrace;
- db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin;
- db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder);
- db->xTrace = 0;
-
- /* Query the schema of the main database. Create a mirror schema
- ** in the temporary database.
- */
- zSql = sqlite3_mprintf(
- "SELECT 'CREATE TABLE %s.' || substr(sql,14) "
- " FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
- " AND rootpage>0"
- , attachedDb);
- rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql);
- if( rc!=SQLITE_OK ) goto end_of_export;
- sqlite3_free(zSql);
-
- zSql = sqlite3_mprintf(
- "SELECT 'CREATE INDEX %s.' || substr(sql,14)"
- " FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %%' "
- , attachedDb);
- rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql);
- if( rc!=SQLITE_OK ) goto end_of_export;
- sqlite3_free(zSql);
-
- zSql = sqlite3_mprintf(
- "SELECT 'CREATE UNIQUE INDEX %s.' || substr(sql,21) "
- " FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %%'"
- , attachedDb);
- rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql);
- if( rc!=SQLITE_OK ) goto end_of_export;
- sqlite3_free(zSql);
-
- /* Loop through the tables in the main database. For each, do
- ** an "INSERT INTO rekey_db.xxx SELECT * FROM main.xxx;" to copy
- ** the contents to the temporary database.
- */
- zSql = sqlite3_mprintf(
- "SELECT 'INSERT INTO %s.' || quote(name) "
- "|| ' SELECT * FROM main.' || quote(name) || ';'"
- "FROM main.sqlite_master "
- "WHERE type = 'table' AND name!='sqlite_sequence' "
- " AND rootpage>0"
- , attachedDb);
- rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql);
- if( rc!=SQLITE_OK ) goto end_of_export;
- sqlite3_free(zSql);
-
- /* Copy over the sequence table
- */
- zSql = sqlite3_mprintf(
- "SELECT 'DELETE FROM %s.' || quote(name) || ';' "
- "FROM %s.sqlite_master WHERE name='sqlite_sequence' "
- , attachedDb, attachedDb);
- rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql);
- if( rc!=SQLITE_OK ) goto end_of_export;
- sqlite3_free(zSql);
-
- zSql = sqlite3_mprintf(
- "SELECT 'INSERT INTO %s.' || quote(name) "
- "|| ' SELECT * FROM main.' || quote(name) || ';' "
- "FROM %s.sqlite_master WHERE name=='sqlite_sequence';"
- , attachedDb, attachedDb);
- rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql);
- if( rc!=SQLITE_OK ) goto end_of_export;
- sqlite3_free(zSql);
-
- /* Copy the triggers, views, and virtual tables from the main database
- ** over to the temporary database. None of these objects has any
- ** associated storage, so all we have to do is copy their entries
- ** from the SQLITE_MASTER table.
- */
- zSql = sqlite3_mprintf(
- "INSERT INTO %s.sqlite_master "
- " SELECT type, name, tbl_name, rootpage, sql"
- " FROM main.sqlite_master"
- " WHERE type='view' OR type='trigger'"
- " OR (type='table' AND rootpage=0)"
- , attachedDb);
- rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execSql(db, &pzErrMsg, zSql);
- if( rc!=SQLITE_OK ) goto end_of_export;
- sqlite3_free(zSql);
-
- zSql = NULL;
-end_of_export:
- db->flags = saved_flags;
- db->nChange = saved_nChange;
- db->nTotalChange = saved_nTotalChange;
- db->xTrace = saved_xTrace;
-
- sqlite3_free(zSql);
-
- if(rc) {
- if(pzErrMsg != NULL) {
- sqlite3_result_error(context, pzErrMsg, -1);
- sqlite3DbFree(db, pzErrMsg);
- } else {
- sqlite3_result_error(context, sqlite3ErrStr(rc), -1);
- }
- }
-}
-
-#endif
#endif
+/* END SQLCIPHER */
diff --git a/src/crypto_libtomcrypt.c b/src/crypto_libtomcrypt.c
new file mode 100644
index 0000000..0299771
--- /dev/null
+++ b/src/crypto_libtomcrypt.c
@@ -0,0 +1,215 @@
+/*
+** SQLCipher
+** http://sqlcipher.net
+**
+** Copyright (c) 2008 - 2013, ZETETIC LLC
+** All rights reserved.
+**
+** Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in the
+** documentation and/or other materials provided with the distribution.
+** * Neither the name of the ZETETIC LLC nor the
+** names of its contributors may be used to endorse or promote products
+** derived from this software without specific prior written permission.
+**
+** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY
+** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY
+** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+**
+*/
+/* BEGIN SQLCIPHER */
+#ifdef SQLITE_HAS_CODEC
+#ifdef SQLCIPHER_CRYPTO_LIBTOMCRYPT
+#include "sqliteInt.h"
+#include "sqlcipher.h"
+#include <tomcrypt.h>
+
+typedef struct {
+ prng_state prng;
+} ltc_ctx;
+
+static unsigned int ltc_init = 0;
+
+static int sqlcipher_ltc_add_random(void *ctx, void *buffer, int length) {
+ ltc_ctx *ltc = (ltc_ctx*)ctx;
+ int rc = fortuna_add_entropy(buffer, length, &(ltc->prng));
+ return rc != CRYPT_OK ? SQLITE_ERROR : SQLITE_OK;
+}
+
+static int sqlcipher_ltc_activate(void *ctx) {
+ ltc_ctx *ltc = (ltc_ctx*)ctx;
+ int random_buffer_sz = 32;
+ unsigned char random_buffer[random_buffer_sz];
+
+ if(ltc_init == 0) {
+ if(register_prng(&fortuna_desc) != CRYPT_OK) return SQLITE_ERROR;
+ if(register_cipher(&rijndael_desc) != CRYPT_OK) return SQLITE_ERROR;
+ if(register_hash(&sha1_desc) != CRYPT_OK) return SQLITE_ERROR;
+ ltc_init = 1;
+ }
+ if(fortuna_start(&(ltc->prng)) != CRYPT_OK) {
+ return SQLITE_ERROR;
+ }
+ sqlite3_randomness(random_buffer_sz, &random_buffer);
+ if(sqlcipher_ltc_add_random(ctx, random_buffer, random_buffer_sz) != SQLITE_OK) {
+ return SQLITE_ERROR;
+ }
+ if(sqlcipher_ltc_add_random(ctx, &ltc, sizeof(ltc_ctx*)) != SQLITE_OK) {
+ return SQLITE_ERROR;
+ }
+ if(fortuna_ready(&(ltc->prng)) != CRYPT_OK) {
+ return SQLITE_ERROR;
+ }
+ return SQLITE_OK;
+}
+
+static int sqlcipher_ltc_deactivate(void *ctx) {
+ ltc_ctx *ltc = (ltc_ctx*)ctx;
+ fortuna_done(&(ltc->prng));
+}
+
+static const char* sqlcipher_ltc_get_provider_name(void *ctx) {
+ return "libtomcrypt";
+}
+
+static int sqlcipher_ltc_random(void *ctx, void *buffer, int length) {
+ ltc_ctx *ltc = (ltc_ctx*)ctx;
+ int rc;
+
+ if((rc = fortuna_ready(&(ltc->prng))) != CRYPT_OK) {
+ return SQLITE_ERROR;
+ }
+ fortuna_read(buffer, length, &(ltc->prng));
+ return SQLITE_OK;
+}
+
+static int sqlcipher_ltc_hmac(void *ctx, unsigned char *hmac_key, int key_sz, unsigned char *in, int in_sz, unsigned char *in2, int in2_sz, unsigned char *out) {
+ int rc, hash_idx;
+ hmac_state hmac;
+ unsigned long outlen = key_sz;
+
+ hash_idx = find_hash("sha1");
+ if((rc = hmac_init(&hmac, hash_idx, hmac_key, key_sz)) != CRYPT_OK) return SQLITE_ERROR;
+ if((rc = hmac_process(&hmac, in, in_sz)) != CRYPT_OK) return SQLITE_ERROR;
+ if((rc = hmac_process(&hmac, in2, in2_sz)) != CRYPT_OK) return SQLITE_ERROR;
+ if((rc = hmac_done(&hmac, out, &outlen)) != CRYPT_OK) return SQLITE_ERROR;
+ return SQLITE_OK;
+}
+
+static int sqlcipher_ltc_kdf(void *ctx, const char *pass, int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key) {
+ int rc, hash_idx;
+ unsigned long outlen = key_sz;
+ unsigned long random_buffer_sz = 256;
+ char random_buffer[random_buffer_sz];
+ ltc_ctx *ltc = (ltc_ctx*)ctx;
+
+ hash_idx = find_hash("sha1");
+ if((rc = pkcs_5_alg2(pass, pass_sz, salt, salt_sz,
+ workfactor, hash_idx, key, &outlen)) != CRYPT_OK) {
+ return SQLITE_ERROR;
+ }
+ if((rc = pkcs_5_alg2(key, key_sz, salt, salt_sz,
+ 1, hash_idx, random_buffer, &random_buffer_sz)) != CRYPT_OK) {
+ return SQLITE_ERROR;
+ }
+ sqlcipher_ltc_add_random(ctx, random_buffer, random_buffer_sz);
+ return SQLITE_OK;
+}
+
+static const char* sqlcipher_ltc_get_cipher(void *ctx) {
+ return "rijndael";
+}
+
+static int sqlcipher_ltc_cipher(void *ctx, int mode, unsigned char *key, int key_sz, unsigned char *iv, unsigned char *in, int in_sz, unsigned char *out) {
+ int rc, cipher_idx, hash_idx;
+ symmetric_CBC cbc;
+
+ if((cipher_idx = find_cipher(sqlcipher_ltc_get_cipher(ctx))) == -1) return SQLITE_ERROR;
+ if((rc = cbc_start(cipher_idx, iv, key, key_sz, 0, &cbc)) != CRYPT_OK) return SQLITE_ERROR;
+ rc = mode == 1 ? cbc_encrypt(in, out, in_sz, &cbc) : cbc_decrypt(in, out, in_sz, &cbc);
+ if(rc != CRYPT_OK) return SQLITE_ERROR;
+ cbc_done(&cbc);
+ return SQLITE_OK;
+}
+
+static int sqlcipher_ltc_set_cipher(void *ctx, const char *cipher_name) {
+ return SQLITE_OK;
+}
+
+static int sqlcipher_ltc_get_key_sz(void *ctx) {
+ int cipher_idx = find_cipher(sqlcipher_ltc_get_cipher(ctx));
+ return cipher_descriptor[cipher_idx].max_key_length;
+}
+
+static int sqlcipher_ltc_get_iv_sz(void *ctx) {
+ int cipher_idx = find_cipher(sqlcipher_ltc_get_cipher(ctx));
+ return cipher_descriptor[cipher_idx].block_length;
+}
+
+static int sqlcipher_ltc_get_block_sz(void *ctx) {
+ int cipher_idx = find_cipher(sqlcipher_ltc_get_cipher(ctx));
+ return cipher_descriptor[cipher_idx].block_length;
+}
+
+static int sqlcipher_ltc_get_hmac_sz(void *ctx) {
+ int hash_idx = find_hash("sha1");
+ return hash_descriptor[hash_idx].hashsize;
+}
+
+static int sqlcipher_ltc_ctx_copy(void *target_ctx, void *source_ctx) {
+ memcpy(target_ctx, source_ctx, sizeof(ltc_ctx));
+ return SQLITE_OK;
+}
+
+static int sqlcipher_ltc_ctx_cmp(void *c1, void *c2) {
+ return 1;
+}
+
+static int sqlcipher_ltc_ctx_init(void **ctx) {
+ *ctx = sqlcipher_malloc(sizeof(ltc_ctx));
+ if(*ctx == NULL) return SQLITE_NOMEM;
+ sqlcipher_ltc_activate(*ctx);
+ return SQLITE_OK;
+}
+
+static int sqlcipher_ltc_ctx_free(void **ctx) {
+ sqlcipher_ltc_deactivate(&ctx);
+ sqlcipher_free(*ctx, sizeof(ltc_ctx));
+ return SQLITE_OK;
+}
+
+int sqlcipher_ltc_setup(sqlcipher_provider *p) {
+ p->activate = sqlcipher_ltc_activate;
+ p->deactivate = sqlcipher_ltc_deactivate;
+ p->get_provider_name = sqlcipher_ltc_get_provider_name;
+ p->random = sqlcipher_ltc_random;
+ p->hmac = sqlcipher_ltc_hmac;
+ p->kdf = sqlcipher_ltc_kdf;
+ p->cipher = sqlcipher_ltc_cipher;
+ p->set_cipher = sqlcipher_ltc_set_cipher;
+ p->get_cipher = sqlcipher_ltc_get_cipher;
+ p->get_key_sz = sqlcipher_ltc_get_key_sz;
+ p->get_iv_sz = sqlcipher_ltc_get_iv_sz;
+ p->get_block_sz = sqlcipher_ltc_get_block_sz;
+ p->get_hmac_sz = sqlcipher_ltc_get_hmac_sz;
+ p->ctx_copy = sqlcipher_ltc_ctx_copy;
+ p->ctx_cmp = sqlcipher_ltc_ctx_cmp;
+ p->ctx_init = sqlcipher_ltc_ctx_init;
+ p->ctx_free = sqlcipher_ltc_ctx_free;
+ p->add_random = sqlcipher_ltc_add_random;
+}
+
+#endif
+#endif
+/* END SQLCIPHER */
diff --git a/src/crypto_openssl.c b/src/crypto_openssl.c
new file mode 100644
index 0000000..6035b7c
--- /dev/null
+++ b/src/crypto_openssl.c
@@ -0,0 +1,251 @@
+/*
+** SQLCipher
+** http://sqlcipher.net
+**
+** Copyright (c) 2008 - 2013, ZETETIC LLC
+** All rights reserved.
+**
+** Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in the
+** documentation and/or other materials provided with the distribution.
+** * Neither the name of the ZETETIC LLC nor the
+** names of its contributors may be used to endorse or promote products
+** derived from this software without specific prior written permission.
+**
+** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY
+** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY
+** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+**
+*/
+/* BEGIN SQLCIPHER */
+#ifdef SQLITE_HAS_CODEC
+#ifdef SQLCIPHER_CRYPTO_OPENSSL
+#include "sqliteInt.h"
+#include "crypto.h"
+#include "sqlcipher.h"
+#include <openssl/rand.h>
+#include <openssl/evp.h>
+#include <openssl/hmac.h>
+
+typedef struct {
+ EVP_CIPHER *evp_cipher;
+} openssl_ctx;
+
+
+static unsigned int openssl_external_init = 0;
+static unsigned int openssl_init_count = 0;
+static sqlite3_mutex* openssl_rand_mutex = NULL;
+
+static int sqlcipher_openssl_add_random(void *ctx, void *buffer, int length) {
+#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
+ sqlite3_mutex_enter(openssl_rand_mutex);
+#endif
+ RAND_add(buffer, length, 0);
+#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
+ sqlite3_mutex_leave(openssl_rand_mutex);
+#endif
+ return SQLITE_OK;
+}
+
+/* activate and initialize sqlcipher. Most importantly, this will automatically
+ intialize OpenSSL's EVP system if it hasn't already be externally. Note that
+ this function may be called multiple times as new codecs are intiialized.
+ Thus it performs some basic counting to ensure that only the last and final
+ sqlcipher_openssl_deactivate() will free the EVP structures.
+*/
+static int sqlcipher_openssl_activate(void *ctx) {
+ /* initialize openssl and increment the internal init counter
+ but only if it hasn't been initalized outside of SQLCipher by this program
+ e.g. on startup */
+ sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
+
+ if(openssl_init_count == 0 && EVP_get_cipherbyname(CIPHER) != NULL) {
+ /* if openssl has not yet been initialized by this library, but
+ a call to get_cipherbyname works, then the openssl library
+ has been initialized externally already. */
+ openssl_external_init = 1;
+ }
+
+ if(openssl_init_count == 0 && openssl_external_init == 0) {
+ /* if the library was not externally initialized, then should be now */
+ OpenSSL_add_all_algorithms();
+ }
+
+#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
+ if(openssl_rand_mutex == NULL) {
+ /* allocate a mutex to guard against concurrent calls to RAND_bytes() */
+ openssl_rand_mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
+ }
+#endif
+
+ openssl_init_count++;
+ sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
+ return SQLITE_OK;
+}
+
+/* deactivate SQLCipher, most imporantly decremeting the activation count and
+ freeing the EVP structures on the final deactivation to ensure that
+ OpenSSL memory is cleaned up */
+static int sqlcipher_openssl_deactivate(void *ctx) {
+ sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
+ openssl_init_count--;
+
+ if(openssl_init_count == 0) {
+ if(openssl_external_init == 0) {
+ /* if OpenSSL hasn't be initialized externally, and the counter reaches zero
+ after it's decremented, release EVP memory
+ Note: this code will only be reached if OpensSSL_add_all_algorithms()
+ is called by SQLCipher internally. This should prevent SQLCipher from
+ "cleaning up" openssl when it was initialized externally by the program */
+ EVP_cleanup();
+ }
+#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
+ sqlite3_mutex_free(openssl_rand_mutex);
+ openssl_rand_mutex = NULL;
+#endif
+ }
+ sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
+ return SQLITE_OK;
+}
+
+static const char* sqlcipher_openssl_get_provider_name(void *ctx) {
+ return "openssl";
+}
+
+/* generate a defined number of random bytes */
+static int sqlcipher_openssl_random (void *ctx, void *buffer, int length) {
+ int rc = 0;
+ /* concurrent calls to RAND_bytes can cause a crash under some openssl versions when a
+ naive application doesn't use CRYPTO_set_locking_callback and
+ CRYPTO_THREADID_set_callback to ensure openssl thread safety.
+ This is simple workaround to prevent this common crash
+ but a more proper solution is that applications setup platform-appropriate
+ thread saftey in openssl externally */
+#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
+ sqlite3_mutex_enter(openssl_rand_mutex);
+#endif
+ rc = RAND_bytes((unsigned char *)buffer, length);
+#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
+ sqlite3_mutex_leave(openssl_rand_mutex);
+#endif
+ return (rc == 1) ? SQLITE_OK : SQLITE_ERROR;
+}
+
+static int sqlcipher_openssl_hmac(void *ctx, unsigned char *hmac_key, int key_sz, unsigned char *in, int in_sz, unsigned char *in2, int in2_sz, unsigned char *out) {
+ HMAC_CTX hctx;
+ unsigned int outlen;
+ HMAC_CTX_init(&hctx);
+ HMAC_Init_ex(&hctx, hmac_key, key_sz, EVP_sha1(), NULL);
+ HMAC_Update(&hctx, in, in_sz);
+ HMAC_Update(&hctx, in2, in2_sz);
+ HMAC_Final(&hctx, out, &outlen);
+ HMAC_CTX_cleanup(&hctx);
+ return SQLITE_OK;
+}
+
+static int sqlcipher_openssl_kdf(void *ctx, const char *pass, int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key) {
+ PKCS5_PBKDF2_HMAC_SHA1(pass, pass_sz, salt, salt_sz, workfactor, key_sz, key);
+ return SQLITE_OK;
+}
+
+static int sqlcipher_openssl_cipher(void *ctx, int mode, unsigned char *key, int key_sz, unsigned char *iv, unsigned char *in, int in_sz, unsigned char *out) {
+ EVP_CIPHER_CTX ectx;
+ int tmp_csz, csz;
+
+ EVP_CipherInit(&ectx, ((openssl_ctx *)ctx)->evp_cipher, NULL, NULL, mode);
+ EVP_CIPHER_CTX_set_padding(&ectx, 0); // no padding
+ EVP_CipherInit(&ectx, NULL, key, iv, mode);
+ EVP_CipherUpdate(&ectx, out, &tmp_csz, in, in_sz);
+ csz = tmp_csz;
+ out += tmp_csz;
+ EVP_CipherFinal(&ectx, out, &tmp_csz);
+ csz += tmp_csz;
+ EVP_CIPHER_CTX_cleanup(&ectx);
+ assert(in_sz == csz);
+ return SQLITE_OK;
+}
+
+static int sqlcipher_openssl_set_cipher(void *ctx, const char *cipher_name) {
+ openssl_ctx *o_ctx = (openssl_ctx *)ctx;
+ o_ctx->evp_cipher = (EVP_CIPHER *) EVP_get_cipherbyname(cipher_name);
+ return SQLITE_OK;
+}
+
+static const char* sqlcipher_openssl_get_cipher(void *ctx) {
+ return EVP_CIPHER_name(((openssl_ctx *)ctx)->evp_cipher);
+}
+
+static int sqlcipher_openssl_get_key_sz(void *ctx) {
+ return EVP_CIPHER_key_length(((openssl_ctx *)ctx)->evp_cipher);
+}
+
+static int sqlcipher_openssl_get_iv_sz(void *ctx) {
+ return EVP_CIPHER_iv_length(((openssl_ctx *)ctx)->evp_cipher);
+}
+
+static int sqlcipher_openssl_get_block_sz(void *ctx) {
+ return EVP_CIPHER_block_size(((openssl_ctx *)ctx)->evp_cipher);
+}
+
+static int sqlcipher_openssl_get_hmac_sz(void *ctx) {
+ return EVP_MD_size(EVP_sha1());
+}
+
+static int sqlcipher_openssl_ctx_copy(void *target_ctx, void *source_ctx) {
+ memcpy(target_ctx, source_ctx, sizeof(openssl_ctx));
+ return SQLITE_OK;
+}
+
+static int sqlcipher_openssl_ctx_cmp(void *c1, void *c2) {
+ return ((openssl_ctx *)c1)->evp_cipher == ((openssl_ctx *)c2)->evp_cipher;
+}
+
+static int sqlcipher_openssl_ctx_init(void **ctx) {
+ *ctx = sqlcipher_malloc(sizeof(openssl_ctx));
+ if(*ctx == NULL) return SQLITE_NOMEM;
+ sqlcipher_openssl_activate(*ctx);
+ return SQLITE_OK;
+}
+
+static int sqlcipher_openssl_ctx_free(void **ctx) {
+ sqlcipher_openssl_deactivate(*ctx);
+ sqlcipher_free(*ctx, sizeof(openssl_ctx));
+ return SQLITE_OK;
+}
+
+int sqlcipher_openssl_setup(sqlcipher_provider *p) {
+ p->activate = sqlcipher_openssl_activate;
+ p->deactivate = sqlcipher_openssl_deactivate;
+ p->get_provider_name = sqlcipher_openssl_get_provider_name;
+ p->random = sqlcipher_openssl_random;
+ p->hmac = sqlcipher_openssl_hmac;
+ p->kdf = sqlcipher_openssl_kdf;
+ p->cipher = sqlcipher_openssl_cipher;
+ p->set_cipher = sqlcipher_openssl_set_cipher;
+ p->get_cipher = sqlcipher_openssl_get_cipher;
+ p->get_key_sz = sqlcipher_openssl_get_key_sz;
+ p->get_iv_sz = sqlcipher_openssl_get_iv_sz;
+ p->get_block_sz = sqlcipher_openssl_get_block_sz;
+ p->get_hmac_sz = sqlcipher_openssl_get_hmac_sz;
+ p->ctx_copy = sqlcipher_openssl_ctx_copy;
+ p->ctx_cmp = sqlcipher_openssl_ctx_cmp;
+ p->ctx_init = sqlcipher_openssl_ctx_init;
+ p->ctx_free = sqlcipher_openssl_ctx_free;
+ p->add_random = sqlcipher_openssl_add_random;
+ return SQLITE_OK;
+}
+
+#endif
+#endif
+/* END SQLCIPHER */
diff --git a/src/ctime.c b/src/ctime.c
index 61cf4e3..60595ff 100644
--- a/src/ctime.c
+++ b/src/ctime.c
@@ -48,15 +48,15 @@ static const char * const azCompileOpt[] = {
#ifdef SQLITE_COVERAGE_TEST
"COVERAGE_TEST",
#endif
-#ifdef SQLITE_CURDIR
- "CURDIR",
-#endif
#ifdef SQLITE_DEBUG
"DEBUG",
#endif
#ifdef SQLITE_DEFAULT_LOCKING_MODE
"DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
#endif
+#if defined(SQLITE_DEFAULT_MMAP_SIZE) && !defined(SQLITE_DEFAULT_MMAP_SIZE_xc)
+ "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE),
+#endif
#ifdef SQLITE_DISABLE_DIRSYNC
"DISABLE_DIRSYNC",
#endif
@@ -147,6 +147,9 @@ static const char * const azCompileOpt[] = {
#ifdef SQLITE_LOCK_TRACE
"LOCK_TRACE",
#endif
+#if defined(SQLITE_MAX_MMAP_SIZE) && !defined(SQLITE_MAX_MMAP_SIZE_xc)
+ "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE),
+#endif
#ifdef SQLITE_MAX_SCHEMA_RETRY
"MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
#endif
@@ -204,11 +207,6 @@ static const char * const azCompileOpt[] = {
#ifdef SQLITE_OMIT_CHECK
"OMIT_CHECK",
#endif
-/* // redundant
-** #ifdef SQLITE_OMIT_COMPILEOPTION_DIAGS
-** "OMIT_COMPILEOPTION_DIAGS",
-** #endif
-*/
#ifdef SQLITE_OMIT_COMPLETE
"OMIT_COMPLETE",
#endif
@@ -263,9 +261,6 @@ static const char * const azCompileOpt[] = {
#ifdef SQLITE_OMIT_MEMORYDB
"OMIT_MEMORYDB",
#endif
-#ifdef SQLITE_OMIT_MERGE_SORT
- "OMIT_MERGE_SORT",
-#endif
#ifdef SQLITE_OMIT_OR_OPTIMIZATION
"OMIT_OR_OPTIMIZATION",
#endif
@@ -338,6 +333,9 @@ static const char * const azCompileOpt[] = {
#ifdef SQLITE_PROXY_DEBUG
"PROXY_DEBUG",
#endif
+#ifdef SQLITE_RTREE_INT_ONLY
+ "RTREE_INT_ONLY",
+#endif
#ifdef SQLITE_SECURE_DELETE
"SECURE_DELETE",
#endif
@@ -350,13 +348,13 @@ static const char * const azCompileOpt[] = {
#ifdef SQLITE_TCL
"TCL",
#endif
-#ifdef SQLITE_TEMP_STORE
+#if defined(SQLITE_TEMP_STORE) && !defined(SQLITE_TEMP_STORE_xc)
"TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
#endif
#ifdef SQLITE_TEST
"TEST",
#endif
-#ifdef SQLITE_THREADSAFE
+#if defined(SQLITE_THREADSAFE)
"THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
#endif
#ifdef SQLITE_USE_ALLOCA
@@ -382,8 +380,11 @@ int sqlite3_compileoption_used(const char *zOptName){
/* Since ArraySize(azCompileOpt) is normally in single digits, a
** linear search is adequate. No need for a binary search. */
for(i=0; i<ArraySize(azCompileOpt); i++){
- if( (sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0)
- && ( (azCompileOpt[i][n]==0) || (azCompileOpt[i][n]=='=') ) ) return 1;
+ if( sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0
+ && sqlite3CtypeMap[(unsigned char)azCompileOpt[i][n]]==0
+ ){
+ return 1;
+ }
}
return 0;
}
diff --git a/src/delete.c b/src/delete.c
index 44e5995..634e115 100644
--- a/src/delete.c
+++ b/src/delete.c
@@ -32,7 +32,7 @@ Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
struct SrcList_item *pItem = pSrc->a;
Table *pTab;
assert( pItem && pSrc->nSrc==1 );
- pTab = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase);
+ pTab = sqlite3LocateTableItem(pParse, 0, pItem);
sqlite3DeleteTable(pParse->db, pItem->pTab);
pItem->pTab = pTab;
if( pTab ){
@@ -93,29 +93,28 @@ void sqlite3MaterializeView(
int iCur /* Cursor number for ephemerial table */
){
SelectDest dest;
- Select *pDup;
+ Select *pSel;
+ SrcList *pFrom;
sqlite3 *db = pParse->db;
+ int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
- pDup = sqlite3SelectDup(db, pView->pSelect, 0);
- if( pWhere ){
- SrcList *pFrom;
-
- pWhere = sqlite3ExprDup(db, pWhere, 0);
- pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
- if( pFrom ){
- assert( pFrom->nSrc==1 );
- pFrom->a[0].zAlias = sqlite3DbStrDup(db, pView->zName);
- pFrom->a[0].pSelect = pDup;
- assert( pFrom->a[0].pOn==0 );
- assert( pFrom->a[0].pUsing==0 );
- }else{
- sqlite3SelectDelete(db, pDup);
- }
- pDup = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0);
+ pWhere = sqlite3ExprDup(db, pWhere, 0);
+ pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
+
+ if( pFrom ){
+ assert( pFrom->nSrc==1 );
+ pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
+ pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
+ assert( pFrom->a[0].pOn==0 );
+ assert( pFrom->a[0].pUsing==0 );
}
+
+ pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0);
+ if( pSel ) pSel->selFlags |= SF_Materialize;
+
sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
- sqlite3Select(pParse, pDup, &dest);
- sqlite3SelectDelete(db, pDup);
+ sqlite3Select(pParse, pSel, &dest);
+ sqlite3SelectDelete(db, pSel);
}
#endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
@@ -638,7 +637,9 @@ int sqlite3GenerateIndexKey(
}
if( doMakeRec ){
const char *zAff;
- if( pTab->pSelect || (pParse->db->flags & SQLITE_IdxRealAsInt)!=0 ){
+ if( pTab->pSelect
+ || OptimizationDisabled(pParse->db, SQLITE_IdxRealAsInt)
+ ){
zAff = 0;
}else{
zAff = sqlite3IndexAffinityStr(v, pIdx);
diff --git a/src/expr.c b/src/expr.c
index 89172f9..660397e 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -31,7 +31,9 @@
** SELECT * FROM t1 WHERE (select a from t1);
*/
char sqlite3ExprAffinity(Expr *pExpr){
- int op = pExpr->op;
+ int op;
+ pExpr = sqlite3ExprSkipCollate(pExpr);
+ op = pExpr->op;
if( op==TK_SELECT ){
assert( pExpr->flags&EP_xIsSelect );
return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
@@ -56,66 +58,89 @@ char sqlite3ExprAffinity(Expr *pExpr){
}
/*
-** Set the explicit collating sequence for an expression to the
-** collating sequence supplied in the second argument.
+** Set the collating sequence for expression pExpr to be the collating
+** sequence named by pToken. Return a pointer to a new Expr node that
+** implements the COLLATE operator.
+**
+** If a memory allocation error occurs, that fact is recorded in pParse->db
+** and the pExpr parameter is returned unchanged.
*/
-Expr *sqlite3ExprSetColl(Expr *pExpr, CollSeq *pColl){
- if( pExpr && pColl ){
- pExpr->pColl = pColl;
- pExpr->flags |= EP_ExpCollate;
+Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr *pExpr, Token *pCollName){
+ if( pCollName->n>0 ){
+ Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, 1);
+ if( pNew ){
+ pNew->pLeft = pExpr;
+ pNew->flags |= EP_Collate;
+ pExpr = pNew;
+ }
}
return pExpr;
}
+Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
+ Token s;
+ assert( zC!=0 );
+ s.z = zC;
+ s.n = sqlite3Strlen30(s.z);
+ return sqlite3ExprAddCollateToken(pParse, pExpr, &s);
+}
/*
-** Set the collating sequence for expression pExpr to be the collating
-** sequence named by pToken. Return a pointer to the revised expression.
-** The collating sequence is marked as "explicit" using the EP_ExpCollate
-** flag. An explicit collating sequence will override implicit
-** collating sequences.
+** Skip over any TK_COLLATE and/or TK_AS operators at the root of
+** an expression.
*/
-Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr *pExpr, Token *pCollName){
- char *zColl = 0; /* Dequoted name of collation sequence */
- CollSeq *pColl;
- sqlite3 *db = pParse->db;
- zColl = sqlite3NameFromToken(db, pCollName);
- pColl = sqlite3LocateCollSeq(pParse, zColl);
- sqlite3ExprSetColl(pExpr, pColl);
- sqlite3DbFree(db, zColl);
+Expr *sqlite3ExprSkipCollate(Expr *pExpr){
+ while( pExpr && (pExpr->op==TK_COLLATE || pExpr->op==TK_AS) ){
+ pExpr = pExpr->pLeft;
+ }
return pExpr;
}
/*
-** Return the default collation sequence for the expression pExpr. If
-** there is no default collation type, return 0.
+** Return the collation sequence for the expression pExpr. If
+** there is no defined collating sequence, return NULL.
+**
+** The collating sequence might be determined by a COLLATE operator
+** or by the presence of a column with a defined collating sequence.
+** COLLATE operators take first precedence. Left operands take
+** precedence over right operands.
*/
CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
+ sqlite3 *db = pParse->db;
CollSeq *pColl = 0;
Expr *p = pExpr;
while( p ){
- int op;
- pColl = p->pColl;
- if( pColl ) break;
- op = p->op;
- if( p->pTab!=0 && (
- op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER || op==TK_TRIGGER
- )){
+ int op = p->op;
+ if( op==TK_CAST || op==TK_UPLUS ){
+ p = p->pLeft;
+ continue;
+ }
+ assert( op!=TK_REGISTER || p->op2!=TK_COLLATE );
+ if( op==TK_COLLATE ){
+ pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
+ break;
+ }
+ if( p->pTab!=0
+ && (op==TK_AGG_COLUMN || op==TK_COLUMN
+ || op==TK_REGISTER || op==TK_TRIGGER)
+ ){
/* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
** a TK_COLUMN but was previously evaluated and cached in a register */
- const char *zColl;
int j = p->iColumn;
if( j>=0 ){
- sqlite3 *db = pParse->db;
- zColl = p->pTab->aCol[j].zColl;
+ const char *zColl = p->pTab->aCol[j].zColl;
pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
- pExpr->pColl = pColl;
}
break;
}
- if( op!=TK_CAST && op!=TK_UPLUS ){
+ if( p->flags & EP_Collate ){
+ if( ALWAYS(p->pLeft) && (p->pLeft->flags & EP_Collate)!=0 ){
+ p = p->pLeft;
+ }else{
+ p = p->pRight;
+ }
+ }else{
break;
}
- p = p->pLeft;
}
if( sqlite3CheckCollSeq(pParse, pColl) ){
pColl = 0;
@@ -219,12 +244,10 @@ CollSeq *sqlite3BinaryCompareCollSeq(
){
CollSeq *pColl;
assert( pLeft );
- if( pLeft->flags & EP_ExpCollate ){
- assert( pLeft->pColl );
- pColl = pLeft->pColl;
- }else if( pRight && pRight->flags & EP_ExpCollate ){
- assert( pRight->pColl );
- pColl = pRight->pColl;
+ if( pLeft->flags & EP_Collate ){
+ pColl = sqlite3ExprCollSeq(pParse, pLeft);
+ }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
+ pColl = sqlite3ExprCollSeq(pParse, pRight);
}else{
pColl = sqlite3ExprCollSeq(pParse, pLeft);
if( !pColl ){
@@ -454,17 +477,11 @@ void sqlite3ExprAttachSubtrees(
}else{
if( pRight ){
pRoot->pRight = pRight;
- if( pRight->flags & EP_ExpCollate ){
- pRoot->flags |= EP_ExpCollate;
- pRoot->pColl = pRight->pColl;
- }
+ pRoot->flags |= EP_Collate & pRight->flags;
}
if( pLeft ){
pRoot->pLeft = pLeft;
- if( pLeft->flags & EP_ExpCollate ){
- pRoot->flags |= EP_ExpCollate;
- pRoot->pColl = pLeft->pColl;
- }
+ pRoot->flags |= EP_Collate & pLeft->flags;
}
exprSetHeight(pRoot);
}
@@ -616,7 +633,7 @@ void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
*/
ynVar i;
for(i=0; i<pParse->nzVar; i++){
- if( pParse->azVar[i] && memcmp(pParse->azVar[i],z,n+1)==0 ){
+ if( pParse->azVar[i] && strcmp(pParse->azVar[i],z)==0 ){
pExpr->iColumn = x = (ynVar)i+1;
break;
}
@@ -722,7 +739,7 @@ static int dupedExprStructSize(Expr *p, int flags){
assert( !ExprHasProperty(p, EP_FromJoin) );
assert( (p->flags2 & EP2_MallocedToken)==0 );
assert( (p->flags2 & EP2_Irreducible)==0 );
- if( p->pLeft || p->pRight || p->pColl || p->x.pList ){
+ if( p->pLeft || p->pRight || p->x.pList ){
nSize = EXPR_REDUCEDSIZE | EP_Reduced;
}else{
nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
@@ -930,6 +947,7 @@ SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
struct SrcList_item *pNewItem = &pNew->a[i];
struct SrcList_item *pOldItem = &p->a[i];
Table *pTab;
+ pNewItem->pSchema = pOldItem->pSchema;
pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
@@ -938,6 +956,7 @@ SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
pNewItem->addrFillSub = pOldItem->addrFillSub;
pNewItem->regReturn = pOldItem->regReturn;
pNewItem->isCorrelated = pOldItem->isCorrelated;
+ pNewItem->viaCoroutine = pOldItem->viaCoroutine;
pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
pNewItem->notIndexed = pOldItem->notIndexed;
pNewItem->pIndex = pOldItem->pIndex;
@@ -1190,6 +1209,7 @@ static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
}
static int exprIsConst(Expr *p, int initFlag){
Walker w;
+ memset(&w, 0, sizeof(w));
w.u.i = initFlag;
w.xExprCallback = exprNodeIsConstant;
w.xSelectCallback = selectNodeIsConstant;
@@ -1420,24 +1440,34 @@ int sqlite3CodeOnce(Parse *pParse){
/*
** This function is used by the implementation of the IN (...) operator.
-** It's job is to find or create a b-tree structure that may be used
-** either to test for membership of the (...) set or to iterate through
-** its members, skipping duplicates.
+** The pX parameter is the expression on the RHS of the IN operator, which
+** might be either a list of expressions or a subquery.
+**
+** The job of this routine is to find or create a b-tree object that can
+** be used either to test for membership in the RHS set or to iterate through
+** all members of the RHS set, skipping duplicates.
+**
+** A cursor is opened on the b-tree object that the RHS of the IN operator
+** and pX->iTable is set to the index of that cursor.
**
-** The index of the cursor opened on the b-tree (database table, database index
-** or ephermal table) is stored in pX->iTable before this function returns.
** The returned value of this function indicates the b-tree type, as follows:
**
-** IN_INDEX_ROWID - The cursor was opened on a database table.
-** IN_INDEX_INDEX - The cursor was opened on a database index.
-** IN_INDEX_EPH - The cursor was opened on a specially created and
-** populated epheremal table.
+** IN_INDEX_ROWID - The cursor was opened on a database table.
+** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
+** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
+** IN_INDEX_EPH - The cursor was opened on a specially created and
+** populated epheremal table.
**
-** An existing b-tree may only be used if the SELECT is of the simple
-** form:
+** An existing b-tree might be used if the RHS expression pX is a simple
+** subquery such as:
**
** SELECT <column> FROM <table>
**
+** If the RHS of the IN operator is a list or a more complex subquery, then
+** an ephemeral table might need to be generated from the RHS and then
+** pX->iTable made to point to the ephermeral table instead of an
+** existing table.
+**
** If the prNotFound parameter is 0, then the b-tree will be used to iterate
** through the set members, skipping any duplicates. In this case an
** epheremal table must be used unless the selected <column> is guaranteed
@@ -1533,8 +1563,7 @@ int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
** comparison is the same as the affinity of the column. If
** it is not, it is not possible to use any index.
*/
- char aff = comparisonAffinity(pX);
- int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
+ int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
if( (pIdx->aiColumn[0]==iCol)
@@ -1550,7 +1579,8 @@ int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
pKey,P4_KEYINFO_HANDOFF);
VdbeComment((v, "%s", pIdx->zName));
- eType = IN_INDEX_INDEX;
+ assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
+ eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
sqlite3VdbeJumpHere(v, iAddr);
if( prNotFound && !pTab->aCol[iCol].notNull ){
@@ -1662,6 +1692,7 @@ int sqlite3CodeSubselect(
case TK_IN: {
char affinity; /* Affinity of the LHS of the IN */
KeyInfo keyInfo; /* Keyinfo for the generated table */
+ static u8 sortOrder = 0; /* Fake aSortOrder for keyInfo */
int addr; /* Address of OP_OpenEphemeral instruction */
Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
@@ -1689,6 +1720,7 @@ int sqlite3CodeSubselect(
if( rMayHaveNull==0 ) sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
memset(&keyInfo, 0, sizeof(keyInfo));
keyInfo.nField = 1;
+ keyInfo.aSortOrder = &sortOrder;
if( ExprHasProperty(pExpr, EP_xIsSelect) ){
/* Case 1: expr IN (SELECT ...)
@@ -1729,6 +1761,7 @@ int sqlite3CodeSubselect(
affinity = SQLITE_AFF_NONE;
}
keyInfo.aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
+ keyInfo.aSortOrder = &sortOrder;
/* Loop through each expression in <exprlist>. */
r1 = sqlite3GetTempReg(pParse);
@@ -2055,7 +2088,7 @@ void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
** for testing only - to verify that SQLite always gets the same answer
** with and without the column cache.
*/
- if( pParse->db->flags & SQLITE_ColumnCache ) return;
+ if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
/* First replace any existing entry.
**
@@ -2252,8 +2285,8 @@ void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
int i;
struct yColCache *p;
- if( NEVER(iFrom==iTo) ) return;
- sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
+ assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
+ sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg-1);
for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
int x = p->iReg;
if( x>=iFrom && x<iFrom+nReg ){
@@ -2262,18 +2295,6 @@ void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
}
}
-/*
-** Generate code to copy content from registers iFrom...iFrom+nReg-1
-** over to iTo..iTo+nReg-1.
-*/
-void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){
- int i;
- if( NEVER(iFrom==iTo) ) return;
- for(i=0; i<nReg; i++){
- sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, iFrom+i, iTo+i);
- }
-}
-
#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
/*
** Return true if any register in the range iFrom..iTo (inclusive)
@@ -2745,6 +2766,7 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
sqlite3ReleaseTempReg(pParse, r4);
break;
}
+ case TK_COLLATE:
case TK_UPLUS: {
inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
break;
@@ -2911,7 +2933,8 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
sqlite3VdbeAddOp4(
v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
}else{
- sqlite3HaltConstraint(pParse, pExpr->affinity, pExpr->u.zToken, 0);
+ sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER,
+ pExpr->affinity, pExpr->u.zToken, 0);
}
break;
@@ -3114,6 +3137,12 @@ void sqlite3ExplainExpr(Vdbe *pOut, Expr *pExpr){
case TK_ISNULL: zUniOp = "ISNULL"; break;
case TK_NOTNULL: zUniOp = "NOTNULL"; break;
+ case TK_COLLATE: {
+ sqlite3ExplainExpr(pOut, pExpr->pLeft);
+ sqlite3ExplainPrintf(pOut,".COLLATE(%s)",pExpr->u.zToken);
+ break;
+ }
+
case TK_AGG_FUNCTION:
case TK_CONST_FUNC:
case TK_FUNCTION: {
@@ -3251,6 +3280,12 @@ void sqlite3ExplainExprList(Vdbe *pOut, ExprList *pList){
sqlite3ExplainPush(pOut);
sqlite3ExplainExpr(pOut, pList->a[i].pExpr);
sqlite3ExplainPop(pOut);
+ if( pList->a[i].zName ){
+ sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName);
+ }
+ if( pList->a[i].bSpanIsTab ){
+ sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan);
+ }
if( i<pList->nExpr-1 ){
sqlite3ExplainNL(pOut);
}
@@ -3332,6 +3367,9 @@ static int evalConstExpr(Walker *pWalker, Expr *pExpr){
case TK_REGISTER: {
return WRC_Prune;
}
+ case TK_COLLATE: {
+ return WRC_Continue;
+ }
case TK_FUNCTION:
case TK_AGG_FUNCTION:
case TK_CONST_FUNC: {
@@ -3353,9 +3391,11 @@ static int evalConstExpr(Walker *pWalker, Expr *pExpr){
}
if( isAppropriateForFactoring(pExpr) ){
int r1 = ++pParse->nMem;
- int r2;
- r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
- if( NEVER(r1!=r2) ) sqlite3ReleaseTempReg(pParse, r1);
+ int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
+ /* If r2!=r1, it means that register r1 is never used. That is harmless
+ ** but suboptimal, so we want to know about the situation to fix it.
+ ** Hence the following assert: */
+ assert( r2==r1 );
pExpr->op2 = pExpr->op;
pExpr->op = TK_REGISTER;
pExpr->iTable = r2;
@@ -3383,9 +3423,9 @@ static int evalConstExpr(Walker *pWalker, Expr *pExpr){
void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
Walker w;
if( pParse->cookieGoto ) return;
- if( (pParse->db->flags & SQLITE_FactorOutConst)!=0 ) return;
+ if( OptimizationDisabled(pParse->db, SQLITE_FactorOutConst) ) return;
+ memset(&w, 0, sizeof(w));
w.xExprCallback = evalConstExpr;
- w.xSelectCallback = 0;
w.pParse = pParse;
sqlite3WalkExpr(&w, pExpr);
}
@@ -3498,7 +3538,7 @@ void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
int r1, r2;
assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
- if( NEVER(v==0) ) return; /* Existance of VDBE checked by caller */
+ if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
if( NEVER(pExpr==0) ) return; /* No way this can happen */
op = pExpr->op;
switch( op ){
@@ -3618,7 +3658,7 @@ void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
int r1, r2;
assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
- if( NEVER(v==0) ) return; /* Existance of VDBE checked by caller */
+ if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
if( pExpr==0 ) return;
/* The value of pExpr->op and op are related as follows:
@@ -3772,7 +3812,15 @@ int sqlite3ExprCompare(Expr *pA, Expr *pB){
return 2;
}
if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
- if( pA->op!=pB->op ) return 2;
+ if( pA->op!=pB->op ){
+ if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB)<2 ){
+ return 1;
+ }
+ if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft)<2 ){
+ return 1;
+ }
+ return 2;
+ }
if( sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 2;
if( sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 2;
if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList) ) return 2;
@@ -3784,11 +3832,9 @@ int sqlite3ExprCompare(Expr *pA, Expr *pB){
}else if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken){
if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
- return 2;
+ return pA->op==TK_COLLATE ? 1 : 2;
}
}
- if( (pA->flags & EP_ExpCollate)!=(pB->flags & EP_ExpCollate) ) return 1;
- if( (pA->flags & EP_ExpCollate)!=0 && pA->pColl!=pB->pColl ) return 2;
return 0;
}
@@ -4029,8 +4075,10 @@ static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
ExprSetIrreducible(pExpr);
pExpr->iAgg = (i16)i;
pExpr->pAggInfo = pAggInfo;
+ return WRC_Prune;
+ }else{
+ return WRC_Continue;
}
- return WRC_Prune;
}
}
return WRC_Continue;
@@ -4042,9 +4090,10 @@ static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
}
/*
-** Analyze the given expression looking for aggregate functions and
-** for variables that need to be added to the pParse->aAgg[] array.
-** Make additional entries to the pParse->aAgg[] array as necessary.
+** Analyze the pExpr expression looking for aggregate functions and
+** for variables that need to be added to AggInfo object that pNC->pAggInfo
+** points to. Additional entries are made on the AggInfo object as
+** necessary.
**
** This routine should only be called after the expression has been
** analyzed by sqlite3ResolveExprNames().
diff --git a/src/fkey.c b/src/fkey.c
index 9db3a71..ac35bc1 100644
--- a/src/fkey.c
+++ b/src/fkey.c
@@ -21,8 +21,9 @@
** --------------------------
**
** Foreign keys in SQLite come in two flavours: deferred and immediate.
-** If an immediate foreign key constraint is violated, SQLITE_CONSTRAINT
-** is returned and the current statement transaction rolled back. If a
+** If an immediate foreign key constraint is violated,
+** SQLITE_CONSTRAINT_FOREIGNKEY is returned and the current
+** statement transaction rolled back. If a
** deferred foreign key constraint is violated, no action is taken
** immediately. However if the application attempts to commit the
** transaction before fixing the constraint violation, the attempt fails.
@@ -86,7 +87,8 @@
** Immediate constraints are usually handled similarly. The only difference
** is that the counter used is stored as part of each individual statement
** object (struct Vdbe). If, after the statement has run, its immediate
-** constraint counter is greater than zero, it returns SQLITE_CONSTRAINT
+** constraint counter is greater than zero,
+** it returns SQLITE_CONSTRAINT_FOREIGNKEY
** and the statement transaction is rolled back. An exception is an INSERT
** statement that inserts a single row only (no triggers). In this case,
** instead of using a counter, an exception is thrown immediately if the
@@ -142,7 +144,7 @@
** A foreign key constraint requires that the key columns in the parent
** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
** Given that pParent is the parent table for foreign key constraint pFKey,
-** search the schema a unique index on the parent key columns.
+** search the schema for a unique index on the parent key columns.
**
** If successful, zero is returned. If the parent key is an INTEGER PRIMARY
** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx
@@ -178,7 +180,7 @@
** into pParse. If an OOM error occurs, non-zero is returned and the
** pParse->db->mallocFailed flag is set.
*/
-static int locateFkeyIndex(
+int sqlite3FkLocateIndex(
Parse *pParse, /* Parse context to store any error in */
Table *pParent, /* Parent table of FK constraint pFKey */
FKey *pFKey, /* Foreign key to find index for */
@@ -275,7 +277,9 @@ static int locateFkeyIndex(
if( !pIdx ){
if( !pParse->disableTriggers ){
- sqlite3ErrorMsg(pParse, "foreign key mismatch");
+ sqlite3ErrorMsg(pParse,
+ "foreign key mismatch - \"%w\" referencing \"%w\"",
+ pFKey->pFrom->zName, pFKey->zTo);
}
sqlite3DbFree(pParse->db, aiCol);
return 1;
@@ -424,8 +428,8 @@ static void fkLookupParent(
** incrementing a counter. This is necessary as the VM code is being
** generated for will not open a statement transaction. */
assert( nIncr==1 );
- sqlite3HaltConstraint(
- pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
+ sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
+ OE_Abort, "foreign key constraint failed", P4_STATIC
);
}else{
if( nIncr>0 && pFKey->isDeferred==0 ){
@@ -511,12 +515,15 @@ static void fkScanChildren(
** expression to the parent key column defaults. */
if( pIdx ){
Column *pCol;
+ const char *zColl;
iCol = pIdx->aiColumn[i];
pCol = &pTab->aCol[iCol];
if( pTab->iPKey==iCol ) iCol = -1;
pLeft->iTable = regData+iCol+1;
pLeft->affinity = pCol->affinity;
- pLeft->pColl = sqlite3LocateCollSeq(pParse, pCol->zColl);
+ zColl = pCol->zColl;
+ if( zColl==0 ) zColl = db->pDfltColl->zName;
+ pLeft = sqlite3ExprAddCollateString(pParse, pLeft, zColl);
}else{
pLeft->iTable = regData;
pLeft->affinity = SQLITE_AFF_INTEGER;
@@ -662,8 +669,8 @@ void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
** any modifications to the schema are made. This is because statement
** transactions are not able to rollback schema changes. */
sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
- sqlite3HaltConstraint(
- pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
+ sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
+ OE_Abort, "foreign key constraint failed", P4_STATIC
);
if( iSkip ){
@@ -733,7 +740,7 @@ void sqlite3FkCheck(
}else{
pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
}
- if( !pTo || locateFkeyIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
+ if( !pTo || sqlite3FkLocateIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
if( !isIgnoreErrors || db->mallocFailed ) return;
if( pTo==0 ){
@@ -813,7 +820,7 @@ void sqlite3FkCheck(
continue;
}
- if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
+ if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
if( !isIgnoreErrors || db->mallocFailed ) return;
continue;
}
@@ -868,7 +875,7 @@ u32 sqlite3FkOldmask(
}
for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
Index *pIdx = 0;
- locateFkeyIndex(pParse, pTab, p, &pIdx, 0);
+ sqlite3FkLocateIndex(pParse, pTab, p, &pIdx, 0);
if( pIdx ){
for(i=0; i<pIdx->nColumn; i++) mask |= COLUMN_MASK(pIdx->aiColumn[i]);
}
@@ -925,7 +932,8 @@ int sqlite3FkRequired(
int iKey;
for(iKey=0; iKey<pTab->nCol; iKey++){
Column *pCol = &pTab->aCol[iKey];
- if( (zKey ? !sqlite3StrICmp(pCol->zName, zKey) : pCol->isPrimKey) ){
+ if( (zKey ? !sqlite3StrICmp(pCol->zName, zKey)
+ : (pCol->colFlags & COLFLAG_PRIMKEY)!=0) ){
if( aChange[iKey]>=0 ) return 1;
if( iKey==pTab->iPKey && chngRowid ) return 1;
}
@@ -993,7 +1001,7 @@ static Trigger *fkActionTrigger(
int i; /* Iterator variable */
Expr *pWhen = 0; /* WHEN clause for the trigger */
- if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
+ if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
assert( aiCol || pFKey->nCol==1 );
for(i=0; i<pFKey->nCol; i++){
diff --git a/src/func.c b/src/func.c
index 2cbaddc..c02f096 100644
--- a/src/func.c
+++ b/src/func.c
@@ -169,6 +169,56 @@ static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
}
/*
+** Implementation of the instr() function.
+**
+** instr(haystack,needle) finds the first occurrence of needle
+** in haystack and returns the number of previous characters plus 1,
+** or 0 if needle does not occur within haystack.
+**
+** If both haystack and needle are BLOBs, then the result is one more than
+** the number of bytes in haystack prior to the first occurrence of needle,
+** or 0 if needle never occurs in haystack.
+*/
+static void instrFunc(
+ sqlite3_context *context,
+ int argc,
+ sqlite3_value **argv
+){
+ const unsigned char *zHaystack;
+ const unsigned char *zNeedle;
+ int nHaystack;
+ int nNeedle;
+ int typeHaystack, typeNeedle;
+ int N = 1;
+ int isText;
+
+ UNUSED_PARAMETER(argc);
+ typeHaystack = sqlite3_value_type(argv[0]);
+ typeNeedle = sqlite3_value_type(argv[1]);
+ if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
+ nHaystack = sqlite3_value_bytes(argv[0]);
+ nNeedle = sqlite3_value_bytes(argv[1]);
+ if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
+ zHaystack = sqlite3_value_blob(argv[0]);
+ zNeedle = sqlite3_value_blob(argv[1]);
+ isText = 0;
+ }else{
+ zHaystack = sqlite3_value_text(argv[0]);
+ zNeedle = sqlite3_value_text(argv[1]);
+ isText = 1;
+ }
+ while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){
+ N++;
+ do{
+ nHaystack--;
+ zHaystack++;
+ }while( isText && (zHaystack[0]&0xc0)==0x80 );
+ }
+ if( nNeedle>nHaystack ) N = 0;
+ sqlite3_result_int(context, N);
+}
+
+/*
** Implementation of the substr() function.
**
** substr(x,p1,p2) returns p2 characters of x[] beginning with p1.
@@ -367,33 +417,14 @@ static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
}
}
-
-#if 0 /* This function is never used. */
-/*
-** The COALESCE() and IFNULL() functions used to be implemented as shown
-** here. But now they are implemented as VDBE code so that unused arguments
-** do not have to be computed. This legacy implementation is retained as
-** comment.
-*/
/*
-** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
-** All three do the same thing. They return the first non-NULL
-** argument.
+** The COALESCE() and IFNULL() functions are implemented as VDBE code so
+** that unused argument values do not have to be computed. However, we
+** still need some kind of function implementation for this routines in
+** the function table. That function implementation will never be called
+** so it doesn't matter what the implementation is. We might as well use
+** the "version()" function as a substitute.
*/
-static void ifnullFunc(
- sqlite3_context *context,
- int argc,
- sqlite3_value **argv
-){
- int i;
- for(i=0; i<argc; i++){
- if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
- sqlite3_result_value(context, argv[i]);
- break;
- }
- }
-}
-#endif /* NOT USED */
#define ifnullFunc versionFunc /* Substitute function - never called */
/*
@@ -512,7 +543,7 @@ struct compareInfo {
** whereas only characters less than 0x80 do in ASCII.
*/
#if defined(SQLITE_EBCDIC)
-# define sqlite3Utf8Read(A,C) (*(A++))
+# define sqlite3Utf8Read(A) (*((*A)++))
# define GlogUpperToLower(A) A = sqlite3UpperToLower[A]
#else
# define GlogUpperToLower(A) if( !((A)&~0x7f) ){ A = sqlite3UpperToLower[A]; }
@@ -569,18 +600,18 @@ static int patternCompare(
u8 noCase = pInfo->noCase;
int prevEscape = 0; /* True if the previous character was 'escape' */
- while( (c = sqlite3Utf8Read(zPattern,&zPattern))!=0 ){
- if( !prevEscape && c==matchAll ){
- while( (c=sqlite3Utf8Read(zPattern,&zPattern)) == matchAll
+ while( (c = sqlite3Utf8Read(&zPattern))!=0 ){
+ if( c==matchAll && !prevEscape ){
+ while( (c=sqlite3Utf8Read(&zPattern)) == matchAll
|| c == matchOne ){
- if( c==matchOne && sqlite3Utf8Read(zString, &zString)==0 ){
+ if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
return 0;
}
}
if( c==0 ){
return 1;
}else if( c==esc ){
- c = sqlite3Utf8Read(zPattern, &zPattern);
+ c = sqlite3Utf8Read(&zPattern);
if( c==0 ){
return 0;
}
@@ -592,25 +623,25 @@ static int patternCompare(
}
return *zString!=0;
}
- while( (c2 = sqlite3Utf8Read(zString,&zString))!=0 ){
+ while( (c2 = sqlite3Utf8Read(&zString))!=0 ){
if( noCase ){
GlogUpperToLower(c2);
GlogUpperToLower(c);
while( c2 != 0 && c2 != c ){
- c2 = sqlite3Utf8Read(zString, &zString);
+ c2 = sqlite3Utf8Read(&zString);
GlogUpperToLower(c2);
}
}else{
while( c2 != 0 && c2 != c ){
- c2 = sqlite3Utf8Read(zString, &zString);
+ c2 = sqlite3Utf8Read(&zString);
}
}
if( c2==0 ) return 0;
if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
}
return 0;
- }else if( !prevEscape && c==matchOne ){
- if( sqlite3Utf8Read(zString, &zString)==0 ){
+ }else if( c==matchOne && !prevEscape ){
+ if( sqlite3Utf8Read(&zString)==0 ){
return 0;
}
}else if( c==matchSet ){
@@ -618,20 +649,20 @@ static int patternCompare(
assert( esc==0 ); /* This only occurs for GLOB, not LIKE */
seen = 0;
invert = 0;
- c = sqlite3Utf8Read(zString, &zString);
+ c = sqlite3Utf8Read(&zString);
if( c==0 ) return 0;
- c2 = sqlite3Utf8Read(zPattern, &zPattern);
+ c2 = sqlite3Utf8Read(&zPattern);
if( c2=='^' ){
invert = 1;
- c2 = sqlite3Utf8Read(zPattern, &zPattern);
+ c2 = sqlite3Utf8Read(&zPattern);
}
if( c2==']' ){
if( c==']' ) seen = 1;
- c2 = sqlite3Utf8Read(zPattern, &zPattern);
+ c2 = sqlite3Utf8Read(&zPattern);
}
while( c2 && c2!=']' ){
if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
- c2 = sqlite3Utf8Read(zPattern, &zPattern);
+ c2 = sqlite3Utf8Read(&zPattern);
if( c>=prior_c && c<=c2 ) seen = 1;
prior_c = 0;
}else{
@@ -640,7 +671,7 @@ static int patternCompare(
}
prior_c = c2;
}
- c2 = sqlite3Utf8Read(zPattern, &zPattern);
+ c2 = sqlite3Utf8Read(&zPattern);
}
if( c2==0 || (seen ^ invert)==0 ){
return 0;
@@ -648,7 +679,7 @@ static int patternCompare(
}else if( esc==c && !prevEscape ){
prevEscape = 1;
}else{
- c2 = sqlite3Utf8Read(zString, &zString);
+ c2 = sqlite3Utf8Read(&zString);
if( noCase ){
GlogUpperToLower(c);
GlogUpperToLower(c2);
@@ -663,6 +694,13 @@ static int patternCompare(
}
/*
+** The sqlite3_strglob() interface.
+*/
+int sqlite3_strglob(const char *zGlobPattern, const char *zString){
+ return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, 0)==0;
+}
+
+/*
** Count the number of times that the LIKE operator (or GLOB which is
** just a variation of LIKE) gets called. This is used for testing
** only.
@@ -720,7 +758,7 @@ static void likeFunc(
"ESCAPE expression must be a single character", -1);
return;
}
- escape = sqlite3Utf8Read(zEsc, &zEsc);
+ escape = sqlite3Utf8Read(&zEsc);
}
if( zA && zB ){
struct compareInfo *pInfo = sqlite3_user_data(context);
@@ -932,6 +970,62 @@ static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
}
/*
+** The unicode() function. Return the integer unicode code-point value
+** for the first character of the input string.
+*/
+static void unicodeFunc(
+ sqlite3_context *context,
+ int argc,
+ sqlite3_value **argv
+){
+ const unsigned char *z = sqlite3_value_text(argv[0]);
+ (void)argc;
+ if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z));
+}
+
+/*
+** The char() function takes zero or more arguments, each of which is
+** an integer. It constructs a string where each character of the string
+** is the unicode character for the corresponding integer argument.
+*/
+static void charFunc(
+ sqlite3_context *context,
+ int argc,
+ sqlite3_value **argv
+){
+ unsigned char *z, *zOut;
+ int i;
+ zOut = z = sqlite3_malloc( argc*4 );
+ if( z==0 ){
+ sqlite3_result_error_nomem(context);
+ return;
+ }
+ for(i=0; i<argc; i++){
+ sqlite3_int64 x;
+ unsigned c;
+ x = sqlite3_value_int64(argv[i]);
+ if( x<0 || x>0x10ffff ) x = 0xfffd;
+ c = (unsigned)(x & 0x1fffff);
+ if( c<0x00080 ){
+ *zOut++ = (u8)(c&0xFF);
+ }else if( c<0x00800 ){
+ *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);
+ *zOut++ = 0x80 + (u8)(c & 0x3F);
+ }else if( c<0x10000 ){
+ *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);
+ *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
+ *zOut++ = 0x80 + (u8)(c & 0x3F);
+ }else{
+ *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);
+ *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);
+ *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
+ *zOut++ = 0x80 + (u8)(c & 0x3F);
+ } \
+ }
+ sqlite3_result_text(context, (char*)z, (int)(zOut-z), sqlite3_free);
+}
+
+/*
** The hex() function. Interpret the argument as a blob. Return
** a hexadecimal rendering as text.
*/
@@ -1450,16 +1544,24 @@ static void groupConcatFinalize(sqlite3_context *context){
*/
void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
int rc = sqlite3_overload_function(db, "MATCH", 2);
+/* BEGIN SQLCIPHER */
+#ifdef SQLITE_HAS_CODEC
#ifndef OMIT_EXPORT
extern void sqlcipher_exportFunc(sqlite3_context *, int, sqlite3_value **);
#endif
+#endif
+/* END SQLCIPHER */
assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
if( rc==SQLITE_NOMEM ){
db->mallocFailed = 1;
}
+/* BEGIN SQLCIPHER */
+#ifdef SQLITE_HAS_CODEC
#ifndef OMIT_EXPORT
sqlite3CreateFunc(db, "sqlcipher_export", 1, SQLITE_TEXT, 0, sqlcipher_exportFunc, 0, 0, 0);
#endif
+#endif
+/* END SQLCIPHER */
}
/*
@@ -1561,8 +1663,11 @@ void sqlite3RegisterGlobalFunctions(void){
AGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize ),
FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF),
FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH),
+ FUNCTION(instr, 2, 0, 0, instrFunc ),
FUNCTION(substr, 2, 0, 0, substrFunc ),
FUNCTION(substr, 3, 0, 0, substrFunc ),
+ FUNCTION(unicode, 1, 0, 0, unicodeFunc ),
+ FUNCTION(char, -1, 0, 0, charFunc ),
FUNCTION(abs, 1, 0, 0, absFunc ),
#ifndef SQLITE_OMIT_FLOATING_POINT
FUNCTION(round, 1, 0, 0, roundFunc ),
diff --git a/src/global.c b/src/global.c
index 7de0668..7b02cf2 100644
--- a/src/global.c
+++ b/src/global.c
@@ -133,6 +133,10 @@ const unsigned char sqlite3CtypeMap[256] = {
# define SQLITE_USE_URI 0
#endif
+#ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN
+# define SQLITE_ALLOW_COVERING_INDEX_SCAN 1
+#endif
+
/*
** The following singleton contains the global configuration for
** the SQLite library.
@@ -142,6 +146,7 @@ SQLITE_WSD struct Sqlite3Config sqlite3Config = {
1, /* bCoreMutex */
SQLITE_THREADSAFE==1, /* bFullMutex */
SQLITE_USE_URI, /* bOpenUri */
+ SQLITE_ALLOW_COVERING_INDEX_SCAN, /* bUseCis */
0x7ffffffe, /* mxStrlen */
128, /* szLookaside */
500, /* nLookaside */
@@ -151,6 +156,8 @@ SQLITE_WSD struct Sqlite3Config sqlite3Config = {
(void*)0, /* pHeap */
0, /* nHeap */
0, 0, /* mnHeap, mxHeap */
+ SQLITE_DEFAULT_MMAP_SIZE, /* szMmap */
+ SQLITE_MAX_MMAP_SIZE, /* mxMmap */
(void*)0, /* pScratch */
0, /* szScratch */
0, /* nScratch */
@@ -170,6 +177,10 @@ SQLITE_WSD struct Sqlite3Config sqlite3Config = {
0, /* xLog */
0, /* pLogArg */
0, /* bLocaltimeFault */
+#ifdef SQLITE_ENABLE_SQLLOG
+ 0, /* xSqllog */
+ 0 /* pSqllogArg */
+#endif
};
diff --git a/src/hash.c b/src/hash.c
index d7625d3..e81dcf9 100644
--- a/src/hash.c
+++ b/src/hash.c
@@ -193,7 +193,7 @@ static void removeElementGivenHash(
}
sqlite3_free( elem );
pH->count--;
- if( pH->count<=0 ){
+ if( pH->count==0 ){
assert( pH->first==0 );
assert( pH->count==0 );
sqlite3HashClear(pH);
diff --git a/src/hash.h b/src/hash.h
index 990a2d6..82b7c58 100644
--- a/src/hash.h
+++ b/src/hash.h
@@ -9,7 +9,7 @@
** May you share freely, never taking more than you give.
**
*************************************************************************
-** This is the header file for the generic hash-table implemenation
+** This is the header file for the generic hash-table implementation
** used in SQLite.
*/
#ifndef _SQLITE_HASH_H_
diff --git a/src/insert.c b/src/insert.c
index a24e8f9..9a5661f 100644
--- a/src/insert.c
+++ b/src/insert.c
@@ -25,7 +25,7 @@ void sqlite3OpenTable(
int opcode /* OP_OpenRead or OP_OpenWrite */
){
Vdbe *v;
- if( IsVirtual(pTab) ) return;
+ assert( !IsVirtual(pTab) );
v = sqlite3GetVdbe(p);
assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite)?1:0, pTab->zName);
@@ -320,6 +320,97 @@ void sqlite3AutoincrementEnd(Parse *pParse){
#endif /* SQLITE_OMIT_AUTOINCREMENT */
+/*
+** Generate code for a co-routine that will evaluate a subquery one
+** row at a time.
+**
+** The pSelect parameter is the subquery that the co-routine will evaluation.
+** Information about the location of co-routine and the registers it will use
+** is returned by filling in the pDest object.
+**
+** Registers are allocated as follows:
+**
+** pDest->iSDParm The register holding the next entry-point of the
+** co-routine. Run the co-routine to its next breakpoint
+** by calling "OP_Yield $X" where $X is pDest->iSDParm.
+**
+** pDest->iSDParm+1 The register holding the "completed" flag for the
+** co-routine. This register is 0 if the previous Yield
+** generated a new result row, or 1 if the subquery
+** has completed. If the Yield is called again
+** after this register becomes 1, then the VDBE will
+** halt with an SQLITE_INTERNAL error.
+**
+** pDest->iSdst First result register.
+**
+** pDest->nSdst Number of result registers.
+**
+** This routine handles all of the register allocation and fills in the
+** pDest structure appropriately.
+**
+** Here is a schematic of the generated code assuming that X is the
+** co-routine entry-point register reg[pDest->iSDParm], that EOF is the
+** completed flag reg[pDest->iSDParm+1], and R and S are the range of
+** registers that hold the result set, reg[pDest->iSdst] through
+** reg[pDest->iSdst+pDest->nSdst-1]:
+**
+** X <- A
+** EOF <- 0
+** goto B
+** A: setup for the SELECT
+** loop rows in the SELECT
+** load results into registers R..S
+** yield X
+** end loop
+** cleanup after the SELECT
+** EOF <- 1
+** yield X
+** halt-error
+** B:
+**
+** To use this subroutine, the caller generates code as follows:
+**
+** [ Co-routine generated by this subroutine, shown above ]
+** S: yield X
+** if EOF goto E
+** if skip this row, goto C
+** if terminate loop, goto E
+** deal with this row
+** C: goto S
+** E:
+*/
+int sqlite3CodeCoroutine(Parse *pParse, Select *pSelect, SelectDest *pDest){
+ int regYield; /* Register holding co-routine entry-point */
+ int regEof; /* Register holding co-routine completion flag */
+ int addrTop; /* Top of the co-routine */
+ int j1; /* Jump instruction */
+ int rc; /* Result code */
+ Vdbe *v; /* VDBE under construction */
+
+ regYield = ++pParse->nMem;
+ regEof = ++pParse->nMem;
+ v = sqlite3GetVdbe(pParse);
+ addrTop = sqlite3VdbeCurrentAddr(v);
+ sqlite3VdbeAddOp2(v, OP_Integer, addrTop+2, regYield); /* X <- A */
+ VdbeComment((v, "Co-routine entry point"));
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof); /* EOF <- 0 */
+ VdbeComment((v, "Co-routine completion flag"));
+ sqlite3SelectDestInit(pDest, SRT_Coroutine, regYield);
+ j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
+ rc = sqlite3Select(pParse, pSelect, pDest);
+ assert( pParse->nErr==0 || rc );
+ if( pParse->db->mallocFailed && rc==SQLITE_OK ) rc = SQLITE_NOMEM;
+ if( rc ) return rc;
+ sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof); /* EOF <- 1 */
+ sqlite3VdbeAddOp1(v, OP_Yield, regYield); /* yield X */
+ sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
+ VdbeComment((v, "End of coroutine"));
+ sqlite3VdbeJumpHere(v, j1); /* label B: */
+ return rc;
+}
+
+
+
/* Forward declaration */
static int xferOptimization(
Parse *pParse, /* Parser context */
@@ -568,51 +659,12 @@ void sqlite3Insert(
** co-routine is the common header to the 3rd and 4th templates.
*/
if( pSelect ){
- /* Data is coming from a SELECT. Generate code to implement that SELECT
- ** as a co-routine. The code is common to both the 3rd and 4th
- ** templates:
- **
- ** EOF <- 0
- ** X <- A
- ** goto B
- ** A: setup for the SELECT
- ** loop over the tables in the SELECT
- ** load value into register R..R+n
- ** yield X
- ** end loop
- ** cleanup after the SELECT
- ** EOF <- 1
- ** yield X
- ** halt-error
- **
- ** On each invocation of the co-routine, it puts a single row of the
- ** SELECT result into registers dest.iMem...dest.iMem+dest.nMem-1.
- ** (These output registers are allocated by sqlite3Select().) When
- ** the SELECT completes, it sets the EOF flag stored in regEof.
- */
- int rc, j1;
-
- regEof = ++pParse->nMem;
- sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof); /* EOF <- 0 */
- VdbeComment((v, "SELECT eof flag"));
- sqlite3SelectDestInit(&dest, SRT_Coroutine, ++pParse->nMem);
- addrSelect = sqlite3VdbeCurrentAddr(v)+2;
- sqlite3VdbeAddOp2(v, OP_Integer, addrSelect-1, dest.iSDParm);
- j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
- VdbeComment((v, "Jump over SELECT coroutine"));
-
- /* Resolve the expressions in the SELECT statement and execute it. */
- rc = sqlite3Select(pParse, pSelect, &dest);
- assert( pParse->nErr==0 || rc );
- if( rc || NEVER(pParse->nErr) || db->mallocFailed ){
- goto insert_cleanup;
- }
- sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof); /* EOF <- 1 */
- sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); /* yield X */
- sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
- VdbeComment((v, "End of SELECT coroutine"));
- sqlite3VdbeJumpHere(v, j1); /* label B: */
+ /* Data is coming from a SELECT. Generate a co-routine to run that
+ ** SELECT. */
+ int rc = sqlite3CodeCoroutine(pParse, pSelect, &dest);
+ if( rc ) goto insert_cleanup;
+ regEof = dest.iSDParm + 1;
regFromSelect = dest.iSdst;
assert( pSelect->pEList );
nColumn = pSelect->pEList->nExpr;
@@ -1193,7 +1245,7 @@ void sqlite3GenerateConstraintChecks(
case OE_Fail: {
char *zMsg;
sqlite3VdbeAddOp3(v, OP_HaltIfNull,
- SQLITE_CONSTRAINT, onError, regData+i);
+ SQLITE_CONSTRAINT_NOTNULL, onError, regData+i);
zMsg = sqlite3MPrintf(db, "%s.%s may not be NULL",
pTab->zName, pTab->aCol[i].zName);
sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC);
@@ -1233,7 +1285,8 @@ void sqlite3GenerateConstraintChecks(
}else{
zConsName = 0;
}
- sqlite3HaltConstraint(pParse, onError, zConsName, P4_DYNAMIC);
+ sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK,
+ onError, zConsName, P4_DYNAMIC);
}
sqlite3VdbeResolveLabel(v, allOk);
}
@@ -1264,8 +1317,8 @@ void sqlite3GenerateConstraintChecks(
case OE_Rollback:
case OE_Abort:
case OE_Fail: {
- sqlite3HaltConstraint(
- pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
+ sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_PRIMARYKEY,
+ onError, "PRIMARY KEY must be unique", P4_STATIC);
break;
}
case OE_Replace: {
@@ -1392,7 +1445,8 @@ void sqlite3GenerateConstraintChecks(
sqlite3StrAccumAppend(&errMsg,
pIdx->nColumn>1 ? " are not unique" : " is not unique", -1);
zErr = sqlite3StrAccumFinish(&errMsg);
- sqlite3HaltConstraint(pParse, onError, zErr, 0);
+ sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_UNIQUE,
+ onError, zErr, 0);
sqlite3DbFree(errMsg.db, zErr);
break;
}
@@ -1691,7 +1745,7 @@ static int xferOptimization(
** we have to check the semantics.
*/
pItem = pSelect->pSrc->a;
- pSrc = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase);
+ pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
if( pSrc==0 ){
return 0; /* FROM clause does not contain a real table */
}
@@ -1800,8 +1854,8 @@ static int xferOptimization(
if( pDest->iPKey>=0 ){
addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
- sqlite3HaltConstraint(
- pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
+ sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_PRIMARYKEY,
+ onError, "PRIMARY KEY must be unique", P4_STATIC);
sqlite3VdbeJumpHere(v, addr2);
autoIncStep(pParse, regAutoinc, regRowid);
}else if( pDest->pIndex==0 ){
diff --git a/src/journal.c b/src/journal.c
index 2f9e222..fed27be 100644
--- a/src/journal.c
+++ b/src/journal.c
@@ -59,6 +59,14 @@ static int createFile(JournalFile *p){
assert(p->iSize<=p->nBuf);
rc = sqlite3OsWrite(p->pReal, p->zBuf, p->iSize, 0);
}
+ if( rc!=SQLITE_OK ){
+ /* If an error occurred while writing to the file, close it before
+ ** returning. This way, SQLite uses the in-memory journal data to
+ ** roll back changes made to the internal page-cache before this
+ ** function was called. */
+ sqlite3OsClose(pReal);
+ p->pReal = 0;
+ }
}
}
return rc;
@@ -228,6 +236,16 @@ int sqlite3JournalCreate(sqlite3_file *p){
return createFile((JournalFile *)p);
}
+/*
+** The file-handle passed as the only argument is guaranteed to be an open
+** file. It may or may not be of class JournalFile. If the file is a
+** JournalFile, and the underlying file on disk has not yet been opened,
+** return 0. Otherwise, return 1.
+*/
+int sqlite3JournalExists(sqlite3_file *p){
+ return (p->pMethods!=&JournalFileMethods || ((JournalFile *)p)->pReal!=0);
+}
+
/*
** Return the number of bytes required to store a JournalFile that uses vfs
** pVfs to create the underlying on-disk files.
diff --git a/src/legacy.c b/src/legacy.c
index ebab2de..94649ae 100644
--- a/src/legacy.c
+++ b/src/legacy.c
@@ -38,7 +38,6 @@ int sqlite3_exec(
const char *zLeftover; /* Tail of unprocessed SQL */
sqlite3_stmt *pStmt = 0; /* The current SQL statement */
char **azCols = 0; /* Names of result columns */
- int nRetry = 0; /* Number of retry attempts */
int callbackIsInit; /* True if callback data is initialized */
if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
@@ -46,12 +45,12 @@ int sqlite3_exec(
sqlite3_mutex_enter(db->mutex);
sqlite3Error(db, SQLITE_OK, 0);
- while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
+ while( rc==SQLITE_OK && zSql[0] ){
int nCol;
char **azVals = 0;
pStmt = 0;
- rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
+ rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
assert( rc==SQLITE_OK || pStmt==0 );
if( rc!=SQLITE_OK ){
continue;
@@ -108,11 +107,8 @@ int sqlite3_exec(
if( rc!=SQLITE_ROW ){
rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
pStmt = 0;
- if( rc!=SQLITE_SCHEMA ){
- nRetry = 0;
- zSql = zLeftover;
- while( sqlite3Isspace(zSql[0]) ) zSql++;
- }
+ zSql = zLeftover;
+ while( sqlite3Isspace(zSql[0]) ) zSql++;
break;
}
}
diff --git a/src/lempar.c b/src/lempar.c
index cb6025e..2afaa6c 100644
--- a/src/lempar.c
+++ b/src/lempar.c
@@ -608,6 +608,7 @@ static void yy_reduce(
*/
%%
};
+ assert( yyruleno>=0 && yyruleno<sizeof(yyRuleInfo)/sizeof(yyRuleInfo[0]) );
yygoto = yyRuleInfo[yyruleno].lhs;
yysize = yyRuleInfo[yyruleno].nrhs;
yypParser->yyidx -= yysize;
diff --git a/src/loadext.c b/src/loadext.c
index 3fcf500..cdcf6a9 100644
--- a/src/loadext.c
+++ b/src/loadext.c
@@ -378,6 +378,19 @@ static const sqlite3_api_routines sqlite3Apis = {
sqlite3_blob_reopen,
sqlite3_vtab_config,
sqlite3_vtab_on_conflict,
+ sqlite3_close_v2,
+ sqlite3_db_filename,
+ sqlite3_db_readonly,
+ sqlite3_db_release_memory,
+ sqlite3_errstr,
+ sqlite3_stmt_busy,
+ sqlite3_stmt_readonly,
+ sqlite3_stricmp,
+ sqlite3_uri_boolean,
+ sqlite3_uri_int64,
+ sqlite3_uri_parameter,
+ sqlite3_vsnprintf,
+ sqlite3_wal_checkpoint_v2
};
/*
@@ -402,8 +415,23 @@ static int sqlite3LoadExtension(
void *handle;
int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
char *zErrmsg = 0;
+ const char *zEntry;
+ char *zAltEntry = 0;
void **aHandle;
int nMsg = 300 + sqlite3Strlen30(zFile);
+ int ii;
+
+ /* Shared library endings to try if zFile cannot be loaded as written */
+ static const char *azEndings[] = {
+#if SQLITE_OS_WIN
+ "dll"
+#elif defined(__APPLE__)
+ "dylib"
+#else
+ "so"
+#endif
+ };
+
if( pzErrMsg ) *pzErrMsg = 0;
@@ -420,11 +448,17 @@ static int sqlite3LoadExtension(
return SQLITE_ERROR;
}
- if( zProc==0 ){
- zProc = "sqlite3_extension_init";
- }
+ zEntry = zProc ? zProc : "sqlite3_extension_init";
handle = sqlite3OsDlOpen(pVfs, zFile);
+#if SQLITE_OS_UNIX || SQLITE_OS_WIN
+ for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
+ char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
+ if( zAltFile==0 ) return SQLITE_NOMEM;
+ handle = sqlite3OsDlOpen(pVfs, zAltFile);
+ sqlite3_free(zAltFile);
+ }
+#endif
if( handle==0 ){
if( pzErrMsg ){
*pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
@@ -437,20 +471,57 @@ static int sqlite3LoadExtension(
return SQLITE_ERROR;
}
xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
- sqlite3OsDlSym(pVfs, handle, zProc);
+ sqlite3OsDlSym(pVfs, handle, zEntry);
+
+ /* If no entry point was specified and the default legacy
+ ** entry point name "sqlite3_extension_init" was not found, then
+ ** construct an entry point name "sqlite3_X_init" where the X is
+ ** replaced by the lowercase value of every ASCII alphabetic
+ ** character in the filename after the last "/" upto the first ".",
+ ** and eliding the first three characters if they are "lib".
+ ** Examples:
+ **
+ ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init
+ ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init
+ */
+ if( xInit==0 && zProc==0 ){
+ int iFile, iEntry, c;
+ int ncFile = sqlite3Strlen30(zFile);
+ zAltEntry = sqlite3_malloc(ncFile+30);
+ if( zAltEntry==0 ){
+ sqlite3OsDlClose(pVfs, handle);
+ return SQLITE_NOMEM;
+ }
+ memcpy(zAltEntry, "sqlite3_", 8);
+ for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){}
+ iFile++;
+ if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3;
+ for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
+ if( sqlite3Isalpha(c) ){
+ zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
+ }
+ }
+ memcpy(zAltEntry+iEntry, "_init", 6);
+ zEntry = zAltEntry;
+ xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
+ sqlite3OsDlSym(pVfs, handle, zEntry);
+ }
if( xInit==0 ){
if( pzErrMsg ){
- nMsg += sqlite3Strlen30(zProc);
+ nMsg += sqlite3Strlen30(zEntry);
*pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
if( zErrmsg ){
sqlite3_snprintf(nMsg, zErrmsg,
- "no entry point [%s] in shared library [%s]", zProc,zFile);
+ "no entry point [%s] in shared library [%s]", zEntry, zFile);
sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
}
- sqlite3OsDlClose(pVfs, handle);
}
+ sqlite3OsDlClose(pVfs, handle);
+ sqlite3_free(zAltEntry);
return SQLITE_ERROR;
- }else if( xInit(db, &zErrmsg, &sqlite3Apis) ){
+ }
+ sqlite3_free(zAltEntry);
+ if( xInit(db, &zErrmsg, &sqlite3Apis) ){
if( pzErrMsg ){
*pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
}
diff --git a/src/main.c b/src/main.c
index 16294a6..39f6042 100644
--- a/src/main.c
+++ b/src/main.c
@@ -132,6 +132,13 @@ int sqlite3_initialize(void){
*/
if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
+#ifdef SQLITE_ENABLE_SQLLOG
+ {
+ extern void sqlite3_init_sqllog(void);
+ sqlite3_init_sqllog();
+ }
+#endif
+
/* Make sure the mutex subsystem is initialized. If unable to
** initialize the mutex subsystem, return early with the error.
** If the system is so sick that we are unable to allocate a mutex,
@@ -475,6 +482,33 @@ int sqlite3_config(int op, ...){
break;
}
+ case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
+ sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
+ break;
+ }
+
+#ifdef SQLITE_ENABLE_SQLLOG
+ case SQLITE_CONFIG_SQLLOG: {
+ typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
+ sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
+ sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
+ break;
+ }
+#endif
+
+ case SQLITE_CONFIG_MMAP_SIZE: {
+ sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64);
+ sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64);
+ if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){
+ mxMmap = SQLITE_MAX_MMAP_SIZE;
+ }
+ sqlite3GlobalConfig.mxMmap = mxMmap;
+ if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE;
+ if( szMmap>mxMmap) szMmap = mxMmap;
+ sqlite3GlobalConfig.szMmap = szMmap;
+ break;
+ }
+
default: {
rc = SQLITE_ERROR;
break;
@@ -814,6 +848,13 @@ static int sqlite3Close(sqlite3 *db, int forceZombie){
return SQLITE_BUSY;
}
+#ifdef SQLITE_ENABLE_SQLLOG
+ if( sqlite3GlobalConfig.xSqllog ){
+ /* Closing the handle. Fourth parameter is passed the value 2. */
+ sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2);
+ }
+#endif
+
/* Convert the connection into a zombie and then close it.
*/
db->magic = SQLITE_MAGIC_ZOMBIE;
@@ -857,10 +898,16 @@ void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
/* If we reach this point, it means that the database connection has
** closed all sqlite3_stmt and sqlite3_backup objects and has been
- ** pased to sqlite3_close (meaning that it is a zombie). Therefore,
+ ** passed to sqlite3_close (meaning that it is a zombie). Therefore,
** go ahead and free all resources.
*/
+ /* If a transaction is open, roll it back. This also ensures that if
+ ** any database schemas have been modified by an uncommitted transaction
+ ** they are reset. And that the required b-tree mutex is held to make
+ ** the pager rollback and schema reset an atomic operation. */
+ sqlite3RollbackAll(db, SQLITE_OK);
+
/* Free any outstanding Savepoint structures. */
sqlite3CloseSavepoints(db);
@@ -961,6 +1008,15 @@ void sqlite3RollbackAll(sqlite3 *db, int tripCode){
int inTrans = 0;
assert( sqlite3_mutex_held(db->mutex) );
sqlite3BeginBenignMalloc();
+
+ /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
+ ** This is important in case the transaction being rolled back has
+ ** modified the database schema. If the b-tree mutexes are not taken
+ ** here, then another shared-cache connection might sneak in between
+ ** the database rollback and schema reset, which can cause false
+ ** corruption reports in some cases. */
+ sqlite3BtreeEnterAll(db);
+
for(i=0; i<db->nDb; i++){
Btree *p = db->aDb[i].pBt;
if( p ){
@@ -974,10 +1030,11 @@ void sqlite3RollbackAll(sqlite3 *db, int tripCode){
sqlite3VtabRollback(db);
sqlite3EndBenignMalloc();
- if( db->flags&SQLITE_InternChanges ){
+ if( (db->flags&SQLITE_InternChanges)!=0 && db->init.busy==0 ){
sqlite3ExpirePreparedStatements(db);
sqlite3ResetAllSchemasOfConnection(db);
}
+ sqlite3BtreeLeaveAll(db);
/* Any deferred constraint violations have now been resolved. */
db->nDeferredCons = 0;
@@ -989,6 +1046,110 @@ void sqlite3RollbackAll(sqlite3 *db, int tripCode){
}
/*
+** Return a static string containing the name corresponding to the error code
+** specified in the argument.
+*/
+#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) || \
+ defined(SQLITE_DEBUG_OS_TRACE)
+const char *sqlite3ErrName(int rc){
+ const char *zName = 0;
+ int i, origRc = rc;
+ for(i=0; i<2 && zName==0; i++, rc &= 0xff){
+ switch( rc ){
+ case SQLITE_OK: zName = "SQLITE_OK"; break;
+ case SQLITE_ERROR: zName = "SQLITE_ERROR"; break;
+ case SQLITE_INTERNAL: zName = "SQLITE_INTERNAL"; break;
+ case SQLITE_PERM: zName = "SQLITE_PERM"; break;
+ case SQLITE_ABORT: zName = "SQLITE_ABORT"; break;
+ case SQLITE_ABORT_ROLLBACK: zName = "SQLITE_ABORT_ROLLBACK"; break;
+ case SQLITE_BUSY: zName = "SQLITE_BUSY"; break;
+ case SQLITE_BUSY_RECOVERY: zName = "SQLITE_BUSY_RECOVERY"; break;
+ case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break;
+ case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break;
+ case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break;
+ case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
+ case SQLITE_READONLY_RECOVERY: zName = "SQLITE_READONLY_RECOVERY"; break;
+ case SQLITE_READONLY_CANTLOCK: zName = "SQLITE_READONLY_CANTLOCK"; break;
+ case SQLITE_READONLY_ROLLBACK: zName = "SQLITE_READONLY_ROLLBACK"; break;
+ case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
+ case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
+ case SQLITE_IOERR_READ: zName = "SQLITE_IOERR_READ"; break;
+ case SQLITE_IOERR_SHORT_READ: zName = "SQLITE_IOERR_SHORT_READ"; break;
+ case SQLITE_IOERR_WRITE: zName = "SQLITE_IOERR_WRITE"; break;
+ case SQLITE_IOERR_FSYNC: zName = "SQLITE_IOERR_FSYNC"; break;
+ case SQLITE_IOERR_DIR_FSYNC: zName = "SQLITE_IOERR_DIR_FSYNC"; break;
+ case SQLITE_IOERR_TRUNCATE: zName = "SQLITE_IOERR_TRUNCATE"; break;
+ case SQLITE_IOERR_FSTAT: zName = "SQLITE_IOERR_FSTAT"; break;
+ case SQLITE_IOERR_UNLOCK: zName = "SQLITE_IOERR_UNLOCK"; break;
+ case SQLITE_IOERR_RDLOCK: zName = "SQLITE_IOERR_RDLOCK"; break;
+ case SQLITE_IOERR_DELETE: zName = "SQLITE_IOERR_DELETE"; break;
+ case SQLITE_IOERR_BLOCKED: zName = "SQLITE_IOERR_BLOCKED"; break;
+ case SQLITE_IOERR_NOMEM: zName = "SQLITE_IOERR_NOMEM"; break;
+ case SQLITE_IOERR_ACCESS: zName = "SQLITE_IOERR_ACCESS"; break;
+ case SQLITE_IOERR_CHECKRESERVEDLOCK:
+ zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
+ case SQLITE_IOERR_LOCK: zName = "SQLITE_IOERR_LOCK"; break;
+ case SQLITE_IOERR_CLOSE: zName = "SQLITE_IOERR_CLOSE"; break;
+ case SQLITE_IOERR_DIR_CLOSE: zName = "SQLITE_IOERR_DIR_CLOSE"; break;
+ case SQLITE_IOERR_SHMOPEN: zName = "SQLITE_IOERR_SHMOPEN"; break;
+ case SQLITE_IOERR_SHMSIZE: zName = "SQLITE_IOERR_SHMSIZE"; break;
+ case SQLITE_IOERR_SHMLOCK: zName = "SQLITE_IOERR_SHMLOCK"; break;
+ case SQLITE_IOERR_SHMMAP: zName = "SQLITE_IOERR_SHMMAP"; break;
+ case SQLITE_IOERR_SEEK: zName = "SQLITE_IOERR_SEEK"; break;
+ case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break;
+ case SQLITE_IOERR_MMAP: zName = "SQLITE_IOERR_MMAP"; break;
+ case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
+ case SQLITE_CORRUPT_VTAB: zName = "SQLITE_CORRUPT_VTAB"; break;
+ case SQLITE_NOTFOUND: zName = "SQLITE_NOTFOUND"; break;
+ case SQLITE_FULL: zName = "SQLITE_FULL"; break;
+ case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
+ case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
+ case SQLITE_CANTOPEN_ISDIR: zName = "SQLITE_CANTOPEN_ISDIR"; break;
+ case SQLITE_CANTOPEN_FULLPATH: zName = "SQLITE_CANTOPEN_FULLPATH"; break;
+ case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
+ case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
+ case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break;
+ case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break;
+ case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
+ case SQLITE_CONSTRAINT_UNIQUE: zName = "SQLITE_CONSTRAINT_UNIQUE"; break;
+ case SQLITE_CONSTRAINT_TRIGGER: zName = "SQLITE_CONSTRAINT_TRIGGER";break;
+ case SQLITE_CONSTRAINT_FOREIGNKEY:
+ zName = "SQLITE_CONSTRAINT_FOREIGNKEY"; break;
+ case SQLITE_CONSTRAINT_CHECK: zName = "SQLITE_CONSTRAINT_CHECK"; break;
+ case SQLITE_CONSTRAINT_PRIMARYKEY:
+ zName = "SQLITE_CONSTRAINT_PRIMARYKEY"; break;
+ case SQLITE_CONSTRAINT_NOTNULL: zName = "SQLITE_CONSTRAINT_NOTNULL";break;
+ case SQLITE_CONSTRAINT_COMMITHOOK:
+ zName = "SQLITE_CONSTRAINT_COMMITHOOK"; break;
+ case SQLITE_CONSTRAINT_VTAB: zName = "SQLITE_CONSTRAINT_VTAB"; break;
+ case SQLITE_CONSTRAINT_FUNCTION:
+ zName = "SQLITE_CONSTRAINT_FUNCTION"; break;
+ case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break;
+ case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break;
+ case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break;
+ case SQLITE_AUTH: zName = "SQLITE_AUTH"; break;
+ case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break;
+ case SQLITE_RANGE: zName = "SQLITE_RANGE"; break;
+ case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break;
+ case SQLITE_ROW: zName = "SQLITE_ROW"; break;
+ case SQLITE_NOTICE: zName = "SQLITE_NOTICE"; break;
+ case SQLITE_NOTICE_RECOVER_WAL: zName = "SQLITE_NOTICE_RECOVER_WAL";break;
+ case SQLITE_NOTICE_RECOVER_ROLLBACK:
+ zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break;
+ case SQLITE_WARNING: zName = "SQLITE_WARNING"; break;
+ case SQLITE_DONE: zName = "SQLITE_DONE"; break;
+ }
+ }
+ if( zName==0 ){
+ static char zBuf[50];
+ sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc);
+ zName = zBuf;
+ }
+ return zName;
+}
+#endif
+
+/*
** Return a static string that describes the kind of error specified in the
** argument.
*/
@@ -1116,6 +1277,7 @@ int sqlite3_busy_handler(
db->busyHandler.xFunc = xBusy;
db->busyHandler.pArg = pArg;
db->busyHandler.nBusy = 0;
+ db->busyTimeout = 0;
sqlite3_mutex_leave(db->mutex);
return SQLITE_OK;
}
@@ -1153,8 +1315,8 @@ void sqlite3_progress_handler(
*/
int sqlite3_busy_timeout(sqlite3 *db, int ms){
if( ms>0 ){
- db->busyTimeout = ms;
sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
+ db->busyTimeout = ms;
}else{
sqlite3_busy_handler(db, 0, 0);
}
@@ -1768,6 +1930,15 @@ int sqlite3_extended_errcode(sqlite3 *db){
}
/*
+** Return a string that describes the kind of error specified in the
+** argument. For now, this simply calls the internal sqlite3ErrStr()
+** function.
+*/
+const char *sqlite3_errstr(int rc){
+ return sqlite3ErrStr(rc);
+}
+
+/*
** Create a new collating function for database "db". The name is zName
** and the encoding is enc.
*/
@@ -2278,6 +2449,7 @@ static int openDatabase(
memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
db->autoCommit = 1;
db->nextAutovac = -1;
+ db->szMmap = sqlite3GlobalConfig.szMmap;
db->nextPagesize = 0;
db->flags |= SQLITE_ShortColNames | SQLITE_AutoIndex | SQLITE_EnableTrigger
#if SQLITE_DEFAULT_FILE_FORMAT<4
@@ -2436,6 +2608,13 @@ opendb_out:
db->magic = SQLITE_MAGIC_SICK;
}
*ppDb = db;
+#ifdef SQLITE_ENABLE_SQLLOG
+ if( sqlite3GlobalConfig.xSqllog ){
+ /* Opening a db handle. Fourth parameter is passed 0. */
+ void *pArg = sqlite3GlobalConfig.pSqllogArg;
+ sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
+ }
+#endif
return sqlite3ApiExit(0, rc);
}
@@ -2741,7 +2920,7 @@ int sqlite3_table_column_metadata(
zDataType = pCol->zType;
zCollSeq = pCol->zColl;
notnull = pCol->notNull!=0;
- primarykey = pCol->isPrimKey!=0;
+ primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
}else{
zDataType = "INTEGER";
@@ -3004,8 +3183,7 @@ int sqlite3_test_control(int op, ...){
*/
case SQLITE_TESTCTRL_OPTIMIZATIONS: {
sqlite3 *db = va_arg(ap, sqlite3*);
- int x = va_arg(ap,int);
- db->flags = (x & SQLITE_OptMask) | (db->flags & ~SQLITE_OptMask);
+ db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
break;
}
diff --git a/src/memjournal.c b/src/memjournal.c
index 3e66e21..0572594 100644
--- a/src/memjournal.c
+++ b/src/memjournal.c
@@ -230,7 +230,9 @@ static const struct sqlite3_io_methods MemJournalMethods = {
0, /* xShmMap */
0, /* xShmLock */
0, /* xShmBarrier */
- 0 /* xShmUnlock */
+ 0, /* xShmUnmap */
+ 0, /* xFetch */
+ 0 /* xUnfetch */
};
/*
diff --git a/src/os.c b/src/os.c
index b5e918a..be2ea4c 100644
--- a/src/os.c
+++ b/src/os.c
@@ -141,6 +141,26 @@ int sqlite3OsShmMap(
return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
}
+#if SQLITE_MAX_MMAP_SIZE>0
+/* The real implementation of xFetch and xUnfetch */
+int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
+ DO_OS_MALLOC_TEST(id);
+ return id->pMethods->xFetch(id, iOff, iAmt, pp);
+}
+int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
+ return id->pMethods->xUnfetch(id, iOff, p);
+}
+#else
+/* No-op stubs to use when memory-mapped I/O is disabled */
+int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
+ *pp = 0;
+ return SQLITE_OK;
+}
+int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
+ return SQLITE_OK;
+}
+#endif
+
/*
** The next group of routines are convenience wrappers around the
** VFS methods.
diff --git a/src/os.h b/src/os.h
index 1ec7d4b..070a2dd 100644
--- a/src/os.h
+++ b/src/os.h
@@ -99,14 +99,6 @@
# define SQLITE_OS_WINRT 0
#endif
-/*
-** When compiled for WinCE or WinRT, there is no concept of the current
-** directory.
- */
-#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
-# define SQLITE_CURDIR 1
-#endif
-
/* If the SET_FULLSYNC macro is not defined above, then make it
** a no-op
*/
@@ -259,6 +251,8 @@ int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
void sqlite3OsShmBarrier(sqlite3_file *id);
int sqlite3OsShmUnmap(sqlite3_file *id, int);
+int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
+int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
/*
diff --git a/src/os_unix.c b/src/os_unix.c
index c0df66e..abc23a4 100644
--- a/src/os_unix.c
+++ b/src/os_unix.c
@@ -46,6 +46,13 @@
#include "sqliteInt.h"
#if SQLITE_OS_UNIX /* This file is used on unix only */
+/* Use posix_fallocate() if it is available
+*/
+#if !defined(HAVE_POSIX_FALLOCATE) \
+ && (_XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L)
+# define HAVE_POSIX_FALLOCATE 1
+#endif
+
/*
** There are various methods for file locking used for concurrency
** control:
@@ -119,7 +126,7 @@
#include <time.h>
#include <sys/time.h>
#include <errno.h>
-#ifndef SQLITE_OMIT_WAL
+#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
#include <sys/mman.h>
#endif
@@ -218,6 +225,15 @@ struct unixFile {
const char *zPath; /* Name of the file */
unixShm *pShm; /* Shared memory segment information */
int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
+ int nFetchOut; /* Number of outstanding xFetch refs */
+ sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */
+ sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */
+ sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
+ void *pMapRegion; /* Memory mapped region */
+#ifdef __QNXNTO__
+ int sectorSize; /* Device sector size */
+ int deviceCharacteristics; /* Precomputed device characteristics */
+#endif
#if SQLITE_ENABLE_LOCKING_STYLE
int openFlags; /* The flags specified at open() */
#endif
@@ -238,7 +254,9 @@ struct unixFile {
unsigned char transCntrChng; /* True if the transaction counter changed */
unsigned char dbUpdate; /* True if any part of database file changed */
unsigned char inNormalWrite; /* True if in a normal write operation */
+
#endif
+
#ifdef SQLITE_TEST
/* In test mode, increase the size of this structure a bit so that
** it is larger than the struct CrashFile defined in test6.c.
@@ -262,6 +280,7 @@ struct unixFile {
#define UNIXFILE_DELETE 0x20 /* Delete on close */
#define UNIXFILE_URI 0x40 /* Filename might have query parameters */
#define UNIXFILE_NOLOCK 0x80 /* Do no file locking */
+#define UNIXFILE_WARNED 0x0100 /* verifyDbFile() warnings have been issued */
/*
** Include code that is common to all os_*.c files
@@ -296,6 +315,17 @@ struct unixFile {
#endif
/*
+** HAVE_MREMAP defaults to true on Linux and false everywhere else.
+*/
+#if !defined(HAVE_MREMAP)
+# if defined(__linux__) && defined(_GNU_SOURCE)
+# define HAVE_MREMAP 1
+# else
+# define HAVE_MREMAP 0
+# endif
+#endif
+
+/*
** Different Unix systems declare open() in different ways. Same use
** open(const char*,int,mode_t). Others use open(const char*,int,...).
** The difference is important when using a pointer to the function.
@@ -326,7 +356,7 @@ static int openDirectory(const char*, int*);
** to all overrideable system calls.
*/
static struct unix_syscall {
- const char *zName; /* Name of the sytem call */
+ const char *zName; /* Name of the system call */
sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
sqlite3_syscall_ptr pDefault; /* Default value */
} aSyscall[] = {
@@ -401,11 +431,7 @@ static struct unix_syscall {
#define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off_t))\
aSyscall[13].pCurrent)
-#if SQLITE_ENABLE_LOCKING_STYLE
{ "fchmod", (sqlite3_syscall_ptr)fchmod, 0 },
-#else
- { "fchmod", (sqlite3_syscall_ptr)0, 0 },
-#endif
#define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent)
#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
@@ -430,8 +456,18 @@ static struct unix_syscall {
{ "fchown", (sqlite3_syscall_ptr)posixFchown, 0 },
#define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
- { "umask", (sqlite3_syscall_ptr)umask, 0 },
-#define osUmask ((mode_t(*)(mode_t))aSyscall[21].pCurrent)
+ { "mmap", (sqlite3_syscall_ptr)mmap, 0 },
+#define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[21].pCurrent)
+
+ { "munmap", (sqlite3_syscall_ptr)munmap, 0 },
+#define osMunmap ((void*(*)(void*,size_t))aSyscall[22].pCurrent)
+
+#if HAVE_MREMAP
+ { "mremap", (sqlite3_syscall_ptr)mremap, 0 },
+#else
+ { "mremap", (sqlite3_syscall_ptr)0, 0 },
+#endif
+#define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
}; /* End of the overrideable system calls */
@@ -537,14 +573,7 @@ static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
*/
static int robust_open(const char *z, int f, mode_t m){
int fd;
- mode_t m2;
- mode_t origM = 0;
- if( m==0 ){
- m2 = SQLITE_DEFAULT_FILE_PERMISSIONS;
- }else{
- m2 = m;
- origM = osUmask(0);
- }
+ mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
do{
#if defined(O_CLOEXEC)
fd = osOpen(z,f|O_CLOEXEC,m2);
@@ -552,12 +581,20 @@ static int robust_open(const char *z, int f, mode_t m){
fd = osOpen(z,f,m2);
#endif
}while( fd<0 && errno==EINTR );
- if( m ){
- osUmask(origM);
- }
+ if( fd>=0 ){
+ if( m!=0 ){
+ struct stat statbuf;
+ if( osFstat(fd, &statbuf)==0
+ && statbuf.st_size==0
+ && (statbuf.st_mode&0777)!=m
+ ){
+ osFchmod(fd, m);
+ }
+ }
#if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
- if( fd>=0 ) osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
+ osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
#endif
+ }
return fd;
}
@@ -763,7 +800,6 @@ static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
}
-
/******************************************************************************
****************** Begin Unique File ID Utility Used By VxWorks ***************
**
@@ -1099,7 +1135,6 @@ static int unixLogErrorAtLine(
zErr = strerror(iErrno);
#endif
- assert( errcode!=SQLITE_OK );
if( zPath==0 ) zPath = "";
sqlite3_log(errcode,
"os_unix.c:%d: (%d) %s(%s) - %s",
@@ -1266,6 +1301,50 @@ static int findInodeInfo(
/*
+** Check a unixFile that is a database. Verify the following:
+**
+** (1) There is exactly one hard link on the file
+** (2) The file is not a symbolic link
+** (3) The file has not been renamed or unlinked
+**
+** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
+*/
+static void verifyDbFile(unixFile *pFile){
+ struct stat buf;
+ int rc;
+ if( pFile->ctrlFlags & UNIXFILE_WARNED ){
+ /* One or more of the following warnings have already been issued. Do not
+ ** repeat them so as not to clutter the error log */
+ return;
+ }
+ rc = osFstat(pFile->h, &buf);
+ if( rc!=0 ){
+ sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
+ pFile->ctrlFlags |= UNIXFILE_WARNED;
+ return;
+ }
+ if( buf.st_nlink==0 && (pFile->ctrlFlags & UNIXFILE_DELETE)==0 ){
+ sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
+ pFile->ctrlFlags |= UNIXFILE_WARNED;
+ return;
+ }
+ if( buf.st_nlink>1 ){
+ sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
+ pFile->ctrlFlags |= UNIXFILE_WARNED;
+ return;
+ }
+ if( pFile->pInode!=0
+ && ((rc = osStat(pFile->zPath, &buf))!=0
+ || buf.st_ino!=pFile->pInode->fileId.ino)
+ ){
+ sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
+ pFile->ctrlFlags |= UNIXFILE_WARNED;
+ return;
+ }
+}
+
+
+/*
** This routine checks if there is a RESERVED lock held on the specified
** file by this or any other process. If such a lock is held, set *pResOut
** to a non-zero value otherwise *pResOut is set to zero. The return value
@@ -1795,9 +1874,13 @@ end_unlock:
** the requested locking level, this routine is a no-op.
*/
static int unixUnlock(sqlite3_file *id, int eFileLock){
+ assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
return posixUnlock(id, eFileLock, 0);
}
+static int unixMapfile(unixFile *pFd, i64 nByte);
+static void unixUnmapfile(unixFile *pFd);
+
/*
** This function performs the parts of the "close file" operation
** common to all locking schemes. It closes the directory and file
@@ -1810,6 +1893,7 @@ static int unixUnlock(sqlite3_file *id, int eFileLock){
*/
static int closeUnixFile(sqlite3_file *id){
unixFile *pFile = (unixFile*)id;
+ unixUnmapfile(pFile);
if( pFile->h>=0 ){
robust_close(pFile, pFile->h, __LINE__);
pFile->h = -1;
@@ -1836,6 +1920,7 @@ static int closeUnixFile(sqlite3_file *id){
static int unixClose(sqlite3_file *id){
int rc = SQLITE_OK;
unixFile *pFile = (unixFile *)id;
+ verifyDbFile(pFile);
unixUnlock(id, NO_LOCK);
unixEnterMutex();
@@ -1904,7 +1989,7 @@ static int nolockClose(sqlite3_file *id) {
/******************************************************************************
************************* Begin dot-file Locking ******************************
**
-** The dotfile locking implementation uses the existance of separate lock
+** The dotfile locking implementation uses the existence of separate lock
** files (really a directory) to control access to the database. This works
** on just about every filesystem imaginable. But there are serious downsides:
**
@@ -1919,7 +2004,7 @@ static int nolockClose(sqlite3_file *id) {
**
** Dotfile locking works by creating a subdirectory in the same directory as
** the database and with the same name but with a ".lock" extension added.
-** The existance of a lock directory implies an EXCLUSIVE lock. All other
+** The existence of a lock directory implies an EXCLUSIVE lock. All other
** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
*/
@@ -2086,13 +2171,13 @@ static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
** Close a file. Make sure the lock has been released before closing.
*/
static int dotlockClose(sqlite3_file *id) {
- int rc;
+ int rc = SQLITE_OK;
if( id ){
unixFile *pFile = (unixFile*)id;
dotlockUnlock(id, NO_LOCK);
sqlite3_free(pFile->lockingContext);
+ rc = closeUnixFile(id);
}
- rc = closeUnixFile(id);
return rc;
}
/****************** End of the dot-file lock implementation *******************
@@ -2296,10 +2381,12 @@ static int flockUnlock(sqlite3_file *id, int eFileLock) {
** Close a file.
*/
static int flockClose(sqlite3_file *id) {
+ int rc = SQLITE_OK;
if( id ){
flockUnlock(id, NO_LOCK);
+ rc = closeUnixFile(id);
}
- return closeUnixFile(id);
+ return rc;
}
#endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
@@ -3010,6 +3097,8 @@ static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
i64 newOffset;
#endif
TIMER_START;
+ assert( cnt==(cnt&0x1ffff) );
+ cnt &= 0x1ffff;
do{
#if defined(USE_PREAD)
got = osPread(id->h, pBuf, cnt, offset);
@@ -3063,6 +3152,8 @@ static int unixRead(
unixFile *pFile = (unixFile *)id;
int got;
assert( id );
+ assert( offset>=0 );
+ assert( amt>0 );
/* If this is a database file (not a journal, master-journal or temp
** file), the bytes in the locking range should never be read or written. */
@@ -3073,6 +3164,23 @@ static int unixRead(
);
#endif
+#if SQLITE_MAX_MMAP_SIZE>0
+ /* Deal with as much of this read request as possible by transfering
+ ** data from the memory mapping using memcpy(). */
+ if( offset<pFile->mmapSize ){
+ if( offset+amt <= pFile->mmapSize ){
+ memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
+ return SQLITE_OK;
+ }else{
+ int nCopy = pFile->mmapSize - offset;
+ memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
+ pBuf = &((u8 *)pBuf)[nCopy];
+ amt -= nCopy;
+ offset += nCopy;
+ }
+ }
+#endif
+
got = seekAndRead(pFile, offset, pBuf, amt);
if( got==amt ){
return SQLITE_OK;
@@ -3088,44 +3196,59 @@ static int unixRead(
}
/*
-** Seek to the offset in id->offset then read cnt bytes into pBuf.
-** Return the number of bytes actually read. Update the offset.
-**
-** To avoid stomping the errno value on a failed write the lastErrno value
-** is set before returning.
+** Attempt to seek the file-descriptor passed as the first argument to
+** absolute offset iOff, then attempt to write nBuf bytes of data from
+** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
+** return the actual number of bytes written (which may be less than
+** nBuf).
*/
-static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
- int got;
-#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
- i64 newOffset;
-#endif
+static int seekAndWriteFd(
+ int fd, /* File descriptor to write to */
+ i64 iOff, /* File offset to begin writing at */
+ const void *pBuf, /* Copy data from this buffer to the file */
+ int nBuf, /* Size of buffer pBuf in bytes */
+ int *piErrno /* OUT: Error number if error occurs */
+){
+ int rc = 0; /* Value returned by system call */
+
+ assert( nBuf==(nBuf&0x1ffff) );
+ nBuf &= 0x1ffff;
TIMER_START;
+
#if defined(USE_PREAD)
- do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
+ do{ rc = osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
#elif defined(USE_PREAD64)
- do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
+ do{ rc = osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR);
#else
do{
- newOffset = lseek(id->h, offset, SEEK_SET);
- SimulateIOError( newOffset-- );
- if( newOffset!=offset ){
- if( newOffset == -1 ){
- ((unixFile*)id)->lastErrno = errno;
- }else{
- ((unixFile*)id)->lastErrno = 0;
- }
+ i64 iSeek = lseek(fd, iOff, SEEK_SET);
+ SimulateIOError( iSeek-- );
+
+ if( iSeek!=iOff ){
+ if( piErrno ) *piErrno = (iSeek==-1 ? errno : 0);
return -1;
}
- got = osWrite(id->h, pBuf, cnt);
- }while( got<0 && errno==EINTR );
+ rc = osWrite(fd, pBuf, nBuf);
+ }while( rc<0 && errno==EINTR );
#endif
+
TIMER_END;
- if( got<0 ){
- ((unixFile*)id)->lastErrno = errno;
- }
+ OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
- OSTRACE(("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
- return got;
+ if( rc<0 && piErrno ) *piErrno = errno;
+ return rc;
+}
+
+
+/*
+** Seek to the offset in id->offset then read cnt bytes into pBuf.
+** Return the number of bytes actually read. Update the offset.
+**
+** To avoid stomping the errno value on a failed write the lastErrno value
+** is set before returning.
+*/
+static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
+ return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
}
@@ -3175,6 +3298,23 @@ static int unixWrite(
}
#endif
+#if SQLITE_MAX_MMAP_SIZE>0
+ /* Deal with as much of this write request as possible by transfering
+ ** data from the memory mapping using memcpy(). */
+ if( offset<pFile->mmapSize ){
+ if( offset+amt <= pFile->mmapSize ){
+ memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
+ return SQLITE_OK;
+ }else{
+ int nCopy = pFile->mmapSize - offset;
+ memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
+ pBuf = &((u8 *)pBuf)[nCopy];
+ amt -= nCopy;
+ offset += nCopy;
+ }
+ }
+#endif
+
while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){
amt -= wrote;
offset += wrote;
@@ -3402,7 +3542,7 @@ static int unixSync(sqlite3_file *id, int flags){
}
/* Also fsync the directory containing the file if the DIRSYNC flag
- ** is set. This is a one-time occurrance. Many systems (examples: AIX)
+ ** is set. This is a one-time occurrence. Many systems (examples: AIX)
** are unable to fsync a directory, so ignore errors on the fsync.
*/
if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
@@ -3457,6 +3597,14 @@ static int unixTruncate(sqlite3_file *id, i64 nByte){
}
#endif
+ /* If the file was just truncated to a size smaller than the currently
+ ** mapped region, reduce the effective mapping size as well. SQLite will
+ ** use read() and write() to access data beyond this point from now on.
+ */
+ if( nByte<pFile->mmapSize ){
+ pFile->mmapSize = nByte;
+ }
+
return SQLITE_OK;
}
}
@@ -3545,6 +3693,19 @@ static int fcntlSizeHint(unixFile *pFile, i64 nByte){
}
}
+ if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
+ int rc;
+ if( pFile->szChunk<=0 ){
+ if( robust_ftruncate(pFile->h, nByte) ){
+ pFile->lastErrno = errno;
+ return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
+ }
+ }
+
+ rc = unixMapfile(pFile, nByte);
+ return rc;
+ }
+
return SQLITE_OK;
}
@@ -3564,6 +3725,9 @@ static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
}
}
+/* Forward declaration */
+static int unixGetTempname(int nBuf, char *zBuf);
+
/*
** Information and control of an open file handle.
*/
@@ -3601,6 +3765,26 @@ static int unixFileControl(sqlite3_file *id, int op, void *pArg){
*(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
return SQLITE_OK;
}
+ case SQLITE_FCNTL_TEMPFILENAME: {
+ char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname );
+ if( zTFile ){
+ unixGetTempname(pFile->pVfs->mxPathname, zTFile);
+ *(char**)pArg = zTFile;
+ }
+ return SQLITE_OK;
+ }
+ case SQLITE_FCNTL_MMAP_SIZE: {
+ i64 newLimit = *(i64*)pArg;
+ if( newLimit>sqlite3GlobalConfig.mxMmap ){
+ newLimit = sqlite3GlobalConfig.mxMmap;
+ }
+ *(i64*)pArg = pFile->mmapSizeMax;
+ if( newLimit>=0 ){
+ pFile->mmapSizeMax = newLimit;
+ if( newLimit<pFile->mmapSize ) pFile->mmapSize = newLimit;
+ }
+ return SQLITE_OK;
+ }
#ifdef SQLITE_DEBUG
/* The pager calls this method to signal that it has done
** a rollback and that the database is therefore unchanged and
@@ -3632,10 +3816,92 @@ static int unixFileControl(sqlite3_file *id, int op, void *pArg){
** a database and its journal file) that the sector size will be the
** same for both.
*/
-static int unixSectorSize(sqlite3_file *pFile){
- (void)pFile;
+#ifndef __QNXNTO__
+static int unixSectorSize(sqlite3_file *NotUsed){
+ UNUSED_PARAMETER(NotUsed);
return SQLITE_DEFAULT_SECTOR_SIZE;
}
+#endif
+
+/*
+** The following version of unixSectorSize() is optimized for QNX.
+*/
+#ifdef __QNXNTO__
+#include <sys/dcmd_blk.h>
+#include <sys/statvfs.h>
+static int unixSectorSize(sqlite3_file *id){
+ unixFile *pFile = (unixFile*)id;
+ if( pFile->sectorSize == 0 ){
+ struct statvfs fsInfo;
+
+ /* Set defaults for non-supported filesystems */
+ pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
+ pFile->deviceCharacteristics = 0;
+ if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
+ return pFile->sectorSize;
+ }
+
+ if( !strcmp(fsInfo.f_basetype, "tmp") ) {
+ pFile->sectorSize = fsInfo.f_bsize;
+ pFile->deviceCharacteristics =
+ SQLITE_IOCAP_ATOMIC4K | /* All ram filesystem writes are atomic */
+ SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
+ ** the write succeeds */
+ SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
+ ** so it is ordered */
+ 0;
+ }else if( strstr(fsInfo.f_basetype, "etfs") ){
+ pFile->sectorSize = fsInfo.f_bsize;
+ pFile->deviceCharacteristics =
+ /* etfs cluster size writes are atomic */
+ (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
+ SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
+ ** the write succeeds */
+ SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
+ ** so it is ordered */
+ 0;
+ }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
+ pFile->sectorSize = fsInfo.f_bsize;
+ pFile->deviceCharacteristics =
+ SQLITE_IOCAP_ATOMIC | /* All filesystem writes are atomic */
+ SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
+ ** the write succeeds */
+ SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
+ ** so it is ordered */
+ 0;
+ }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
+ pFile->sectorSize = fsInfo.f_bsize;
+ pFile->deviceCharacteristics =
+ /* full bitset of atomics from max sector size and smaller */
+ ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
+ SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
+ ** so it is ordered */
+ 0;
+ }else if( strstr(fsInfo.f_basetype, "dos") ){
+ pFile->sectorSize = fsInfo.f_bsize;
+ pFile->deviceCharacteristics =
+ /* full bitset of atomics from max sector size and smaller */
+ ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
+ SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
+ ** so it is ordered */
+ 0;
+ }else{
+ pFile->deviceCharacteristics =
+ SQLITE_IOCAP_ATOMIC512 | /* blocks are atomic */
+ SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
+ ** the write succeeds */
+ 0;
+ }
+ }
+ /* Last chance verification. If the sector size isn't a multiple of 512
+ ** then it isn't valid.*/
+ if( pFile->sectorSize % 512 != 0 ){
+ pFile->deviceCharacteristics = 0;
+ pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
+ }
+ return pFile->sectorSize;
+}
+#endif /* __QNXNTO__ */
/*
** Return the device characteristics for the file.
@@ -3652,11 +3918,15 @@ static int unixSectorSize(sqlite3_file *pFile){
*/
static int unixDeviceCharacteristics(sqlite3_file *id){
unixFile *p = (unixFile*)id;
+ int rc = 0;
+#ifdef __QNXNTO__
+ if( p->sectorSize==0 ) unixSectorSize(id);
+ rc = p->deviceCharacteristics;
+#endif
if( p->ctrlFlags & UNIXFILE_PSOW ){
- return SQLITE_IOCAP_POWERSAFE_OVERWRITE;
- }else{
- return 0;
+ rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
}
+ return rc;
}
#ifndef SQLITE_OMIT_WAL
@@ -3827,7 +4097,7 @@ static void unixShmPurge(unixFile *pFd){
sqlite3_mutex_free(p->mutex);
for(i=0; i<p->nRegion; i++){
if( p->h>=0 ){
- munmap(p->apRegion[i], p->szRegion);
+ osMunmap(p->apRegion[i], p->szRegion);
}else{
sqlite3_free(p->apRegion[i]);
}
@@ -4067,16 +4337,32 @@ static int unixShmMap(
if( sStat.st_size<nByte ){
/* The requested memory region does not exist. If bExtend is set to
** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
- **
- ** Alternatively, if bExtend is true, use ftruncate() to allocate
- ** the requested memory region.
*/
- if( !bExtend ) goto shmpage_out;
- if( robust_ftruncate(pShmNode->h, nByte) ){
- rc = unixLogError(SQLITE_IOERR_SHMSIZE, "ftruncate",
- pShmNode->zFilename);
+ if( !bExtend ){
goto shmpage_out;
}
+
+ /* Alternatively, if bExtend is true, extend the file. Do this by
+ ** writing a single byte to the end of each (OS) page being
+ ** allocated or extended. Technically, we need only write to the
+ ** last page in order to extend the file. But writing to all new
+ ** pages forces the OS to allocate them immediately, which reduces
+ ** the chances of SIGBUS while accessing the mapped region later on.
+ */
+ else{
+ static const int pgsz = 4096;
+ int iPg;
+
+ /* Write to the last byte of each newly allocated or extended page */
+ assert( (nByte % pgsz)==0 );
+ for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
+ if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, 0)!=1 ){
+ const char *zFile = pShmNode->zFilename;
+ rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile);
+ goto shmpage_out;
+ }
+ }
+ }
}
}
@@ -4092,9 +4378,9 @@ static int unixShmMap(
while(pShmNode->nRegion<=iRegion){
void *pMem;
if( pShmNode->h>=0 ){
- pMem = mmap(0, szRegion,
+ pMem = osMmap(0, szRegion,
pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
- MAP_SHARED, pShmNode->h, pShmNode->nRegion*szRegion
+ MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
);
if( pMem==MAP_FAILED ){
rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
@@ -4310,6 +4596,236 @@ static int unixShmUnmap(
#endif /* #ifndef SQLITE_OMIT_WAL */
/*
+** If it is currently memory mapped, unmap file pFd.
+*/
+static void unixUnmapfile(unixFile *pFd){
+ assert( pFd->nFetchOut==0 );
+#if SQLITE_MAX_MMAP_SIZE>0
+ if( pFd->pMapRegion ){
+ osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
+ pFd->pMapRegion = 0;
+ pFd->mmapSize = 0;
+ pFd->mmapSizeActual = 0;
+ }
+#endif
+}
+
+#if SQLITE_MAX_MMAP_SIZE>0
+/*
+** Return the system page size.
+*/
+static int unixGetPagesize(void){
+#if HAVE_MREMAP
+ return 512;
+#elif defined(_BSD_SOURCE)
+ return getpagesize();
+#else
+ return (int)sysconf(_SC_PAGESIZE);
+#endif
+}
+#endif /* SQLITE_MAX_MMAP_SIZE>0 */
+
+#if SQLITE_MAX_MMAP_SIZE>0
+/*
+** Attempt to set the size of the memory mapping maintained by file
+** descriptor pFd to nNew bytes. Any existing mapping is discarded.
+**
+** If successful, this function sets the following variables:
+**
+** unixFile.pMapRegion
+** unixFile.mmapSize
+** unixFile.mmapSizeActual
+**
+** If unsuccessful, an error message is logged via sqlite3_log() and
+** the three variables above are zeroed. In this case SQLite should
+** continue accessing the database using the xRead() and xWrite()
+** methods.
+*/
+static void unixRemapfile(
+ unixFile *pFd, /* File descriptor object */
+ i64 nNew /* Required mapping size */
+){
+ const char *zErr = "mmap";
+ int h = pFd->h; /* File descriptor open on db file */
+ u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
+ i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
+ u8 *pNew = 0; /* Location of new mapping */
+ int flags = PROT_READ; /* Flags to pass to mmap() */
+
+ assert( pFd->nFetchOut==0 );
+ assert( nNew>pFd->mmapSize );
+ assert( nNew<=pFd->mmapSizeMax );
+ assert( nNew>0 );
+ assert( pFd->mmapSizeActual>=pFd->mmapSize );
+ assert( MAP_FAILED!=0 );
+
+ if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
+
+ if( pOrig ){
+ const int szSyspage = unixGetPagesize();
+ i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
+ u8 *pReq = &pOrig[nReuse];
+
+ /* Unmap any pages of the existing mapping that cannot be reused. */
+ if( nReuse!=nOrig ){
+ osMunmap(pReq, nOrig-nReuse);
+ }
+
+#if HAVE_MREMAP
+ pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE);
+ zErr = "mremap";
+#else
+ pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
+ if( pNew!=MAP_FAILED ){
+ if( pNew!=pReq ){
+ osMunmap(pNew, nNew - nReuse);
+ pNew = 0;
+ }else{
+ pNew = pOrig;
+ }
+ }
+#endif
+
+ /* The attempt to extend the existing mapping failed. Free it. */
+ if( pNew==MAP_FAILED || pNew==0 ){
+ osMunmap(pOrig, nReuse);
+ }
+ }
+
+ /* If pNew is still NULL, try to create an entirely new mapping. */
+ if( pNew==0 ){
+ pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0);
+ }
+
+ if( pNew==MAP_FAILED ){
+ pNew = 0;
+ nNew = 0;
+ unixLogError(SQLITE_OK, zErr, pFd->zPath);
+
+ /* If the mmap() above failed, assume that all subsequent mmap() calls
+ ** will probably fail too. Fall back to using xRead/xWrite exclusively
+ ** in this case. */
+ pFd->mmapSizeMax = 0;
+ }
+ pFd->pMapRegion = (void *)pNew;
+ pFd->mmapSize = pFd->mmapSizeActual = nNew;
+}
+#endif
+
+/*
+** Memory map or remap the file opened by file-descriptor pFd (if the file
+** is already mapped, the existing mapping is replaced by the new). Or, if
+** there already exists a mapping for this file, and there are still
+** outstanding xFetch() references to it, this function is a no-op.
+**
+** If parameter nByte is non-negative, then it is the requested size of
+** the mapping to create. Otherwise, if nByte is less than zero, then the
+** requested size is the size of the file on disk. The actual size of the
+** created mapping is either the requested size or the value configured
+** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
+**
+** SQLITE_OK is returned if no error occurs (even if the mapping is not
+** recreated as a result of outstanding references) or an SQLite error
+** code otherwise.
+*/
+static int unixMapfile(unixFile *pFd, i64 nByte){
+#if SQLITE_MAX_MMAP_SIZE>0
+ i64 nMap = nByte;
+ int rc;
+
+ assert( nMap>=0 || pFd->nFetchOut==0 );
+ if( pFd->nFetchOut>0 ) return SQLITE_OK;
+
+ if( nMap<0 ){
+ struct stat statbuf; /* Low-level file information */
+ rc = osFstat(pFd->h, &statbuf);
+ if( rc!=SQLITE_OK ){
+ return SQLITE_IOERR_FSTAT;
+ }
+ nMap = statbuf.st_size;
+ }
+ if( nMap>pFd->mmapSizeMax ){
+ nMap = pFd->mmapSizeMax;
+ }
+
+ if( nMap!=pFd->mmapSize ){
+ if( nMap>0 ){
+ unixRemapfile(pFd, nMap);
+ }else{
+ unixUnmapfile(pFd);
+ }
+ }
+#endif
+
+ return SQLITE_OK;
+}
+
+/*
+** If possible, return a pointer to a mapping of file fd starting at offset
+** iOff. The mapping must be valid for at least nAmt bytes.
+**
+** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
+** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
+** Finally, if an error does occur, return an SQLite error code. The final
+** value of *pp is undefined in this case.
+**
+** If this function does return a pointer, the caller must eventually
+** release the reference by calling unixUnfetch().
+*/
+static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
+#if SQLITE_MAX_MMAP_SIZE>0
+ unixFile *pFd = (unixFile *)fd; /* The underlying database file */
+#endif
+ *pp = 0;
+
+#if SQLITE_MAX_MMAP_SIZE>0
+ if( pFd->mmapSizeMax>0 ){
+ if( pFd->pMapRegion==0 ){
+ int rc = unixMapfile(pFd, -1);
+ if( rc!=SQLITE_OK ) return rc;
+ }
+ if( pFd->mmapSize >= iOff+nAmt ){
+ *pp = &((u8 *)pFd->pMapRegion)[iOff];
+ pFd->nFetchOut++;
+ }
+ }
+#endif
+ return SQLITE_OK;
+}
+
+/*
+** If the third argument is non-NULL, then this function releases a
+** reference obtained by an earlier call to unixFetch(). The second
+** argument passed to this function must be the same as the corresponding
+** argument that was passed to the unixFetch() invocation.
+**
+** Or, if the third argument is NULL, then this function is being called
+** to inform the VFS layer that, according to POSIX, any existing mapping
+** may now be invalid and should be unmapped.
+*/
+static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
+ unixFile *pFd = (unixFile *)fd; /* The underlying database file */
+ UNUSED_PARAMETER(iOff);
+
+ /* If p==0 (unmap the entire file) then there must be no outstanding
+ ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
+ ** then there must be at least one outstanding. */
+ assert( (p==0)==(pFd->nFetchOut==0) );
+
+ /* If p!=0, it must match the iOff value. */
+ assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
+
+ if( p ){
+ pFd->nFetchOut--;
+ }else{
+ unixUnmapfile(pFd);
+ }
+
+ assert( pFd->nFetchOut>=0 );
+ return SQLITE_OK;
+}
+
+/*
** Here ends the implementation of all sqlite3_file methods.
**
********************** End sqlite3_file Methods *******************************
@@ -4367,7 +4883,9 @@ static const sqlite3_io_methods METHOD = { \
unixShmMap, /* xShmMap */ \
unixShmLock, /* xShmLock */ \
unixShmBarrier, /* xShmBarrier */ \
- unixShmUnmap /* xShmUnmap */ \
+ unixShmUnmap, /* xShmUnmap */ \
+ unixFetch, /* xFetch */ \
+ unixUnfetch, /* xUnfetch */ \
}; \
static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \
UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \
@@ -4384,7 +4902,7 @@ static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \
IOMETHODS(
posixIoFinder, /* Finder function name */
posixIoMethods, /* sqlite3_io_methods object name */
- 2, /* shared memory is enabled */
+ 3, /* shared memory and mmap are enabled */
unixClose, /* xClose method */
unixLock, /* xLock method */
unixUnlock, /* xUnlock method */
@@ -4635,11 +5153,12 @@ static int fillInUnixFile(
pNew->pVfs = pVfs;
pNew->zPath = zFilename;
pNew->ctrlFlags = (u8)ctrlFlags;
+ pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
"psow", SQLITE_POWERSAFE_OVERWRITE) ){
pNew->ctrlFlags |= UNIXFILE_PSOW;
}
- if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
+ if( strcmp(pVfs->zName,"unix-excl")==0 ){
pNew->ctrlFlags |= UNIXFILE_EXCL;
}
@@ -4671,7 +5190,7 @@ static int fillInUnixFile(
unixEnterMutex();
rc = findInodeInfo(pNew, &pNew->pInode);
if( rc!=SQLITE_OK ){
- /* If an error occured in findInodeInfo(), close the file descriptor
+ /* If an error occurred in findInodeInfo(), close the file descriptor
** immediately, before releasing the mutex. findInodeInfo() may fail
** in two scenarios:
**
@@ -4770,15 +5289,15 @@ static int fillInUnixFile(
if( h>=0 ) robust_close(pNew, h, __LINE__);
h = -1;
osUnlink(zFilename);
- isDelete = 0;
+ pNew->ctrlFlags |= UNIXFILE_DELETE;
}
- if( isDelete ) pNew->ctrlFlags |= UNIXFILE_DELETE;
#endif
if( rc!=SQLITE_OK ){
if( h>=0 ) robust_close(pNew, h, __LINE__);
}else{
pNew->pMethod = pLockingStyle;
OpenCounter(+1);
+ verifyDbFile(pNew);
}
return rc;
}
@@ -5278,8 +5797,13 @@ static int unixDelete(
int rc = SQLITE_OK;
UNUSED_PARAMETER(NotUsed);
SimulateIOError(return SQLITE_IOERR_DELETE);
- if( osUnlink(zPath)==(-1) && errno!=ENOENT ){
- return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
+ if( osUnlink(zPath)==(-1) ){
+ if( errno==ENOENT ){
+ rc = SQLITE_IOERR_DELETE_NOENT;
+ }else{
+ rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
+ }
+ return rc;
}
#ifndef SQLITE_DISABLE_DIRSYNC
if( (dirSync & 1)!=0 ){
@@ -5304,7 +5828,7 @@ static int unixDelete(
}
/*
-** Test the existance of or access permissions of file zPath. The
+** Test the existence of or access permissions of file zPath. The
** test performed depends on the value of flags:
**
** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
@@ -6867,7 +7391,7 @@ int sqlite3_os_init(void){
/* Double-check that the aSyscall[] array has been constructed
** correctly. See ticket [bb3a86e890c8e96ab] */
- assert( ArraySize(aSyscall)==22 );
+ assert( ArraySize(aSyscall)==24 );
/* Register all VFSes defined in the aVfs[] array */
for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
diff --git a/src/os_win.c b/src/os_win.c
index cbf17b1..aeb0881 100644
--- a/src/os_win.c
+++ b/src/os_win.c
@@ -25,6 +25,66 @@
#include "os_common.h"
/*
+** Compiling and using WAL mode requires several APIs that are only
+** available in Windows platforms based on the NT kernel.
+*/
+#if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
+# error "WAL mode requires support from the Windows NT kernel, compile\
+ with SQLITE_OMIT_WAL."
+#endif
+
+/*
+** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
+** based on the sub-platform)?
+*/
+#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
+# define SQLITE_WIN32_HAS_ANSI
+#endif
+
+/*
+** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
+** based on the sub-platform)?
+*/
+#if SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT
+# define SQLITE_WIN32_HAS_WIDE
+#endif
+
+/*
+** Do we need to manually define the Win32 file mapping APIs for use with WAL
+** mode (e.g. these APIs are available in the Windows CE SDK; however, they
+** are not present in the header file)?
+*/
+#if SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL)
+/*
+** Two of the file mapping APIs are different under WinRT. Figure out which
+** set we need.
+*/
+#if SQLITE_OS_WINRT
+WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \
+ LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR);
+
+WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T);
+#else
+#if defined(SQLITE_WIN32_HAS_ANSI)
+WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \
+ DWORD, DWORD, DWORD, LPCSTR);
+#endif /* defined(SQLITE_WIN32_HAS_ANSI) */
+
+#if defined(SQLITE_WIN32_HAS_WIDE)
+WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \
+ DWORD, DWORD, DWORD, LPCWSTR);
+#endif /* defined(SQLITE_WIN32_HAS_WIDE) */
+
+WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
+#endif /* SQLITE_OS_WINRT */
+
+/*
+** This file mapping API is common to both Win32 and WinRT.
+*/
+WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
+#endif /* SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) */
+
+/*
** Macro to find the minimum of two numeric values.
*/
#ifndef MIN
@@ -90,11 +150,20 @@ struct winFile {
winceLock local; /* Locks obtained by this instance of winFile */
winceLock *shared; /* Global shared lock memory for the file */
#endif
+#if SQLITE_MAX_MMAP_SIZE>0
+ int nFetchOut; /* Number of outstanding xFetch references */
+ HANDLE hMap; /* Handle for accessing memory mapping */
+ void *pMapRegion; /* Area memory mapped */
+ sqlite3_int64 mmapSize; /* Usable size of mapped region */
+ sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */
+ sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
+#endif
};
/*
** Allowed values for winFile.ctrlFlags
*/
+#define WINFILE_RDONLY 0x02 /* Connection is read only */
#define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
#define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
@@ -229,14 +298,6 @@ int sqlite3_os_type = 0;
static int sqlite3_os_type = 0;
#endif
-#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
-# define SQLITE_WIN32_HAS_ANSI
-#endif
-
-#if SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT
-# define SQLITE_WIN32_HAS_WIDE
-#endif
-
#ifndef SYSCALL
# define SYSCALL sqlite3_syscall_ptr
#endif
@@ -256,7 +317,7 @@ static int sqlite3_os_type = 0;
** to all overrideable system calls.
*/
static struct win_syscall {
- const char *zName; /* Name of the sytem call */
+ const char *zName; /* Name of the system call */
sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
sqlite3_syscall_ptr pDefault; /* Default value */
} aSyscall[] = {
@@ -308,6 +369,16 @@ static struct win_syscall {
#define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
+#if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
+ !defined(SQLITE_OMIT_WAL))
+ { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 },
+#else
+ { "CreateFileMappingA", (SYSCALL)0, 0 },
+#endif
+
+#define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
+ DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
+
#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
!defined(SQLITE_OMIT_WAL))
{ "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 },
@@ -316,7 +387,7 @@ static struct win_syscall {
#endif
#define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
- DWORD,DWORD,DWORD,LPCWSTR))aSyscall[6].pCurrent)
+ DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
{ "CreateMutexW", (SYSCALL)CreateMutexW, 0 },
@@ -325,7 +396,7 @@ static struct win_syscall {
#endif
#define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
- LPCWSTR))aSyscall[7].pCurrent)
+ LPCWSTR))aSyscall[8].pCurrent)
#if defined(SQLITE_WIN32_HAS_ANSI)
{ "DeleteFileA", (SYSCALL)DeleteFileA, 0 },
@@ -333,7 +404,7 @@ static struct win_syscall {
{ "DeleteFileA", (SYSCALL)0, 0 },
#endif
-#define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[8].pCurrent)
+#define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
#if defined(SQLITE_WIN32_HAS_WIDE)
{ "DeleteFileW", (SYSCALL)DeleteFileW, 0 },
@@ -341,7 +412,7 @@ static struct win_syscall {
{ "DeleteFileW", (SYSCALL)0, 0 },
#endif
-#define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[9].pCurrent)
+#define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
#if SQLITE_OS_WINCE
{ "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
@@ -350,7 +421,7 @@ static struct win_syscall {
#endif
#define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
- LPFILETIME))aSyscall[10].pCurrent)
+ LPFILETIME))aSyscall[11].pCurrent)
#if SQLITE_OS_WINCE
{ "FileTimeToSystemTime", (SYSCALL)FileTimeToSystemTime, 0 },
@@ -359,11 +430,11 @@ static struct win_syscall {
#endif
#define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
- LPSYSTEMTIME))aSyscall[11].pCurrent)
+ LPSYSTEMTIME))aSyscall[12].pCurrent)
{ "FlushFileBuffers", (SYSCALL)FlushFileBuffers, 0 },
-#define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[12].pCurrent)
+#define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
#if defined(SQLITE_WIN32_HAS_ANSI)
{ "FormatMessageA", (SYSCALL)FormatMessageA, 0 },
@@ -372,7 +443,7 @@ static struct win_syscall {
#endif
#define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
- DWORD,va_list*))aSyscall[13].pCurrent)
+ DWORD,va_list*))aSyscall[14].pCurrent)
#if defined(SQLITE_WIN32_HAS_WIDE)
{ "FormatMessageW", (SYSCALL)FormatMessageW, 0 },
@@ -381,15 +452,19 @@ static struct win_syscall {
#endif
#define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
- DWORD,va_list*))aSyscall[14].pCurrent)
+ DWORD,va_list*))aSyscall[15].pCurrent)
+#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
{ "FreeLibrary", (SYSCALL)FreeLibrary, 0 },
+#else
+ { "FreeLibrary", (SYSCALL)0, 0 },
+#endif
-#define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[15].pCurrent)
+#define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
{ "GetCurrentProcessId", (SYSCALL)GetCurrentProcessId, 0 },
-#define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[16].pCurrent)
+#define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
{ "GetDiskFreeSpaceA", (SYSCALL)GetDiskFreeSpaceA, 0 },
@@ -398,7 +473,7 @@ static struct win_syscall {
#endif
#define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
- LPDWORD))aSyscall[17].pCurrent)
+ LPDWORD))aSyscall[18].pCurrent)
#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
{ "GetDiskFreeSpaceW", (SYSCALL)GetDiskFreeSpaceW, 0 },
@@ -407,7 +482,7 @@ static struct win_syscall {
#endif
#define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
- LPDWORD))aSyscall[18].pCurrent)
+ LPDWORD))aSyscall[19].pCurrent)
#if defined(SQLITE_WIN32_HAS_ANSI)
{ "GetFileAttributesA", (SYSCALL)GetFileAttributesA, 0 },
@@ -415,7 +490,7 @@ static struct win_syscall {
{ "GetFileAttributesA", (SYSCALL)0, 0 },
#endif
-#define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[19].pCurrent)
+#define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
{ "GetFileAttributesW", (SYSCALL)GetFileAttributesW, 0 },
@@ -423,7 +498,7 @@ static struct win_syscall {
{ "GetFileAttributesW", (SYSCALL)0, 0 },
#endif
-#define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[20].pCurrent)
+#define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
#if defined(SQLITE_WIN32_HAS_WIDE)
{ "GetFileAttributesExW", (SYSCALL)GetFileAttributesExW, 0 },
@@ -432,7 +507,7 @@ static struct win_syscall {
#endif
#define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
- LPVOID))aSyscall[21].pCurrent)
+ LPVOID))aSyscall[22].pCurrent)
#if !SQLITE_OS_WINRT
{ "GetFileSize", (SYSCALL)GetFileSize, 0 },
@@ -440,7 +515,7 @@ static struct win_syscall {
{ "GetFileSize", (SYSCALL)0, 0 },
#endif
-#define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[22].pCurrent)
+#define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
{ "GetFullPathNameA", (SYSCALL)GetFullPathNameA, 0 },
@@ -449,7 +524,7 @@ static struct win_syscall {
#endif
#define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
- LPSTR*))aSyscall[23].pCurrent)
+ LPSTR*))aSyscall[24].pCurrent)
#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
{ "GetFullPathNameW", (SYSCALL)GetFullPathNameW, 0 },
@@ -458,12 +533,13 @@ static struct win_syscall {
#endif
#define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
- LPWSTR*))aSyscall[24].pCurrent)
+ LPWSTR*))aSyscall[25].pCurrent)
{ "GetLastError", (SYSCALL)GetLastError, 0 },
-#define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[25].pCurrent)
+#define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
+#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
#if SQLITE_OS_WINCE
/* The GetProcAddressA() routine is only available on Windows CE. */
{ "GetProcAddressA", (SYSCALL)GetProcAddressA, 0 },
@@ -472,9 +548,12 @@ static struct win_syscall {
** an ANSI string regardless of the _UNICODE setting */
{ "GetProcAddressA", (SYSCALL)GetProcAddress, 0 },
#endif
+#else
+ { "GetProcAddressA", (SYSCALL)0, 0 },
+#endif
#define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
- LPCSTR))aSyscall[26].pCurrent)
+ LPCSTR))aSyscall[27].pCurrent)
#if !SQLITE_OS_WINRT
{ "GetSystemInfo", (SYSCALL)GetSystemInfo, 0 },
@@ -482,11 +561,11 @@ static struct win_syscall {
{ "GetSystemInfo", (SYSCALL)0, 0 },
#endif
-#define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[27].pCurrent)
+#define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
{ "GetSystemTime", (SYSCALL)GetSystemTime, 0 },
-#define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[28].pCurrent)
+#define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
#if !SQLITE_OS_WINCE
{ "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
@@ -495,7 +574,7 @@ static struct win_syscall {
#endif
#define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
- LPFILETIME))aSyscall[29].pCurrent)
+ LPFILETIME))aSyscall[30].pCurrent)
#if defined(SQLITE_WIN32_HAS_ANSI)
{ "GetTempPathA", (SYSCALL)GetTempPathA, 0 },
@@ -503,7 +582,7 @@ static struct win_syscall {
{ "GetTempPathA", (SYSCALL)0, 0 },
#endif
-#define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[30].pCurrent)
+#define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
{ "GetTempPathW", (SYSCALL)GetTempPathW, 0 },
@@ -511,7 +590,7 @@ static struct win_syscall {
{ "GetTempPathW", (SYSCALL)0, 0 },
#endif
-#define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[31].pCurrent)
+#define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
#if !SQLITE_OS_WINRT
{ "GetTickCount", (SYSCALL)GetTickCount, 0 },
@@ -519,7 +598,7 @@ static struct win_syscall {
{ "GetTickCount", (SYSCALL)0, 0 },
#endif
-#define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[32].pCurrent)
+#define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
#if defined(SQLITE_WIN32_HAS_ANSI)
{ "GetVersionExA", (SYSCALL)GetVersionExA, 0 },
@@ -528,12 +607,12 @@ static struct win_syscall {
#endif
#define osGetVersionExA ((BOOL(WINAPI*)( \
- LPOSVERSIONINFOA))aSyscall[33].pCurrent)
+ LPOSVERSIONINFOA))aSyscall[34].pCurrent)
{ "HeapAlloc", (SYSCALL)HeapAlloc, 0 },
#define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
- SIZE_T))aSyscall[34].pCurrent)
+ SIZE_T))aSyscall[35].pCurrent)
#if !SQLITE_OS_WINRT
{ "HeapCreate", (SYSCALL)HeapCreate, 0 },
@@ -542,7 +621,7 @@ static struct win_syscall {
#endif
#define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
- SIZE_T))aSyscall[35].pCurrent)
+ SIZE_T))aSyscall[36].pCurrent)
#if !SQLITE_OS_WINRT
{ "HeapDestroy", (SYSCALL)HeapDestroy, 0 },
@@ -550,21 +629,21 @@ static struct win_syscall {
{ "HeapDestroy", (SYSCALL)0, 0 },
#endif
-#define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[36].pCurrent)
+#define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[37].pCurrent)
{ "HeapFree", (SYSCALL)HeapFree, 0 },
-#define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[37].pCurrent)
+#define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[38].pCurrent)
{ "HeapReAlloc", (SYSCALL)HeapReAlloc, 0 },
#define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
- SIZE_T))aSyscall[38].pCurrent)
+ SIZE_T))aSyscall[39].pCurrent)
{ "HeapSize", (SYSCALL)HeapSize, 0 },
#define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
- LPCVOID))aSyscall[39].pCurrent)
+ LPCVOID))aSyscall[40].pCurrent)
#if !SQLITE_OS_WINRT
{ "HeapValidate", (SYSCALL)HeapValidate, 0 },
@@ -573,23 +652,24 @@ static struct win_syscall {
#endif
#define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
- LPCVOID))aSyscall[40].pCurrent)
+ LPCVOID))aSyscall[41].pCurrent)
-#if defined(SQLITE_WIN32_HAS_ANSI)
+#if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
{ "LoadLibraryA", (SYSCALL)LoadLibraryA, 0 },
#else
{ "LoadLibraryA", (SYSCALL)0, 0 },
#endif
-#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[41].pCurrent)
+#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[42].pCurrent)
-#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
+#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
+ !defined(SQLITE_OMIT_LOAD_EXTENSION)
{ "LoadLibraryW", (SYSCALL)LoadLibraryW, 0 },
#else
{ "LoadLibraryW", (SYSCALL)0, 0 },
#endif
-#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[42].pCurrent)
+#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[43].pCurrent)
#if !SQLITE_OS_WINRT
{ "LocalFree", (SYSCALL)LocalFree, 0 },
@@ -597,7 +677,7 @@ static struct win_syscall {
{ "LocalFree", (SYSCALL)0, 0 },
#endif
-#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[43].pCurrent)
+#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[44].pCurrent)
#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
{ "LockFile", (SYSCALL)LockFile, 0 },
@@ -607,7 +687,7 @@ static struct win_syscall {
#ifndef osLockFile
#define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
- DWORD))aSyscall[44].pCurrent)
+ DWORD))aSyscall[45].pCurrent)
#endif
#if !SQLITE_OS_WINCE
@@ -618,7 +698,7 @@ static struct win_syscall {
#ifndef osLockFileEx
#define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
- LPOVERLAPPED))aSyscall[45].pCurrent)
+ LPOVERLAPPED))aSyscall[46].pCurrent)
#endif
#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL))
@@ -628,26 +708,26 @@ static struct win_syscall {
#endif
#define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
- SIZE_T))aSyscall[46].pCurrent)
+ SIZE_T))aSyscall[47].pCurrent)
{ "MultiByteToWideChar", (SYSCALL)MultiByteToWideChar, 0 },
#define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
- int))aSyscall[47].pCurrent)
+ int))aSyscall[48].pCurrent)
{ "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
#define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
- LARGE_INTEGER*))aSyscall[48].pCurrent)
+ LARGE_INTEGER*))aSyscall[49].pCurrent)
{ "ReadFile", (SYSCALL)ReadFile, 0 },
#define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
- LPOVERLAPPED))aSyscall[49].pCurrent)
+ LPOVERLAPPED))aSyscall[50].pCurrent)
{ "SetEndOfFile", (SYSCALL)SetEndOfFile, 0 },
-#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[50].pCurrent)
+#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[51].pCurrent)
#if !SQLITE_OS_WINRT
{ "SetFilePointer", (SYSCALL)SetFilePointer, 0 },
@@ -656,7 +736,7 @@ static struct win_syscall {
#endif
#define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
- DWORD))aSyscall[51].pCurrent)
+ DWORD))aSyscall[52].pCurrent)
#if !SQLITE_OS_WINRT
{ "Sleep", (SYSCALL)Sleep, 0 },
@@ -664,12 +744,12 @@ static struct win_syscall {
{ "Sleep", (SYSCALL)0, 0 },
#endif
-#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[52].pCurrent)
+#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[53].pCurrent)
{ "SystemTimeToFileTime", (SYSCALL)SystemTimeToFileTime, 0 },
#define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
- LPFILETIME))aSyscall[53].pCurrent)
+ LPFILETIME))aSyscall[54].pCurrent)
#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
{ "UnlockFile", (SYSCALL)UnlockFile, 0 },
@@ -679,7 +759,7 @@ static struct win_syscall {
#ifndef osUnlockFile
#define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
- DWORD))aSyscall[54].pCurrent)
+ DWORD))aSyscall[55].pCurrent)
#endif
#if !SQLITE_OS_WINCE
@@ -689,7 +769,7 @@ static struct win_syscall {
#endif
#define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
- LPOVERLAPPED))aSyscall[55].pCurrent)
+ LPOVERLAPPED))aSyscall[56].pCurrent)
#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL)
{ "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 },
@@ -697,17 +777,17 @@ static struct win_syscall {
{ "UnmapViewOfFile", (SYSCALL)0, 0 },
#endif
-#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[56].pCurrent)
+#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[57].pCurrent)
{ "WideCharToMultiByte", (SYSCALL)WideCharToMultiByte, 0 },
#define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
- LPCSTR,LPBOOL))aSyscall[57].pCurrent)
+ LPCSTR,LPBOOL))aSyscall[58].pCurrent)
{ "WriteFile", (SYSCALL)WriteFile, 0 },
#define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
- LPOVERLAPPED))aSyscall[58].pCurrent)
+ LPOVERLAPPED))aSyscall[59].pCurrent)
#if SQLITE_OS_WINRT
{ "CreateEventExW", (SYSCALL)CreateEventExW, 0 },
@@ -716,7 +796,7 @@ static struct win_syscall {
#endif
#define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
- DWORD,DWORD))aSyscall[59].pCurrent)
+ DWORD,DWORD))aSyscall[60].pCurrent)
#if !SQLITE_OS_WINRT
{ "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 },
@@ -725,7 +805,7 @@ static struct win_syscall {
#endif
#define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
- DWORD))aSyscall[60].pCurrent)
+ DWORD))aSyscall[61].pCurrent)
#if SQLITE_OS_WINRT
{ "WaitForSingleObjectEx", (SYSCALL)WaitForSingleObjectEx, 0 },
@@ -734,7 +814,7 @@ static struct win_syscall {
#endif
#define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
- BOOL))aSyscall[61].pCurrent)
+ BOOL))aSyscall[62].pCurrent)
#if SQLITE_OS_WINRT
{ "SetFilePointerEx", (SYSCALL)SetFilePointerEx, 0 },
@@ -743,7 +823,7 @@ static struct win_syscall {
#endif
#define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
- PLARGE_INTEGER,DWORD))aSyscall[62].pCurrent)
+ PLARGE_INTEGER,DWORD))aSyscall[63].pCurrent)
#if SQLITE_OS_WINRT
{ "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
@@ -752,7 +832,7 @@ static struct win_syscall {
#endif
#define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
- FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[63].pCurrent)
+ FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[64].pCurrent)
#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
{ "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 },
@@ -761,7 +841,7 @@ static struct win_syscall {
#endif
#define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
- SIZE_T))aSyscall[64].pCurrent)
+ SIZE_T))aSyscall[65].pCurrent)
#if SQLITE_OS_WINRT
{ "CreateFile2", (SYSCALL)CreateFile2, 0 },
@@ -770,16 +850,16 @@ static struct win_syscall {
#endif
#define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
- LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[65].pCurrent)
+ LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[66].pCurrent)
-#if SQLITE_OS_WINRT
+#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION)
{ "LoadPackagedLibrary", (SYSCALL)LoadPackagedLibrary, 0 },
#else
{ "LoadPackagedLibrary", (SYSCALL)0, 0 },
#endif
#define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
- DWORD))aSyscall[66].pCurrent)
+ DWORD))aSyscall[67].pCurrent)
#if SQLITE_OS_WINRT
{ "GetTickCount64", (SYSCALL)GetTickCount64, 0 },
@@ -787,7 +867,7 @@ static struct win_syscall {
{ "GetTickCount64", (SYSCALL)0, 0 },
#endif
-#define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[67].pCurrent)
+#define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[68].pCurrent)
#if SQLITE_OS_WINRT
{ "GetNativeSystemInfo", (SYSCALL)GetNativeSystemInfo, 0 },
@@ -796,7 +876,7 @@ static struct win_syscall {
#endif
#define osGetNativeSystemInfo ((VOID(WINAPI*)( \
- LPSYSTEM_INFO))aSyscall[68].pCurrent)
+ LPSYSTEM_INFO))aSyscall[69].pCurrent)
#if defined(SQLITE_WIN32_HAS_ANSI)
{ "OutputDebugStringA", (SYSCALL)OutputDebugStringA, 0 },
@@ -804,7 +884,7 @@ static struct win_syscall {
{ "OutputDebugStringA", (SYSCALL)0, 0 },
#endif
-#define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[69].pCurrent)
+#define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[70].pCurrent)
#if defined(SQLITE_WIN32_HAS_WIDE)
{ "OutputDebugStringW", (SYSCALL)OutputDebugStringW, 0 },
@@ -812,11 +892,11 @@ static struct win_syscall {
{ "OutputDebugStringW", (SYSCALL)0, 0 },
#endif
-#define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[70].pCurrent)
+#define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[71].pCurrent)
{ "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 },
-#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[71].pCurrent)
+#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[72].pCurrent)
#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
{ "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
@@ -825,7 +905,7 @@ static struct win_syscall {
#endif
#define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
- LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[72].pCurrent)
+ LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[73].pCurrent)
}; /* End of the overrideable system calls */
@@ -917,7 +997,7 @@ static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
** (if available).
*/
-void sqlite3_win32_write_debug(char *zBuf, int nBuf){
+void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
@@ -983,6 +1063,8 @@ void sqlite3_win32_sleep(DWORD milliseconds){
*/
#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
# define isNT() (1)
+#elif !defined(SQLITE_WIN32_HAS_WIDE)
+# define isNT() (0)
#else
static int isNT(void){
if( sqlite3_os_type==0 ){
@@ -993,7 +1075,7 @@ void sqlite3_win32_sleep(DWORD milliseconds){
}
return sqlite3_os_type==2;
}
-#endif /* SQLITE_OS_WINCE */
+#endif
#ifdef SQLITE_WIN32_MALLOC
/*
@@ -1203,7 +1285,7 @@ static LPWSTR utf8ToUnicode(const char *zFilename){
if( nChar==0 ){
return 0;
}
- zWideFilename = sqlite3_malloc( nChar*sizeof(zWideFilename[0]) );
+ zWideFilename = sqlite3MallocZero( nChar*sizeof(zWideFilename[0]) );
if( zWideFilename==0 ){
return 0;
}
@@ -1228,7 +1310,7 @@ static char *unicodeToUtf8(LPCWSTR zWideFilename){
if( nByte == 0 ){
return 0;
}
- zFilename = sqlite3_malloc( nByte );
+ zFilename = sqlite3MallocZero( nByte );
if( zFilename==0 ){
return 0;
}
@@ -1258,7 +1340,7 @@ static LPWSTR mbcsToUnicode(const char *zFilename){
if( nByte==0 ){
return 0;
}
- zMbcsFilename = sqlite3_malloc( nByte*sizeof(zMbcsFilename[0]) );
+ zMbcsFilename = sqlite3MallocZero( nByte*sizeof(zMbcsFilename[0]) );
if( zMbcsFilename==0 ){
return 0;
}
@@ -1287,7 +1369,7 @@ static char *unicodeToMbcs(LPCWSTR zWideFilename){
if( nByte == 0 ){
return 0;
}
- zFilename = sqlite3_malloc( nByte );
+ zFilename = sqlite3MallocZero( nByte );
if( zFilename==0 ){
return 0;
}
@@ -1441,7 +1523,7 @@ static int getLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
}
#endif
if( 0 == dwLen ){
- sqlite3_snprintf(nBuf, zBuf, "OsError 0x%x (%u)", lastErrno, lastErrno);
+ sqlite3_snprintf(nBuf, zBuf, "OsError 0x%lx (%lu)", lastErrno, lastErrno);
}else{
/* copy a maximum of nBuf chars to output buffer */
sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
@@ -1484,7 +1566,7 @@ static int winLogErrorAtLine(
for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
zMsg[i] = 0;
sqlite3_log(errcode,
- "os_win.c:%d: (%d) %s(%s) - %s",
+ "os_win.c:%d: (%lu) %s(%s) - %s",
iLine, lastErrno, zFunc, zPath, zMsg
);
@@ -1548,9 +1630,10 @@ static void logIoerr(int nRetry){
/*************************************************************************
** This section contains code for WinCE only.
*/
+#if !defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API
/*
-** Windows CE does not have a localtime() function. So create a
-** substitute.
+** The MSVC CRT on Windows CE may not have a localtime() function. So
+** create a substitute.
*/
#include <time.h>
struct tm *__cdecl localtime(const time_t *t)
@@ -1574,6 +1657,7 @@ struct tm *__cdecl localtime(const time_t *t)
y.tm_sec = pTm.wSecond;
return &y;
}
+#endif
#define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
@@ -1595,15 +1679,17 @@ static void winceMutexAcquire(HANDLE h){
** Create the mutex and shared memory used for locking in the file
** descriptor pFile
*/
-static BOOL winceCreateLock(const char *zFilename, winFile *pFile){
+static int winceCreateLock(const char *zFilename, winFile *pFile){
LPWSTR zTok;
LPWSTR zName;
+ DWORD lastErrno;
+ BOOL bLogged = FALSE;
BOOL bInit = TRUE;
zName = utf8ToUnicode(zFilename);
if( zName==0 ){
/* out of memory */
- return FALSE;
+ return SQLITE_IOERR_NOMEM;
}
/* Initialize the local lockdata */
@@ -1620,9 +1706,10 @@ static BOOL winceCreateLock(const char *zFilename, winFile *pFile){
pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
if (!pFile->hMutex){
pFile->lastErrno = osGetLastError();
- winLogError(SQLITE_ERROR, pFile->lastErrno, "winceCreateLock1", zFilename);
+ winLogError(SQLITE_IOERR, pFile->lastErrno,
+ "winceCreateLock1", zFilename);
sqlite3_free(zName);
- return FALSE;
+ return SQLITE_IOERR;
}
/* Acquire the mutex before continuing */
@@ -1639,41 +1726,49 @@ static BOOL winceCreateLock(const char *zFilename, winFile *pFile){
/* Set a flag that indicates we're the first to create the memory so it
** must be zero-initialized */
- if (osGetLastError() == ERROR_ALREADY_EXISTS){
+ lastErrno = osGetLastError();
+ if (lastErrno == ERROR_ALREADY_EXISTS){
bInit = FALSE;
}
sqlite3_free(zName);
/* If we succeeded in making the shared memory handle, map it. */
- if (pFile->hShared){
+ if( pFile->hShared ){
pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
/* If mapping failed, close the shared memory handle and erase it */
- if (!pFile->shared){
+ if( !pFile->shared ){
pFile->lastErrno = osGetLastError();
- winLogError(SQLITE_ERROR, pFile->lastErrno,
- "winceCreateLock2", zFilename);
+ winLogError(SQLITE_IOERR, pFile->lastErrno,
+ "winceCreateLock2", zFilename);
+ bLogged = TRUE;
osCloseHandle(pFile->hShared);
pFile->hShared = NULL;
}
}
/* If shared memory could not be created, then close the mutex and fail */
- if (pFile->hShared == NULL){
+ if( pFile->hShared==NULL ){
+ if( !bLogged ){
+ pFile->lastErrno = lastErrno;
+ winLogError(SQLITE_IOERR, pFile->lastErrno,
+ "winceCreateLock3", zFilename);
+ bLogged = TRUE;
+ }
winceMutexRelease(pFile->hMutex);
osCloseHandle(pFile->hMutex);
pFile->hMutex = NULL;
- return FALSE;
+ return SQLITE_IOERR;
}
/* Initialize the shared memory if we're supposed to */
- if (bInit) {
+ if( bInit ){
memset(pFile->shared, 0, sizeof(winceLock));
}
winceMutexRelease(pFile->hMutex);
- return TRUE;
+ return SQLITE_OK;
}
/*
@@ -1752,7 +1847,8 @@ static BOOL winceLockFile(
}
/* Want a pending lock? */
- else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToLockLow == 1){
+ else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
+ && nNumberOfBytesToLockLow == 1){
/* If no pending lock has been acquired, then acquire it */
if (pFile->shared->bPending == 0) {
pFile->shared->bPending = TRUE;
@@ -1762,7 +1858,8 @@ static BOOL winceLockFile(
}
/* Want a reserved lock? */
- else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToLockLow == 1){
+ else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
+ && nNumberOfBytesToLockLow == 1){
if (pFile->shared->bReserved == 0) {
pFile->shared->bReserved = TRUE;
pFile->local.bReserved = TRUE;
@@ -1805,7 +1902,8 @@ static BOOL winceUnlockFile(
/* Did we just have a reader lock? */
else if (pFile->local.nReaders){
- assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE || nNumberOfBytesToUnlockLow == 1);
+ assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE
+ || nNumberOfBytesToUnlockLow == 1);
pFile->local.nReaders --;
if (pFile->local.nReaders == 0)
{
@@ -1816,7 +1914,8 @@ static BOOL winceUnlockFile(
}
/* Releasing a pending lock */
- else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToUnlockLow == 1){
+ else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
+ && nNumberOfBytesToUnlockLow == 1){
if (pFile->local.bPending){
pFile->local.bPending = FALSE;
pFile->shared->bPending = FALSE;
@@ -1824,7 +1923,8 @@ static BOOL winceUnlockFile(
}
}
/* Releasing a reserved lock */
- else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToUnlockLow == 1){
+ else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
+ && nNumberOfBytesToUnlockLow == 1){
if (pFile->local.bReserved) {
pFile->local.bReserved = FALSE;
pFile->shared->bReserved = FALSE;
@@ -1927,6 +2027,8 @@ static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
DWORD dwRet; /* Value returned by SetFilePointer() */
DWORD lastErrno; /* Value returned by GetLastError() */
+ OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
+
upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
lowerBits = (LONG)(iOffset & 0xffffffff);
@@ -1934,7 +2036,7 @@ static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
** containing the lower 32-bits of the new file-offset. Or, if it fails,
** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
- ** whether an error has actually occured, it is also necessary to call
+ ** whether an error has actually occurred, it is also necessary to call
** GetLastError().
*/
dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
@@ -1944,9 +2046,11 @@ static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
pFile->lastErrno = lastErrno;
winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
"seekWinFile", pFile->zPath);
+ OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
return 1;
}
+ OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
return 0;
#else
/*
@@ -1963,13 +2067,20 @@ static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
pFile->lastErrno = osGetLastError();
winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
"seekWinFile", pFile->zPath);
+ OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
return 1;
}
+ OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
return 0;
#endif
}
+#if SQLITE_MAX_MMAP_SIZE>0
+/* Forward references to VFS methods */
+static int winUnmapfile(winFile*);
+#endif
+
/*
** Close a file.
**
@@ -1989,7 +2100,14 @@ static int winClose(sqlite3_file *id){
#ifndef SQLITE_OMIT_WAL
assert( pFile->pShm==0 );
#endif
- OSTRACE(("CLOSE %d\n", pFile->h));
+ assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
+ OSTRACE(("CLOSE file=%p\n", pFile->h));
+
+#if SQLITE_MAX_MMAP_SIZE>0
+ rc = winUnmapfile(pFile);
+ if( rc!=SQLITE_OK ) return rc;
+#endif
+
do{
rc = osCloseHandle(pFile->h);
/* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
@@ -2009,11 +2127,11 @@ static int winClose(sqlite3_file *id){
sqlite3_free(pFile->zDeleteOnClose);
}
#endif
- OSTRACE(("CLOSE %d %s\n", pFile->h, rc ? "ok" : "failed"));
if( rc ){
pFile->h = NULL;
}
OpenCounter(-1);
+ OSTRACE(("CLOSE file=%p, rc=%s\n", pFile->h, rc ? "ok" : "failed"));
return rc ? SQLITE_OK
: winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
"winClose", pFile->zPath);
@@ -2038,11 +2156,33 @@ static int winRead(
int nRetry = 0; /* Number of retrys */
assert( id!=0 );
+ assert( amt>0 );
+ assert( offset>=0 );
SimulateIOError(return SQLITE_IOERR_READ);
- OSTRACE(("READ %d lock=%d\n", pFile->h, pFile->locktype));
+ OSTRACE(("READ file=%p, buffer=%p, amount=%d, offset=%lld, lock=%d\n",
+ pFile->h, pBuf, amt, offset, pFile->locktype));
+
+#if SQLITE_MAX_MMAP_SIZE>0
+ /* Deal with as much of this read request as possible by transfering
+ ** data from the memory mapping using memcpy(). */
+ 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));
+ return SQLITE_OK;
+ }else{
+ int nCopy = (int)(pFile->mmapSize - offset);
+ memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
+ pBuf = &((u8 *)pBuf)[nCopy];
+ amt -= nCopy;
+ offset += nCopy;
+ }
+ }
+#endif
#if SQLITE_OS_WINCE
if( seekWinFile(pFile, offset) ){
+ OSTRACE(("READ file=%p, rc=SQLITE_FULL\n", pFile->h));
return SQLITE_FULL;
}
while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
@@ -2056,6 +2196,7 @@ static int winRead(
DWORD lastErrno;
if( retryIoerr(&nRetry, &lastErrno) ) continue;
pFile->lastErrno = lastErrno;
+ OSTRACE(("READ file=%p, rc=SQLITE_IOERR_READ\n", pFile->h));
return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
"winRead", pFile->zPath);
}
@@ -2063,9 +2204,11 @@ static int winRead(
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));
return SQLITE_IOERR_SHORT_READ;
}
+ OSTRACE(("READ file=%p, rc=SQLITE_OK\n", pFile->h));
return SQLITE_OK;
}
@@ -2079,7 +2222,7 @@ static int winWrite(
int amt, /* Number of bytes to write */
sqlite3_int64 offset /* Offset into the file to begin writing at */
){
- int rc = 0; /* True if error has occured, else false */
+ int rc = 0; /* True if error has occurred, else false */
winFile *pFile = (winFile*)id; /* File handle */
int nRetry = 0; /* Number of retries */
@@ -2088,7 +2231,26 @@ static int winWrite(
SimulateIOError(return SQLITE_IOERR_WRITE);
SimulateDiskfullError(return SQLITE_FULL);
- OSTRACE(("WRITE %d lock=%d\n", pFile->h, pFile->locktype));
+ OSTRACE(("WRITE file=%p, buffer=%p, amount=%d, offset=%lld, lock=%d\n",
+ pFile->h, pBuf, amt, offset, pFile->locktype));
+
+#if SQLITE_MAX_MMAP_SIZE>0
+ /* Deal with as much of this write request as possible by transfering
+ ** data from the memory mapping using memcpy(). */
+ 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));
+ return SQLITE_OK;
+ }else{
+ int nCopy = (int)(pFile->mmapSize - offset);
+ memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
+ pBuf = &((u8 *)pBuf)[nCopy];
+ amt -= nCopy;
+ offset += nCopy;
+ }
+ }
+#endif
#if SQLITE_OS_WINCE
rc = seekWinFile(pFile, offset);
@@ -2119,7 +2281,8 @@ static int winWrite(
if( retryIoerr(&nRetry, &lastErrno) ) continue;
break;
}
- if( nWrite<=0 ){
+ assert( nWrite==0 || nWrite<=(DWORD)nRem );
+ if( nWrite==0 || nWrite>(DWORD)nRem ){
lastErrno = osGetLastError();
break;
}
@@ -2140,13 +2303,16 @@ static int winWrite(
if( rc ){
if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
|| ( pFile->lastErrno==ERROR_DISK_FULL )){
+ OSTRACE(("WRITE file=%p, rc=SQLITE_FULL\n", pFile->h));
return SQLITE_FULL;
}
+ OSTRACE(("WRITE file=%p, rc=SQLITE_IOERR_WRITE\n", pFile->h));
return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
"winWrite", pFile->zPath);
}else{
logIoerr(nRetry);
}
+ OSTRACE(("WRITE file=%p, rc=SQLITE_OK\n", pFile->h));
return SQLITE_OK;
}
@@ -2156,11 +2322,12 @@ static int winWrite(
static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
winFile *pFile = (winFile*)id; /* File handle object */
int rc = SQLITE_OK; /* Return code for this function */
+ DWORD lastErrno;
assert( pFile );
-
- OSTRACE(("TRUNCATE %d %lld\n", pFile->h, nByte));
SimulateIOError(return SQLITE_IOERR_TRUNCATE);
+ OSTRACE(("TRUNCATE file=%p, size=%lld, lock=%d\n",
+ 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
@@ -2174,14 +2341,25 @@ static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
/* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
if( seekWinFile(pFile, nByte) ){
rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
- "winTruncate1", pFile->zPath);
- }else if( 0==osSetEndOfFile(pFile->h) ){
- pFile->lastErrno = osGetLastError();
+ "winTruncate1", pFile->zPath);
+ }else if( 0==osSetEndOfFile(pFile->h) &&
+ ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
+ pFile->lastErrno = lastErrno;
rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
- "winTruncate2", pFile->zPath);
+ "winTruncate2", pFile->zPath);
}
- OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc ? "failed" : "ok"));
+#if SQLITE_MAX_MMAP_SIZE>0
+ /* If the file was truncated to a size smaller than the currently
+ ** mapped region, reduce the effective mapping size as well. SQLite will
+ ** use read() and write() to access data beyond this point from now on.
+ */
+ if( pFile->pMapRegion && nByte<pFile->mmapSize ){
+ pFile->mmapSize = nByte;
+ }
+#endif
+
+ OSTRACE(("TRUNCATE file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
return rc;
}
@@ -2221,13 +2399,14 @@ static int winSync(sqlite3_file *id, int flags){
|| (flags&0x0F)==SQLITE_SYNC_FULL
);
- OSTRACE(("SYNC %d lock=%d\n", pFile->h, pFile->locktype));
-
/* Unix cannot, but some systems may return SQLITE_FULL from here. This
** line is to test that doing so does not cause any problems.
*/
SimulateDiskfullError( return SQLITE_FULL );
+ OSTRACE(("SYNC file=%p, flags=%x, lock=%d\n",
+ pFile->h, flags, pFile->locktype));
+
#ifndef SQLITE_TEST
UNUSED_PARAMETER(flags);
#else
@@ -2246,9 +2425,11 @@ static int winSync(sqlite3_file *id, int flags){
rc = osFlushFileBuffers(pFile->h);
SimulateIOError( rc=FALSE );
if( rc ){
+ OSTRACE(("SYNC file=%p, rc=SQLITE_OK\n", pFile->h));
return SQLITE_OK;
}else{
pFile->lastErrno = osGetLastError();
+ OSTRACE(("SYNC file=%p, rc=SQLITE_IOERR_FSYNC\n", pFile->h));
return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
"winSync", pFile->zPath);
}
@@ -2263,7 +2444,10 @@ static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
int rc = SQLITE_OK;
assert( id!=0 );
+ assert( pSize!=0 );
SimulateIOError(return SQLITE_IOERR_FSTAT);
+ OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
+
#if SQLITE_OS_WINRT
{
FILE_STANDARD_INFO info;
@@ -2292,6 +2476,8 @@ static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
}
}
#endif
+ OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n",
+ pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
return rc;
}
@@ -2333,6 +2519,7 @@ static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
*/
static int getReadLock(winFile *pFile){
int res;
+ OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
if( isNT() ){
#if SQLITE_OS_WINCE
/*
@@ -2358,6 +2545,7 @@ static int getReadLock(winFile *pFile){
pFile->lastErrno = osGetLastError();
/* No need to log a failure to lock */
}
+ OSTRACE(("READ-LOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
return res;
}
@@ -2367,6 +2555,7 @@ static int getReadLock(winFile *pFile){
static int unlockReadLock(winFile *pFile){
int res;
DWORD lastErrno;
+ OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
if( isNT() ){
res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
}
@@ -2380,6 +2569,7 @@ static int unlockReadLock(winFile *pFile){
winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
"unlockReadLock", pFile->zPath);
}
+ OSTRACE(("READ-UNLOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
return res;
}
@@ -2418,14 +2608,15 @@ static int winLock(sqlite3_file *id, int locktype){
DWORD lastErrno = NO_ERROR;
assert( id!=0 );
- OSTRACE(("LOCK %d %d was %d(%d)\n",
- pFile->h, locktype, pFile->locktype, pFile->sharedLockByte));
+ OSTRACE(("LOCK file=%p, oldLock=%d(%d), newLock=%d\n",
+ pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
/* If there is already a lock of this type or more restrictive on the
** OsFile, do nothing. Don't use the end_lock: exit path, as
** sqlite3OsEnterMutex() hasn't been called yet.
*/
if( pFile->locktype>=locktype ){
+ OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
return SQLITE_OK;
}
@@ -2453,7 +2644,8 @@ static int winLock(sqlite3_file *id, int locktype){
** If you are using this code as a model for alternative VFSes, do not
** copy this retry logic. It is a hack intended for Windows only.
*/
- OSTRACE(("could not get a PENDING lock. cnt=%d\n", cnt));
+ OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, rc=%s\n",
+ pFile->h, cnt, sqlite3ErrName(res)));
if( cnt ) sqlite3_win32_sleep(1);
}
gotPendingLock = res;
@@ -2498,14 +2690,12 @@ static int winLock(sqlite3_file *id, int locktype){
if( locktype==EXCLUSIVE_LOCK && res ){
assert( pFile->locktype>=SHARED_LOCK );
res = unlockReadLock(pFile);
- OSTRACE(("unreadlock = %d\n", res));
res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
SHARED_SIZE, 0);
if( res ){
newLocktype = EXCLUSIVE_LOCK;
}else{
lastErrno = osGetLastError();
- OSTRACE(("error-code = %d\n", lastErrno));
getReadLock(pFile);
}
}
@@ -2523,12 +2713,14 @@ static int winLock(sqlite3_file *id, int locktype){
if( res ){
rc = SQLITE_OK;
}else{
- OSTRACE(("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
- locktype, newLocktype));
+ OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
+ pFile->h, locktype, newLocktype));
pFile->lastErrno = lastErrno;
rc = SQLITE_BUSY;
}
pFile->locktype = (u8)newLocktype;
+ OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
+ pFile->h, pFile->locktype, sqlite3ErrName(rc)));
return rc;
}
@@ -2542,20 +2734,23 @@ static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
winFile *pFile = (winFile*)id;
SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
+ OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
assert( id!=0 );
if( pFile->locktype>=RESERVED_LOCK ){
rc = 1;
- OSTRACE(("TEST WR-LOCK %d %d (local)\n", pFile->h, rc));
+ OSTRACE(("TEST-WR-LOCK file=%p, rc=%d (local)\n", pFile->h, rc));
}else{
- rc = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
+ rc = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE, 0, 1, 0);
if( rc ){
winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
}
rc = !rc;
- OSTRACE(("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc));
+ OSTRACE(("TEST-WR-LOCK file=%p, rc=%d (remote)\n", pFile->h, rc));
}
*pResOut = rc;
+ OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
+ pFile->h, pResOut, *pResOut));
return SQLITE_OK;
}
@@ -2576,8 +2771,8 @@ static int winUnlock(sqlite3_file *id, int locktype){
int rc = SQLITE_OK;
assert( pFile!=0 );
assert( locktype<=SHARED_LOCK );
- OSTRACE(("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
- pFile->locktype, pFile->sharedLockByte));
+ OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
+ pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
type = pFile->locktype;
if( type>=EXCLUSIVE_LOCK ){
winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
@@ -2598,6 +2793,8 @@ static int winUnlock(sqlite3_file *id, int locktype){
winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
}
pFile->locktype = (u8)locktype;
+ OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
+ pFile->h, pFile->locktype, sqlite3ErrName(rc)));
return rc;
}
@@ -2617,22 +2814,29 @@ static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
}
}
+/* Forward declaration */
+static int getTempname(int nBuf, char *zBuf);
+
/*
** Control and query of the open file handle.
*/
static int winFileControl(sqlite3_file *id, int op, void *pArg){
winFile *pFile = (winFile*)id;
+ OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
switch( op ){
case SQLITE_FCNTL_LOCKSTATE: {
*(int*)pArg = pFile->locktype;
+ OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
return SQLITE_OK;
}
case SQLITE_LAST_ERRNO: {
*(int*)pArg = (int)pFile->lastErrno;
+ OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
return SQLITE_OK;
}
case SQLITE_FCNTL_CHUNK_SIZE: {
pFile->szChunk = *(int *)pArg;
+ OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
return SQLITE_OK;
}
case SQLITE_FCNTL_SIZE_HINT: {
@@ -2647,20 +2851,25 @@ static int winFileControl(sqlite3_file *id, int op, void *pArg){
SimulateIOErrorBenign(0);
}
}
+ OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
return rc;
}
+ OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
return SQLITE_OK;
}
case SQLITE_FCNTL_PERSIST_WAL: {
winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
+ OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
return SQLITE_OK;
}
case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
+ OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
return SQLITE_OK;
}
case SQLITE_FCNTL_VFSNAME: {
*(char**)pArg = sqlite3_mprintf("win32");
+ OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
return SQLITE_OK;
}
case SQLITE_FCNTL_WIN32_AV_RETRY: {
@@ -2675,9 +2884,32 @@ static int winFileControl(sqlite3_file *id, int op, void *pArg){
}else{
a[1] = win32IoerrRetryDelay;
}
+ OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
return SQLITE_OK;
}
+ case SQLITE_FCNTL_TEMPFILENAME: {
+ char *zTFile = sqlite3MallocZero( pFile->pVfs->mxPathname );
+ if( zTFile ){
+ getTempname(pFile->pVfs->mxPathname, zTFile);
+ *(char**)pArg = zTFile;
+ }
+ OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
+ return SQLITE_OK;
+ }
+#if SQLITE_MAX_MMAP_SIZE>0
+ case SQLITE_FCNTL_MMAP_SIZE: {
+ i64 newLimit = *(i64*)pArg;
+ if( newLimit>sqlite3GlobalConfig.mxMmap ){
+ newLimit = sqlite3GlobalConfig.mxMmap;
+ }
+ *(i64*)pArg = pFile->mmapSizeMax;
+ if( newLimit>=0 ) pFile->mmapSizeMax = newLimit;
+ OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
+ return SQLITE_OK;
+ }
+#endif
}
+ OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
return SQLITE_NOTFOUND;
}
@@ -2705,8 +2937,6 @@ static int winDeviceCharacteristics(sqlite3_file *id){
((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
}
-#ifndef SQLITE_OMIT_WAL
-
/*
** Windows will only let you create file view mappings
** on allocation size granularity boundaries.
@@ -2715,6 +2945,8 @@ static int winDeviceCharacteristics(sqlite3_file *id){
*/
SYSTEM_INFO winSysInfo;
+#ifndef SQLITE_OMIT_WAL
+
/*
** Helper functions to obtain and relinquish the global mutex. The
** global mutex is used to protect the winLockInfo objects used by
@@ -2838,6 +3070,9 @@ static int winShmSystemLock(
/* Access to the winShmNode object is serialized by the caller */
assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
+ OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
+ pFile->hFile.h, lockType, ofst, nByte));
+
/* Release/Acquire the system-level lock */
if( lockType==_SHM_UNLCK ){
rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
@@ -2855,11 +3090,9 @@ static int winShmSystemLock(
rc = SQLITE_BUSY;
}
- OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n",
- pFile->hFile.h,
- rc==SQLITE_OK ? "ok" : "failed",
- lockType==_SHM_UNLCK ? "UnlockFileEx" : "LockFileEx",
- pFile->lastErrno));
+ OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
+ pFile->hFile.h, (lockType == _SHM_UNLCK) ? "winUnlockFile" :
+ "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
return rc;
}
@@ -2879,6 +3112,8 @@ static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
winShmNode *p;
BOOL bRc;
assert( winShmMutexHeld() );
+ OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
+ osGetCurrentProcessId(), deleteFlag));
pp = &winShmNodeList;
while( (p = *pp)!=0 ){
if( p->nRef==0 ){
@@ -2886,15 +3121,13 @@ static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
if( p->mutex ) sqlite3_mutex_free(p->mutex);
for(i=0; i<p->nRegion; i++){
bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
- OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
- (int)osGetCurrentProcessId(), i,
- bRc ? "ok" : "failed"));
+ OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
+ osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
bRc = osCloseHandle(p->aRegion[i].hMap);
- OSTRACE(("SHM-PURGE pid-%d close region=%d %s\n",
- (int)osGetCurrentProcessId(), i,
- bRc ? "ok" : "failed"));
+ OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
+ osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
}
- if( p->hFile.h != INVALID_HANDLE_VALUE ){
+ if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
SimulateIOErrorBenign(1);
winClose((sqlite3_file *)&p->hFile);
SimulateIOErrorBenign(0);
@@ -2934,16 +3167,14 @@ static int winOpenSharedMemory(winFile *pDbFd){
/* Allocate space for the new sqlite3_shm object. Also speculatively
** allocate space for a new winShmNode and filename.
*/
- p = sqlite3_malloc( sizeof(*p) );
+ p = sqlite3MallocZero( sizeof(*p) );
if( p==0 ) return SQLITE_IOERR_NOMEM;
- memset(p, 0, sizeof(*p));
nName = sqlite3Strlen30(pDbFd->zPath);
- pNew = sqlite3_malloc( sizeof(*pShmNode) + nName + 17 );
+ pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
if( pNew==0 ){
sqlite3_free(p);
return SQLITE_IOERR_NOMEM;
}
- memset(pNew, 0, sizeof(*pNew) + nName + 17);
pNew->zFilename = (char*)&pNew[1];
sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
@@ -2976,7 +3207,7 @@ static int winOpenSharedMemory(winFile *pDbFd){
rc = winOpen(pDbFd->pVfs,
pShmNode->zFilename, /* Name of the file (UTF-8) */
(sqlite3_file*)&pShmNode->hFile, /* File handle here */
- SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */
+ SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
0);
if( SQLITE_OK!=rc ){
goto shm_open_err;
@@ -3173,9 +3404,9 @@ static int winShmLock(
}
}
sqlite3_mutex_leave(pShmNode->mutex);
- OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
- p->id, (int)osGetCurrentProcessId(), p->sharedMask, p->exclMask,
- rc ? "failed" : "ok"));
+ OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
+ osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
+ sqlite3ErrName(rc)));
return rc;
}
@@ -3280,20 +3511,24 @@ static int winShmMap(
pShmNode->aRegion = apNew;
while( pShmNode->nRegion<=iRegion ){
- HANDLE hMap; /* file-mapping handle */
+ HANDLE hMap = NULL; /* file-mapping handle */
void *pMap = 0; /* Mapped memory region */
#if SQLITE_OS_WINRT
hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
NULL, PAGE_READWRITE, nByte, NULL
);
-#else
+#elif defined(SQLITE_WIN32_HAS_WIDE)
hMap = osCreateFileMappingW(pShmNode->hFile.h,
NULL, PAGE_READWRITE, 0, nByte, NULL
);
+#elif defined(SQLITE_WIN32_HAS_ANSI)
+ hMap = osCreateFileMappingA(pShmNode->hFile.h,
+ NULL, PAGE_READWRITE, 0, nByte, NULL
+ );
#endif
- OSTRACE(("SHM-MAP pid-%d create region=%d nbyte=%d %s\n",
- (int)osGetCurrentProcessId(), pShmNode->nRegion, nByte,
+ OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
+ osGetCurrentProcessId(), pShmNode->nRegion, nByte,
hMap ? "ok" : "failed"));
if( hMap ){
int iOffset = pShmNode->nRegion*szRegion;
@@ -3307,8 +3542,8 @@ static int winShmMap(
0, iOffset - iOffsetShift, szRegion + iOffsetShift
);
#endif
- OSTRACE(("SHM-MAP pid-%d map region=%d offset=%d size=%d %s\n",
- (int)osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
+ OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
+ osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
szRegion, pMap ? "ok" : "failed"));
}
if( !pMap ){
@@ -3346,6 +3581,230 @@ shmpage_out:
#endif /* #ifndef SQLITE_OMIT_WAL */
/*
+** Cleans up the mapped region of the specified file, if any.
+*/
+#if SQLITE_MAX_MMAP_SIZE>0
+static int winUnmapfile(winFile *pFile){
+ assert( pFile!=0 );
+ OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
+ "mmapSize=%lld, mmapSizeActual=%lld, mmapSizeMax=%lld\n",
+ osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
+ pFile->mmapSize, pFile->mmapSizeActual, pFile->mmapSizeMax));
+ if( pFile->pMapRegion ){
+ if( !osUnmapViewOfFile(pFile->pMapRegion) ){
+ pFile->lastErrno = osGetLastError();
+ OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
+ "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
+ pFile->pMapRegion));
+ return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
+ "winUnmap1", pFile->zPath);
+ }
+ pFile->pMapRegion = 0;
+ pFile->mmapSize = 0;
+ pFile->mmapSizeActual = 0;
+ }
+ if( pFile->hMap!=NULL ){
+ if( !osCloseHandle(pFile->hMap) ){
+ pFile->lastErrno = osGetLastError();
+ OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
+ osGetCurrentProcessId(), pFile, pFile->hMap));
+ return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
+ "winUnmap2", pFile->zPath);
+ }
+ pFile->hMap = NULL;
+ }
+ OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
+ osGetCurrentProcessId(), pFile));
+ return SQLITE_OK;
+}
+
+/*
+** Memory map or remap the file opened by file-descriptor pFd (if the file
+** is already mapped, the existing mapping is replaced by the new). Or, if
+** there already exists a mapping for this file, and there are still
+** outstanding xFetch() references to it, this function is a no-op.
+**
+** If parameter nByte is non-negative, then it is the requested size of
+** the mapping to create. Otherwise, if nByte is less than zero, then the
+** requested size is the size of the file on disk. The actual size of the
+** created mapping is either the requested size or the value configured
+** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
+**
+** SQLITE_OK is returned if no error occurs (even if the mapping is not
+** recreated as a result of outstanding references) or an SQLite error
+** code otherwise.
+*/
+static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
+ sqlite3_int64 nMap = nByte;
+ int rc;
+
+ assert( nMap>=0 || pFd->nFetchOut==0 );
+ OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
+ osGetCurrentProcessId(), pFd, nByte));
+
+ if( pFd->nFetchOut>0 ) return SQLITE_OK;
+
+ if( nMap<0 ){
+ rc = winFileSize((sqlite3_file*)pFd, &nMap);
+ if( rc ){
+ OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
+ osGetCurrentProcessId(), pFd));
+ return SQLITE_IOERR_FSTAT;
+ }
+ }
+ if( nMap>pFd->mmapSizeMax ){
+ nMap = pFd->mmapSizeMax;
+ }
+ nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
+
+ if( nMap==0 && pFd->mmapSize>0 ){
+ winUnmapfile(pFd);
+ }
+ if( nMap!=pFd->mmapSize ){
+ void *pNew = 0;
+ DWORD protect = PAGE_READONLY;
+ DWORD flags = FILE_MAP_READ;
+
+ winUnmapfile(pFd);
+ if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
+ protect = PAGE_READWRITE;
+ flags |= FILE_MAP_WRITE;
+ }
+#if SQLITE_OS_WINRT
+ pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
+#elif defined(SQLITE_WIN32_HAS_WIDE)
+ pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
+ (DWORD)((nMap>>32) & 0xffffffff),
+ (DWORD)(nMap & 0xffffffff), NULL);
+#elif defined(SQLITE_WIN32_HAS_ANSI)
+ pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
+ (DWORD)((nMap>>32) & 0xffffffff),
+ (DWORD)(nMap & 0xffffffff), NULL);
+#endif
+ if( pFd->hMap==NULL ){
+ pFd->lastErrno = osGetLastError();
+ rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
+ "winMapfile", pFd->zPath);
+ /* Log the error, but continue normal operation using xRead/xWrite */
+ OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=SQLITE_IOERR_MMAP\n",
+ osGetCurrentProcessId(), pFd));
+ return SQLITE_OK;
+ }
+ assert( (nMap % winSysInfo.dwPageSize)==0 );
+#if SQLITE_OS_WINRT
+ pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, nMap);
+#else
+ assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff );
+ pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
+#endif
+ if( pNew==NULL ){
+ osCloseHandle(pFd->hMap);
+ pFd->hMap = NULL;
+ pFd->lastErrno = osGetLastError();
+ winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
+ "winMapfile", pFd->zPath);
+ OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=SQLITE_IOERR_MMAP\n",
+ osGetCurrentProcessId(), pFd));
+ return SQLITE_OK;
+ }
+ pFd->pMapRegion = pNew;
+ pFd->mmapSize = nMap;
+ pFd->mmapSizeActual = nMap;
+ }
+
+ OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
+ osGetCurrentProcessId(), pFd));
+ return SQLITE_OK;
+}
+#endif /* SQLITE_MAX_MMAP_SIZE>0 */
+
+/*
+** If possible, return a pointer to a mapping of file fd starting at offset
+** iOff. The mapping must be valid for at least nAmt bytes.
+**
+** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
+** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
+** Finally, if an error does occur, return an SQLite error code. The final
+** value of *pp is undefined in this case.
+**
+** If this function does return a pointer, the caller must eventually
+** release the reference by calling winUnfetch().
+*/
+static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
+#if SQLITE_MAX_MMAP_SIZE>0
+ winFile *pFd = (winFile*)fd; /* The underlying database file */
+#endif
+ *pp = 0;
+
+ OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n",
+ osGetCurrentProcessId(), fd, iOff, nAmt, pp));
+
+#if SQLITE_MAX_MMAP_SIZE>0
+ if( pFd->mmapSizeMax>0 ){
+ if( pFd->pMapRegion==0 ){
+ int rc = winMapfile(pFd, -1);
+ if( rc!=SQLITE_OK ){
+ OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n",
+ osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
+ return rc;
+ }
+ }
+ if( pFd->mmapSize >= iOff+nAmt ){
+ *pp = &((u8 *)pFd->pMapRegion)[iOff];
+ pFd->nFetchOut++;
+ }
+ }
+#endif
+
+ OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n",
+ osGetCurrentProcessId(), fd, pp, *pp));
+ return SQLITE_OK;
+}
+
+/*
+** If the third argument is non-NULL, then this function releases a
+** reference obtained by an earlier call to winFetch(). The second
+** argument passed to this function must be the same as the corresponding
+** argument that was passed to the winFetch() invocation.
+**
+** Or, if the third argument is NULL, then this function is being called
+** to inform the VFS layer that, according to POSIX, any existing mapping
+** may now be invalid and should be unmapped.
+*/
+static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
+#if SQLITE_MAX_MMAP_SIZE>0
+ winFile *pFd = (winFile*)fd; /* The underlying database file */
+
+ /* If p==0 (unmap the entire file) then there must be no outstanding
+ ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
+ ** then there must be at least one outstanding. */
+ assert( (p==0)==(pFd->nFetchOut==0) );
+
+ /* If p!=0, it must match the iOff value. */
+ assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
+
+ OSTRACE(("UNFETCH pid=%lu, pFile=%p, offset=%lld, p=%p\n",
+ osGetCurrentProcessId(), pFd, iOff, p));
+
+ if( p ){
+ pFd->nFetchOut--;
+ }else{
+ /* FIXME: If Windows truly always prevents truncating or deleting a
+ ** file while a mapping is held, then the following winUnmapfile() call
+ ** is unnecessary can can be omitted - potentially improving
+ ** performance. */
+ winUnmapfile(pFd);
+ }
+
+ assert( pFd->nFetchOut>=0 );
+#endif
+
+ OSTRACE(("UNFETCH pid=%lu, pFile=%p, rc=SQLITE_OK\n",
+ osGetCurrentProcessId(), fd));
+ return SQLITE_OK;
+}
+
+/*
** Here ends the implementation of all sqlite3_file methods.
**
********************** End sqlite3_file Methods *******************************
@@ -3356,7 +3815,7 @@ shmpage_out:
** sqlite3_file for win32.
*/
static const sqlite3_io_methods winIoMethod = {
- 2, /* iVersion */
+ 3, /* iVersion */
winClose, /* xClose */
winRead, /* xRead */
winWrite, /* xWrite */
@@ -3372,7 +3831,9 @@ static const sqlite3_io_methods winIoMethod = {
winShmMap, /* xShmMap */
winShmLock, /* xShmLock */
winShmBarrier, /* xShmBarrier */
- winShmUnmap /* xShmUnmap */
+ winShmUnmap, /* xShmUnmap */
+ winFetch, /* xFetch */
+ winUnfetch /* xUnfetch */
};
/****************************************************************************
@@ -3436,6 +3897,7 @@ static int getTempname(int nBuf, char *zBuf){
sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zMulti);
sqlite3_free(zMulti);
}else{
+ OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
return SQLITE_IOERR_NOMEM;
}
}
@@ -3449,6 +3911,7 @@ static int getTempname(int nBuf, char *zBuf){
sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8);
sqlite3_free(zUtf8);
}else{
+ OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
return SQLITE_IOERR_NOMEM;
}
}
@@ -3461,6 +3924,7 @@ static int getTempname(int nBuf, char *zBuf){
nTempPath = sqlite3Strlen30(zTempPath);
if( (nTempPath + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 18) >= nBuf ){
+ OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
return SQLITE_ERROR;
}
@@ -3478,8 +3942,8 @@ static int getTempname(int nBuf, char *zBuf){
zBuf[j] = 0;
zBuf[j+1] = 0;
- OSTRACE(("TEMP FILENAME: %s\n", zBuf));
- return SQLITE_OK;
+ OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
+ return SQLITE_OK;
}
/*
@@ -3548,9 +4012,7 @@ static int winOpen(
int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
int isCreate = (flags & SQLITE_OPEN_CREATE);
-#ifndef NDEBUG
int isReadonly = (flags & SQLITE_OPEN_READONLY);
-#endif
int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
#ifndef NDEBUG
@@ -3561,6 +4023,9 @@ static int winOpen(
));
#endif
+ OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
+ zUtf8Name, id, flags, pOutFlags));
+
/* Check the following statements are true:
**
** (a) Exactly one of the READWRITE and READONLY flags must be set, and
@@ -3587,8 +4052,9 @@ static int winOpen(
|| eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
);
- assert( id!=0 );
- UNUSED_PARAMETER(pVfs);
+ assert( pFile!=0 );
+ memset(pFile, 0, sizeof(winFile));
+ pFile->h = INVALID_HANDLE_VALUE;
#if SQLITE_OS_WINRT
if( !sqlite3_temp_directory ){
@@ -3597,15 +4063,15 @@ static int winOpen(
}
#endif
- pFile->h = INVALID_HANDLE_VALUE;
-
/* If the second argument to this function is NULL, generate a
** temporary file name to use
*/
if( !zUtf8Name ){
assert(isDelete && !isOpenJournal);
+ memset(zTmpname, 0, MAX_PATH+2);
rc = getTempname(MAX_PATH+2, zTmpname);
if( rc!=SQLITE_OK ){
+ OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
return rc;
}
zUtf8Name = zTmpname;
@@ -3621,11 +4087,13 @@ static int winOpen(
/* Convert the filename to the system encoding. */
zConverted = convertUtf8Filename(zUtf8Name);
if( zConverted==0 ){
+ OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
return SQLITE_IOERR_NOMEM;
}
if( winIsDir(zConverted) ){
sqlite3_free(zConverted);
+ OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
return SQLITE_CANTOPEN_ISDIR;
}
@@ -3716,9 +4184,8 @@ static int winOpen(
#endif
logIoerr(cnt);
- OSTRACE(("OPEN %d %s 0x%lx %s\n",
- h, zName, dwDesiredAccess,
- h==INVALID_HANDLE_VALUE ? "failed" : "ok"));
+ OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
+ dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
if( h==INVALID_HANDLE_VALUE ){
pFile->lastErrno = lastErrno;
@@ -3726,7 +4193,9 @@ static int winOpen(
sqlite3_free(zConverted);
if( isReadWrite && !isExclusive ){
return winOpen(pVfs, zName, id,
- ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags);
+ ((flags|SQLITE_OPEN_READONLY) &
+ ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
+ pOutFlags);
}else{
return SQLITE_CANTOPEN_BKPT;
}
@@ -3740,26 +4209,18 @@ static int winOpen(
}
}
- memset(pFile, 0, sizeof(*pFile));
- pFile->pMethod = &winIoMethod;
- pFile->h = h;
- pFile->lastErrno = NO_ERROR;
- pFile->pVfs = pVfs;
-#ifndef SQLITE_OMIT_WAL
- pFile->pShm = 0;
-#endif
- pFile->zPath = zName;
- if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
- pFile->ctrlFlags |= WINFILE_PSOW;
- }
+ OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, "
+ "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ?
+ *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
#if SQLITE_OS_WINCE
if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
- && !winceCreateLock(zName, pFile)
+ && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
){
osCloseHandle(h);
sqlite3_free(zConverted);
- return SQLITE_CANTOPEN_BKPT;
+ OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
+ return rc;
}
if( isTemp ){
pFile->zDeleteOnClose = zConverted;
@@ -3769,6 +4230,25 @@ static int winOpen(
sqlite3_free(zConverted);
}
+ pFile->pMethod = &winIoMethod;
+ pFile->pVfs = pVfs;
+ pFile->h = h;
+ if( isReadonly ){
+ pFile->ctrlFlags |= WINFILE_RDONLY;
+ }
+ if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
+ pFile->ctrlFlags |= WINFILE_PSOW;
+ }
+ pFile->lastErrno = NO_ERROR;
+ pFile->zPath = zName;
+#if SQLITE_MAX_MMAP_SIZE>0
+ pFile->hMap = NULL;
+ pFile->pMapRegion = 0;
+ pFile->mmapSize = 0;
+ pFile->mmapSizeActual = 0;
+ pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
+#endif
+
OpenCounter(+1);
return rc;
}
@@ -3799,6 +4279,8 @@ static int winDelete(
UNUSED_PARAMETER(syncDir);
SimulateIOError(return SQLITE_IOERR_DELETE);
+ OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
+
zConverted = convertUtf8Filename(zFilename);
if( zConverted==0 ){
return SQLITE_IOERR_NOMEM;
@@ -3812,14 +4294,26 @@ static int winDelete(
&sAttrData) ){
attr = sAttrData.dwFileAttributes;
}else{
- rc = SQLITE_OK; /* Already gone? */
+ lastErrno = osGetLastError();
+ if( lastErrno==ERROR_FILE_NOT_FOUND
+ || lastErrno==ERROR_PATH_NOT_FOUND ){
+ rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
+ }else{
+ rc = SQLITE_ERROR;
+ }
break;
}
#else
attr = osGetFileAttributesW(zConverted);
#endif
if ( attr==INVALID_FILE_ATTRIBUTES ){
- rc = SQLITE_OK; /* Already gone? */
+ lastErrno = osGetLastError();
+ if( lastErrno==ERROR_FILE_NOT_FOUND
+ || lastErrno==ERROR_PATH_NOT_FOUND ){
+ rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
+ }else{
+ rc = SQLITE_ERROR;
+ }
break;
}
if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
@@ -3841,7 +4335,13 @@ static int winDelete(
do {
attr = osGetFileAttributesA(zConverted);
if ( attr==INVALID_FILE_ATTRIBUTES ){
- rc = SQLITE_OK; /* Already gone? */
+ lastErrno = osGetLastError();
+ if( lastErrno==ERROR_FILE_NOT_FOUND
+ || lastErrno==ERROR_PATH_NOT_FOUND ){
+ rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
+ }else{
+ rc = SQLITE_ERROR;
+ }
break;
}
if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
@@ -3859,19 +4359,19 @@ static int winDelete(
} while(1);
}
#endif
- if( rc ){
+ if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
rc = winLogError(SQLITE_IOERR_DELETE, lastErrno,
"winDelete", zFilename);
}else{
logIoerr(cnt);
}
sqlite3_free(zConverted);
- OSTRACE(("DELETE \"%s\" %s\n", zFilename, (rc ? "failed" : "ok" )));
+ OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
return rc;
}
/*
-** Check the existance and status of a file.
+** Check the existence and status of a file.
*/
static int winAccess(
sqlite3_vfs *pVfs, /* Not used on win32 */
@@ -3886,8 +4386,12 @@ static int winAccess(
UNUSED_PARAMETER(pVfs);
SimulateIOError( return SQLITE_IOERR_ACCESS; );
+ OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
+ zFilename, flags, pResOut));
+
zConverted = convertUtf8Filename(zFilename);
if( zConverted==0 ){
+ OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
return SQLITE_IOERR_NOMEM;
}
if( isNT() ){
@@ -3938,6 +4442,8 @@ static int winAccess(
assert(!"Invalid flags argument");
}
*pResOut = rc;
+ OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
+ zFilename, pResOut, *pResOut));
return SQLITE_OK;
}
@@ -4005,16 +4511,12 @@ static int winFullPathname(
*/
char zOut[MAX_PATH+1];
memset(zOut, 0, MAX_PATH+1);
- cygwin_conv_to_win32_path(zRelative, zOut); /* POSIX to Win32 */
+ cygwin_conv_path(CCP_POSIX_TO_WIN_A|CCP_RELATIVE, zRelative, zOut,
+ MAX_PATH+1);
sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
sqlite3_data_directory, zOut);
}else{
- /*
- ** NOTE: The Cygwin docs state that the maximum length needed
- ** for the buffer passed to cygwin_conv_to_full_win32_path
- ** is MAX_PATH.
- */
- cygwin_conv_to_full_win32_path(zRelative, zFull);
+ cygwin_conv_path(CCP_POSIX_TO_WIN_A, zRelative, zFull, nFull);
}
return SQLITE_OK;
#endif
@@ -4039,7 +4541,7 @@ static int winFullPathname(
#endif
#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
- int nByte;
+ DWORD nByte;
void *zConverted;
char *zOut;
@@ -4073,13 +4575,27 @@ static int winFullPathname(
}
if( isNT() ){
LPWSTR zTemp;
- nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0) + 3;
- zTemp = sqlite3_malloc( nByte*sizeof(zTemp[0]) );
+ nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
+ if( nByte==0 ){
+ winLogError(SQLITE_ERROR, osGetLastError(),
+ "GetFullPathNameW1", zConverted);
+ sqlite3_free(zConverted);
+ return SQLITE_CANTOPEN_FULLPATH;
+ }
+ nByte += 3;
+ zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
if( zTemp==0 ){
sqlite3_free(zConverted);
return SQLITE_IOERR_NOMEM;
}
- osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
+ nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
+ if( nByte==0 ){
+ winLogError(SQLITE_ERROR, osGetLastError(),
+ "GetFullPathNameW2", zConverted);
+ sqlite3_free(zConverted);
+ sqlite3_free(zTemp);
+ return SQLITE_CANTOPEN_FULLPATH;
+ }
sqlite3_free(zConverted);
zOut = unicodeToUtf8(zTemp);
sqlite3_free(zTemp);
@@ -4087,13 +4603,27 @@ static int winFullPathname(
#ifdef SQLITE_WIN32_HAS_ANSI
else{
char *zTemp;
- nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0) + 3;
- zTemp = sqlite3_malloc( nByte*sizeof(zTemp[0]) );
+ nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
+ if( nByte==0 ){
+ winLogError(SQLITE_ERROR, osGetLastError(),
+ "GetFullPathNameA1", zConverted);
+ sqlite3_free(zConverted);
+ return SQLITE_CANTOPEN_FULLPATH;
+ }
+ nByte += 3;
+ zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
if( zTemp==0 ){
sqlite3_free(zConverted);
return SQLITE_IOERR_NOMEM;
}
- osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
+ nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
+ if( nByte==0 ){
+ winLogError(SQLITE_ERROR, osGetLastError(),
+ "GetFullPathNameA2", zConverted);
+ sqlite3_free(zConverted);
+ sqlite3_free(zTemp);
+ return SQLITE_CANTOPEN_FULLPATH;
+ }
sqlite3_free(zConverted);
zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
sqlite3_free(zTemp);
@@ -4144,9 +4674,9 @@ static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
UNUSED_PARAMETER(pVfs);
getLastErrorMsg(osGetLastError(), nBuf, zBufOut);
}
-static void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
+static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
UNUSED_PARAMETER(pVfs);
- return (void(*)(void))osGetProcAddressA((HANDLE)pHandle, zSymbol);
+ return (void(*)(void))osGetProcAddressA((HANDLE)pH, zSym);
}
static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
UNUSED_PARAMETER(pVfs);
@@ -4244,7 +4774,8 @@ static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
#endif
/* 2^32 - to avoid use of LL and warnings in gcc */
static const sqlite3_int64 max32BitValue =
- (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + (sqlite3_int64)294967296;
+ (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
+ (sqlite3_int64)294967296;
#if SQLITE_OS_WINCE
SYSTEMTIME time;
@@ -4351,9 +4882,8 @@ int sqlite3_os_init(void){
/* Double-check that the aSyscall[] array has been constructed
** correctly. See ticket [bb3a86e890c8e96ab] */
- assert( ArraySize(aSyscall)==73 );
+ assert( ArraySize(aSyscall)==74 );
-#ifndef SQLITE_OMIT_WAL
/* get memory map allocation granularity */
memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
#if SQLITE_OS_WINRT
@@ -4361,8 +4891,8 @@ int sqlite3_os_init(void){
#else
osGetSystemInfo(&winSysInfo);
#endif
- assert(winSysInfo.dwAllocationGranularity > 0);
-#endif
+ assert( winSysInfo.dwAllocationGranularity>0 );
+ assert( winSysInfo.dwPageSize>0 );
sqlite3_vfs_register(&winVfs, 1);
return SQLITE_OK;
@@ -4370,7 +4900,7 @@ int sqlite3_os_init(void){
int sqlite3_os_end(void){
#if SQLITE_OS_WINRT
- if( sleepObj != NULL ){
+ if( sleepObj!=NULL ){
osCloseHandle(sleepObj);
sleepObj = NULL;
}
diff --git a/src/pager.c b/src/pager.c
index faf0223..61727fa 100644
--- a/src/pager.c
+++ b/src/pager.c
@@ -273,7 +273,7 @@ int sqlite3PagerTrace=1; /* True to enable tracing */
** * A write transaction is active.
** * An EXCLUSIVE or greater lock is held on the database file.
** * All writing and syncing of journal and database data has finished.
-** If no error occured, all that remains is to finalize the journal to
+** If no error occurred, all that remains is to finalize the journal to
** commit the transaction. If an error did occur, the caller will need
** to rollback the transaction.
**
@@ -521,7 +521,7 @@ struct PagerSavepoint {
**
** doNotSpill, doNotSyncSpill
**
-** These two boolean variables control the behaviour of cache-spills
+** These two boolean variables control the behavior of cache-spills
** (calls made by the pcache module to the pagerStress() routine to
** write cached data to the file-system in order to free up memory).
**
@@ -655,6 +655,11 @@ struct Pager {
PagerSavepoint *aSavepoint; /* Array of active savepoints */
int nSavepoint; /* Number of elements in aSavepoint[] */
char dbFileVers[16]; /* Changes whenever database file changes */
+
+ u8 bUseFetch; /* True to use xFetch() */
+ int nMmapOut; /* Number of mmap pages currently outstanding */
+ sqlite3_int64 szMmap; /* Desired maximum mmap size */
+ PgHdr *pMmapFreelist; /* List of free mmap page headers (pDirty) */
/*
** End of the routinely-changing class members
***************************************************************************/
@@ -766,6 +771,16 @@ static const unsigned char aJournalMagic[] = {
#endif
/*
+** The macro USEFETCH is true if we are allowed to use the xFetch and xUnfetch
+** interfaces to access the database using memory-mapped I/O.
+*/
+#if SQLITE_MAX_MMAP_SIZE>0
+# define USEFETCH(x) ((x)->bUseFetch)
+#else
+# define USEFETCH(x) 0
+#endif
+
+/*
** The maximum legal page number is (2^31 - 1).
*/
#define PAGER_MAX_PGNO 2147483647
@@ -1399,7 +1414,7 @@ static int writeJournalHdr(Pager *pPager){
memset(zHeader, 0, sizeof(aJournalMagic)+4);
}
- /* The random check-hash initialiser */
+ /* The random check-hash initializer */
sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
/* The initial database size */
@@ -1838,6 +1853,8 @@ static int pager_error(Pager *pPager, int rc){
return rc;
}
+static int pager_truncate(Pager *pPager, Pgno nPage);
+
/*
** This routine ends a transaction. A transaction is usually ended by
** either a COMMIT or a ROLLBACK operation. This routine may be called
@@ -1891,7 +1908,7 @@ static int pager_error(Pager *pPager, int rc){
** to the first error encountered (the journal finalization one) is
** returned.
*/
-static int pager_end_transaction(Pager *pPager, int hasMaster){
+static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){
int rc = SQLITE_OK; /* Error code from journal finalization operation */
int rc2 = SQLITE_OK; /* Error code from db file unlock operation */
@@ -1941,12 +1958,13 @@ static int pager_end_transaction(Pager *pPager, int hasMaster){
** file should be closed and deleted. If this connection writes to
** the database file, it will do so using an in-memory journal.
*/
+ int bDelete = (!pPager->tempFile && sqlite3JournalExists(pPager->jfd));
assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
|| pPager->journalMode==PAGER_JOURNALMODE_MEMORY
|| pPager->journalMode==PAGER_JOURNALMODE_WAL
);
sqlite3OsClose(pPager->jfd);
- if( !pPager->tempFile ){
+ if( bDelete ){
rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
}
}
@@ -1976,7 +1994,17 @@ static int pager_end_transaction(Pager *pPager, int hasMaster){
*/
rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
assert( rc2==SQLITE_OK );
+ }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
+ /* This branch is taken when committing a transaction in rollback-journal
+ ** mode if the database file on disk is larger than the database image.
+ ** At this point the journal has been finalized and the transaction
+ ** successfully committed, but the EXCLUSIVE lock is still held on the
+ ** file. So it is safe to truncate the database file to its minimum
+ ** required size. */
+ assert( pPager->eLock==EXCLUSIVE_LOCK );
+ rc = pager_truncate(pPager, pPager->dbSize);
}
+
if( !pPager->exclusiveMode
&& (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
){
@@ -2015,7 +2043,7 @@ static void pagerUnlockAndRollback(Pager *pPager){
sqlite3EndBenignMalloc();
}else if( !pPager->exclusiveMode ){
assert( pPager->eState==PAGER_READER );
- pager_end_transaction(pPager, 0);
+ pager_end_transaction(pPager, 0, 0);
}
}
pager_unlock(pPager);
@@ -2239,7 +2267,7 @@ static int pager_playback_one_page(
i64 ofst = (pgno-1)*(i64)pPager->pageSize;
testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
assert( !pagerUseWal(pPager) );
- rc = sqlite3OsWrite(pPager->fd, (u8*)aData, pPager->pageSize, ofst);
+ rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
if( pgno>pPager->dbFileSize ){
pPager->dbFileSize = pgno;
}
@@ -2510,6 +2538,21 @@ static int pager_truncate(Pager *pPager, Pgno nPage){
}
/*
+** Return a sanitized version of the sector-size of OS file pFile. The
+** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE.
+*/
+int sqlite3SectorSize(sqlite3_file *pFile){
+ int iRet = sqlite3OsSectorSize(pFile);
+ if( iRet<32 ){
+ iRet = 512;
+ }else if( iRet>MAX_SECTOR_SIZE ){
+ assert( MAX_SECTOR_SIZE>=512 );
+ iRet = MAX_SECTOR_SIZE;
+ }
+ return iRet;
+}
+
+/*
** Set the value of the Pager.sectorSize variable for the given
** pager based on the value returned by the xSectorSize method
** of the open database file. The sector size will be used used
@@ -2544,14 +2587,7 @@ static void setSectorSize(Pager *pPager){
** call will segfault. */
pPager->sectorSize = 512;
}else{
- pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
- if( pPager->sectorSize<32 ){
- pPager->sectorSize = 512;
- }
- if( pPager->sectorSize>MAX_SECTOR_SIZE ){
- assert( MAX_SECTOR_SIZE>=512 );
- pPager->sectorSize = MAX_SECTOR_SIZE;
- }
+ pPager->sectorSize = sqlite3SectorSize(pPager->fd);
}
}
@@ -2622,6 +2658,7 @@ static int pager_playback(Pager *pPager, int isHot){
int res = 1; /* Value returned by sqlite3OsAccess() */
char *zMaster = 0; /* Name of master journal file if any */
int needPagerReset; /* True to reset page prior to first page rollback */
+ int nPlayback = 0; /* Total number of pages restored from journal */
/* Figure out how many records are in the journal. Abort early if
** the journal is empty.
@@ -2722,7 +2759,9 @@ static int pager_playback(Pager *pPager, int isHot){
needPagerReset = 0;
}
rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
- if( rc!=SQLITE_OK ){
+ if( rc==SQLITE_OK ){
+ nPlayback++;
+ }else{
if( rc==SQLITE_DONE ){
pPager->journalOff = szJ;
break;
@@ -2782,7 +2821,7 @@ end_playback:
rc = sqlite3PagerSync(pPager);
}
if( rc==SQLITE_OK ){
- rc = pager_end_transaction(pPager, zMaster[0]!='\0');
+ rc = pager_end_transaction(pPager, zMaster[0]!='\0', 0);
testcase( rc!=SQLITE_OK );
}
if( rc==SQLITE_OK && zMaster[0] && res ){
@@ -2792,6 +2831,10 @@ end_playback:
rc = pager_delmaster(pPager, zMaster);
testcase( rc!=SQLITE_OK );
}
+ if( isHot && nPlayback ){
+ sqlite3_log(SQLITE_NOTICE_RECOVER_ROLLBACK, "recovered %d pages from %s",
+ nPlayback, pPager->zJournal);
+ }
/* The Pager.sectorSize variable may have been updated while rolling
** back a journal created by a process with a different sector size
@@ -2813,11 +2856,10 @@ end_playback:
** If an IO error occurs, then the IO error is returned to the caller.
** Otherwise, SQLITE_OK is returned.
*/
-static int readDbPage(PgHdr *pPg){
+static int readDbPage(PgHdr *pPg, u32 iFrame){
Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
Pgno pgno = pPg->pgno; /* Page number to read */
int rc = SQLITE_OK; /* Return code */
- int isInWal = 0; /* True if page is in log file */
int pgsz = pPager->pageSize; /* Number of bytes to read */
assert( pPager->eState>=PAGER_READER && !MEMDB );
@@ -2829,11 +2871,13 @@ static int readDbPage(PgHdr *pPg){
return SQLITE_OK;
}
- if( pagerUseWal(pPager) ){
+#ifndef SQLITE_OMIT_WAL
+ if( iFrame ){
/* Try to pull the page from the write-ahead log. */
- rc = sqlite3WalRead(pPager->pWal, pgno, &isInWal, pgsz, pPg->pData);
- }
- if( rc==SQLITE_OK && !isInWal ){
+ rc = sqlite3WalReadFrame(pPager->pWal, iFrame, pgsz, pPg->pData);
+ }else
+#endif
+ {
i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
if( rc==SQLITE_IOERR_SHORT_READ ){
@@ -2912,12 +2956,17 @@ static int pagerUndoCallback(void *pCtx, Pgno iPg){
Pager *pPager = (Pager *)pCtx;
PgHdr *pPg;
+ assert( pagerUseWal(pPager) );
pPg = sqlite3PagerLookup(pPager, iPg);
if( pPg ){
if( sqlite3PcachePageRefcount(pPg)==1 ){
sqlite3PcacheDrop(pPg);
}else{
- rc = readDbPage(pPg);
+ u32 iFrame = 0;
+ rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
+ if( rc==SQLITE_OK ){
+ rc = readDbPage(pPg, iFrame);
+ }
if( rc==SQLITE_OK ){
pPager->xReiniter(pPg);
}
@@ -3061,6 +3110,7 @@ static int pagerBeginReadTransaction(Pager *pPager){
rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
if( rc!=SQLITE_OK || changed ){
pager_reset(pPager);
+ if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
}
return rc;
@@ -3151,6 +3201,7 @@ static int pagerOpenWalIfPresent(Pager *pPager){
if( rc ) return rc;
if( nPage==0 ){
rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
+ if( rc==SQLITE_IOERR_DELETE_NOENT ) rc = SQLITE_OK;
isWal = 0;
}else{
rc = sqlite3OsAccess(
@@ -3322,6 +3373,29 @@ void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
}
/*
+** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap.
+*/
+static void pagerFixMaplimit(Pager *pPager){
+#if SQLITE_MAX_MMAP_SIZE>0
+ sqlite3_file *fd = pPager->fd;
+ if( isOpen(fd) ){
+ sqlite3_int64 sz;
+ pPager->bUseFetch = (fd->pMethods->iVersion>=3) && pPager->szMmap>0;
+ sz = pPager->szMmap;
+ sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
+ }
+#endif
+}
+
+/*
+** Change the maximum size of any memory mapping made of the database file.
+*/
+void sqlite3PagerSetMmapLimit(Pager *pPager, sqlite3_int64 szMmap){
+ pPager->szMmap = szMmap;
+ pagerFixMaplimit(pPager);
+}
+
+/*
** Free as much memory as possible from the pager.
*/
void sqlite3PagerShrink(Pager *pPager){
@@ -3468,9 +3542,16 @@ void sqlite3PagerSetBusyhandler(
Pager *pPager, /* Pager object */
int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
-){
+){
pPager->xBusyHandler = xBusyHandler;
pPager->pBusyHandlerArg = pBusyHandlerArg;
+
+ if( isOpen(pPager->fd) ){
+ void **ap = (void **)&pPager->xBusyHandler;
+ assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
+ assert( ap[1]==pBusyHandlerArg );
+ sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
+ }
}
/*
@@ -3549,6 +3630,7 @@ int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
assert( nReserve>=0 && nReserve<1000 );
pPager->nReserve = (i16)nReserve;
pagerReportSize(pPager);
+ pagerFixMaplimit(pPager);
}
return rc;
}
@@ -3702,7 +3784,7 @@ static int pager_wait_on_lock(Pager *pPager, int locktype){
** dirty page were to be discarded from the cache via the pagerStress()
** routine, pagerStress() would not write the current page content to
** the database file. If a savepoint transaction were rolled back after
-** this happened, the correct behaviour would be to restore the current
+** this happened, the correct behavior would be to restore the current
** content of the page. However, since this content is not present in either
** the database file or the portion of the rollback journal and
** sub-journal rolled back the content could not be restored and the
@@ -3726,12 +3808,26 @@ static void assertTruncateConstraint(Pager *pPager){
** function does not actually modify the database file on disk. It
** just sets the internal state of the pager object so that the
** truncation will be done when the current transaction is committed.
+**
+** This function is only called right before committing a transaction.
+** Once this function has been called, the transaction must either be
+** rolled back or committed. It is not safe to call this function and
+** then continue writing to the database.
*/
void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
assert( pPager->dbSize>=nPage );
assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
pPager->dbSize = nPage;
- assertTruncateConstraint(pPager);
+
+ /* At one point the code here called assertTruncateConstraint() to
+ ** ensure that all pages being truncated away by this operation are,
+ ** if one or more savepoints are open, present in the savepoint
+ ** journal so that they can be restored if the savepoint is rolled
+ ** back. This is no longer necessary as this function is now only
+ ** called right before committing a transaction. So although the
+ ** Pager object may still have open savepoints (Pager.nSavepoint!=0),
+ ** they cannot be rolled back. So the assertTruncateConstraint() call
+ ** is no longer correct. */
}
@@ -3761,6 +3857,81 @@ static int pagerSyncHotJournal(Pager *pPager){
}
/*
+** Obtain a reference to a memory mapped page object for page number pgno.
+** The new object will use the pointer pData, obtained from xFetch().
+** If successful, set *ppPage to point to the new page reference
+** and return SQLITE_OK. Otherwise, return an SQLite error code and set
+** *ppPage to zero.
+**
+** Page references obtained by calling this function should be released
+** by calling pagerReleaseMapPage().
+*/
+static int pagerAcquireMapPage(
+ Pager *pPager, /* Pager object */
+ Pgno pgno, /* Page number */
+ void *pData, /* xFetch()'d data for this page */
+ PgHdr **ppPage /* OUT: Acquired page object */
+){
+ PgHdr *p; /* Memory mapped page to return */
+
+ if( pPager->pMmapFreelist ){
+ *ppPage = p = pPager->pMmapFreelist;
+ pPager->pMmapFreelist = p->pDirty;
+ p->pDirty = 0;
+ memset(p->pExtra, 0, pPager->nExtra);
+ }else{
+ *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
+ if( p==0 ){
+ sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
+ return SQLITE_NOMEM;
+ }
+ p->pExtra = (void *)&p[1];
+ p->flags = PGHDR_MMAP;
+ p->nRef = 1;
+ p->pPager = pPager;
+ }
+
+ assert( p->pExtra==(void *)&p[1] );
+ assert( p->pPage==0 );
+ assert( p->flags==PGHDR_MMAP );
+ assert( p->pPager==pPager );
+ assert( p->nRef==1 );
+
+ p->pgno = pgno;
+ p->pData = pData;
+ pPager->nMmapOut++;
+
+ return SQLITE_OK;
+}
+
+/*
+** Release a reference to page pPg. pPg must have been returned by an
+** earlier call to pagerAcquireMapPage().
+*/
+static void pagerReleaseMapPage(PgHdr *pPg){
+ Pager *pPager = pPg->pPager;
+ pPager->nMmapOut--;
+ pPg->pDirty = pPager->pMmapFreelist;
+ pPager->pMmapFreelist = pPg;
+
+ assert( pPager->fd->pMethods->iVersion>=3 );
+ sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
+}
+
+/*
+** Free all PgHdr objects stored in the Pager.pMmapFreelist list.
+*/
+static void pagerFreeMapHdrs(Pager *pPager){
+ PgHdr *p;
+ PgHdr *pNext;
+ for(p=pPager->pMmapFreelist; p; p=pNext){
+ pNext = p->pDirty;
+ sqlite3_free(p);
+ }
+}
+
+
+/*
** Shutdown the page cache. Free all memory and close all files.
**
** If a transaction was in progress when this routine is called, that
@@ -3780,6 +3951,7 @@ int sqlite3PagerClose(Pager *pPager){
assert( assert_pager_state(pPager) );
disable_simulated_io_errors();
sqlite3BeginBenignMalloc();
+ pagerFreeMapHdrs(pPager);
/* pPager->errCode = 0; */
pPager->exclusiveMode = 0;
#ifndef SQLITE_OMIT_WAL
@@ -4041,7 +4213,9 @@ static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
** file size will be.
*/
assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
- if( rc==SQLITE_OK && pPager->dbSize>pPager->dbHintSize ){
+ if( rc==SQLITE_OK
+ && (pList->pDirty ? pPager->dbSize : pList->pgno+1)>pPager->dbHintSize
+ ){
sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
pPager->dbHintSize = pPager->dbSize;
@@ -4448,7 +4622,7 @@ int sqlite3PagerOpen(
memcpy(pPager->zFilename, zPathname, nPathname);
if( nUri ) memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
memcpy(pPager->zJournal, zPathname, nPathname);
- memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+1);
+ memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+2);
sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
#ifndef SQLITE_OMIT_WAL
pPager->zWal = &pPager->zJournal[nPathname+8+1];
@@ -4595,6 +4769,7 @@ int sqlite3PagerOpen(
/* pPager->pBusyHandlerArg = 0; */
pPager->xReiniter = xReinit;
/* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
+ /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
*ppPager = pPager;
return SQLITE_OK;
@@ -4784,6 +4959,11 @@ int sqlite3PagerSharedLock(Pager *pPager){
goto failed;
}
if( bHotJournal ){
+ if( pPager->readOnly ){
+ rc = SQLITE_READONLY_ROLLBACK;
+ goto failed;
+ }
+
/* Get an EXCLUSIVE lock on the database file. At this point it is
** important that a RESERVED lock is not obtained on the way to the
** EXCLUSIVE lock. If it were, another process might open the
@@ -4881,9 +5061,11 @@ int sqlite3PagerSharedLock(Pager *pPager){
);
}
- if( !pPager->tempFile
- && (pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0)
- ){
+ if( !pPager->tempFile && (
+ pPager->pBackup
+ || sqlite3PcachePagecount(pPager->pPCache)>0
+ || USEFETCH(pPager)
+ )){
/* The shared-lock has just been acquired on the database file
** and there are already pages in the cache (from a previous
** read or write transaction). Check to see if the database
@@ -4909,7 +5091,7 @@ int sqlite3PagerSharedLock(Pager *pPager){
if( nPage>0 ){
IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
- if( rc!=SQLITE_OK ){
+ if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
goto failed;
}
}else{
@@ -4918,6 +5100,16 @@ int sqlite3PagerSharedLock(Pager *pPager){
if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
pager_reset(pPager);
+
+ /* Unmap the database file. It is possible that external processes
+ ** may have truncated the database file and then extended it back
+ ** to its original size while this process was not holding a lock.
+ ** In this case there may exist a Pager.pMap mapping that appears
+ ** to be the right size but is not actually valid. Avoid this
+ ** possibility by unmapping the db here. */
+ if( USEFETCH(pPager) ){
+ sqlite3OsUnfetch(pPager->fd, 0, 0);
+ }
}
}
@@ -4959,7 +5151,7 @@ int sqlite3PagerSharedLock(Pager *pPager){
** nothing to rollback, so this routine is a no-op.
*/
static void pagerUnlockIfUnused(Pager *pPager){
- if( (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
+ if( pPager->nMmapOut==0 && (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
pagerUnlockAndRollback(pPager);
}
}
@@ -5018,13 +5210,27 @@ int sqlite3PagerAcquire(
Pager *pPager, /* The pager open on the database file */
Pgno pgno, /* Page number to fetch */
DbPage **ppPage, /* Write a pointer to the page here */
- int noContent /* Do not bother reading content from disk if true */
+ int flags /* PAGER_ACQUIRE_XXX flags */
){
- int rc;
- PgHdr *pPg;
+ int rc = SQLITE_OK;
+ PgHdr *pPg = 0;
+ u32 iFrame = 0; /* Frame to read from WAL file */
+ const int noContent = (flags & PAGER_ACQUIRE_NOCONTENT);
+
+ /* It is acceptable to use a read-only (mmap) page for any page except
+ ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
+ ** flag was specified by the caller. And so long as the db is not a
+ ** temporary or in-memory database. */
+ const int bMmapOk = (pgno!=1 && USEFETCH(pPager)
+ && (pPager->eState==PAGER_READER || (flags & PAGER_ACQUIRE_READONLY))
+#ifdef SQLITE_HAS_CODEC
+ && pPager->xCodec==0
+#endif
+ );
assert( pPager->eState>=PAGER_READER );
assert( assert_pager_state(pPager) );
+ assert( noContent==0 || bMmapOk==0 );
if( pgno==0 ){
return SQLITE_CORRUPT_BKPT;
@@ -5035,6 +5241,39 @@ int sqlite3PagerAcquire(
if( pPager->errCode!=SQLITE_OK ){
rc = pPager->errCode;
}else{
+
+ if( bMmapOk && pagerUseWal(pPager) ){
+ rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
+ if( rc!=SQLITE_OK ) goto pager_acquire_err;
+ }
+
+ if( iFrame==0 && bMmapOk ){
+ void *pData = 0;
+
+ rc = sqlite3OsFetch(pPager->fd,
+ (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
+ );
+
+ if( rc==SQLITE_OK && pData ){
+ if( pPager->eState>PAGER_READER ){
+ (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
+ }
+ if( pPg==0 ){
+ rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
+ }else{
+ sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
+ }
+ if( pPg ){
+ assert( rc==SQLITE_OK );
+ *ppPage = pPg;
+ return SQLITE_OK;
+ }
+ }
+ if( rc!=SQLITE_OK ){
+ goto pager_acquire_err;
+ }
+ }
+
rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
}
@@ -5093,9 +5332,13 @@ int sqlite3PagerAcquire(
memset(pPg->pData, 0, pPager->pageSize);
IOTRACE(("ZERO %p %d\n", pPager, pgno));
}else{
+ if( pagerUseWal(pPager) && bMmapOk==0 ){
+ rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
+ if( rc!=SQLITE_OK ) goto pager_acquire_err;
+ }
assert( pPg->pPager==pPager );
pPager->aStat[PAGER_STAT_MISS]++;
- rc = readDbPage(pPg);
+ rc = readDbPage(pPg, iFrame);
if( rc!=SQLITE_OK ){
goto pager_acquire_err;
}
@@ -5148,7 +5391,11 @@ DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
void sqlite3PagerUnref(DbPage *pPg){
if( pPg ){
Pager *pPager = pPg->pPager;
- sqlite3PcacheRelease(pPg);
+ if( pPg->flags & PGHDR_MMAP ){
+ pagerReleaseMapPage(pPg);
+ }else{
+ sqlite3PcacheRelease(pPg);
+ }
pagerUnlockIfUnused(pPager);
}
}
@@ -5483,6 +5730,7 @@ int sqlite3PagerWrite(DbPage *pDbPage){
Pager *pPager = pPg->pPager;
Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
+ assert( (pPg->flags & PGHDR_MMAP)==0 );
assert( pPager->eState>=PAGER_WRITER_LOCKED );
assert( pPager->eState!=PAGER_ERROR );
assert( assert_pager_state(pPager) );
@@ -5650,7 +5898,7 @@ static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
# define DIRECT_MODE isDirectMode
#endif
- if( !pPager->changeCountDone && pPager->dbSize>0 ){
+ if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
PgHdr *pPgHdr; /* Reference to page 1 */
assert( !pPager->tempFile && isOpen(pPager->fd) );
@@ -5682,6 +5930,11 @@ static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
pPager->aStat[PAGER_STAT_WRITE]++;
}
if( rc==SQLITE_OK ){
+ /* Update the pager's copy of the change-counter. Otherwise, the
+ ** next time a read transaction is opened the cache will be
+ ** flushed (as the change-counter values will not match). */
+ const void *pCopy = (const void *)&((const char *)zBuf)[24];
+ memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
pPager->changeCountDone = 1;
}
}else{
@@ -5868,38 +6121,6 @@ int sqlite3PagerCommitPhaseOne(
#endif
if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
- /* If this transaction has made the database smaller, then all pages
- ** being discarded by the truncation must be written to the journal
- ** file. This can only happen in auto-vacuum mode.
- **
- ** Before reading the pages with page numbers larger than the
- ** current value of Pager.dbSize, set dbSize back to the value
- ** that it took at the start of the transaction. Otherwise, the
- ** calls to sqlite3PagerGet() return zeroed pages instead of
- ** reading data from the database file.
- */
- #ifndef SQLITE_OMIT_AUTOVACUUM
- if( pPager->dbSize<pPager->dbOrigSize
- && pPager->journalMode!=PAGER_JOURNALMODE_OFF
- ){
- Pgno i; /* Iterator variable */
- const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
- const Pgno dbSize = pPager->dbSize; /* Database image size */
- pPager->dbSize = pPager->dbOrigSize;
- for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
- if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
- PgHdr *pPage; /* Page to journal */
- rc = sqlite3PagerGet(pPager, i, &pPage);
- if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
- rc = sqlite3PagerWrite(pPage);
- sqlite3PagerUnref(pPage);
- if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
- }
- }
- pPager->dbSize = dbSize;
- }
- #endif
-
/* Write the master journal name into the journal file. If a master
** journal file name has already been written to the journal file,
** or if zMaster is NULL (no master journal), then this call is a no-op.
@@ -5927,11 +6148,14 @@ int sqlite3PagerCommitPhaseOne(
goto commit_phase_one_exit;
}
sqlite3PcacheCleanAll(pPager->pPCache);
-
- /* If the file on disk is not the same size as the database image,
- ** then use pager_truncate to grow or shrink the file here.
- */
- if( pPager->dbSize!=pPager->dbFileSize ){
+
+ /* If the file on disk is smaller than the database image, use
+ ** pager_truncate to grow the file here. This can happen if the database
+ ** image was extended as part of the current transaction and then the
+ ** last page in the db image moved to the free-list. In this case the
+ ** last page is never written out to disk, leaving the database file
+ ** undersized. Fix this now if it is the case. */
+ if( pPager->dbSize>pPager->dbFileSize ){
Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
assert( pPager->eState==PAGER_WRITER_DBMOD );
rc = pager_truncate(pPager, nNew);
@@ -6004,7 +6228,7 @@ int sqlite3PagerCommitPhaseTwo(Pager *pPager){
}
PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
- rc = pager_end_transaction(pPager, pPager->setMaster);
+ rc = pager_end_transaction(pPager, pPager->setMaster, 1);
return pager_error(pPager, rc);
}
@@ -6049,11 +6273,11 @@ int sqlite3PagerRollback(Pager *pPager){
if( pagerUseWal(pPager) ){
int rc2;
rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
- rc2 = pager_end_transaction(pPager, pPager->setMaster);
+ rc2 = pager_end_transaction(pPager, pPager->setMaster, 0);
if( rc==SQLITE_OK ) rc = rc2;
}else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
int eState = pPager->eState;
- rc = pager_end_transaction(pPager, 0);
+ rc = pager_end_transaction(pPager, 0, 0);
if( !MEMDB && eState>PAGER_WRITER_LOCKED ){
/* This can happen using journal_mode=off. Move the pager to the error
** state to indicate that the contents of the cache may not be trusted.
@@ -6068,7 +6292,7 @@ int sqlite3PagerRollback(Pager *pPager){
}
assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
- assert( rc==SQLITE_OK || rc==SQLITE_FULL
+ assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT
|| rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR );
/* If an error occurs during a ROLLBACK, we can no longer trust the pager
@@ -6451,7 +6675,8 @@ int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
*/
if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
needSyncPgno = pPg->pgno;
- assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
+ assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
+ pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
assert( pPg->flags&PGHDR_DIRTY );
}
@@ -6801,11 +7026,12 @@ static int pagerOpenWal(Pager *pPager){
** (e.g. due to malloc() failure), return an error code.
*/
if( rc==SQLITE_OK ){
- rc = sqlite3WalOpen(pPager->pVfs,
+ rc = sqlite3WalOpen(pPager->pVfs,
pPager->fd, pPager->zWal, pPager->exclusiveMode,
pPager->journalSizeLimit, &pPager->pWal
);
}
+ pagerFixMaplimit(pPager);
return rc;
}
@@ -6896,11 +7122,14 @@ int sqlite3PagerCloseWal(Pager *pPager){
rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
pPager->pageSize, (u8*)pPager->pTmpSpace);
pPager->pWal = 0;
+ pagerFixMaplimit(pPager);
}
}
return rc;
}
+#endif /* !SQLITE_OMIT_WAL */
+
#ifdef SQLITE_ENABLE_ZIPVFS
/*
** A read-lock must be held on the pager when this function is called. If
@@ -6930,11 +7159,9 @@ void *sqlite3PagerCodec(PgHdr *pPg){
}
#endif /* SQLITE_HAS_CODEC */
-#endif /* !SQLITE_OMIT_WAL */
-
#endif /* SQLITE_OMIT_DISKIO */
-/* BEGIN CRYPTO */
+/* BEGIN SQLCIPHER */
#ifdef SQLITE_HAS_CODEC
void sqlite3pager_get_codec(Pager *pPager, void **ctx) {
*ctx = pPager->pCodec;
@@ -6963,5 +7190,5 @@ void sqlite3pager_sqlite3PagerSetError( Pager *pPager, int error) {
}
#endif
-/* END CRYPTO */
+/* END SQLCIPHER */
diff --git a/src/pager.h b/src/pager.h
index 2b60e05..6f65913 100644
--- a/src/pager.h
+++ b/src/pager.h
@@ -79,6 +79,12 @@ typedef struct PgHdr DbPage;
#define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
/*
+** Flags that make up the mask passed to sqlite3PagerAcquire().
+*/
+#define PAGER_ACQUIRE_NOCONTENT 0x01 /* Do not load data from disk */
+#define PAGER_ACQUIRE_READONLY 0x02 /* Read-only page is acceptable */
+
+/*
** The remainder of this file contains the declarations of the functions
** that make up the Pager sub-system API. See source code comments for
** a detailed description of each routine.
@@ -102,6 +108,7 @@ void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
int sqlite3PagerSetPagesize(Pager*, u32*, int);
int sqlite3PagerMaxPageCount(Pager*, int);
void sqlite3PagerSetCachesize(Pager*, int);
+void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
void sqlite3PagerShrink(Pager*);
void sqlite3PagerSetSafetyLevel(Pager*,int,int,int);
int sqlite3PagerLockingMode(Pager *, int);
@@ -138,11 +145,14 @@ int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
int sqlite3PagerSharedLock(Pager *pPager);
-int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
-int sqlite3PagerWalSupported(Pager *pPager);
-int sqlite3PagerWalCallback(Pager *pPager);
-int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
-int sqlite3PagerCloseWal(Pager *pPager);
+#ifndef SQLITE_OMIT_WAL
+ int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
+ int sqlite3PagerWalSupported(Pager *pPager);
+ int sqlite3PagerWalCallback(Pager *pPager);
+ int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
+ int sqlite3PagerCloseWal(Pager *pPager);
+#endif
+
#ifdef SQLITE_ENABLE_ZIPVFS
int sqlite3PagerWalFramesize(Pager *pPager);
#endif
@@ -160,6 +170,7 @@ void *sqlite3PagerTempSpace(Pager*);
int sqlite3PagerIsMemdb(Pager*);
void sqlite3PagerCacheStat(Pager *, int, int, int *);
void sqlite3PagerClearCache(Pager *);
+int sqlite3SectorSize(sqlite3_file *);
/* Functions used to truncate the database file. */
void sqlite3PagerTruncateImage(Pager*,Pgno);
diff --git a/src/parse.y b/src/parse.y
index 94433d5..8310b26 100644
--- a/src/parse.y
+++ b/src/parse.y
@@ -435,8 +435,8 @@ oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
// The "distinct" nonterminal is true (1) if the DISTINCT keyword is
// present and false (0) if it is not.
//
-%type distinct {int}
-distinct(A) ::= DISTINCT. {A = 1;}
+%type distinct {u16}
+distinct(A) ::= DISTINCT. {A = SF_Distinct;}
distinct(A) ::= ALL. {A = 0;}
distinct(A) ::= . {A = 0;}
@@ -499,7 +499,8 @@ stl_prefix(A) ::= seltablist(X) joinop(Y). {
if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].jointype = (u8)Y;
}
stl_prefix(A) ::= . {A = 0;}
-seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) indexed_opt(I) on_opt(N) using_opt(U). {
+seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) indexed_opt(I)
+ on_opt(N) using_opt(U). {
A = sqlite3SrcListAppendFromTerm(pParse,X,&Y,&D,&Z,0,N,U);
sqlite3SrcListIndexedBy(pParse, A, &I);
}
@@ -512,25 +513,25 @@ seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) indexed_opt(I) on_opt(N) usi
as(Z) on_opt(N) using_opt(U). {
if( X==0 && Z.n==0 && N==0 && U==0 ){
A = F;
+ }else if( F->nSrc==1 ){
+ A = sqlite3SrcListAppendFromTerm(pParse,X,0,0,&Z,0,N,U);
+ if( A ){
+ struct SrcList_item *pNew = &A->a[A->nSrc-1];
+ struct SrcList_item *pOld = F->a;
+ pNew->zName = pOld->zName;
+ pNew->zDatabase = pOld->zDatabase;
+ pNew->pSelect = pOld->pSelect;
+ pOld->zName = pOld->zDatabase = 0;
+ pOld->pSelect = 0;
+ }
+ sqlite3SrcListDelete(pParse->db, F);
}else{
Select *pSubquery;
sqlite3SrcListShiftJoinType(F);
- pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,0,0,0);
+ pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0,0);
A = sqlite3SrcListAppendFromTerm(pParse,X,0,0,&Z,pSubquery,N,U);
}
}
-
- // A seltablist_paren nonterminal represents anything in a FROM that
- // is contained inside parentheses. This can be either a subquery or
- // a grouping of table and subqueries.
- //
-// %type seltablist_paren {Select*}
-// %destructor seltablist_paren {sqlite3SelectDelete(pParse->db, $$);}
-// seltablist_paren(A) ::= select(S). {A = S;}
-// seltablist_paren(A) ::= seltablist(F). {
-// sqlite3SrcListShiftJoinType(F);
-// A = sqlite3SelectNew(pParse,0,F,0,0,0,0,0,0,0);
-// }
%endif SQLITE_OMIT_SUBQUERY
%type dbnm {Token}
@@ -653,7 +654,8 @@ where_opt(A) ::= WHERE expr(X). {A = X.pExpr;}
////////////////////////// The UPDATE command ////////////////////////////////
//
%ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
-cmd ::= UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y) where_opt(W) orderby_opt(O) limit_opt(L). {
+cmd ::= UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y) where_opt(W)
+ orderby_opt(O) limit_opt(L). {
sqlite3SrcListIndexedBy(pParse, X, &I);
sqlite3ExprListCheckLength(pParse,Y,"set list");
W = sqlite3LimitWhere(pParse, X, W, O, L.pLimit, L.pOffset, "UPDATE");
@@ -661,7 +663,8 @@ cmd ::= UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y) where_opt(W)
}
%endif
%ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
-cmd ::= UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y) where_opt(W). {
+cmd ::= UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y)
+ where_opt(W). {
sqlite3SrcListIndexedBy(pParse, X, &I);
sqlite3ExprListCheckLength(pParse,Y,"set list");
sqlite3Update(pParse,X,Y,W,R);
@@ -815,7 +818,7 @@ expr(A) ::= VARIABLE(X). {
spanSet(&A, &X, &X);
}
expr(A) ::= expr(E) COLLATE ids(C). {
- A.pExpr = sqlite3ExprSetCollByToken(pParse, E.pExpr, &C);
+ A.pExpr = sqlite3ExprAddCollateToken(pParse, E.pExpr, &C);
A.zStart = E.zStart;
A.zEnd = &C.z[C.n];
}
@@ -1140,22 +1143,14 @@ uniqueflag(A) ::= . {A = OE_None;}
idxlist_opt(A) ::= . {A = 0;}
idxlist_opt(A) ::= LP idxlist(X) RP. {A = X;}
idxlist(A) ::= idxlist(X) COMMA nm(Y) collate(C) sortorder(Z). {
- Expr *p = 0;
- if( C.n>0 ){
- p = sqlite3Expr(pParse->db, TK_COLUMN, 0);
- sqlite3ExprSetCollByToken(pParse, p, &C);
- }
+ Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &C);
A = sqlite3ExprListAppend(pParse,X, p);
sqlite3ExprListSetName(pParse,A,&Y,1);
sqlite3ExprListCheckLength(pParse, A, "index");
if( A ) A->a[A->nExpr-1].sortOrder = (u8)Z;
}
idxlist(A) ::= nm(Y) collate(C) sortorder(Z). {
- Expr *p = 0;
- if( C.n>0 ){
- p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
- sqlite3ExprSetCollByToken(pParse, p, &C);
- }
+ Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &C);
A = sqlite3ExprListAppend(pParse,0, p);
sqlite3ExprListSetName(pParse, A, &Y, 1);
sqlite3ExprListCheckLength(pParse, A, "index");
diff --git a/src/pcache.h b/src/pcache.h
index b9135fd..f4d4ad7 100644
--- a/src/pcache.h
+++ b/src/pcache.h
@@ -53,6 +53,8 @@ struct PgHdr {
#define PGHDR_REUSE_UNLIKELY 0x010 /* A hint that reuse is unlikely */
#define PGHDR_DONT_WRITE 0x020 /* Do not write content to disk */
+#define PGHDR_MMAP 0x040 /* This is an mmap page object */
+
/* Initialize and shutdown the page cache subsystem */
int sqlite3PcacheInitialize(void);
void sqlite3PcacheShutdown(void);
diff --git a/src/pragma.c b/src/pragma.c
index e5a9ac2..2297716 100644
--- a/src/pragma.c
+++ b/src/pragma.c
@@ -184,6 +184,9 @@ static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
{ "sql_trace", SQLITE_SqlTrace },
{ "vdbe_listing", SQLITE_VdbeListing },
{ "vdbe_trace", SQLITE_VdbeTrace },
+ { "vdbe_addoptrace", SQLITE_VdbeAddopTrace},
+ { "vdbe_debug", SQLITE_SqlTrace | SQLITE_VdbeListing
+ | SQLITE_VdbeTrace },
#endif
#ifndef SQLITE_OMIT_CHECK
{ "ignore_check_constraints", SQLITE_IgnoreChecks },
@@ -316,12 +319,12 @@ void sqlite3Pragma(
int rc; /* return value form SQLITE_FCNTL_PRAGMA */
sqlite3 *db = pParse->db; /* The database connection */
Db *pDb; /* The specific database being pragmaed */
- Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db); /* Prepared statement */
-/** BEGIN CRYPTO **/
+ Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
+/* BEGIN SQLCIPHER */
#ifdef SQLITE_HAS_CODEC
extern int codec_pragma(sqlite3*, int, Parse *, const char *, const char *);
#endif
-/** END CRYPTO **/
+/* END SQLCIPHER */
if( v==0 ) return;
@@ -363,6 +366,7 @@ void sqlite3Pragma(
aFcntl[1] = zLeft;
aFcntl[2] = zRight;
aFcntl[3] = 0;
+ db->busyHandler.nBusy = 0;
rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
if( rc==SQLITE_OK ){
if( aFcntl[0] ){
@@ -382,13 +386,13 @@ void sqlite3Pragma(
pParse->rc = rc;
}else
-/** BEGIN CRYPTO **/
+/* BEGIN SQLCIPHER */
#ifdef SQLITE_HAS_CODEC
if(codec_pragma(db, iDb, pParse, zLeft, zRight)) {
/* codec_pragma executes internal */
}else
#endif
-/** END CRYPTO **/
+/* END SQLCIPHER */
#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
/*
@@ -411,11 +415,12 @@ void sqlite3Pragma(
static const VdbeOpList getCacheSize[] = {
{ OP_Transaction, 0, 0, 0}, /* 0 */
{ OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
- { OP_IfPos, 1, 7, 0},
+ { OP_IfPos, 1, 8, 0},
{ OP_Integer, 0, 2, 0},
{ OP_Subtract, 1, 2, 1},
- { OP_IfPos, 1, 7, 0},
+ { OP_IfPos, 1, 8, 0},
{ OP_Integer, 0, 1, 0}, /* 6 */
+ { OP_Noop, 0, 0, 0},
{ OP_ResultRow, 1, 1, 0},
};
int addr;
@@ -754,6 +759,43 @@ void sqlite3Pragma(
}else
/*
+ ** PRAGMA [database.]mmap_size(N)
+ **
+ ** Used to set mapping size limit. The mapping size limit is
+ ** used to limit the aggregate size of all memory mapped regions of the
+ ** database file. If this parameter is set to zero, then memory mapping
+ ** is not used at all. If N is negative, then the default memory map
+ ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
+ ** The parameter N is measured in bytes.
+ **
+ ** This value is advisory. The underlying VFS is free to memory map
+ ** as little or as much as it wants. Except, if N is set to 0 then the
+ ** upper layers will never invoke the xFetch interfaces to the VFS.
+ */
+ if( sqlite3StrICmp(zLeft,"mmap_size")==0 ){
+ sqlite3_int64 sz;
+ assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
+ if( zRight ){
+ int ii;
+ sqlite3Atoi64(zRight, &sz, 1000, SQLITE_UTF8);
+ if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
+ if( pId2->n==0 ) db->szMmap = sz;
+ for(ii=db->nDb-1; ii>=0; ii--){
+ if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
+ sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
+ }
+ }
+ }
+ sz = -1;
+ if( sqlite3_file_control(db,zDb,SQLITE_FCNTL_MMAP_SIZE,&sz)==SQLITE_OK ){
+#if SQLITE_MAX_MMAP_SIZE==0
+ sz = 0;
+#endif
+ returnSingleInt(pParse, "mmap_size", sz);
+ }
+ }else
+
+ /*
** PRAGMA temp_store
** PRAGMA temp_store = "default"|"memory"|"file"
**
@@ -960,11 +1002,14 @@ void sqlite3Pragma(
if( sqlite3ReadSchema(pParse) ) goto pragma_out;
pTab = sqlite3FindTable(db, zRight, zDb);
if( pTab ){
- int i;
+ int i, k;
int nHidden = 0;
Column *pCol;
+ Index *pPk;
+ for(pPk=pTab->pIndex; pPk && pPk->autoIndex!=2; pPk=pPk->pNext){}
sqlite3VdbeSetNumCols(v, 6);
pParse->nMem = 6;
+ sqlite3CodeVerifySchema(pParse, iDb);
sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
@@ -987,7 +1032,14 @@ void sqlite3Pragma(
}else{
sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
}
- sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
+ if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
+ k = 0;
+ }else if( pPk==0 ){
+ k = 1;
+ }else{
+ for(k=1; ALWAYS(k<=pTab->nCol) && pPk->aiColumn[k-1]!=i; k++){}
+ }
+ sqlite3VdbeAddOp2(v, OP_Integer, k, 6);
sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
}
}
@@ -1003,6 +1055,7 @@ void sqlite3Pragma(
pTab = pIdx->pTable;
sqlite3VdbeSetNumCols(v, 3);
pParse->nMem = 3;
+ sqlite3CodeVerifySchema(pParse, iDb);
sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
@@ -1029,6 +1082,7 @@ void sqlite3Pragma(
int i = 0;
sqlite3VdbeSetNumCols(v, 3);
pParse->nMem = 3;
+ sqlite3CodeVerifySchema(pParse, iDb);
sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
@@ -1092,6 +1146,7 @@ void sqlite3Pragma(
int i = 0;
sqlite3VdbeSetNumCols(v, 8);
pParse->nMem = 8;
+ sqlite3CodeVerifySchema(pParse, iDb);
sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
@@ -1125,6 +1180,122 @@ void sqlite3Pragma(
}else
#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
+#ifndef SQLITE_OMIT_FOREIGN_KEY
+#ifndef SQLITE_OMIT_TRIGGER
+ if( sqlite3StrICmp(zLeft, "foreign_key_check")==0 ){
+ FKey *pFK; /* A foreign key constraint */
+ Table *pTab; /* Child table contain "REFERENCES" keyword */
+ Table *pParent; /* Parent table that child points to */
+ Index *pIdx; /* Index in the parent table */
+ int i; /* Loop counter: Foreign key number for pTab */
+ int j; /* Loop counter: Field of the foreign key */
+ HashElem *k; /* Loop counter: Next table in schema */
+ int x; /* result variable */
+ int regResult; /* 3 registers to hold a result row */
+ int regKey; /* Register to hold key for checking the FK */
+ int regRow; /* Registers to hold a row from pTab */
+ int addrTop; /* Top of a loop checking foreign keys */
+ int addrOk; /* Jump here if the key is OK */
+ int *aiCols; /* child to parent column mapping */
+
+ if( sqlite3ReadSchema(pParse) ) goto pragma_out;
+ regResult = pParse->nMem+1;
+ pParse->nMem += 4;
+ regKey = ++pParse->nMem;
+ regRow = ++pParse->nMem;
+ v = sqlite3GetVdbe(pParse);
+ sqlite3VdbeSetNumCols(v, 4);
+ sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
+ sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "rowid", SQLITE_STATIC);
+ sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "parent", SQLITE_STATIC);
+ sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "fkid", SQLITE_STATIC);
+ sqlite3CodeVerifySchema(pParse, iDb);
+ k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
+ while( k ){
+ if( zRight ){
+ pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
+ k = 0;
+ }else{
+ pTab = (Table*)sqliteHashData(k);
+ k = sqliteHashNext(k);
+ }
+ if( pTab==0 || pTab->pFKey==0 ) continue;
+ sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
+ if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
+ sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
+ sqlite3VdbeAddOp4(v, OP_String8, 0, regResult, 0, pTab->zName,
+ P4_TRANSIENT);
+ for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
+ pParent = sqlite3LocateTable(pParse, 0, pFK->zTo, zDb);
+ if( pParent==0 ) break;
+ pIdx = 0;
+ sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
+ x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
+ if( x==0 ){
+ if( pIdx==0 ){
+ sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
+ }else{
+ KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
+ sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
+ sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
+ }
+ }else{
+ k = 0;
+ break;
+ }
+ }
+ if( pFK ) break;
+ if( pParse->nTab<i ) pParse->nTab = i;
+ addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0);
+ for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
+ pParent = sqlite3LocateTable(pParse, 0, pFK->zTo, zDb);
+ assert( pParent!=0 );
+ pIdx = 0;
+ aiCols = 0;
+ x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
+ assert( x==0 );
+ addrOk = sqlite3VdbeMakeLabel(v);
+ if( pIdx==0 ){
+ int iKey = pFK->aCol[0].iFrom;
+ assert( iKey>=0 && iKey<pTab->nCol );
+ if( iKey!=pTab->iPKey ){
+ sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
+ sqlite3ColumnDefault(v, pTab, iKey, regRow);
+ sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk);
+ sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow,
+ sqlite3VdbeCurrentAddr(v)+3);
+ }else{
+ sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
+ }
+ sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow);
+ sqlite3VdbeAddOp2(v, OP_Goto, 0, addrOk);
+ sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
+ }else{
+ for(j=0; j<pFK->nCol; j++){
+ sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
+ aiCols ? aiCols[j] : pFK->aCol[0].iFrom, regRow+j);
+ sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk);
+ }
+ sqlite3VdbeAddOp3(v, OP_MakeRecord, regRow, pFK->nCol, regKey);
+ sqlite3VdbeChangeP4(v, -1,
+ sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
+ sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
+ }
+ sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
+ sqlite3VdbeAddOp4(v, OP_String8, 0, regResult+2, 0,
+ pFK->zTo, P4_TRANSIENT);
+ sqlite3VdbeAddOp2(v, OP_Integer, i-1, regResult+3);
+ sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
+ sqlite3VdbeResolveLabel(v, addrOk);
+ sqlite3DbFree(db, aiCols);
+ }
+ sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1);
+ sqlite3VdbeJumpHere(v, addrTop);
+ }
+ }else
+#endif /* !defined(SQLITE_OMIT_TRIGGER) */
+#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
+
#ifndef NDEBUG
if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
if( zRight ){
@@ -1246,7 +1417,7 @@ void sqlite3Pragma(
sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
P4_DYNAMIC);
- sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
+ sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
sqlite3VdbeJumpHere(v, addr);
@@ -1409,6 +1580,11 @@ void sqlite3Pragma(
** PRAGMA [database.]user_version
** PRAGMA [database.]user_version = <integer>
**
+ ** PRAGMA [database.]freelist_count = <integer>
+ **
+ ** PRAGMA [database.]application_id
+ ** PRAGMA [database.]application_id = <integer>
+ **
** The pragma's schema_version and user_version are used to set or get
** the value of the schema-version and user-version, respectively. Both
** the schema-version and the user-version are 32-bit signed integers
@@ -1430,10 +1606,14 @@ void sqlite3Pragma(
if( sqlite3StrICmp(zLeft, "schema_version")==0
|| sqlite3StrICmp(zLeft, "user_version")==0
|| sqlite3StrICmp(zLeft, "freelist_count")==0
+ || sqlite3StrICmp(zLeft, "application_id")==0
){
int iCookie; /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
sqlite3VdbeUsesBtree(v, iDb);
switch( zLeft[0] ){
+ case 'a': case 'A':
+ iCookie = BTREE_APPLICATION_ID;
+ break;
case 'f': case 'F':
iCookie = BTREE_FREE_PAGE_COUNT;
break;
@@ -1549,6 +1729,22 @@ void sqlite3Pragma(
sqlite3_db_release_memory(db);
}else
+ /*
+ ** PRAGMA busy_timeout
+ ** PRAGMA busy_timeout = N
+ **
+ ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
+ ** if one is set. If no busy handler or a different busy handler is set
+ ** then 0 is returned. Setting the busy_timeout to 0 or negative
+ ** disables the timeout.
+ */
+ if( sqlite3StrICmp(zLeft, "busy_timeout")==0 ){
+ if( zRight ){
+ sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
+ }
+ returnSingleInt(pParse, "timeout", db->busyTimeout);
+ }else
+
#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
/*
** Report the current state of file logs for all databases
@@ -1564,13 +1760,12 @@ void sqlite3Pragma(
sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
for(i=0; i<db->nDb; i++){
Btree *pBt;
- Pager *pPager;
const char *zState = "unknown";
int j;
if( db->aDb[i].zName==0 ) continue;
sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
pBt = db->aDb[i].pBt;
- if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
+ if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
zState = "closed";
}else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
@@ -1607,7 +1802,7 @@ void sqlite3Pragma(
}else
#endif
#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
- if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
+ if( sqlite3StrICmp(zLeft, "activate_extensions")==0 && zRight ){
#ifdef SQLITE_HAS_CODEC
if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
sqlite3_activate_see(&zRight[4]);
diff --git a/src/prepare.c b/src/prepare.c
index bd2cf7a..d78d83c 100644
--- a/src/prepare.c
+++ b/src/prepare.c
@@ -134,7 +134,9 @@ int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
int rc;
int i;
+#ifndef SQLITE_OMIT_DEPRECATED
int size;
+#endif
Table *pTab;
Db *pDb;
char const *azArg[4];
@@ -177,7 +179,7 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
/* zMasterSchema and zInitScript are set to point at the master schema
** and initialisation script appropriate for the database being
- ** initialised. zMasterName is the name of the master table.
+ ** initialized. zMasterName is the name of the master table.
*/
if( !OMIT_TEMPDB && iDb==1 ){
zMasterSchema = temp_master_schema;
@@ -257,11 +259,15 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
*/
if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
if( iDb==0 ){
+#ifndef SQLITE_OMIT_UTF16
u8 encoding;
/* If opening the main database, set ENC(db). */
encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
if( encoding==0 ) encoding = SQLITE_UTF8;
ENC(db) = encoding;
+#else
+ ENC(db) = SQLITE_UTF8;
+#endif
}else{
/* If opening an attached database, the encoding much match ENC(db) */
if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
@@ -398,7 +404,7 @@ int sqlite3Init(sqlite3 *db, char **pzErrMsg){
}
}
- /* Once all the other databases have been initialised, load the schema
+ /* Once all the other databases have been initialized, load the schema
** for the TEMP database. This is loaded last, as the TEMP database
** schema may contain references to objects in other databases.
*/
@@ -421,7 +427,7 @@ int sqlite3Init(sqlite3 *db, char **pzErrMsg){
}
/*
-** This routine is a no-op if the database schema is already initialised.
+** This routine is a no-op if the database schema is already initialized.
** Otherwise, the schema is loaded. An error code is returned.
*/
int sqlite3ReadSchema(Parse *pParse){
@@ -648,7 +654,6 @@ static int sqlite3Prepare(
}
#endif
- assert( db->init.busy==0 || saveSqlFlag==0 );
if( db->init.busy==0 ){
Vdbe *pVdbe = pParse->pVdbe;
sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
diff --git a/src/resolve.c b/src/resolve.c
index b87d231..91efcaa 100644
--- a/src/resolve.c
+++ b/src/resolve.c
@@ -68,6 +68,15 @@ static void incrAggFunctionDepth(Expr *pExpr, int N){
** from the result in the result-set. We might fix this someday. Or
** then again, we might not...
**
+** If the reference is followed by a COLLATE operator, then make sure
+** the COLLATE operator is preserved. For example:
+**
+** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
+**
+** Should be transformed into:
+**
+** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
+**
** The nSubquery parameter specifies how many levels of subquery the
** alias is removed from the original expression. The usually value is
** zero but it might be more if the alias is contained within a subquery
@@ -91,8 +100,9 @@ static void resolveAlias(
assert( pOrig!=0 );
assert( pOrig->flags & EP_Resolved );
db = pParse->db;
+ pDup = sqlite3ExprDup(db, pOrig, 0);
+ if( pDup==0 ) return;
if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
- pDup = sqlite3ExprDup(db, pOrig, 0);
incrAggFunctionDepth(pDup, nSubquery);
pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
if( pDup==0 ) return;
@@ -100,32 +110,26 @@ static void resolveAlias(
pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
}
pDup->iTable = pEList->a[iCol].iAlias;
- }else if( ExprHasProperty(pOrig, EP_IntValue) || pOrig->u.zToken==0 ){
- pDup = sqlite3ExprDup(db, pOrig, 0);
- if( pDup==0 ) return;
- }else{
- char *zToken = pOrig->u.zToken;
- assert( zToken!=0 );
- pOrig->u.zToken = 0;
- pDup = sqlite3ExprDup(db, pOrig, 0);
- pOrig->u.zToken = zToken;
- if( pDup==0 ) return;
- assert( (pDup->flags & (EP_Reduced|EP_TokenOnly))==0 );
- pDup->flags2 |= EP2_MallocedToken;
- pDup->u.zToken = sqlite3DbStrDup(db, zToken);
}
- if( pExpr->flags & EP_ExpCollate ){
- pDup->pColl = pExpr->pColl;
- pDup->flags |= EP_ExpCollate;
+ if( pExpr->op==TK_COLLATE ){
+ pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
}
/* Before calling sqlite3ExprDelete(), set the EP_Static flag. This
** prevents ExprDelete() from deleting the Expr structure itself,
** allowing it to be repopulated by the memcpy() on the following line.
+ ** The pExpr->u.zToken might point into memory that will be freed by the
+ ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
+ ** make a copy of the token before doing the sqlite3DbFree().
*/
ExprSetProperty(pExpr, EP_Static);
sqlite3ExprDelete(db, pExpr);
memcpy(pExpr, pDup, sizeof(*pExpr));
+ if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
+ assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
+ pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
+ pExpr->flags2 |= EP2_MallocedToken;
+ }
sqlite3DbFree(db, pDup);
}
@@ -146,6 +150,35 @@ static int nameInUsingClause(IdList *pUsing, const char *zCol){
return 0;
}
+/*
+** Subqueries stores the original database, table and column names for their
+** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN".
+** Check to see if the zSpan given to this routine matches the zDb, zTab,
+** and zCol. If any of zDb, zTab, and zCol are NULL then those fields will
+** match anything.
+*/
+int sqlite3MatchSpanName(
+ const char *zSpan,
+ const char *zCol,
+ const char *zTab,
+ const char *zDb
+){
+ int n;
+ for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
+ if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){
+ return 0;
+ }
+ zSpan += n+1;
+ for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
+ if( zTab && (sqlite3StrNICmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){
+ return 0;
+ }
+ zSpan += n+1;
+ if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){
+ return 0;
+ }
+ return 1;
+}
/*
** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
@@ -195,13 +228,27 @@ static int lookupName(
assert( pNC ); /* the name context cannot be NULL. */
assert( zCol ); /* The Z in X.Y.Z cannot be NULL */
- assert( ~ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
+ assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
/* Initialize the node to no-match */
pExpr->iTable = -1;
pExpr->pTab = 0;
ExprSetIrreducible(pExpr);
+ /* Translate the schema name in zDb into a pointer to the corresponding
+ ** schema. If not found, pSchema will remain NULL and nothing will match
+ ** resulting in an appropriate error message toward the end of this routine
+ */
+ if( zDb ){
+ for(i=0; i<db->nDb; i++){
+ assert( db->aDb[i].zName );
+ if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){
+ pSchema = db->aDb[i].pSchema;
+ break;
+ }
+ }
+ }
+
/* Start at the inner-most context and move outward until a match is found */
while( pNC && cnt==0 ){
ExprList *pEList;
@@ -210,31 +257,36 @@ static int lookupName(
if( pSrcList ){
for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
Table *pTab;
- int iDb;
Column *pCol;
pTab = pItem->pTab;
assert( pTab!=0 && pTab->zName!=0 );
- iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
assert( pTab->nCol>0 );
- if( zTab ){
- if( pItem->zAlias ){
- char *zTabName = pItem->zAlias;
- if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
- }else{
- char *zTabName = pTab->zName;
- if( NEVER(zTabName==0) || sqlite3StrICmp(zTabName, zTab)!=0 ){
- continue;
- }
- if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
- continue;
+ if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
+ int hit = 0;
+ pEList = pItem->pSelect->pEList;
+ for(j=0; j<pEList->nExpr; j++){
+ if( sqlite3MatchSpanName(pEList->a[j].zSpan, zCol, zTab, zDb) ){
+ cnt++;
+ cntTab = 2;
+ pMatch = pItem;
+ pExpr->iColumn = j;
+ hit = 1;
}
}
+ if( hit || zTab==0 ) continue;
+ }
+ if( zDb && pTab->pSchema!=pSchema ){
+ continue;
+ }
+ if( zTab ){
+ const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
+ assert( zTabName!=0 );
+ if( sqlite3StrICmp(zTabName, zTab)!=0 ){
+ continue;
+ }
}
if( 0==(cntTab++) ){
- pExpr->iTable = pItem->iCursor;
- pExpr->pTab = pTab;
- pSchema = pTab->pSchema;
pMatch = pItem;
}
for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
@@ -248,17 +300,19 @@ static int lookupName(
if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
}
cnt++;
- pExpr->iTable = pItem->iCursor;
- pExpr->pTab = pTab;
pMatch = pItem;
- pSchema = pTab->pSchema;
/* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
break;
}
}
}
- }
+ if( pMatch ){
+ pExpr->iTable = pMatch->iCursor;
+ pExpr->pTab = pMatch->pTab;
+ pSchema = pExpr->pTab->pSchema;
+ }
+ } /* if( pSrcList ) */
#ifndef SQLITE_OMIT_TRIGGER
/* If we have not already resolved the name, then maybe
@@ -334,7 +388,10 @@ static int lookupName(
** Note that the expression in the result set should have already been
** resolved by the time the WHERE clause is resolved.
*/
- if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
+ if( (pEList = pNC->pEList)!=0
+ && zTab==0
+ && ((pNC->ncFlags & NC_AsMaybe)==0 || cnt==0)
+ ){
for(j=0; j<pEList->nExpr; j++){
char *zAs = pEList->a[j].zName;
if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
@@ -425,7 +482,9 @@ static int lookupName(
lookupname_end:
if( cnt==1 ){
assert( pNC!=0 );
- sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
+ if( pExpr->op!=TK_AS ){
+ sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
+ }
/* Increment the nRef value on all name contexts from TopNC up to
** the point where the name matched. */
for(;;){
@@ -593,7 +652,7 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
pNC->nErr++;
is_agg = 0;
- }else if( no_such_func ){
+ }else if( no_such_func && pParse->db->init.busy==0 ){
sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
pNC->nErr++;
}else if( wrong_num_args ){
@@ -812,7 +871,7 @@ static int resolveCompoundOrderBy(
int iCol = -1;
Expr *pE, *pDup;
if( pItem->done ) continue;
- pE = pItem->pExpr;
+ pE = sqlite3ExprSkipCollate(pItem->pExpr);
if( sqlite3ExprIsInteger(pE, &iCol) ){
if( iCol<=0 || iCol>pEList->nExpr ){
resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
@@ -830,14 +889,20 @@ static int resolveCompoundOrderBy(
}
}
if( iCol>0 ){
- CollSeq *pColl = pE->pColl;
- int flags = pE->flags & EP_ExpCollate;
+ /* Convert the ORDER BY term into an integer column number iCol,
+ ** taking care to preserve the COLLATE clause if it exists */
+ Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
+ if( pNew==0 ) return 1;
+ pNew->flags |= EP_IntValue;
+ pNew->u.iValue = iCol;
+ if( pItem->pExpr==pE ){
+ pItem->pExpr = pNew;
+ }else{
+ assert( pItem->pExpr->op==TK_COLLATE );
+ assert( pItem->pExpr->pLeft==pE );
+ pItem->pExpr->pLeft = pNew;
+ }
sqlite3ExprDelete(db, pE);
- pItem->pExpr = pE = sqlite3Expr(db, TK_INTEGER, 0);
- if( pE==0 ) return 1;
- pE->pColl = pColl;
- pE->flags |= EP_IntValue | flags;
- pE->u.iValue = iCol;
pItem->iOrderByCol = (u16)iCol;
pItem->done = 1;
}else{
@@ -942,11 +1007,11 @@ static int resolveOrderGroupBy(
pItem->iOrderByCol = (u16)iCol;
continue;
}
- if( sqlite3ExprIsInteger(pE, &iCol) ){
+ if( sqlite3ExprIsInteger(sqlite3ExprSkipCollate(pE), &iCol) ){
/* The ORDER BY term is an integer constant. Again, set the column
** number so that sqlite3ResolveOrderGroupBy() will convert the
** order-by term to a copy of the result-set expression */
- if( iCol<1 ){
+ if( iCol<1 || iCol>0xffff ){
resolveOutOfRangeError(pParse, zType, i+1, nResult);
return 1;
}
@@ -1023,23 +1088,6 @@ static int resolveSelectStep(Walker *pWalker, Select *p){
return WRC_Abort;
}
- /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
- ** resolve the result-set expression list.
- */
- sNC.ncFlags = NC_AllowAgg;
- sNC.pSrcList = p->pSrc;
- sNC.pNext = pOuterNC;
-
- /* Resolve names in the result set. */
- pEList = p->pEList;
- assert( pEList!=0 );
- for(i=0; i<pEList->nExpr; i++){
- Expr *pX = pEList->a[i].pExpr;
- if( sqlite3ResolveExprNames(&sNC, pX) ){
- return WRC_Abort;
- }
- }
-
/* Recursively resolve names in all subqueries
*/
for(i=0; i<p->pSrc->nSrc; i++){
@@ -1067,6 +1115,23 @@ static int resolveSelectStep(Walker *pWalker, Select *p){
}
}
+ /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
+ ** resolve the result-set expression list.
+ */
+ sNC.ncFlags = NC_AllowAgg;
+ sNC.pSrcList = p->pSrc;
+ sNC.pNext = pOuterNC;
+
+ /* Resolve names in the result set. */
+ pEList = p->pEList;
+ assert( pEList!=0 );
+ for(i=0; i<pEList->nExpr; i++){
+ Expr *pX = pEList->a[i].pExpr;
+ if( sqlite3ResolveExprNames(&sNC, pX) ){
+ return WRC_Abort;
+ }
+ }
+
/* If there are no aggregate functions in the result-set, and no GROUP BY
** expression, do not allow aggregates in any of the other expressions.
*/
@@ -1094,11 +1159,10 @@ static int resolveSelectStep(Walker *pWalker, Select *p){
** re-evaluated for each reference to it.
*/
sNC.pEList = p->pEList;
- if( sqlite3ResolveExprNames(&sNC, p->pWhere) ||
- sqlite3ResolveExprNames(&sNC, p->pHaving)
- ){
- return WRC_Abort;
- }
+ sNC.ncFlags |= NC_AsMaybe;
+ if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
+ if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
+ sNC.ncFlags &= ~NC_AsMaybe;
/* The ORDER BY and GROUP BY clauses may not refer to terms in
** outer queries
@@ -1219,6 +1283,7 @@ int sqlite3ResolveExprNames(
#endif
savedHasAgg = pNC->ncFlags & NC_HasAgg;
pNC->ncFlags &= ~NC_HasAgg;
+ memset(&w, 0, sizeof(w));
w.xExprCallback = resolveExprStep;
w.xSelectCallback = resolveSelectStep;
w.pParse = pNC->pParse;
@@ -1259,6 +1324,7 @@ void sqlite3ResolveSelectNames(
Walker w;
assert( p!=0 );
+ memset(&w, 0, sizeof(w));
w.xExprCallback = resolveExprStep;
w.xSelectCallback = resolveSelectStep;
w.pParse = pParse;
diff --git a/src/select.c b/src/select.c
index 6ec9da3..f3f1490 100644
--- a/src/select.c
+++ b/src/select.c
@@ -55,7 +55,7 @@ Select *sqlite3SelectNew(
ExprList *pGroupBy, /* the GROUP BY clause */
Expr *pHaving, /* the HAVING clause */
ExprList *pOrderBy, /* the ORDER BY clause */
- int isDistinct, /* true if the DISTINCT keyword is present */
+ u16 selFlags, /* Flag parameters, such as SF_Distinct */
Expr *pLimit, /* LIMIT value. NULL means not used */
Expr *pOffset /* OFFSET value. NULL means no offset */
){
@@ -79,7 +79,7 @@ Select *sqlite3SelectNew(
pNew->pGroupBy = pGroupBy;
pNew->pHaving = pHaving;
pNew->pOrderBy = pOrderBy;
- pNew->selFlags = isDistinct ? SF_Distinct : 0;
+ pNew->selFlags = selFlags;
pNew->op = TK_SELECT;
pNew->pLimit = pLimit;
pNew->pOffset = pOffset;
@@ -526,6 +526,19 @@ static int checkForMultiColumnSelectError(
#endif
/*
+** An instance of the following object is used to record information about
+** how to process the DISTINCT keyword, to simplify passing that information
+** into the selectInnerLoop() routine.
+*/
+typedef struct DistinctCtx DistinctCtx;
+struct DistinctCtx {
+ u8 isTnct; /* True if the DISTINCT keyword is present */
+ u8 eTnctType; /* One of the WHERE_DISTINCT_* operators */
+ int tabTnct; /* Ephemeral table used for DISTINCT processing */
+ int addrTnct; /* Address of OP_OpenEphemeral opcode for tabTnct */
+};
+
+/*
** This routine generates the code for the inside of the inner loop
** of a SELECT.
**
@@ -541,7 +554,7 @@ static void selectInnerLoop(
int srcTab, /* Pull data from this table */
int nColumn, /* Number of columns in the source table */
ExprList *pOrderBy, /* If not NULL, sort results using this key */
- int distinct, /* If >=0, make sure results are distinct */
+ DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
SelectDest *pDest, /* How to dispose of the results */
int iContinue, /* Jump here to continue with next row */
int iBreak /* Jump here to break out of the inner loop */
@@ -557,7 +570,7 @@ static void selectInnerLoop(
assert( v );
if( NEVER(v==0) ) return;
assert( pEList!=0 );
- hasDistinct = distinct>=0;
+ hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
if( pOrderBy==0 && !hasDistinct ){
codeOffset(v, p, iContinue);
}
@@ -597,7 +610,55 @@ static void selectInnerLoop(
if( hasDistinct ){
assert( pEList!=0 );
assert( pEList->nExpr==nColumn );
- codeDistinct(pParse, distinct, iContinue, nColumn, regResult);
+ switch( pDistinct->eTnctType ){
+ case WHERE_DISTINCT_ORDERED: {
+ VdbeOp *pOp; /* No longer required OpenEphemeral instr. */
+ int iJump; /* Jump destination */
+ int regPrev; /* Previous row content */
+
+ /* Allocate space for the previous row */
+ regPrev = pParse->nMem+1;
+ pParse->nMem += nColumn;
+
+ /* Change the OP_OpenEphemeral coded earlier to an OP_Null
+ ** sets the MEM_Cleared bit on the first register of the
+ ** previous value. This will cause the OP_Ne below to always
+ ** fail on the first iteration of the loop even if the first
+ ** row is all NULLs.
+ */
+ sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
+ pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
+ pOp->opcode = OP_Null;
+ pOp->p1 = 1;
+ pOp->p2 = regPrev;
+
+ iJump = sqlite3VdbeCurrentAddr(v) + nColumn;
+ for(i=0; i<nColumn; i++){
+ CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
+ if( i<nColumn-1 ){
+ sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
+ }else{
+ sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
+ }
+ sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
+ sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
+ }
+ assert( sqlite3VdbeCurrentAddr(v)==iJump );
+ sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nColumn-1);
+ break;
+ }
+
+ case WHERE_DISTINCT_UNIQUE: {
+ sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
+ break;
+ }
+
+ default: {
+ assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
+ codeDistinct(pParse, pDistinct->tabTnct, iContinue, nColumn, regResult);
+ break;
+ }
+ }
if( pOrderBy==0 ){
codeOffset(v, p, iContinue);
}
@@ -655,7 +716,8 @@ static void selectInnerLoop(
*/
case SRT_Set: {
assert( nColumn==1 );
- p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
+ pDest->affSdst =
+ sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
if( pOrderBy ){
/* At first glance you would think we could optimize out the
** ORDER BY in this case since the order of entries in the set
@@ -664,7 +726,7 @@ static void selectInnerLoop(
pushOntoSorter(pParse, pOrderBy, p, regResult);
}else{
int r1 = sqlite3GetTempReg(pParse);
- sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, 1, r1, &p->affinity, 1);
+ sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult,1,r1, &pDest->affSdst, 1);
sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
sqlite3ReleaseTempReg(pParse, r1);
@@ -931,7 +993,8 @@ static void generateSortTail(
#ifndef SQLITE_OMIT_SUBQUERY
case SRT_Set: {
assert( nColumn==1 );
- sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid, &p->affinity, 1);
+ sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid,
+ &pDest->affSdst, 1);
sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
break;
@@ -1246,7 +1309,7 @@ static void generateColumnNames(
static int selectColumnsFromExprList(
Parse *pParse, /* Parsing context */
ExprList *pEList, /* Expr list from which to derive column names */
- int *pnCol, /* Write the number of columns here */
+ i16 *pnCol, /* Write the number of columns here */
Column **paCol /* Write the new column list here */
){
sqlite3 *db = pParse->db; /* Database connection */
@@ -1272,9 +1335,7 @@ static int selectColumnsFromExprList(
for(i=0, pCol=aCol; i<nCol; i++, pCol++){
/* Get an appropriate name for the column
*/
- p = pEList->a[i].pExpr;
- assert( p->pRight==0 || ExprHasProperty(p->pRight, EP_IntValue)
- || p->pRight->u.zToken==0 || p->pRight->u.zToken[0]!=0 );
+ p = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
if( (zName = pEList->a[i].zName)!=0 ){
/* If the column contains an "AS <name>" phrase, use <name> as the name */
zName = sqlite3DbStrDup(db, zName);
@@ -1312,6 +1373,9 @@ static int selectColumnsFromExprList(
for(j=cnt=0; j<i; j++){
if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
char *zNewName;
+ int k;
+ for(k=nName-1; k>1 && sqlite3Isdigit(zName[k]); k--){}
+ if( zName[k]==':' ) nName = k;
zName[nName] = 0;
zNewName = sqlite3MPrintf(db, "%s:%d", zName, ++cnt);
sqlite3DbFree(db, zName);
@@ -1643,6 +1707,8 @@ static int multiSelect(
int addr = 0;
int nLimit;
assert( !pPrior->pLimit );
+ pPrior->iLimit = p->iLimit;
+ pPrior->iOffset = p->iOffset;
pPrior->pLimit = p->pLimit;
pPrior->pOffset = p->pOffset;
explainSetInteger(iSub1, pParse->iNextSelectId);
@@ -1768,7 +1834,7 @@ static int multiSelect(
sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak);
iStart = sqlite3VdbeCurrentAddr(v);
selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
- 0, -1, &dest, iCont, iBreak);
+ 0, 0, &dest, iCont, iBreak);
sqlite3VdbeResolveLabel(v, iCont);
sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart);
sqlite3VdbeResolveLabel(v, iBreak);
@@ -1846,7 +1912,7 @@ static int multiSelect(
sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
sqlite3ReleaseTempReg(pParse, r1);
selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
- 0, -1, &dest, iCont, iBreak);
+ 0, 0, &dest, iCont, iBreak);
sqlite3VdbeResolveLabel(v, iCont);
sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart);
sqlite3VdbeResolveLabel(v, iBreak);
@@ -1892,6 +1958,7 @@ static int multiSelect(
*apColl = db->pDfltColl;
}
}
+ pKeyInfo->aSortOrder = (u8*)apColl;
for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
for(i=0; i<2; i++){
@@ -1965,7 +2032,7 @@ static int generateOutputSubroutine(
(char*)pKeyInfo, p4type);
sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
sqlite3VdbeJumpHere(v, j1);
- sqlite3ExprCodeCopy(pParse, pIn->iSdst, regPrev+1, pIn->nSdst);
+ sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
}
if( pParse->db->mallocFailed ) return 0;
@@ -2000,10 +2067,10 @@ static int generateOutputSubroutine(
case SRT_Set: {
int r1;
assert( pIn->nSdst==1 );
- p->affinity =
+ pDest->affSdst =
sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst);
r1 = sqlite3GetTempReg(pParse);
- sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &p->affinity, 1);
+ sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &pDest->affSdst,1);
sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1);
sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
sqlite3ReleaseTempReg(pParse, r1);
@@ -2269,12 +2336,13 @@ static int multiSelectOrderBy(
for(i=0; i<nOrderBy; i++){
CollSeq *pColl;
Expr *pTerm = pOrderBy->a[i].pExpr;
- if( pTerm->flags & EP_ExpCollate ){
- pColl = pTerm->pColl;
+ if( pTerm->flags & EP_Collate ){
+ pColl = sqlite3ExprCollSeq(pParse, pTerm);
}else{
pColl = multiSelectCollSeq(pParse, p, aPermute[i]);
- pTerm->flags |= EP_ExpCollate;
- pTerm->pColl = pColl;
+ if( pColl==0 ) pColl = db->pDfltColl;
+ pOrderBy->a[i].pExpr =
+ sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
}
pKeyMerge->aColl[i] = pColl;
pKeyMerge->aSortOrder[i] = pOrderBy->a[i].sortOrder;
@@ -2298,7 +2366,8 @@ static int multiSelectOrderBy(
}else{
int nExpr = p->pEList->nExpr;
assert( nOrderBy>=nExpr || db->mallocFailed );
- regPrev = sqlite3GetTempRange(pParse, nExpr+1);
+ regPrev = pParse->nMem+1;
+ pParse->nMem += nExpr+1;
sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
pKeyDup = sqlite3DbMallocZero(db,
sizeof(*pKeyDup) + nExpr*(sizeof(CollSeq*)+1) );
@@ -2477,14 +2546,9 @@ static int multiSelectOrderBy(
sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
(char*)pKeyMerge, P4_KEYINFO_HANDOFF);
+ sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
- /* Release temporary registers
- */
- if( regPrev ){
- sqlite3ReleaseTempRange(pParse, regPrev, nOrderBy+1);
- }
-
/* Jump to the this point in order to terminate the query.
*/
sqlite3VdbeResolveLabel(v, labelEnd);
@@ -2544,9 +2608,6 @@ static Expr *substExpr(
assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
assert( pExpr->pLeft==0 && pExpr->pRight==0 );
pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0);
- if( pNew && pExpr->pColl ){
- pNew->pColl = pExpr->pColl;
- }
sqlite3ExprDelete(db, pExpr);
pExpr = pNew;
}
@@ -2745,7 +2806,7 @@ static int flattenSubquery(
*/
assert( p!=0 );
assert( p->pPrior==0 ); /* Unable to flatten compound queries */
- if( db->flags & SQLITE_QueryFlattener ) return 0;
+ if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
pSrc = p->pSrc;
assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
pSubitem = &pSrc->a[iFrom];
@@ -2899,12 +2960,15 @@ static int flattenSubquery(
Select *pNew;
ExprList *pOrderBy = p->pOrderBy;
Expr *pLimit = p->pLimit;
+ Expr *pOffset = p->pOffset;
Select *pPrior = p->pPrior;
p->pOrderBy = 0;
p->pSrc = 0;
p->pPrior = 0;
p->pLimit = 0;
+ p->pOffset = 0;
pNew = sqlite3SelectDup(db, p, 0);
+ p->pOffset = pOffset;
p->pLimit = pLimit;
p->pOrderBy = pOrderBy;
p->pSrc = pSrc;
@@ -3034,10 +3098,9 @@ static int flattenSubquery(
pList = pParent->pEList;
for(i=0; i<pList->nExpr; i++){
if( pList->a[i].zName==0 ){
- const char *zSpan = pList->a[i].zSpan;
- if( ALWAYS(zSpan) ){
- pList->a[i].zName = sqlite3DbStrDup(db, zSpan);
- }
+ char *zName = sqlite3DbStrDup(db, pList->a[i].zSpan);
+ sqlite3Dequote(zName);
+ pList->a[i].zName = zName;
}
}
substExprList(db, pParent->pEList, iParent, pSub->pEList);
@@ -3098,34 +3161,43 @@ static int flattenSubquery(
#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
/*
-** Analyze the SELECT statement passed as an argument to see if it
-** is a min() or max() query. Return WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX if
-** it is, or 0 otherwise. At present, a query is considered to be
-** a min()/max() query if:
+** Based on the contents of the AggInfo structure indicated by the first
+** argument, this function checks if the following are true:
+**
+** * the query contains just a single aggregate function,
+** * the aggregate function is either min() or max(), and
+** * the argument to the aggregate function is a column value.
**
-** 1. There is a single object in the FROM clause.
+** If all of the above are true, then WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX
+** is returned as appropriate. Also, *ppMinMax is set to point to the
+** list of arguments passed to the aggregate before returning.
**
-** 2. There is a single expression in the result set, and it is
-** either min(x) or max(x), where x is a column reference.
+** Or, if the conditions above are not met, *ppMinMax is set to 0 and
+** WHERE_ORDERBY_NORMAL is returned.
*/
-static u8 minMaxQuery(Select *p){
- Expr *pExpr;
- ExprList *pEList = p->pEList;
-
- if( pEList->nExpr!=1 ) return WHERE_ORDERBY_NORMAL;
- pExpr = pEList->a[0].pExpr;
- if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
- if( NEVER(ExprHasProperty(pExpr, EP_xIsSelect)) ) return 0;
- pEList = pExpr->x.pList;
- if( pEList==0 || pEList->nExpr!=1 ) return 0;
- if( pEList->a[0].pExpr->op!=TK_AGG_COLUMN ) return WHERE_ORDERBY_NORMAL;
- assert( !ExprHasProperty(pExpr, EP_IntValue) );
- if( sqlite3StrICmp(pExpr->u.zToken,"min")==0 ){
- return WHERE_ORDERBY_MIN;
- }else if( sqlite3StrICmp(pExpr->u.zToken,"max")==0 ){
- return WHERE_ORDERBY_MAX;
+static u8 minMaxQuery(AggInfo *pAggInfo, ExprList **ppMinMax){
+ int eRet = WHERE_ORDERBY_NORMAL; /* Return value */
+
+ *ppMinMax = 0;
+ if( pAggInfo->nFunc==1 ){
+ Expr *pExpr = pAggInfo->aFunc[0].pExpr; /* Aggregate function */
+ ExprList *pEList = pExpr->x.pList; /* Arguments to agg function */
+
+ assert( pExpr->op==TK_AGG_FUNCTION );
+ if( pEList && pEList->nExpr==1 && pEList->a[0].pExpr->op==TK_AGG_COLUMN ){
+ const char *zFunc = pExpr->u.zToken;
+ if( sqlite3StrICmp(zFunc, "min")==0 ){
+ eRet = WHERE_ORDERBY_MIN;
+ *ppMinMax = pEList;
+ }else if( sqlite3StrICmp(zFunc, "max")==0 ){
+ eRet = WHERE_ORDERBY_MAX;
+ *ppMinMax = pEList;
+ }
+ }
}
- return WHERE_ORDERBY_NORMAL;
+
+ assert( *ppMinMax==0 || (*ppMinMax)->nExpr==1 );
+ return eRet;
}
/*
@@ -3188,6 +3260,69 @@ int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
}
return SQLITE_OK;
}
+/*
+** Detect compound SELECT statements that use an ORDER BY clause with
+** an alternative collating sequence.
+**
+** SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ...
+**
+** These are rewritten as a subquery:
+**
+** SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
+** ORDER BY ... COLLATE ...
+**
+** This transformation is necessary because the multiSelectOrderBy() routine
+** above that generates the code for a compound SELECT with an ORDER BY clause
+** uses a merge algorithm that requires the same collating sequence on the
+** result columns as on the ORDER BY clause. See ticket
+** http://www.sqlite.org/src/info/6709574d2a
+**
+** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
+** The UNION ALL operator works fine with multiSelectOrderBy() even when
+** there are COLLATE terms in the ORDER BY.
+*/
+static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
+ int i;
+ Select *pNew;
+ Select *pX;
+ sqlite3 *db;
+ struct ExprList_item *a;
+ SrcList *pNewSrc;
+ Parse *pParse;
+ Token dummy;
+
+ if( p->pPrior==0 ) return WRC_Continue;
+ if( p->pOrderBy==0 ) return WRC_Continue;
+ for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
+ if( pX==0 ) return WRC_Continue;
+ a = p->pOrderBy->a;
+ for(i=p->pOrderBy->nExpr-1; i>=0; i--){
+ if( a[i].pExpr->flags & EP_Collate ) break;
+ }
+ if( i<0 ) return WRC_Continue;
+
+ /* If we reach this point, that means the transformation is required. */
+
+ pParse = pWalker->pParse;
+ db = pParse->db;
+ pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
+ if( pNew==0 ) return WRC_Abort;
+ memset(&dummy, 0, sizeof(dummy));
+ pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0,0);
+ if( pNewSrc==0 ) return WRC_Abort;
+ *pNew = *p;
+ p->pSrc = pNewSrc;
+ p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ALL, 0));
+ p->op = TK_SELECT;
+ p->pWhere = 0;
+ pNew->pGroupBy = 0;
+ pNew->pHaving = 0;
+ pNew->pOrderBy = 0;
+ p->pPrior = 0;
+ pNew->pLimit = 0;
+ pNew->pOffset = 0;
+ return WRC_Continue;
+}
/*
** This routine is a Walker callback for "expanding" a SELECT statement.
@@ -3220,14 +3355,16 @@ static int selectExpander(Walker *pWalker, Select *p){
ExprList *pEList;
struct SrcList_item *pFrom;
sqlite3 *db = pParse->db;
+ Expr *pE, *pRight, *pExpr;
+ u16 selFlags = p->selFlags;
+ p->selFlags |= SF_Expanded;
if( db->mallocFailed ){
return WRC_Abort;
}
- if( NEVER(p->pSrc==0) || (p->selFlags & SF_Expanded)!=0 ){
+ if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
return WRC_Prune;
}
- p->selFlags |= SF_Expanded;
pTabList = p->pSrc;
pEList = p->pEList;
@@ -3268,9 +3405,14 @@ static int selectExpander(Walker *pWalker, Select *p){
}else{
/* An ordinary table or view name in the FROM clause */
assert( pFrom->pTab==0 );
- pFrom->pTab = pTab =
- sqlite3LocateTable(pParse,0,pFrom->zName,pFrom->zDatabase);
+ pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
if( pTab==0 ) return WRC_Abort;
+ if( pTab->nRef==0xffff ){
+ sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535",
+ pTab->zName);
+ pFrom->pTab = 0;
+ return WRC_Abort;
+ }
pTab->nRef++;
#if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
if( pTab->pSelect || IsVirtual(pTab) ){
@@ -3306,7 +3448,7 @@ static int selectExpander(Walker *pWalker, Select *p){
** that need expanding.
*/
for(k=0; k<pEList->nExpr; k++){
- Expr *pE = pEList->a[k].pExpr;
+ pE = pEList->a[k].pExpr;
if( pE->op==TK_ALL ) break;
assert( pE->op!=TK_DOT || pE->pRight!=0 );
assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
@@ -3324,10 +3466,18 @@ static int selectExpander(Walker *pWalker, Select *p){
int longNames = (flags & SQLITE_FullColNames)!=0
&& (flags & SQLITE_ShortColNames)==0;
+ /* When processing FROM-clause subqueries, it is always the case
+ ** that full_column_names=OFF and short_column_names=ON. The
+ ** sqlite3ResultSetOfSelect() routine makes it so. */
+ assert( (p->selFlags & SF_NestedFrom)==0
+ || ((flags & SQLITE_FullColNames)==0 &&
+ (flags & SQLITE_ShortColNames)!=0) );
+
for(k=0; k<pEList->nExpr; k++){
- Expr *pE = a[k].pExpr;
- assert( pE->op!=TK_DOT || pE->pRight!=0 );
- if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pE->pRight->op!=TK_ALL) ){
+ pE = a[k].pExpr;
+ pRight = pE->pRight;
+ assert( pE->op!=TK_DOT || pRight!=0 );
+ if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pRight->op!=TK_ALL) ){
/* This particular expression does not need to be expanded.
*/
pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
@@ -3342,32 +3492,43 @@ static int selectExpander(Walker *pWalker, Select *p){
/* This expression is a "*" or a "TABLE.*" and needs to be
** expanded. */
int tableSeen = 0; /* Set to 1 when TABLE matches */
- char *zTName; /* text of name of TABLE */
+ char *zTName = 0; /* text of name of TABLE */
if( pE->op==TK_DOT ){
assert( pE->pLeft!=0 );
assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
zTName = pE->pLeft->u.zToken;
- }else{
- zTName = 0;
}
for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
Table *pTab = pFrom->pTab;
+ Select *pSub = pFrom->pSelect;
char *zTabName = pFrom->zAlias;
+ const char *zSchemaName = 0;
+ int iDb;
if( zTabName==0 ){
zTabName = pTab->zName;
}
if( db->mallocFailed ) break;
- if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
- continue;
+ if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){
+ pSub = 0;
+ if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
+ continue;
+ }
+ iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
+ zSchemaName = iDb>=0 ? db->aDb[iDb].zName : "*";
}
- tableSeen = 1;
for(j=0; j<pTab->nCol; j++){
- Expr *pExpr, *pRight;
char *zName = pTab->aCol[j].zName;
char *zColname; /* The computed column name */
char *zToFree; /* Malloced string that needs to be freed */
Token sColname; /* Computed column name as a token */
+ assert( zName );
+ if( zTName && pSub
+ && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0
+ ){
+ continue;
+ }
+
/* If a column is marked as 'hidden' (currently only possible
** for virtual tables), do not include it in the expanded
** result-set list.
@@ -3376,6 +3537,7 @@ static int selectExpander(Walker *pWalker, Select *p){
assert(IsVirtual(pTab));
continue;
}
+ tableSeen = 1;
if( i>0 && zTName==0 ){
if( (pFrom->jointype & JT_NATURAL)!=0
@@ -3398,6 +3560,10 @@ static int selectExpander(Walker *pWalker, Select *p){
Expr *pLeft;
pLeft = sqlite3Expr(db, TK_ID, zTabName);
pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
+ if( zSchemaName ){
+ pLeft = sqlite3Expr(db, TK_ID, zSchemaName);
+ pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr, 0);
+ }
if( longNames ){
zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
zToFree = zColname;
@@ -3409,6 +3575,18 @@ static int selectExpander(Walker *pWalker, Select *p){
sColname.z = zColname;
sColname.n = sqlite3Strlen30(zColname);
sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
+ if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){
+ struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
+ if( pSub ){
+ pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan);
+ testcase( pX->zSpan==0 );
+ }else{
+ pX->zSpan = sqlite3MPrintf(db, "%s.%s.%s",
+ zSchemaName, zTabName, zColname);
+ testcase( pX->zSpan==0 );
+ }
+ pX->bSpanIsTab = 1;
+ }
sqlite3DbFree(db, zToFree);
}
}
@@ -3461,10 +3639,13 @@ static int exprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
*/
static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
Walker w;
- w.xSelectCallback = selectExpander;
+ memset(&w, 0, sizeof(w));
+ w.xSelectCallback = convertCompoundSelectToSubquery;
w.xExprCallback = exprWalkNoop;
w.pParse = pParse;
sqlite3WalkSelect(&w, pSelect);
+ w.xSelectCallback = selectExpander;
+ sqlite3WalkSelect(&w, pSelect);
}
@@ -3519,9 +3700,11 @@ static int selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
#ifndef SQLITE_OMIT_SUBQUERY
Walker w;
+ memset(&w, 0, sizeof(w));
w.xSelectCallback = selectAddSubqueryTypeInfo;
w.xExprCallback = exprWalkNoop;
w.pParse = pParse;
+ w.bSelectDepthFirst = 1;
sqlite3WalkSelect(&w, pSelect);
#endif
}
@@ -3547,6 +3730,7 @@ void sqlite3SelectPrep(
sqlite3 *db;
if( NEVER(p==0) ) return;
db = pParse->db;
+ if( db->mallocFailed ) return;
if( p->selFlags & SF_HasTypeInfo ) return;
sqlite3SelectExpand(pParse, p);
if( pParse->nErr || db->mallocFailed ) return;
@@ -3785,11 +3969,9 @@ int sqlite3Select(
ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
Expr *pHaving; /* The HAVING clause. May be NULL */
- int isDistinct; /* True if the DISTINCT keyword is present */
- int distinct; /* Table to use for the distinct set */
int rc = 1; /* Value to return from this function */
int addrSortIndex; /* Address of an OP_OpenEphemeral instruction */
- int addrDistinctIndex; /* Address of an OP_OpenEphemeral instruction */
+ DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
AggInfo sAggInfo; /* Information used by aggregate queries */
int iEnd; /* Address of the end of the query */
sqlite3 *db; /* The database connection */
@@ -3849,8 +4031,17 @@ int sqlite3Select(
int isAggSub;
if( pSub==0 ) continue;
+
+ /* Sometimes the code for a subquery will be generated more than
+ ** once, if the subquery is part of the WHERE clause in a LEFT JOIN,
+ ** for example. In that case, do not regenerate the code to manifest
+ ** a view or the co-routine to implement a view. The first instance
+ ** is sufficient, though the subroutine to manifest the view does need
+ ** to be invoked again. */
if( pItem->addrFillSub ){
- sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
+ if( pItem->viaCoroutine==0 ){
+ sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
+ }
continue;
}
@@ -3871,6 +4062,44 @@ int sqlite3Select(
p->selFlags |= SF_Aggregate;
}
i = -1;
+ }else if( pTabList->nSrc==1 && (p->selFlags & SF_Materialize)==0
+ && OptimizationEnabled(db, SQLITE_SubqCoroutine)
+ ){
+ /* Implement a co-routine that will return a single row of the result
+ ** set on each invocation.
+ */
+ int addrTop;
+ int addrEof;
+ pItem->regReturn = ++pParse->nMem;
+ addrEof = ++pParse->nMem;
+ /* Before coding the OP_Goto to jump to the start of the main routine,
+ ** ensure that the jump to the verify-schema routine has already
+ ** been coded. Otherwise, the verify-schema would likely be coded as
+ ** part of the co-routine. If the main routine then accessed the
+ ** database before invoking the co-routine for the first time (for
+ ** example to initialize a LIMIT register from a sub-select), it would
+ ** be doing so without having verified the schema version and obtained
+ ** the required db locks. See ticket d6b36be38. */
+ sqlite3CodeVerifySchema(pParse, -1);
+ sqlite3VdbeAddOp0(v, OP_Goto);
+ addrTop = sqlite3VdbeAddOp1(v, OP_OpenPseudo, pItem->iCursor);
+ sqlite3VdbeChangeP5(v, 1);
+ VdbeComment((v, "coroutine for %s", pItem->pTab->zName));
+ pItem->addrFillSub = addrTop;
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, addrEof);
+ sqlite3VdbeChangeP5(v, 1);
+ sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
+ explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
+ sqlite3Select(pParse, pSub, &dest);
+ pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
+ pItem->viaCoroutine = 1;
+ sqlite3VdbeChangeP2(v, addrTop, dest.iSdst);
+ sqlite3VdbeChangeP3(v, addrTop, dest.nSdst);
+ sqlite3VdbeAddOp2(v, OP_Integer, 1, addrEof);
+ sqlite3VdbeAddOp1(v, OP_Yield, pItem->regReturn);
+ VdbeComment((v, "end %s", pItem->pTab->zName));
+ sqlite3VdbeJumpHere(v, addrTop-1);
+ sqlite3ClearTempRegCache(pParse);
}else{
/* Generate a subroutine that will fill an ephemeral table with
** the content of this subquery. pItem->addrFillSub will point
@@ -3886,7 +4115,7 @@ int sqlite3Select(
pItem->addrFillSub = topAddr+1;
VdbeNoopComment((v, "materialize %s", pItem->pTab->zName));
if( pItem->isCorrelated==0 ){
- /* If the subquery is no correlated and if we are not inside of
+ /* If the subquery is not correlated and if we are not inside of
** a trigger, then we only need to compute the value of the subquery
** once. */
onceAddr = sqlite3CodeOnce(pParse);
@@ -3915,7 +4144,7 @@ int sqlite3Select(
pWhere = p->pWhere;
pGroupBy = p->pGroupBy;
pHaving = p->pHaving;
- isDistinct = (p->selFlags & SF_Distinct)!=0;
+ sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
#ifndef SQLITE_OMIT_COMPOUND_SELECT
/* If there is are a sequence of queries, do the earlier ones first.
@@ -3950,7 +4179,7 @@ int sqlite3Select(
** to disable this optimization for testing purposes.
*/
if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy)==0
- && (db->flags & SQLITE_GroupByOrder)==0 ){
+ && OptimizationEnabled(db, SQLITE_GroupByOrder) ){
pOrderBy = 0;
}
@@ -3976,6 +4205,10 @@ int sqlite3Select(
p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
pGroupBy = p->pGroupBy;
pOrderBy = 0;
+ /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
+ ** the sDistinct.isTnct is still set. Hence, isTnct represents the
+ ** original setting of the SF_Distinct flag, not the current setting */
+ assert( sDistinct.isTnct );
}
/* If there is an ORDER BY clause, then this sorting
@@ -4016,24 +4249,27 @@ int sqlite3Select(
/* Open a virtual index to use for the distinct set.
*/
if( p->selFlags & SF_Distinct ){
- KeyInfo *pKeyInfo;
- distinct = pParse->nTab++;
- pKeyInfo = keyInfoFromExprList(pParse, p->pEList);
- addrDistinctIndex = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, distinct, 0, 0,
- (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
+ sDistinct.tabTnct = pParse->nTab++;
+ sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
+ sDistinct.tabTnct, 0, 0,
+ (char*)keyInfoFromExprList(pParse, p->pEList),
+ P4_KEYINFO_HANDOFF);
sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
+ sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
}else{
- distinct = addrDistinctIndex = -1;
+ sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
}
- /* Aggregate and non-aggregate queries are handled differently */
if( !isAgg && pGroupBy==0 ){
- ExprList *pDist = (isDistinct ? p->pEList : 0);
+ /* No aggregate functions and no GROUP BY clause */
+ ExprList *pDist = (sDistinct.isTnct ? p->pEList : 0);
/* Begin the database scan. */
- pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy, pDist, 0,0);
+ pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pOrderBy, pDist, 0,0);
if( pWInfo==0 ) goto select_end;
if( pWInfo->nRowOut < p->nSelectRow ) p->nSelectRow = pWInfo->nRowOut;
+ if( pWInfo->eDistinct ) sDistinct.eTnctType = pWInfo->eDistinct;
+ if( pOrderBy && pWInfo->nOBSat==pOrderBy->nExpr ) pOrderBy = 0;
/* If sorting index that was created by a prior OP_OpenEphemeral
** instruction ended up not being needed, then change the OP_OpenEphemeral
@@ -4044,59 +4280,16 @@ int sqlite3Select(
p->addrOpenEphm[2] = -1;
}
- if( pWInfo->eDistinct ){
- VdbeOp *pOp; /* No longer required OpenEphemeral instr. */
-
- assert( addrDistinctIndex>=0 );
- pOp = sqlite3VdbeGetOp(v, addrDistinctIndex);
-
- assert( isDistinct );
- assert( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
- || pWInfo->eDistinct==WHERE_DISTINCT_UNIQUE
- );
- distinct = -1;
- if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED ){
- int iJump;
- int iExpr;
- int iFlag = ++pParse->nMem;
- int iBase = pParse->nMem+1;
- int iBase2 = iBase + pEList->nExpr;
- pParse->nMem += (pEList->nExpr*2);
-
- /* Change the OP_OpenEphemeral coded earlier to an OP_Integer. The
- ** OP_Integer initializes the "first row" flag. */
- pOp->opcode = OP_Integer;
- pOp->p1 = 1;
- pOp->p2 = iFlag;
-
- sqlite3ExprCodeExprList(pParse, pEList, iBase, 1);
- iJump = sqlite3VdbeCurrentAddr(v) + 1 + pEList->nExpr + 1 + 1;
- sqlite3VdbeAddOp2(v, OP_If, iFlag, iJump-1);
- for(iExpr=0; iExpr<pEList->nExpr; iExpr++){
- CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[iExpr].pExpr);
- sqlite3VdbeAddOp3(v, OP_Ne, iBase+iExpr, iJump, iBase2+iExpr);
- sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
- sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
- }
- sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iContinue);
-
- sqlite3VdbeAddOp2(v, OP_Integer, 0, iFlag);
- assert( sqlite3VdbeCurrentAddr(v)==iJump );
- sqlite3VdbeAddOp3(v, OP_Move, iBase, iBase2, pEList->nExpr);
- }else{
- pOp->opcode = OP_Noop;
- }
- }
-
/* Use the standard inner loop. */
- selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, pDest,
+ selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, &sDistinct, pDest,
pWInfo->iContinue, pWInfo->iBreak);
/* End the database scan loop.
*/
sqlite3WhereEnd(pWInfo);
}else{
- /* This is the processing for aggregate queries */
+ /* This case when there exist aggregate functions or a GROUP BY clause
+ ** or both */
NameContext sNC; /* Name context for processing aggregate information */
int iAMem; /* First Mem address for storing current GROUP BY */
int iBMem; /* First Mem address for previous GROUP BY */
@@ -4204,14 +4397,13 @@ int sqlite3Select(
** in the right order to begin with.
*/
sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
- pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy, 0, 0, 0);
+ pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0, 0, 0);
if( pWInfo==0 ) goto select_end;
- if( pGroupBy==0 ){
+ if( pWInfo->nOBSat==pGroupBy->nExpr ){
/* The optimizer is able to deliver rows in group by order so
** we do not have to sort. The OP_OpenEphemeral table will be
** cancelled later because we still need to use the pKeyInfo
*/
- pGroupBy = p->pGroupBy;
groupBySort = 0;
}else{
/* Rows are coming out in undetermined order. We have to push
@@ -4225,7 +4417,8 @@ int sqlite3Select(
int nGroupBy;
explainTempTable(pParse,
- isDistinct && !(p->selFlags&SF_Distinct)?"DISTINCT":"GROUP BY");
+ (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
+ "DISTINCT" : "GROUP BY");
groupBySort = 1;
nGroupBy = pGroupBy->nExpr;
@@ -4357,7 +4550,7 @@ int sqlite3Select(
finalizeAggFunctions(pParse, &sAggInfo);
sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy,
- distinct, pDest,
+ &sDistinct, pDest,
addrOutputRow+1, addrSetAbort);
sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
VdbeComment((v, "end groupby result generator"));
@@ -4445,7 +4638,7 @@ int sqlite3Select(
** value of x, the only row required).
**
** A special flag must be passed to sqlite3WhereBegin() to slightly
- ** modify behaviour as follows:
+ ** modify behavior as follows:
**
** + If the query is a "SELECT min(x)", then the loop coded by
** where.c should not iterate over any values with a NULL value
@@ -4457,10 +4650,17 @@ int sqlite3Select(
** Refer to code and comments in where.c for details.
*/
ExprList *pMinMax = 0;
- u8 flag = minMaxQuery(p);
+ u8 flag = WHERE_ORDERBY_NORMAL;
+
+ assert( p->pGroupBy==0 );
+ assert( flag==0 );
+ if( p->pHaving==0 ){
+ flag = minMaxQuery(&sAggInfo, &pMinMax);
+ }
+ assert( flag==0 || (pMinMax!=0 && pMinMax->nExpr==1) );
+
if( flag ){
- assert( !ExprHasProperty(p->pEList->a[0].pExpr, EP_xIsSelect) );
- pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->x.pList,0);
+ pMinMax = sqlite3ExprListDup(db, pMinMax, 0);
pDel = pMinMax;
if( pMinMax && !db->mallocFailed ){
pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
@@ -4473,13 +4673,14 @@ int sqlite3Select(
** of output.
*/
resetAccumulator(pParse, &sAggInfo);
- pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pMinMax,0,flag,0);
+ pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0);
if( pWInfo==0 ){
sqlite3ExprListDelete(db, pDel);
goto select_end;
}
updateAccumulator(pParse, &sAggInfo);
- if( !pMinMax && flag ){
+ assert( pMinMax==0 || pMinMax->nExpr==1 );
+ if( pWInfo->nOBSat>0 ){
sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak);
VdbeComment((v, "%s() by index",
(flag==WHERE_ORDERBY_MIN?"min":"max")));
@@ -4490,7 +4691,7 @@ int sqlite3Select(
pOrderBy = 0;
sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
- selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, -1,
+ selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, 0,
pDest, addrEnd, addrEnd);
sqlite3ExprListDelete(db, pDel);
}
@@ -4498,7 +4699,7 @@ int sqlite3Select(
} /* endif aggregate query */
- if( distinct>=0 ){
+ if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
explainTempTable(pParse, "DISTINCT");
}
@@ -4615,7 +4816,10 @@ void sqlite3ExplainSelect(Vdbe *pVdbe, Select *p){
sqlite3ExplainPrintf(pVdbe, "(null-select)");
return;
}
- while( p->pPrior ) p = p->pPrior;
+ while( p->pPrior ){
+ p->pPrior->pNext = p;
+ p = p->pPrior;
+ }
sqlite3ExplainPush(pVdbe);
while( p ){
explainOneSelect(pVdbe, p);
diff --git a/src/shell.c b/src/shell.c
index a17d966..faaec80 100644
--- a/src/shell.c
+++ b/src/shell.c
@@ -90,7 +90,8 @@ static int enableTimer = 0;
#define IsDigit(X) isdigit((unsigned char)X)
#define ToLower(X) (char)tolower((unsigned char)X)
-#if !defined(_WIN32) && !defined(WIN32) && !defined(_WRS_KERNEL)
+#if !defined(_WIN32) && !defined(WIN32) && !defined(_WRS_KERNEL) \
+ && !defined(__minux)
#include <sys/time.h>
#include <sys/resource.h>
@@ -541,6 +542,9 @@ static void output_c_string(FILE *out, const char *z){
if( c=='\\' ){
fputc(c, out);
fputc(c, out);
+ }else if( c=='"' ){
+ fputc('\\', out);
+ fputc('"', out);
}else if( c=='\t' ){
fputc('\\', out);
fputc('t', out);
@@ -696,7 +700,7 @@ static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int
}else{
w = 0;
}
- if( w<=0 ){
+ if( w==0 ){
w = strlen30(azCol[i] ? azCol[i] : "");
if( w<10 ) w = 10;
n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullvalue);
@@ -706,7 +710,11 @@ static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int
p->actualWidth[i] = w;
}
if( p->showHeader ){
- fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
+ if( w<0 ){
+ fprintf(p->out,"%*.*s%s",-w,-w,azCol[i], i==nArg-1 ? "\n": " ");
+ }else{
+ fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
+ }
}
}
if( p->showHeader ){
@@ -714,6 +722,7 @@ static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int
int w;
if( i<ArraySize(p->actualWidth) ){
w = p->actualWidth[i];
+ if( w<0 ) w = -w;
}else{
w = 10;
}
@@ -735,8 +744,13 @@ static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int
strlen30(azArg[i])>w ){
w = strlen30(azArg[i]);
}
- fprintf(p->out,"%-*.*s%s",w,w,
- azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
+ if( w<0 ){
+ fprintf(p->out,"%*.*s%s",-w,-w,
+ azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
+ }else{
+ fprintf(p->out,"%-*.*s%s",w,w,
+ azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
+ }
}
break;
}
@@ -786,14 +800,14 @@ static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int
if( p->cnt++==0 && p->showHeader ){
for(i=0; i<nArg; i++){
output_c_string(p->out,azCol[i] ? azCol[i] : "");
- fprintf(p->out, "%s", p->separator);
+ if(i<nArg-1) fprintf(p->out, "%s", p->separator);
}
fprintf(p->out,"\n");
}
if( azArg==0 ) break;
for(i=0; i<nArg; i++){
output_c_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
- fprintf(p->out, "%s", p->separator);
+ if(i<nArg-1) fprintf(p->out, "%s", p->separator);
}
fprintf(p->out,"\n");
break;
@@ -1416,9 +1430,10 @@ static char zHelp[] =
" list Values delimited by .separator string\n"
" tabs Tab-separated values\n"
" tcl TCL list elements\n"
- ".nullvalue STRING Print STRING in place of NULL values\n"
+ ".nullvalue STRING Use STRING in place of NULL values\n"
".output FILENAME Send output to FILENAME\n"
".output stdout Send output to the screen\n"
+ ".print STRING... Print literal STRING\n"
".prompt MAIN CONTINUE Replace the standard prompts\n"
".quit Exit this program\n"
".read FILENAME Execute SQL in FILENAME\n"
@@ -1511,17 +1526,55 @@ static void resolve_backslashes(char *z){
** Interpret zArg as a boolean value. Return either 0 or 1.
*/
static int booleanValue(char *zArg){
- int val = atoi(zArg);
- int j;
- for(j=0; zArg[j]; j++){
- zArg[j] = ToLower(zArg[j]);
+ int i;
+ for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
+ if( i>0 && zArg[i]==0 ) return atoi(zArg);
+ if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
+ return 1;
}
- if( strcmp(zArg,"on")==0 ){
- val = 1;
- }else if( strcmp(zArg,"yes")==0 ){
- val = 1;
+ if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
+ return 0;
+ }
+ fprintf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
+ zArg);
+ return 0;
+}
+
+/*
+** Interpret zArg as an integer value, possibly with suffixes.
+*/
+static sqlite3_int64 integerValue(const char *zArg){
+ sqlite3_int64 v = 0;
+ static const struct { char *zSuffix; int iMult; } aMult[] = {
+ { "KiB", 1024 },
+ { "MiB", 1024*1024 },
+ { "GiB", 1024*1024*1024 },
+ { "KB", 1000 },
+ { "MB", 1000000 },
+ { "GB", 1000000000 },
+ { "K", 1000 },
+ { "M", 1000000 },
+ { "G", 1000000000 },
+ };
+ int i;
+ int isNeg = 0;
+ if( zArg[0]=='-' ){
+ isNeg = 1;
+ zArg++;
+ }else if( zArg[0]=='+' ){
+ zArg++;
+ }
+ while( isdigit(zArg[0]) ){
+ v = v*10 + zArg[0] - '0';
+ zArg++;
+ }
+ for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){
+ if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
+ v *= aMult[i].iMult;
+ break;
+ }
}
- return val;
+ return isNeg? -v : v;
}
/*
@@ -1609,24 +1662,50 @@ static int do_meta_command(char *zLine, struct callback_data *p){
if( nArg==0 ) return 0; /* no tokens, no error */
n = strlen30(azArg[0]);
c = azArg[0][0];
- if( c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0 && nArg>1 && nArg<4){
- const char *zDestFile;
- const char *zDb;
+ if( c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0 ){
+ const char *zDestFile = 0;
+ const char *zDb = 0;
+ const char *zKey = 0;
sqlite3 *pDest;
sqlite3_backup *pBackup;
- if( nArg==2 ){
- zDestFile = azArg[1];
- zDb = "main";
- }else{
- zDestFile = azArg[2];
- zDb = azArg[1];
+ int j;
+ for(j=1; j<nArg; j++){
+ const char *z = azArg[j];
+ if( z[0]=='-' ){
+ while( z[0]=='-' ) z++;
+ if( strcmp(z,"key")==0 && j<nArg-1 ){
+ zKey = azArg[++j];
+ }else
+ {
+ fprintf(stderr, "unknown option: %s\n", azArg[j]);
+ return 1;
+ }
+ }else if( zDestFile==0 ){
+ zDestFile = azArg[j];
+ }else if( zDb==0 ){
+ zDb = zDestFile;
+ zDestFile = azArg[j];
+ }else{
+ fprintf(stderr, "too many arguments to .backup\n");
+ return 1;
+ }
+ }
+ if( zDestFile==0 ){
+ fprintf(stderr, "missing FILENAME argument on .backup\n");
+ return 1;
}
+ if( zDb==0 ) zDb = "main";
rc = sqlite3_open(zDestFile, &pDest);
if( rc!=SQLITE_OK ){
fprintf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
sqlite3_close(pDest);
return 1;
}
+#ifdef SQLITE_HAS_CODEC
+ sqlite3_key(pDest, zKey, (int)strlen(zKey));
+#else
+ (void)zKey;
+#endif
open_db(p);
pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
if( pBackup==0 ){
@@ -1728,7 +1807,8 @@ static int do_meta_command(char *zLine, struct callback_data *p){
p->echoOn = booleanValue(azArg[1]);
}else
- if( c=='e' && strncmp(azArg[0], "exit", n)==0 && nArg==1 ){
+ if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
+ if( nArg>1 && (rc = atoi(azArg[1]))!=0 ) exit(rc);
rc = 2;
}else
@@ -2007,6 +2087,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
p->mode = MODE_Html;
}else if( n2==3 && strncmp(azArg[1],"tcl",n2)==0 ){
p->mode = MODE_Tcl;
+ sqlite3_snprintf(sizeof(p->separator), p->separator, " ");
}else if( n2==3 && strncmp(azArg[1],"csv",n2)==0 ){
p->mode = MODE_Csv;
sqlite3_snprintf(sizeof(p->separator), p->separator, ",");
@@ -2070,6 +2151,15 @@ static int do_meta_command(char *zLine, struct callback_data *p){
}
}else
+ if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
+ int i;
+ for(i=1; i<nArg; i++){
+ if( i>1 ) fprintf(p->out, " ");
+ fprintf(p->out, "%s", azArg[i]);
+ }
+ fprintf(p->out, "\n");
+ }else
+
if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
if( nArg >= 2) {
strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
@@ -2188,8 +2278,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
" SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
"WHERE lower(tbl_name) LIKE shellstatic()"
" AND type!='meta' AND sql NOTNULL "
- "ORDER BY substr(type,2,1), "
- " CASE type WHEN 'view' THEN rowid ELSE name END",
+ "ORDER BY rowid",
callback, &data, &zErrMsg);
zShellStatic = 0;
}
@@ -2200,8 +2289,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
" FROM sqlite_master UNION ALL"
" SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
"WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'"
- "ORDER BY substr(type,2,1),"
- " CASE type WHEN 'view' THEN rowid ELSE name END",
+ "ORDER BY rowid",
callback, &data, &zErrMsg
);
}
@@ -2323,9 +2411,9 @@ static int do_meta_command(char *zLine, struct callback_data *p){
for(i=0; i<nPrintRow; i++){
for(j=i; j<nRow; j+=nPrintRow){
char *zSp = j<nPrintRow ? "" : " ";
- printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
+ fprintf(p->out, "%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
}
- printf("\n");
+ fprintf(p->out, "\n");
}
}
for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
@@ -2382,7 +2470,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
if( nArg==3 ){
int opt = (int)strtol(azArg[2], 0, 0);
rc = sqlite3_test_control(testctrl, p->db, opt);
- printf("%d (0x%08x)\n", rc, rc);
+ fprintf(p->out, "%d (0x%08x)\n", rc, rc);
} else {
fprintf(stderr,"Error: testctrl %s takes a single int option\n",
azArg[1]);
@@ -2395,7 +2483,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
case SQLITE_TESTCTRL_PRNG_RESET:
if( nArg==2 ){
rc = sqlite3_test_control(testctrl);
- printf("%d (0x%08x)\n", rc, rc);
+ fprintf(p->out, "%d (0x%08x)\n", rc, rc);
} else {
fprintf(stderr,"Error: testctrl %s takes no options\n", azArg[1]);
}
@@ -2404,9 +2492,9 @@ static int do_meta_command(char *zLine, struct callback_data *p){
/* sqlite3_test_control(int, uint) */
case SQLITE_TESTCTRL_PENDING_BYTE:
if( nArg==3 ){
- unsigned int opt = (unsigned int)atoi(azArg[2]);
+ unsigned int opt = (unsigned int)integerValue(azArg[2]);
rc = sqlite3_test_control(testctrl, opt);
- printf("%d (0x%08x)\n", rc, rc);
+ fprintf(p->out, "%d (0x%08x)\n", rc, rc);
} else {
fprintf(stderr,"Error: testctrl %s takes a single unsigned"
" int option\n", azArg[1]);
@@ -2419,7 +2507,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
if( nArg==3 ){
int opt = atoi(azArg[2]);
rc = sqlite3_test_control(testctrl, opt);
- printf("%d (0x%08x)\n", rc, rc);
+ fprintf(p->out, "%d (0x%08x)\n", rc, rc);
} else {
fprintf(stderr,"Error: testctrl %s takes a single int option\n",
azArg[1]);
@@ -2432,7 +2520,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
if( nArg==3 ){
const char *opt = azArg[2];
rc = sqlite3_test_control(testctrl, opt);
- printf("%d (0x%08x)\n", rc, rc);
+ fprintf(p->out, "%d (0x%08x)\n", rc, rc);
} else {
fprintf(stderr,"Error: testctrl %s takes a single char * option\n",
azArg[1]);
@@ -2477,7 +2565,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
}else
if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
- printf("SQLite %s %s\n" /*extra-version-info*/,
+ fprintf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
sqlite3_libversion(), sqlite3_sourceid());
}else
@@ -2487,12 +2575,19 @@ static int do_meta_command(char *zLine, struct callback_data *p){
if( p->db ){
sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
if( zVfsName ){
- printf("%s\n", zVfsName);
+ fprintf(p->out, "%s\n", zVfsName);
sqlite3_free(zVfsName);
}
}
}else
+#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
+ if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
+ extern int sqlite3WhereTrace;
+ sqlite3WhereTrace = booleanValue(azArg[1]);
+ }else
+#endif
+
if( c=='w' && strncmp(azArg[0], "width", n)==0 && nArg>1 ){
int j;
assert( nArg<=ArraySize(azArg) );
@@ -2675,6 +2770,10 @@ static int process_input(struct callback_data *p, FILE *in){
free(zSql);
zSql = 0;
nSql = 0;
+ }else if( zSql && _all_whitespace(zSql) ){
+ free(zSql);
+ zSql = 0;
+ nSql = 0;
}
}
if( zSql ){
@@ -2684,7 +2783,7 @@ static int process_input(struct callback_data *p, FILE *in){
free(zSql);
}
free(zLine);
- return errCnt;
+ return errCnt>0;
}
/*
@@ -2797,21 +2896,25 @@ static const char zOptions[] =
" -bail stop after hitting an error\n"
" -batch force batch I/O\n"
" -column set output mode to 'column'\n"
- " -cmd command run \"command\" before reading stdin\n"
+ " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
" -csv set output mode to 'csv'\n"
" -echo print commands before execution\n"
- " -init filename read/process named file\n"
+ " -init FILENAME read/process named file\n"
" -[no]header turn headers on or off\n"
+#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
+ " -heap SIZE Size of heap for memsys3 or memsys5\n"
+#endif
" -help show this message\n"
" -html set output mode to HTML\n"
" -interactive force interactive I/O\n"
" -line set output mode to 'line'\n"
" -list set output mode to 'list'\n"
+ " -mmap N default mmap size set to N\n"
#ifdef SQLITE_ENABLE_MULTIPLEX
" -multiplex enable the multiplexor VFS\n"
#endif
- " -nullvalue 'text' set text string for NULL values\n"
- " -separator 'x' set output field separator (|)\n"
+ " -nullvalue TEXT set text string for NULL values. Default ''\n"
+ " -separator SEP set output field separator. Default: '|'\n"
" -stats print memory stats before each finalize\n"
" -version show SQLite version\n"
" -vfs NAME use NAME as the default VFS\n"
@@ -2847,6 +2950,19 @@ static void main_init(struct callback_data *data) {
sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
}
+/*
+** Get the argument to an --option. Throw an error and die if no argument
+** is available.
+*/
+static char *cmdline_option_value(int argc, char **argv, int i){
+ if( i==argc ){
+ fprintf(stderr, "%s: Error: missing argument to %s\n",
+ argv[0], argv[argc-1]);
+ exit(1);
+ }
+ return argv[i];
+}
+
int main(int argc, char **argv){
char *zErrMsg = 0;
struct callback_data data;
@@ -2876,24 +2992,35 @@ int main(int argc, char **argv){
** the size of the alternative malloc heap,
** and the first command to execute.
*/
- for(i=1; i<argc-1; i++){
+ for(i=1; i<argc; i++){
char *z;
- if( argv[i][0]!='-' ) break;
z = argv[i];
+ if( z[0]!='-' ){
+ if( data.zDbFilename==0 ){
+ data.zDbFilename = z;
+ continue;
+ }
+ if( zFirstCmd==0 ){
+ zFirstCmd = z;
+ continue;
+ }
+ fprintf(stderr,"%s: Error: too many options: \"%s\"\n", Argv0, argv[i]);
+ fprintf(stderr,"Use -help for a list of options.\n");
+ return 1;
+ }
if( z[1]=='-' ) z++;
if( strcmp(z,"-separator")==0
|| strcmp(z,"-nullvalue")==0
|| strcmp(z,"-cmd")==0
){
- i++;
+ (void)cmdline_option_value(argc, argv, ++i);
}else if( strcmp(z,"-init")==0 ){
- i++;
- zInitFile = argv[i];
- /* Need to check for batch mode here to so we can avoid printing
- ** informational messages (like from process_sqliterc) before
- ** we do the actual processing of arguments later in a second pass.
- */
+ zInitFile = cmdline_option_value(argc, argv, ++i);
}else if( strcmp(z,"-batch")==0 ){
+ /* Need to check for batch mode here to so we can avoid printing
+ ** informational messages (like from process_sqliterc) before
+ ** we do the actual processing of arguments later in a second pass.
+ */
stdin_is_interactive = 0;
}else if( strcmp(z,"-heap")==0 ){
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
@@ -2901,13 +3028,8 @@ int main(int argc, char **argv){
const char *zSize;
sqlite3_int64 szHeap;
- zSize = argv[++i];
- szHeap = atoi(zSize);
- for(j=0; (c = zSize[j])!=0; j++){
- if( c=='M' ){ szHeap *= 1000000; break; }
- if( c=='K' ){ szHeap *= 1000; break; }
- if( c=='G' ){ szHeap *= 1000000000; break; }
- }
+ zSize = cmdline_option_value(argc, argv, ++i);
+ szHeap = integerValue(zSize);
if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
#endif
@@ -2927,8 +3049,11 @@ int main(int argc, char **argv){
extern int sqlite3_multiple_initialize(const char*,int);
sqlite3_multiplex_initialize(0, 1);
#endif
+ }else if( strcmp(z,"-mmap")==0 ){
+ sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
+ sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
}else if( strcmp(z,"-vfs")==0 ){
- sqlite3_vfs *pVfs = sqlite3_vfs_find(argv[++i]);
+ sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i));
if( pVfs ){
sqlite3_vfs_register(pVfs, 1);
}else{
@@ -2937,31 +3062,15 @@ int main(int argc, char **argv){
}
}
}
- if( i<argc ){
- data.zDbFilename = argv[i++];
- }else{
+ if( data.zDbFilename==0 ){
#ifndef SQLITE_OMIT_MEMORYDB
data.zDbFilename = ":memory:";
#else
- data.zDbFilename = 0;
-#endif
- }
- if( i<argc ){
- zFirstCmd = argv[i++];
- }
- if( i<argc ){
- fprintf(stderr,"%s: Error: too many options: \"%s\"\n", Argv0, argv[i]);
- fprintf(stderr,"Use -help for a list of options.\n");
- return 1;
- }
- data.out = stdout;
-
-#ifdef SQLITE_OMIT_MEMORYDB
- if( data.zDbFilename==0 ){
fprintf(stderr,"%s: Error: no database filename specified\n", Argv0);
return 1;
- }
#endif
+ }
+ data.out = stdout;
/* Go ahead and open the database file if it already exists. If the
** file does not exist, delay opening it. This prevents empty database
@@ -2986,8 +3095,9 @@ int main(int argc, char **argv){
** file is processed so that the command-line arguments will override
** settings in the initialization file.
*/
- for(i=1; i<argc && argv[i][0]=='-'; i++){
+ for(i=1; i<argc; i++){
char *z = argv[i];
+ if( z[0]!='-' ) continue;
if( z[1]=='-' ){ z++; }
if( strcmp(z,"-init")==0 ){
i++;
@@ -3003,25 +3113,11 @@ int main(int argc, char **argv){
data.mode = MODE_Csv;
memcpy(data.separator,",",2);
}else if( strcmp(z,"-separator")==0 ){
- i++;
- if(i>=argc){
- fprintf(stderr,"%s: Error: missing argument for option: %s\n",
- Argv0, z);
- fprintf(stderr,"Use -help for a list of options.\n");
- return 1;
- }
sqlite3_snprintf(sizeof(data.separator), data.separator,
- "%.*s",(int)sizeof(data.separator)-1,argv[i]);
+ "%s",cmdline_option_value(argc,argv,++i));
}else if( strcmp(z,"-nullvalue")==0 ){
- i++;
- if(i>=argc){
- fprintf(stderr,"%s: Error: missing argument for option: %s\n",
- Argv0, z);
- fprintf(stderr,"Use -help for a list of options.\n");
- return 1;
- }
sqlite3_snprintf(sizeof(data.nullvalue), data.nullvalue,
- "%.*s",(int)sizeof(data.nullvalue)-1,argv[i]);
+ "%s",cmdline_option_value(argc,argv,++i));
}else if( strcmp(z,"-header")==0 ){
data.showHeader = 1;
}else if( strcmp(z,"-noheader")==0 ){
@@ -3041,6 +3137,8 @@ int main(int argc, char **argv){
stdin_is_interactive = 0;
}else if( strcmp(z,"-heap")==0 ){
i++;
+ }else if( strcmp(z,"-mmap")==0 ){
+ i++;
}else if( strcmp(z,"-vfs")==0 ){
i++;
#ifdef SQLITE_ENABLE_VFSTRACE
@@ -3055,11 +3153,10 @@ int main(int argc, char **argv){
usage(1);
}else if( strcmp(z,"-cmd")==0 ){
if( i==argc-1 ) break;
- i++;
- z = argv[i];
+ z = cmdline_option_value(argc,argv,++i);
if( z[0]=='.' ){
rc = do_meta_command(z, &data);
- if( rc && bail_on_error ) return rc;
+ if( rc && bail_on_error ) return rc==2 ? 0 : rc;
}else{
open_db(&data);
rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg);
@@ -3083,6 +3180,7 @@ int main(int argc, char **argv){
*/
if( zFirstCmd[0]=='.' ){
rc = do_meta_command(zFirstCmd, &data);
+ if( rc==2 ) rc = 0;
}else{
open_db(&data);
rc = shell_exec(data.db, zFirstCmd, shell_callback, &data, &zErrMsg);
@@ -3102,7 +3200,13 @@ int main(int argc, char **argv){
char *zHistory = 0;
int nHistory;
printf(
+/* BEGIN SQLCIPHER */
+#ifdef SQLITE_HAS_CODEC
+ "SQLCipher version %s %.19s\n" /*extra-version-info*/
+#else
"SQLite version %s %.19s\n" /*extra-version-info*/
+#endif
+/* END SQLCIPHER */
"Enter \".help\" for instructions\n"
"Enter SQL statements terminated with a \";\"\n",
sqlite3_libversion(), sqlite3_sourceid()
diff --git a/src/sqlcipher.h b/src/sqlcipher.h
new file mode 100644
index 0000000..41f8f83
--- /dev/null
+++ b/src/sqlcipher.h
@@ -0,0 +1,75 @@
+/*
+** SQLCipher
+** sqlcipher.h developed by Stephen Lombardo (Zetetic LLC)
+** sjlombardo at zetetic dot net
+** http://zetetic.net
+**
+** Copyright (c) 2008, ZETETIC LLC
+** All rights reserved.
+**
+** Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in the
+** documentation and/or other materials provided with the distribution.
+** * Neither the name of the ZETETIC LLC nor the
+** names of its contributors may be used to endorse or promote products
+** derived from this software without specific prior written permission.
+**
+** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY
+** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY
+** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+**
+*/
+/* BEGIN SQLCIPHER */
+#ifdef SQLITE_HAS_CODEC
+#ifndef SQLCIPHER_H
+#define SQLCIPHER_H
+
+
+typedef struct {
+ int (*activate)(void *ctx);
+ int (*deactivate)(void *ctx);
+ const char* (*get_provider_name)(void *ctx);
+ int (*add_random)(void *ctx, void *buffer, int length);
+ int (*random)(void *ctx, void *buffer, int length);
+ int (*hmac)(void *ctx, unsigned char *hmac_key, int key_sz, unsigned char *in, int in_sz, unsigned char *in2, int in2_sz, unsigned char *out);
+ int (*kdf)(void *ctx, const char *pass, int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key);
+ int (*cipher)(void *ctx, int mode, unsigned char *key, int key_sz, unsigned char *iv, unsigned char *in, int in_sz, unsigned char *out);
+ int (*set_cipher)(void *ctx, const char *cipher_name);
+ const char* (*get_cipher)(void *ctx);
+ int (*get_key_sz)(void *ctx);
+ int (*get_iv_sz)(void *ctx);
+ int (*get_block_sz)(void *ctx);
+ int (*get_hmac_sz)(void *ctx);
+ int (*ctx_copy)(void *target_ctx, void *source_ctx);
+ int (*ctx_cmp)(void *c1, void *c2);
+ int (*ctx_init)(void **ctx);
+ int (*ctx_free)(void **ctx);
+} sqlcipher_provider;
+
+/* utility functions */
+void sqlcipher_free(void *ptr, int sz);
+void* sqlcipher_malloc(int sz);
+void* sqlcipher_memset(void *v, unsigned char value, int len);
+int sqlcipher_ismemset(const void *v, unsigned char value, int len);
+int sqlcipher_memcmp(const void *v0, const void *v1, int len);
+void sqlcipher_free(void *, int);
+
+/* provider interfaces */
+int sqlcipher_register_provider(sqlcipher_provider *p);
+sqlcipher_provider* sqlcipher_get_provider();
+
+#endif
+#endif
+/* END SQLCIPHER */
+
diff --git a/src/sqlite.h.in b/src/sqlite.h.in
index 9a31e22..6608823 100644
--- a/src/sqlite.h.in
+++ b/src/sqlite.h.in
@@ -283,7 +283,7 @@ typedef sqlite_uint64 sqlite3_uint64;
** [sqlite3_blob_close | close] all [BLOB handles], and
** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
** with the [sqlite3] object prior to attempting to close the object. ^If
-** sqlite3_close() is called on a [database connection] that still has
+** sqlite3_close_v2() is called on a [database connection] that still has
** outstanding [prepared statements], [BLOB handles], and/or
** [sqlite3_backup] objects then it returns SQLITE_OK but the deallocation
** of resources is deferred until all [prepared statements], [BLOB handles],
@@ -420,6 +420,8 @@ int sqlite3_exec(
#define SQLITE_FORMAT 24 /* Auxiliary database format error */
#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */
#define SQLITE_NOTADB 26 /* File opened that is not a database file */
+#define SQLITE_NOTICE 27 /* Notifications from sqlite3_log() */
+#define SQLITE_WARNING 28 /* Warnings from sqlite3_log() */
#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
/* end-of-error-codes */
@@ -469,14 +471,29 @@ int sqlite3_exec(
#define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8))
#define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8))
#define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8))
+#define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8))
+#define SQLITE_IOERR_MMAP (SQLITE_IOERR | (24<<8))
#define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
#define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
#define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
#define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
+#define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
#define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8))
#define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8))
#define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8))
+#define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8))
#define SQLITE_ABORT_ROLLBACK (SQLITE_ABORT | (2<<8))
+#define SQLITE_CONSTRAINT_CHECK (SQLITE_CONSTRAINT | (1<<8))
+#define SQLITE_CONSTRAINT_COMMITHOOK (SQLITE_CONSTRAINT | (2<<8))
+#define SQLITE_CONSTRAINT_FOREIGNKEY (SQLITE_CONSTRAINT | (3<<8))
+#define SQLITE_CONSTRAINT_FUNCTION (SQLITE_CONSTRAINT | (4<<8))
+#define SQLITE_CONSTRAINT_NOTNULL (SQLITE_CONSTRAINT | (5<<8))
+#define SQLITE_CONSTRAINT_PRIMARYKEY (SQLITE_CONSTRAINT | (6<<8))
+#define SQLITE_CONSTRAINT_TRIGGER (SQLITE_CONSTRAINT | (7<<8))
+#define SQLITE_CONSTRAINT_UNIQUE (SQLITE_CONSTRAINT | (8<<8))
+#define SQLITE_CONSTRAINT_VTAB (SQLITE_CONSTRAINT | (9<<8))
+#define SQLITE_NOTICE_RECOVER_WAL (SQLITE_NOTICE | (1<<8))
+#define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8))
/*
** CAPI3REF: Flags For File Open Operations
@@ -716,6 +733,9 @@ struct sqlite3_io_methods {
void (*xShmBarrier)(sqlite3_file*);
int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
/* Methods above are valid for version 2 */
+ int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
+ int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
+ /* Methods above are valid for version 3 */
/* Additional methods may be added in future releases */
};
@@ -850,6 +870,38 @@ struct sqlite3_io_methods {
** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
** file control occurs at the beginning of pragma statement analysis and so
** it is able to override built-in [PRAGMA] statements.
+**
+** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
+** ^The [SQLITE_FCNTL_BUSYHANDLER]
+** file-control may be invoked by SQLite on the database file handle
+** shortly after it is opened in order to provide a custom VFS with access
+** to the connections busy-handler callback. The argument is of type (void **)
+** - an array of two (void *) values. The first (void *) actually points
+** to a function of type (int (*)(void *)). In order to invoke the connections
+** busy-handler, this function should be invoked with the second (void *) in
+** the array as the only argument. If it returns non-zero, then the operation
+** should be retried. If it returns zero, the custom VFS should abandon the
+** current operation.
+**
+** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
+** ^Application can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
+** to have SQLite generate a
+** temporary filename using the same algorithm that is followed to generate
+** temporary filenames for TEMP tables and other internal uses. The
+** argument should be a char** which will be filled with the filename
+** written into memory obtained from [sqlite3_malloc()]. The caller should
+** invoke [sqlite3_free()] on the result to avoid a memory leak.
+**
+** <li>[[SQLITE_FCNTL_MMAP_SIZE]]
+** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the
+** maximum number of bytes that will be used for memory-mapped I/O.
+** The argument is a pointer to a value of type sqlite3_int64 that
+** is an advisory maximum number of bytes in the file to memory map. The
+** pointer is overwritten with the old value. The limit is not changed if
+** the value originally pointed to is negative, and so the current limit
+** can be queried by passing in a pointer to a negative number. This
+** file-control is used internally to implement [PRAGMA mmap_size].
+**
** </ul>
*/
#define SQLITE_FCNTL_LOCKSTATE 1
@@ -866,6 +918,9 @@ struct sqlite3_io_methods {
#define SQLITE_FCNTL_VFSNAME 12
#define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
#define SQLITE_FCNTL_PRAGMA 14
+#define SQLITE_FCNTL_BUSYHANDLER 15
+#define SQLITE_FCNTL_TEMPFILENAME 16
+#define SQLITE_FCNTL_MMAP_SIZE 18
/*
** CAPI3REF: Mutex Handle
@@ -1532,7 +1587,9 @@ struct sqlite3_mem_methods {
** page cache implementation into that object.)^ </dd>
**
** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
-** <dd> ^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
+** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
+** global [error log].
+** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
** function with a call signature of void(*)(void*,int,const char*),
** and a pointer to void. ^If the function pointer is not NULL, it is
** invoked by [sqlite3_log()] to process each logging event. ^If the
@@ -1562,10 +1619,54 @@ struct sqlite3_mem_methods {
** disabled. The default value may be changed by compiling with the
** [SQLITE_USE_URI] symbol defined.
**
+** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
+** <dd> This option takes a single integer argument which is interpreted as
+** a boolean in order to enable or disable the use of covering indices for
+** full table scans in the query optimizer. The default setting is determined
+** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
+** if that compile-time option is omitted.
+** The ability to disable the use of covering indices for full table scans
+** is because some incorrectly coded legacy applications might malfunction
+** malfunction when the optimization is enabled. Providing the ability to
+** disable the optimization allows the older, buggy application code to work
+** without change even with newer versions of SQLite.
+**
** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
** <dd> These options are obsolete and should not be used by new code.
** They are retained for backwards compatibility but are now no-ops.
+** </dd>
+**
+** [[SQLITE_CONFIG_SQLLOG]]
+** <dt>SQLITE_CONFIG_SQLLOG
+** <dd>This option is only available if sqlite is compiled with the
+** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
+** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
+** The second should be of type (void*). The callback is invoked by the library
+** in three separate circumstances, identified by the value passed as the
+** fourth parameter. If the fourth parameter is 0, then the database connection
+** passed as the second argument has just been opened. The third argument
+** points to a buffer containing the name of the main database file. If the
+** fourth parameter is 1, then the SQL statement that the third parameter
+** points to has just been executed. Or, if the fourth parameter is 2, then
+** the connection being passed as the second parameter is being closed. The
+** third parameter is passed NULL In this case. An example of using this
+** configuration option can be seen in the "test_sqllog.c" source file in
+** the canonical SQLite source tree.</dd>
+**
+** [[SQLITE_CONFIG_MMAP_SIZE]]
+** <dt>SQLITE_CONFIG_MMAP_SIZE
+** <dd>SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
+** that are the default mmap size limit (the default setting for
+** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
+** The default setting can be overridden by each database connection using
+** either the [PRAGMA mmap_size] command, or by using the
+** [SQLITE_FCNTL_MMAP_SIZE] file control. The maximum allowed mmap size
+** cannot be changed at run-time. Nor may the maximum allowed mmap size
+** exceed the compile-time maximum mmap size set by the
+** [SQLITE_MAX_MMAP_SIZE] compile-time option.
+** If either argument to this option is negative, then that argument is
+** changed to its compile-time default.
** </dl>
*/
#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
@@ -1587,6 +1688,9 @@ struct sqlite3_mem_methods {
#define SQLITE_CONFIG_URI 17 /* int */
#define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */
#define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
+#define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */
+#define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */
+#define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */
/*
** CAPI3REF: Database Connection Configuration Options
@@ -2420,6 +2524,9 @@ int sqlite3_set_authorizer(
** as each triggered subprogram is entered. The callbacks for triggers
** contain a UTF-8 SQL comment that identifies the trigger.)^
**
+** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
+** the length of [bound parameter] expansion in the output of sqlite3_trace().
+**
** ^The callback function registered by sqlite3_profile() is invoked
** as each SQL statement finishes. ^The profile callback contains
** the original statement text and an estimate of wall-clock time
@@ -2595,7 +2702,7 @@ void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
** an error)^.
** ^If "ro" is specified, then the database is opened for read-only
** access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the
-** third argument to sqlite3_prepare_v2(). ^If the mode option is set to
+** third argument to sqlite3_open_v2(). ^If the mode option is set to
** "rw", then the database is opened for read-write (but not create)
** access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had
** been set. ^Value "rwc" is equivalent to setting both
@@ -2611,7 +2718,7 @@ void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
** sqlite3_open_v2(). ^Setting the cache parameter to "private" is
** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
-** a URI filename, its value overrides any behaviour requested by setting
+** a URI filename, its value overrides any behavior requested by setting
** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
** </ul>
**
@@ -2747,6 +2854,11 @@ sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
** However, the error string might be overwritten or deallocated by
** subsequent calls to other SQLite interface functions.)^
**
+** ^The sqlite3_errstr() interface returns the English-language text
+** that describes the [result code], as UTF-8.
+** ^(Memory to hold the error message string is managed internally
+** and must not be freed by the application)^.
+**
** When the serialized [threading mode] is in use, it might be the
** case that a second error occurs on a separate thread in between
** the time of the first error and the call to these interfaces.
@@ -2765,6 +2877,7 @@ int sqlite3_errcode(sqlite3 *db);
int sqlite3_extended_errcode(sqlite3 *db);
const char *sqlite3_errmsg(sqlite3*);
const void *sqlite3_errmsg16(sqlite3*);
+const char *sqlite3_errstr(int);
/*
** CAPI3REF: SQL Statement Object
@@ -2952,7 +3065,8 @@ int sqlite3_limit(sqlite3*, int id, int newVal);
** <li>
** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
** always used to do, [sqlite3_step()] will automatically recompile the SQL
-** statement and try to run it again.
+** statement and try to run it again. As many as [SQLITE_MAX_SCHEMA_RETRY]
+** retries will occur before sqlite3_step() gives up and returns an error.
** </li>
**
** <li>
@@ -3156,6 +3270,9 @@ typedef struct sqlite3_context sqlite3_context;
** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999).
**
** ^The third argument is the value to bind to the parameter.
+** ^If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16()
+** or sqlite3_bind_blob() is a NULL pointer then the fourth parameter
+** is ignored and the end result is the same as sqlite3_bind_null().
**
** ^(In those routines that have a fourth argument, its value is the
** number of bytes in the parameter. To be clear: the value is the
@@ -3923,7 +4040,8 @@ SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
SQLITE_DEPRECATED int sqlite3_global_recover(void);
SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
-SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64);
+SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
+ void*,sqlite3_int64);
#endif
/*
@@ -4003,14 +4121,17 @@ int sqlite3_value_numeric_type(sqlite3_value*);
** In those cases, sqlite3_aggregate_context() might be called for the
** first time from within xFinal().)^
**
-** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer if N is
-** less than or equal to zero or if a memory allocate error occurs.
+** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
+** when first called if N is less than or equal to zero or if a memory
+** allocate error occurs.
**
** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
** determined by the N parameter on first successful call. Changing the
** value of N in subsequent call to sqlite3_aggregate_context() within
** the same aggregate function instance will not resize the memory
-** allocation.)^
+** allocation.)^ Within the xFinal callback, it is customary to set
+** N=0 in calls to sqlite3_aggregate_context(C,N) so that no
+** pointless memory allocations occur.
**
** ^SQLite automatically frees the memory allocated by
** sqlite3_aggregate_context() when the aggregate query concludes.
@@ -4108,7 +4229,7 @@ void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
** the content before returning.
**
** The typedef is necessary to work around problems in certain
-** C++ compilers. See ticket #2191.
+** C++ compilers.
*/
typedef void (*sqlite3_destructor_type)(void*);
#define SQLITE_STATIC ((sqlite3_destructor_type)0)
@@ -4727,6 +4848,9 @@ void *sqlite3_update_hook(
** future releases of SQLite. Applications that care about shared
** cache setting should set it explicitly.
**
+** This interface is threadsafe on processors where writing a
+** 32-bit integer is atomic.
+**
** See Also: [SQLite Shared-Cache Mode]
*/
int sqlite3_enable_shared_cache(int);
@@ -4904,11 +5028,20 @@ int sqlite3_table_column_metadata(
** ^This interface loads an SQLite extension library from the named file.
**
** ^The sqlite3_load_extension() interface attempts to load an
-** SQLite extension library contained in the file zFile.
+** [SQLite extension] library contained in the file zFile. If
+** the file cannot be loaded directly, attempts are made to load
+** with various operating-system specific extensions added.
+** So for example, if "samplelib" cannot be loaded, then names like
+** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might
+** be tried also.
**
** ^The entry point is zProc.
-** ^zProc may be 0, in which case the name of the entry point
-** defaults to "sqlite3_extension_init".
+** ^(zProc may be 0, in which case SQLite will try to come up with an
+** entry point name on its own. It first tries "sqlite3_extension_init".
+** If that does not work, it constructs a name "sqlite3_X_init" where the
+** X is consists of the lower-case equivalent of all ASCII alphabetic
+** characters in the filename from the last "/" to the first following
+** "." and omitting any initial "lib".)^
** ^The sqlite3_load_extension() interface returns
** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
** ^If an error occurs and pzErrMsg is not 0, then the
@@ -4934,11 +5067,11 @@ int sqlite3_load_extension(
** CAPI3REF: Enable Or Disable Extension Loading
**
** ^So as not to open security holes in older applications that are
-** unprepared to deal with extension loading, and as a means of disabling
-** extension loading while evaluating user-entered SQL, the following API
+** unprepared to deal with [extension loading], and as a means of disabling
+** [extension loading] while evaluating user-entered SQL, the following API
** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
**
-** ^Extension loading is off by default. See ticket #1863.
+** ^Extension loading is off by default.
** ^Call the sqlite3_enable_load_extension() routine with onoff==1
** to turn extension loading on and call it with onoff==0 to turn
** it back off again.
@@ -4950,7 +5083,7 @@ int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
**
** ^This interface causes the xEntryPoint() function to be invoked for
** each new [database connection] that is created. The idea here is that
-** xEntryPoint() is the entry point for a statically linked SQLite extension
+** xEntryPoint() is the entry point for a statically linked [SQLite extension]
** that is to be automatically loaded into all new database connections.
**
** ^(Even though the function prototype shows that xEntryPoint() takes
@@ -6301,7 +6434,7 @@ struct sqlite3_pcache_page {
** parameter to help it determined what action to take:
**
** <table border=1 width=85% align=center>
-** <tr><th> createFlag <th> Behaviour when page is not already in cache
+** <tr><th> createFlag <th> Behavior when page is not already in cache
** <tr><td> 0 <td> Do not allocate a new page. Return NULL.
** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
** Otherwise return NULL.
@@ -6731,9 +6864,24 @@ int sqlite3_stricmp(const char *, const char *);
int sqlite3_strnicmp(const char *, const char *, int);
/*
+** CAPI3REF: String Globbing
+*
+** ^The [sqlite3_strglob(P,X)] interface returns zero if string X matches
+** the glob pattern P, and it returns non-zero if string X does not match
+** the glob pattern P. ^The definition of glob pattern matching used in
+** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
+** SQL dialect used by SQLite. ^The sqlite3_strglob(P,X) function is case
+** sensitive.
+**
+** Note that this routine returns zero on a match and non-zero if the strings
+** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
+*/
+int sqlite3_strglob(const char *zGlob, const char *zStr);
+
+/*
** CAPI3REF: Error Logging Interface
**
-** ^The [sqlite3_log()] interface writes a message into the error log
+** ^The [sqlite3_log()] interface writes a message into the [error log]
** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
** ^If logging is enabled, the zFormat string and subsequent arguments are
** used with [sqlite3_snprintf()] to generate the final output string.
diff --git a/src/sqlite3ext.h b/src/sqlite3ext.h
index 5abcde2..928bb3b 100644
--- a/src/sqlite3ext.h
+++ b/src/sqlite3ext.h
@@ -236,6 +236,20 @@ struct sqlite3_api_routines {
int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
int (*vtab_config)(sqlite3*,int op,...);
int (*vtab_on_conflict)(sqlite3*);
+ /* Version 3.7.16 and later */
+ int (*close_v2)(sqlite3*);
+ const char *(*db_filename)(sqlite3*,const char*);
+ int (*db_readonly)(sqlite3*,const char*);
+ int (*db_release_memory)(sqlite3*);
+ const char *(*errstr)(int);
+ int (*stmt_busy)(sqlite3_stmt*);
+ int (*stmt_readonly)(sqlite3_stmt*);
+ int (*stricmp)(const char*,const char*);
+ int (*uri_boolean)(const char*,const char*,int);
+ sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
+ const char *(*uri_parameter)(const char*,const char*);
+ char *(*vsnprintf)(int,char*,const char*,va_list);
+ int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
};
/*
@@ -439,9 +453,32 @@ struct sqlite3_api_routines {
#define sqlite3_blob_reopen sqlite3_api->blob_reopen
#define sqlite3_vtab_config sqlite3_api->vtab_config
#define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
+/* Version 3.7.16 and later */
+#define sqlite3_close_v2 sqlite3_api->close_v2
+#define sqlite3_db_filename sqlite3_api->db_filename
+#define sqlite3_db_readonly sqlite3_api->db_readonly
+#define sqlite3_db_release_memory sqlite3_api->db_release_memory
+#define sqlite3_errstr sqlite3_api->errstr
+#define sqlite3_stmt_busy sqlite3_api->stmt_busy
+#define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
+#define sqlite3_stricmp sqlite3_api->stricmp
+#define sqlite3_uri_boolean sqlite3_api->uri_boolean
+#define sqlite3_uri_int64 sqlite3_api->uri_int64
+#define sqlite3_uri_parameter sqlite3_api->uri_parameter
+#define sqlite3_uri_vsnprintf sqlite3_api->vsnprintf
+#define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
#endif /* SQLITE_CORE */
-#define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api = 0;
-#define SQLITE_EXTENSION_INIT2(v) sqlite3_api = v;
+#ifndef SQLITE_CORE
+ /* This case when the file really is being compiled as a loadable
+ ** extension */
+# define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0;
+# define SQLITE_EXTENSION_INIT2(v) sqlite3_api=v;
+#else
+ /* This case when the file is being statically linked into the
+ ** application */
+# define SQLITE_EXTENSION_INIT1 /*no-op*/
+# define SQLITE_EXTENSION_INIT2(v) (void)v; /* unused parameter */
+#endif
#endif /* _SQLITE3EXT_H_ */
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index 09163bf..5950f23 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -66,6 +66,10 @@
# define _GNU_SOURCE
#endif
+#if defined(__OpenBSD__) && !defined(_BSD_SOURCE)
+# define _BSD_SOURCE
+#endif
+
/*
** Include standard header files as necessary
*/
@@ -118,11 +122,11 @@
** We support that for legacy.
*/
#if !defined(SQLITE_THREADSAFE)
-#if defined(THREADSAFE)
-# define SQLITE_THREADSAFE THREADSAFE
-#else
-# define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
-#endif
+# if defined(THREADSAFE)
+# define SQLITE_THREADSAFE THREADSAFE
+# else
+# define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
+# endif
#endif
/*
@@ -200,7 +204,8 @@
**
** See also ticket #2741.
*/
-#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__) && SQLITE_THREADSAFE
+#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) \
+ && !defined(__APPLE__) && SQLITE_THREADSAFE
# define _XOPEN_SOURCE 500 /* Needed to enable pthread recursive mutexes */
#endif
@@ -387,6 +392,7 @@
*/
#ifndef SQLITE_TEMP_STORE
# define SQLITE_TEMP_STORE 1
+# define SQLITE_TEMP_STORE_xc 1 /* Exclude from ctime.c */
#endif
/*
@@ -534,6 +540,49 @@ extern const int sqlite3one;
# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0)
#endif
+/*
+** Disable MMAP on platforms where it is known to not work
+*/
+#if defined(__OpenBSD__) || defined(__QNXNTO__)
+# undef SQLITE_MAX_MMAP_SIZE
+# define SQLITE_MAX_MMAP_SIZE 0
+#endif
+
+/*
+** Default maximum size of memory used by memory-mapped I/O in the VFS
+*/
+#ifdef __APPLE__
+# include <TargetConditionals.h>
+# if TARGET_OS_IPHONE
+# undef SQLITE_MAX_MMAP_SIZE
+# define SQLITE_MAX_MMAP_SIZE 0
+# endif
+#endif
+#ifndef SQLITE_MAX_MMAP_SIZE
+# if defined(__linux__) \
+ || defined(_WIN32) \
+ || (defined(__APPLE__) && defined(__MACH__)) \
+ || defined(__sun)
+# define SQLITE_MAX_MMAP_SIZE 0x7fff0000 /* 2147418112 */
+# else
+# define SQLITE_MAX_MMAP_SIZE 0
+# endif
+# define SQLITE_MAX_MMAP_SIZE_xc 1 /* exclude from ctime.c */
+#endif
+
+/*
+** The default MMAP_SIZE is zero on all platforms. Or, even if a larger
+** default MMAP_SIZE is specified at compile-time, make sure that it does
+** not exceed the maximum mmap size.
+*/
+#ifndef SQLITE_DEFAULT_MMAP_SIZE
+# define SQLITE_DEFAULT_MMAP_SIZE 0
+# define SQLITE_DEFAULT_MMAP_SIZE_xc 1 /* Exclude from ctime.c */
+#endif
+#if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
+# undef SQLITE_DEFAULT_MMAP_SIZE
+# define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
+#endif
/*
** An instance of the following structure is used to store the busy-handler
@@ -576,6 +625,11 @@ struct BusyHandler {
#define ArraySize(X) ((int)(sizeof(X)/sizeof(X[0])))
/*
+** Determine if the argument is a power of two
+*/
+#define IsPowerOfTwo(X) (((X)&((X)-1))==0)
+
+/*
** The following value as a destructor means to use sqlite3DbFree().
** The sqlite3DbFree() routine requires two parameters instead of the
** one parameter that destructors normally want. So we have to introduce
@@ -661,6 +715,7 @@ typedef struct Parse Parse;
typedef struct RowSet RowSet;
typedef struct Savepoint Savepoint;
typedef struct Select Select;
+typedef struct SelectDest SelectDest;
typedef struct SrcList SrcList;
typedef struct StrAccum StrAccum;
typedef struct Table Table;
@@ -823,9 +878,11 @@ struct sqlite3 {
int nDb; /* Number of backends currently in use */
int flags; /* Miscellaneous flags. See below */
i64 lastRowid; /* ROWID of most recent insert (see above) */
+ i64 szMmap; /* Default mmap_size setting */
unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
int errCode; /* Most recent error code (SQLITE_*) */
int errMask; /* & result codes with this before returning */
+ u16 dbOptFlags; /* Flags to enable/disable optimizations */
u8 autoCommit; /* The auto-commit flag. */
u8 temp_store; /* 1: file 2: memory 0: default */
u8 mallocFailed; /* True if we have seen a malloc failure */
@@ -930,48 +987,60 @@ struct sqlite3 {
/*
** Possible values for the sqlite3.flags.
*/
-#define SQLITE_VdbeTrace 0x00000100 /* True to trace VDBE execution */
-#define SQLITE_InternChanges 0x00000200 /* Uncommitted Hash table changes */
-#define SQLITE_FullColNames 0x00000400 /* Show full column names on SELECT */
-#define SQLITE_ShortColNames 0x00000800 /* Show short columns names */
-#define SQLITE_CountRows 0x00001000 /* Count rows changed by INSERT, */
+#define SQLITE_VdbeTrace 0x00000001 /* True to trace VDBE execution */
+#define SQLITE_InternChanges 0x00000002 /* Uncommitted Hash table changes */
+#define SQLITE_FullColNames 0x00000004 /* Show full column names on SELECT */
+#define SQLITE_ShortColNames 0x00000008 /* Show short columns names */
+#define SQLITE_CountRows 0x00000010 /* Count rows changed by INSERT, */
/* DELETE, or UPDATE and return */
/* the count using a callback. */
-#define SQLITE_NullCallback 0x00002000 /* Invoke the callback once if the */
+#define SQLITE_NullCallback 0x00000020 /* Invoke the callback once if the */
/* result set is empty */
-#define SQLITE_SqlTrace 0x00004000 /* Debug print SQL as it executes */
-#define SQLITE_VdbeListing 0x00008000 /* Debug listings of VDBE programs */
-#define SQLITE_WriteSchema 0x00010000 /* OK to update SQLITE_MASTER */
- /* 0x00020000 Unused */
-#define SQLITE_IgnoreChecks 0x00040000 /* Do not enforce check constraints */
-#define SQLITE_ReadUncommitted 0x0080000 /* For shared-cache mode */
-#define SQLITE_LegacyFileFmt 0x00100000 /* Create new databases in format 1 */
-#define SQLITE_FullFSync 0x00200000 /* Use full fsync on the backend */
-#define SQLITE_CkptFullFSync 0x00400000 /* Use full fsync for checkpoint */
-#define SQLITE_RecoveryMode 0x00800000 /* Ignore schema errors */
-#define SQLITE_ReverseOrder 0x01000000 /* Reverse unordered SELECTs */
-#define SQLITE_RecTriggers 0x02000000 /* Enable recursive triggers */
-#define SQLITE_ForeignKeys 0x04000000 /* Enforce foreign key constraints */
-#define SQLITE_AutoIndex 0x08000000 /* Enable automatic indexes */
-#define SQLITE_PreferBuiltin 0x10000000 /* Preference to built-in funcs */
-#define SQLITE_LoadExtension 0x20000000 /* Enable load_extension */
-#define SQLITE_EnableTrigger 0x40000000 /* True to enable triggers */
-
-/*
-** Bits of the sqlite3.flags field that are used by the
-** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface.
-** These must be the low-order bits of the flags field.
-*/
-#define SQLITE_QueryFlattener 0x01 /* Disable query flattening */
-#define SQLITE_ColumnCache 0x02 /* Disable the column cache */
-#define SQLITE_IndexSort 0x04 /* Disable indexes for sorting */
-#define SQLITE_IndexSearch 0x08 /* Disable indexes for searching */
-#define SQLITE_IndexCover 0x10 /* Disable index covering table */
-#define SQLITE_GroupByOrder 0x20 /* Disable GROUPBY cover of ORDERBY */
-#define SQLITE_FactorOutConst 0x40 /* Disable factoring out constants */
-#define SQLITE_IdxRealAsInt 0x80 /* Store REAL as INT in indices */
-#define SQLITE_DistinctOpt 0x80 /* DISTINCT using indexes */
-#define SQLITE_OptMask 0xff /* Mask of all disablable opts */
+#define SQLITE_SqlTrace 0x00000040 /* Debug print SQL as it executes */
+#define SQLITE_VdbeListing 0x00000080 /* Debug listings of VDBE programs */
+#define SQLITE_WriteSchema 0x00000100 /* OK to update SQLITE_MASTER */
+#define SQLITE_VdbeAddopTrace 0x00000200 /* Trace sqlite3VdbeAddOp() calls */
+#define SQLITE_IgnoreChecks 0x00000400 /* Do not enforce check constraints */
+#define SQLITE_ReadUncommitted 0x0000800 /* For shared-cache mode */
+#define SQLITE_LegacyFileFmt 0x00001000 /* Create new databases in format 1 */
+#define SQLITE_FullFSync 0x00002000 /* Use full fsync on the backend */
+#define SQLITE_CkptFullFSync 0x00004000 /* Use full fsync for checkpoint */
+#define SQLITE_RecoveryMode 0x00008000 /* Ignore schema errors */
+#define SQLITE_ReverseOrder 0x00010000 /* Reverse unordered SELECTs */
+#define SQLITE_RecTriggers 0x00020000 /* Enable recursive triggers */
+#define SQLITE_ForeignKeys 0x00040000 /* Enforce foreign key constraints */
+#define SQLITE_AutoIndex 0x00080000 /* Enable automatic indexes */
+#define SQLITE_PreferBuiltin 0x00100000 /* Preference to built-in funcs */
+#define SQLITE_LoadExtension 0x00200000 /* Enable load_extension */
+#define SQLITE_EnableTrigger 0x00400000 /* True to enable triggers */
+
+/*
+** Bits of the sqlite3.dbOptFlags field that are used by the
+** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
+** selectively disable various optimizations.
+*/
+#define SQLITE_QueryFlattener 0x0001 /* Query flattening */
+#define SQLITE_ColumnCache 0x0002 /* Column cache */
+#define SQLITE_GroupByOrder 0x0004 /* GROUPBY cover of ORDERBY */
+#define SQLITE_FactorOutConst 0x0008 /* Constant factoring */
+#define SQLITE_IdxRealAsInt 0x0010 /* Store REAL as INT in indices */
+#define SQLITE_DistinctOpt 0x0020 /* DISTINCT using indexes */
+#define SQLITE_CoverIdxScan 0x0040 /* Covering index scans */
+#define SQLITE_OrderByIdxJoin 0x0080 /* ORDER BY of joins via index */
+#define SQLITE_SubqCoroutine 0x0100 /* Evaluate subqueries as coroutines */
+#define SQLITE_Transitive 0x0200 /* Transitive constraints */
+#define SQLITE_AllOpts 0xffff /* All optimizations */
+
+/*
+** Macros for testing whether or not optimizations are enabled or disabled.
+*/
+#ifndef SQLITE_OMIT_BUILTIN_TEST
+#define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
+#define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
+#else
+#define OptimizationDisabled(db, mask) 0
+#define OptimizationEnabled(db, mask) 1
+#endif
/*
** Possible values for the sqlite.magic field.
@@ -1122,32 +1191,22 @@ struct Column {
char *zDflt; /* Original text of the default value */
char *zType; /* Data type for this column */
char *zColl; /* Collating sequence. If NULL, use the default */
- u8 notNull; /* True if there is a NOT NULL constraint */
- u8 isPrimKey; /* True if this column is part of the PRIMARY KEY */
+ u8 notNull; /* An OE_ code for handling a NOT NULL constraint */
char affinity; /* One of the SQLITE_AFF_... values */
-#ifndef SQLITE_OMIT_VIRTUALTABLE
- u8 isHidden; /* True if this column is 'hidden' */
-#endif
+ u16 colFlags; /* Boolean properties. See COLFLAG_ defines below */
};
+/* Allowed values for Column.colFlags:
+*/
+#define COLFLAG_PRIMKEY 0x0001 /* Column is part of the primary key */
+#define COLFLAG_HIDDEN 0x0002 /* A hidden column in a virtual table */
+
/*
** A "Collating Sequence" is defined by an instance of the following
** structure. Conceptually, a collating sequence consists of a name and
** a comparison routine that defines the order of that sequence.
**
-** There may two separate implementations of the collation function, one
-** that processes text in UTF-8 encoding (CollSeq.xCmp) and another that
-** processes text encoded in UTF-16 (CollSeq.xCmp16), using the machine
-** native byte order. When a collation sequence is invoked, SQLite selects
-** the version that will require the least expensive encoding
-** translations, if any.
-**
-** The CollSeq.pUser member variable is an extra parameter that passed in
-** as the first argument to the UTF-8 comparison function, xCmp.
-** CollSeq.pUser16 is the equivalent for the UTF-16 comparison function,
-** xCmp16.
-**
-** If both CollSeq.xCmp and CollSeq.xCmp16 are NULL, it means that the
+** If CollSeq.xCmp is NULL, it means that the
** collating sequence is undefined. Indices built on an undefined
** collating sequence may not be read or written.
*/
@@ -1285,28 +1344,28 @@ struct VTable {
*/
struct Table {
char *zName; /* Name of the table or view */
- int iPKey; /* If not negative, use aCol[iPKey] as the primary key */
- int nCol; /* Number of columns in this table */
Column *aCol; /* Information about each column */
Index *pIndex; /* List of SQL indexes on this table. */
- int tnum; /* Root BTree node for this table (see note above) */
- tRowcnt nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
Select *pSelect; /* NULL for tables. Points to definition if a view. */
- u16 nRef; /* Number of pointers to this Table */
- u8 tabFlags; /* Mask of TF_* values */
- u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
FKey *pFKey; /* Linked list of all foreign keys in this table */
char *zColAff; /* String defining the affinity of each column */
#ifndef SQLITE_OMIT_CHECK
ExprList *pCheck; /* All CHECK constraints */
#endif
+ tRowcnt nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
+ int tnum; /* Root BTree node for this table (see note above) */
+ i16 iPKey; /* If not negative, use aCol[iPKey] as the primary key */
+ i16 nCol; /* Number of columns in this table */
+ u16 nRef; /* Number of pointers to this Table */
+ u8 tabFlags; /* Mask of TF_* values */
+ u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
#ifndef SQLITE_OMIT_ALTERTABLE
int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
#endif
#ifndef SQLITE_OMIT_VIRTUALTABLE
- VTable *pVTable; /* List of VTable objects. */
int nModuleArg; /* Number of arguments to the module */
char **azModuleArg; /* Text of all module args. [0] is module name */
+ VTable *pVTable; /* List of VTable objects. */
#endif
Trigger *pTrigger; /* List of triggers stored in pSchema */
Schema *pSchema; /* Schema that contains this table */
@@ -1330,7 +1389,7 @@ struct Table {
*/
#ifndef SQLITE_OMIT_VIRTUALTABLE
# define IsVirtual(X) (((X)->tabFlags & TF_Virtual)!=0)
-# define IsHiddenColumn(X) ((X)->isHidden)
+# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
#else
# define IsVirtual(X) 0
# define IsHiddenColumn(X) 0
@@ -1481,20 +1540,20 @@ struct UnpackedRecord {
** element.
*/
struct Index {
- char *zName; /* Name of this index */
- int *aiColumn; /* Which columns are used by this index. 1st is 0 */
- tRowcnt *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
- Table *pTable; /* The SQL table being indexed */
- char *zColAff; /* String defining the affinity of each column */
- Index *pNext; /* The next index associated with the same table */
- Schema *pSchema; /* Schema containing this index */
- u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */
- char **azColl; /* Array of collation sequence names for index */
- int nColumn; /* Number of columns in the table used by this index */
- int tnum; /* Page containing root of this index in database file */
- u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
- u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */
- u8 bUnordered; /* Use this index for == or IN queries only */
+ char *zName; /* Name of this index */
+ int *aiColumn; /* Which columns are used by this index. 1st is 0 */
+ tRowcnt *aiRowEst; /* From ANALYZE: Est. rows selected by each column */
+ Table *pTable; /* The SQL table being indexed */
+ char *zColAff; /* String defining the affinity of each column */
+ Index *pNext; /* The next index associated with the same table */
+ Schema *pSchema; /* Schema containing this index */
+ u8 *aSortOrder; /* for each column: True==DESC, False==ASC */
+ char **azColl; /* Array of collation sequence names for index */
+ int tnum; /* DB Page containing root of this index */
+ u16 nColumn; /* Number of columns in table used by this index */
+ u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
+ unsigned autoIndex:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
+ unsigned bUnordered:1; /* Use this index for == or IN queries only */
#ifdef SQLITE_ENABLE_STAT3
int nSample; /* Number of elements in aSample[] */
tRowcnt avgEq; /* Average nEq value for key values not in aSample */
@@ -1675,13 +1734,15 @@ struct Expr {
ExprList *pList; /* Function arguments or in "<expr> IN (<expr-list)" */
Select *pSelect; /* Used for sub-selects and "<expr> IN (<select>)" */
} x;
- CollSeq *pColl; /* The collation type of the column or 0 */
/* If the EP_Reduced flag is set in the Expr.flags mask, then no
** space is allocated for the fields below this point. An attempt to
** access them will result in a segfault or malfunction.
*********************************************************************/
+#if SQLITE_MAX_EXPR_DEPTH>0
+ int nHeight; /* Height of the tree headed by this node */
+#endif
int iTable; /* TK_COLUMN: cursor number of table holding column
** TK_REGISTER: register number
** TK_TRIGGER: 1 -> new, 0 -> old */
@@ -1695,9 +1756,6 @@ struct Expr {
** TK_AGG_FUNCTION: nesting depth */
AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
Table *pTab; /* Table for TK_COLUMN expressions. */
-#if SQLITE_MAX_EXPR_DEPTH>0
- int nHeight; /* Height of the tree headed by this node */
-#endif
};
/*
@@ -1711,7 +1769,7 @@ struct Expr {
#define EP_VarSelect 0x0020 /* pSelect is correlated, not constant */
#define EP_DblQuoted 0x0040 /* token.z was originally in "..." */
#define EP_InfixFunc 0x0080 /* True for an infix function: LIKE, GLOB, etc */
-#define EP_ExpCollate 0x0100 /* Collating sequence specified explicitly */
+#define EP_Collate 0x0100 /* Tree contains a TK_COLLATE opeartor */
#define EP_FixedDest 0x0200 /* Result needed in a specific register */
#define EP_IntValue 0x0400 /* Integer value contained in u.iValue */
#define EP_xIsSelect 0x0800 /* x.pSelect is valid (otherwise x.pList is) */
@@ -1769,18 +1827,27 @@ struct Expr {
** list of "ID = expr" items in an UPDATE. A list of expressions can
** also be used as the argument to a function, in which case the a.zName
** field is not used.
+**
+** By default the Expr.zSpan field holds a human-readable description of
+** the expression that is used in the generation of error messages and
+** column labels. In this case, Expr.zSpan is typically the text of a
+** column expression as it exists in a SELECT statement. However, if
+** the bSpanIsTab flag is set, then zSpan is overloaded to mean the name
+** of the result column in the form: DATABASE.TABLE.COLUMN. This later
+** form is used for name resolution with nested FROM clauses.
*/
struct ExprList {
int nExpr; /* Number of expressions on the list */
int iECursor; /* VDBE Cursor associated with this ExprList */
struct ExprList_item { /* For each expression in the list */
- Expr *pExpr; /* The list of expressions */
- char *zName; /* Token associated with this expression */
- char *zSpan; /* Original text of the expression */
- u8 sortOrder; /* 1 for DESC or 0 for ASC */
- u8 done; /* A flag to indicate when processing is finished */
- u16 iOrderByCol; /* For ORDER BY, column number in result set */
- u16 iAlias; /* Index into Parse.aAlias[] for zName */
+ Expr *pExpr; /* The list of expressions */
+ char *zName; /* Token associated with this expression */
+ char *zSpan; /* Original text of the expression */
+ u8 sortOrder; /* 1 for DESC or 0 for ASC */
+ unsigned done :1; /* A flag to indicate when processing is finished */
+ unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
+ u16 iOrderByCol; /* For ORDER BY, column number in result set */
+ u16 iAlias; /* Index into Parse.aAlias[] for zName */
} *a; /* Alloc a power of two greater or equal to nExpr */
};
@@ -1855,6 +1922,7 @@ struct SrcList {
i16 nSrc; /* Number of tables or subqueries in the FROM clause */
i16 nAlloc; /* Number of entries allocated in a[] below */
struct SrcList_item {
+ Schema *pSchema; /* Schema to which this item is fixed */
char *zDatabase; /* Name of database holding this table */
char *zName; /* Name of the table */
char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
@@ -1863,8 +1931,9 @@ struct SrcList {
int addrFillSub; /* Address of subroutine to manifest a subquery */
int regReturn; /* Register holding return address of addrFillSub */
u8 jointype; /* Type of join between this able and the previous */
- u8 notIndexed; /* True if there is a NOT INDEXED clause */
- u8 isCorrelated; /* True if sub-query is correlated */
+ unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */
+ unsigned isCorrelated :1; /* True if sub-query is correlated */
+ unsigned viaCoroutine :1; /* Implemented as a co-routine */
#ifndef SQLITE_OMIT_EXPLAIN
u8 iSelectId; /* If pSelect!=0, the id of the sub-select in EQP */
#endif
@@ -1905,7 +1974,8 @@ struct SrcList {
*/
struct WherePlan {
u32 wsFlags; /* WHERE_* flags that describe the strategy */
- u32 nEq; /* Number of == constraints */
+ u16 nEq; /* Number of == constraints */
+ u16 nOBSat; /* Number of ORDER BY terms satisfied */
double nRow; /* Estimated number of rows (for EQP) */
union {
Index *pIdx; /* Index when WHERE_INDEXED is true */
@@ -1945,10 +2015,12 @@ struct WhereLevel {
struct InLoop {
int iCur; /* The VDBE cursor used by this IN operator */
int addrInTop; /* Top of the IN loop */
+ u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */
} *aInLoop; /* Information about each nested IN operator */
} in; /* Used when plan.wsFlags&WHERE_IN_ABLE */
Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */
} u;
+ double rOptCost; /* "Optimal" cost for this level */
/* The following field is really not part of the current level. But
** we need a place to cache virtual table index information for each
@@ -1981,24 +2053,28 @@ struct WhereLevel {
** into the second half to give some continuity.
*/
struct WhereInfo {
- Parse *pParse; /* Parsing and code generating context */
- u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
- u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE or DELETE */
- u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */
- u8 eDistinct;
- SrcList *pTabList; /* List of tables in the join */
- int iTop; /* The very beginning of the WHERE loop */
- int iContinue; /* Jump here to continue with next record */
- int iBreak; /* Jump here to break out of the loop */
- int nLevel; /* Number of nested loop */
- struct WhereClause *pWC; /* Decomposition of the WHERE clause */
- double savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
- double nRowOut; /* Estimated number of output rows */
- WhereLevel a[1]; /* Information about each nest loop in WHERE */
+ Parse *pParse; /* Parsing and code generating context */
+ SrcList *pTabList; /* List of tables in the join */
+ u16 nOBSat; /* Number of ORDER BY terms satisfied by indices */
+ u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
+ u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE/DELETE */
+ u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */
+ u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */
+ int iTop; /* The very beginning of the WHERE loop */
+ int iContinue; /* Jump here to continue with next record */
+ int iBreak; /* Jump here to break out of the loop */
+ int nLevel; /* Number of nested loop */
+ struct WhereClause *pWC; /* Decomposition of the WHERE clause */
+ double savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
+ double nRowOut; /* Estimated number of output rows */
+ WhereLevel a[1]; /* Information about each nest loop in WHERE */
};
-#define WHERE_DISTINCT_UNIQUE 1
-#define WHERE_DISTINCT_ORDERED 2
+/* Allowed values for WhereInfo.eDistinct and DistinctCtx.eTnctType */
+#define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */
+#define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */
+#define WHERE_DISTINCT_ORDERED 2 /* All duplicates are adjacent */
+#define WHERE_DISTINCT_UNORDERED 3 /* Duplicates are scattered */
/*
** A NameContext defines a context in which to resolve table and column
@@ -2039,6 +2115,8 @@ struct NameContext {
#define NC_HasAgg 0x02 /* One or more aggregate functions seen */
#define NC_IsCheck 0x04 /* True if resolving names in a CHECK constraint */
#define NC_InAggFunc 0x08 /* True if analyzing arguments to an agg func */
+#define NC_AsMaybe 0x10 /* Resolve to AS terms of the result set only
+ ** if no other resolution is available */
/*
** An instance of the following structure contains all information
@@ -2057,13 +2135,12 @@ struct NameContext {
** as the OP_OpenEphm instruction is coded because not
** enough information about the compound query is known at that point.
** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
-** for the result set. The KeyInfo for addrOpenTran[2] contains collating
+** for the result set. The KeyInfo for addrOpenEphm[2] contains collating
** sequences for the ORDER BY clause.
*/
struct Select {
ExprList *pEList; /* The fields of the result */
u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
- char affinity; /* MakeRecord with this affinity for SRT_Set */
u16 selFlags; /* Various SF_* values */
int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
int addrOpenEphm[3]; /* OP_OpenEphem opcodes related to this select */
@@ -2084,14 +2161,16 @@ struct Select {
** Allowed values for Select.selFlags. The "SF" prefix stands for
** "Select Flag".
*/
-#define SF_Distinct 0x01 /* Output should be DISTINCT */
-#define SF_Resolved 0x02 /* Identifiers have been resolved */
-#define SF_Aggregate 0x04 /* Contains aggregate functions */
-#define SF_UsesEphemeral 0x08 /* Uses the OpenEphemeral opcode */
-#define SF_Expanded 0x10 /* sqlite3SelectExpand() called on this */
-#define SF_HasTypeInfo 0x20 /* FROM subqueries have Table metadata */
-#define SF_UseSorter 0x40 /* Sort using a sorter */
-#define SF_Values 0x80 /* Synthesized from VALUES clause */
+#define SF_Distinct 0x0001 /* Output should be DISTINCT */
+#define SF_Resolved 0x0002 /* Identifiers have been resolved */
+#define SF_Aggregate 0x0004 /* Contains aggregate functions */
+#define SF_UsesEphemeral 0x0008 /* Uses the OpenEphemeral opcode */
+#define SF_Expanded 0x0010 /* sqlite3SelectExpand() called on this */
+#define SF_HasTypeInfo 0x0020 /* FROM subqueries have Table metadata */
+#define SF_UseSorter 0x0040 /* Sort using a sorter */
+#define SF_Values 0x0080 /* Synthesized from VALUES clause */
+#define SF_Materialize 0x0100 /* Force materialization of views */
+#define SF_NestedFrom 0x0200 /* Part of a parenthesized FROM clause */
/*
@@ -2114,13 +2193,12 @@ struct Select {
#define SRT_Coroutine 10 /* Generate a single row of result */
/*
-** A structure used to customize the behavior of sqlite3Select(). See
-** comments above sqlite3Select() for details.
+** An instance of this object describes where to put of the results of
+** a SELECT statement.
*/
-typedef struct SelectDest SelectDest;
struct SelectDest {
- u8 eDest; /* How to dispose of the results */
- u8 affSdst; /* Affinity used when eDest==SRT_Set */
+ u8 eDest; /* How to dispose of the results. On of SRT_* above. */
+ char affSdst; /* Affinity used when eDest==SRT_Set */
int iSDParm; /* A parameter used by the eDest disposal method */
int iSdst; /* Base register where results are written */
int nSdst; /* Number of registers allocated */
@@ -2321,6 +2399,7 @@ struct AuthContext {
#define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */
#define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */
#define OPFLAG_P2ISREG 0x02 /* P2 to OP_Open** is a register number */
+#define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */
/*
* Each trigger present in the database schema is stored as an instance of
@@ -2420,6 +2499,7 @@ struct TriggerStep {
typedef struct DbFixer DbFixer;
struct DbFixer {
Parse *pParse; /* The parsing context. Error messages written here */
+ Schema *pSchema; /* Fix items to this schema */
const char *zDb; /* Make sure all objects are contained in this database */
const char *zType; /* Type of the container - used for error messages */
const Token *pName; /* Name of the container - used for error messages */
@@ -2462,6 +2542,7 @@ struct Sqlite3Config {
int bCoreMutex; /* True to enable core mutexing */
int bFullMutex; /* True to enable full mutexing */
int bOpenUri; /* True to interpret filenames as URIs */
+ int bUseCis; /* Use covering indices for full-scans */
int mxStrlen; /* Maximum string length */
int szLookaside; /* Default lookaside buffer size */
int nLookaside; /* Default lookaside buffer count */
@@ -2471,6 +2552,8 @@ struct Sqlite3Config {
void *pHeap; /* Heap storage space */
int nHeap; /* Size of pHeap[] */
int mnReq, mxReq; /* Min and max heap requests sizes */
+ sqlite3_int64 szMmap; /* mmap() space per open file */
+ sqlite3_int64 mxMmap; /* Maximum value for szMmap */
void *pScratch; /* Scratch memory */
int szScratch; /* Size of each scratch buffer */
int nScratch; /* Number of scratch buffers */
@@ -2491,6 +2574,10 @@ struct Sqlite3Config {
void (*xLog)(void*,int,const char*); /* Function for logging */
void *pLogArg; /* First argument to xLog() */
int bLocaltimeFault; /* True to fail localtime() calls */
+#ifdef SQLITE_ENABLE_SQLLOG
+ void(*xSqllog)(void*,sqlite3*,const char*, int);
+ void *pSqllogArg;
+#endif
};
/*
@@ -2501,6 +2588,7 @@ struct Walker {
int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
Parse *pParse; /* Parser context. */
int walkerDepth; /* Number of subqueries */
+ u8 bSelectDepthFirst; /* Do subqueries first */
union { /* Extra data for callback */
NameContext *pNC; /* Naming context */
int i; /* Integer value */
@@ -2778,6 +2866,7 @@ void sqlite3DeleteTable(sqlite3*, Table*);
# define sqlite3AutoincrementBegin(X)
# define sqlite3AutoincrementEnd(X)
#endif
+int sqlite3CodeCoroutine(Parse*, Select*, SelectDest*);
void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
@@ -2797,23 +2886,21 @@ Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
void sqlite3DropIndex(Parse*, SrcList*, int);
int sqlite3Select(Parse*, Select*, SelectDest*);
Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
- Expr*,ExprList*,int,Expr*,Expr*);
+ Expr*,ExprList*,u16,Expr*,Expr*);
void sqlite3SelectDelete(sqlite3*, Select*);
Table *sqlite3SrcListLookup(Parse*, SrcList*);
int sqlite3IsReadOnly(Parse*, Table*, int);
void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
-Expr *sqlite3LimitWhere(Parse *, SrcList *, Expr *, ExprList *, Expr *, Expr *, char *);
+Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,Expr*,char*);
#endif
void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
-WhereInfo *sqlite3WhereBegin(
- Parse*,SrcList*,Expr*,ExprList**,ExprList*,u16,int);
+WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
void sqlite3WhereEnd(WhereInfo*);
int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
void sqlite3ExprCodeMove(Parse*, int, int, int);
-void sqlite3ExprCodeCopy(Parse*, int, int, int);
void sqlite3ExprCacheStore(Parse*, int, int, int);
void sqlite3ExprCachePush(Parse*);
void sqlite3ExprCachePop(Parse*, int);
@@ -2830,6 +2917,7 @@ void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
Table *sqlite3FindTable(sqlite3*,const char*, const char*);
Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
+Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *);
Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
@@ -2872,7 +2960,7 @@ int sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
void sqlite3BeginWriteOperation(Parse*, int, int);
void sqlite3MultiWrite(Parse*);
void sqlite3MayAbort(Parse*);
-void sqlite3HaltConstraint(Parse*, int, char*, int);
+void sqlite3HaltConstraint(Parse*, int, int, char*, int);
Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
@@ -2953,7 +3041,7 @@ int sqlite3GetInt32(const char *, int*);
int sqlite3Atoi(const char*);
int sqlite3Utf16ByteLen(const void *pData, int nChar);
int sqlite3Utf8CharLen(const char *pData, int nByte);
-u32 sqlite3Utf8Read(const u8*, const u8**);
+u32 sqlite3Utf8Read(const u8**);
/*
** Routines to read and write variable-length integers. These used to
@@ -2985,8 +3073,11 @@ int sqlite3VarintLen(u64 v);
** x = putVarint32( A, B );
**
*/
-#define getVarint32(A,B) (u8)((*(A)<(u8)0x80) ? ((B) = (u32)*(A)),1 : sqlite3GetVarint32((A), (u32 *)&(B)))
-#define putVarint32(A,B) (u8)(((u32)(B)<(u32)0x80) ? (*(A) = (unsigned char)(B)),1 : sqlite3PutVarint32((A), (B)))
+#define getVarint32(A,B) \
+ (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
+#define putVarint32(A,B) \
+ (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
+ sqlite3PutVarint32((A),(B)))
#define getVarint sqlite3GetVarint
#define putVarint sqlite3PutVarint
@@ -3001,13 +3092,20 @@ void sqlite3Error(sqlite3*, int, const char*,...);
void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
u8 sqlite3HexToInt(int h);
int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
+
+#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) || \
+ defined(SQLITE_DEBUG_OS_TRACE)
+const char *sqlite3ErrName(int);
+#endif
+
const char *sqlite3ErrStr(int);
int sqlite3ReadSchema(Parse *pParse);
CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
-Expr *sqlite3ExprSetColl(Expr*, CollSeq*);
-Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr*, Token*);
+Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, Token*);
+Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
+Expr *sqlite3ExprSkipCollate(Expr*);
int sqlite3CheckCollSeq(Parse *, CollSeq *);
int sqlite3CheckObjectName(Parse *, const char *);
void sqlite3VdbeSetChanges(sqlite3 *, int);
@@ -3054,13 +3152,14 @@ void sqlite3NestedParse(Parse*, const char*, ...);
void sqlite3ExpirePreparedStatements(sqlite3*);
int sqlite3CodeSubselect(Parse *, Expr *, int, int);
void sqlite3SelectPrep(Parse*, Select*, NameContext*);
+int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
int sqlite3ResolveExprNames(NameContext*, Expr*);
void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
void sqlite3AlterFinishAddColumn(Parse *, Token *);
void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
-CollSeq *sqlite3GetCollSeq(sqlite3*, u8, CollSeq *, const char*);
+CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
char sqlite3AffinityType(const char*);
void sqlite3Analyze(Parse*, Token*, Token*);
int sqlite3InvokeBusyHandler(BusyHandler*);
@@ -3164,8 +3263,10 @@ void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
int sqlite3TempInMemory(const sqlite3*);
const char *sqlite3JournalModename(int);
-int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
-int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
+#ifndef SQLITE_OMIT_WAL
+ int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
+ int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
+#endif
/* Declarations for functions in fkey.c. All of these are replaced by
** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
@@ -3190,8 +3291,10 @@ int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
#endif
#ifndef SQLITE_OMIT_FOREIGN_KEY
void sqlite3FkDelete(sqlite3 *, Table*);
+ int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
#else
#define sqlite3FkDelete(a,b)
+ #define sqlite3FkLocateIndex(a,b,c,d,e)
#endif
@@ -3216,15 +3319,18 @@ int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
#define IN_INDEX_ROWID 1
#define IN_INDEX_EPH 2
-#define IN_INDEX_INDEX 3
+#define IN_INDEX_INDEX_ASC 3
+#define IN_INDEX_INDEX_DESC 4
int sqlite3FindInIndex(Parse *, Expr *, int*);
#ifdef SQLITE_ENABLE_ATOMIC_WRITE
int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
int sqlite3JournalSize(sqlite3_vfs *);
int sqlite3JournalCreate(sqlite3_file *);
+ int sqlite3JournalExists(sqlite3_file *p);
#else
#define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile)
+ #define sqlite3JournalExists(p) 1
#endif
void sqlite3MemJournalOpen(sqlite3_file *);
diff --git a/src/status.c b/src/status.c
index 04b7656..28349e6 100644
--- a/src/status.c
+++ b/src/status.c
@@ -208,7 +208,8 @@ int sqlite3_db_status(
db->pnBytesFreed = &nByte;
for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
- sqlite3VdbeDeleteObject(db, pVdbe);
+ sqlite3VdbeClearObject(db, pVdbe);
+ sqlite3DbFree(db, pVdbe);
}
db->pnBytesFreed = 0;
diff --git a/src/tclsqlite.c b/src/tclsqlite.c
index a2072f8..f1bb292 100644
--- a/src/tclsqlite.c
+++ b/src/tclsqlite.c
@@ -1005,7 +1005,7 @@ static int DbTransPostCmd(
/* This is a tricky scenario to handle. The most likely cause of an
** error is that the exec() above was an attempt to commit the
** top-level transaction that returned SQLITE_BUSY. Or, less likely,
- ** that an IO-error has occured. In either case, throw a Tcl exception
+ ** that an IO-error has occurred. In either case, throw a Tcl exception
** and try to rollback the transaction.
**
** But it could also be that the user executed one or more BEGIN,
@@ -1489,7 +1489,7 @@ static Tcl_Obj *dbEvalColumnValue(DbEvalContext *p, int iCol){
}
}
- return Tcl_NewStringObj(sqlite3_column_text(pStmt, iCol), -1);
+ return Tcl_NewStringObj((char*)sqlite3_column_text(pStmt, iCol), -1);
}
/*
@@ -2343,7 +2343,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
const char *zDb = "main";
const char *zTable;
const char *zColumn;
- sqlite_int64 iRow;
+ Tcl_WideInt iRow;
/* Check for the -readonly option */
if( objc>3 && strcmp(Tcl_GetString(objv[2]), "-readonly")==0 ){
@@ -2534,7 +2534,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
rc = sqlite3_rekey(pDb->db, pKey, nKey);
if( rc ){
- Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
+ Tcl_AppendResult(interp, sqlite3_errstr(rc), 0);
rc = TCL_ERROR;
}
#endif
@@ -2905,6 +2905,7 @@ static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
void *pKey = 0;
int nKey = 0;
#endif
+ int rc;
/* In normal use, each TCL interpreter runs in a single thread. So
** by default, we can turn of mutexing on SQLite database connections.
@@ -3009,12 +3010,16 @@ static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
memset(p, 0, sizeof(*p));
zFile = Tcl_GetStringFromObj(objv[2], 0);
zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename);
- sqlite3_open_v2(zFile, &p->db, flags, zVfs);
+ rc = sqlite3_open_v2(zFile, &p->db, flags, zVfs);
Tcl_DStringFree(&translatedFilename);
- if( SQLITE_OK!=sqlite3_errcode(p->db) ){
- zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db));
- sqlite3_close(p->db);
- p->db = 0;
+ if( p->db ){
+ if( SQLITE_OK!=sqlite3_errcode(p->db) ){
+ zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db));
+ sqlite3_close(p->db);
+ p->db = 0;
+ }
+ }else{
+ zErrMsg = sqlite3_mprintf("%s", sqlite3_errstr(rc));
}
#ifdef SQLITE_HAS_CODEC
if( p->db ){
@@ -3666,6 +3671,7 @@ static void init_all(Tcl_Interp *interp){
extern int Sqlitetestschema_Init(Tcl_Interp*);
extern int Sqlitetestsse_Init(Tcl_Interp*);
extern int Sqlitetesttclvar_Init(Tcl_Interp*);
+ extern int Sqlitetestfs_Init(Tcl_Interp*);
extern int SqlitetestThread_Init(Tcl_Interp*);
extern int SqlitetestOnefile_Init();
extern int SqlitetestOsinst_Init(Tcl_Interp*);
@@ -3677,8 +3683,6 @@ static void init_all(Tcl_Interp *interp){
extern int Sqlitemultiplex_Init(Tcl_Interp*);
extern int SqliteSuperlock_Init(Tcl_Interp*);
extern int SqlitetestSyscall_Init(Tcl_Interp*);
- extern int Sqlitetestfuzzer_Init(Tcl_Interp*);
- extern int Sqlitetestwholenumber_Init(Tcl_Interp*);
#if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
extern int Sqlitetestfts3_Init(Tcl_Interp *interp);
@@ -3709,6 +3713,7 @@ static void init_all(Tcl_Interp *interp){
Sqlitetest_mutex_Init(interp);
Sqlitetestschema_Init(interp);
Sqlitetesttclvar_Init(interp);
+ Sqlitetestfs_Init(interp);
SqlitetestThread_Init(interp);
SqlitetestOnefile_Init(interp);
SqlitetestOsinst_Init(interp);
@@ -3720,8 +3725,6 @@ static void init_all(Tcl_Interp *interp){
Sqlitemultiplex_Init(interp);
SqliteSuperlock_Init(interp);
SqlitetestSyscall_Init(interp);
- Sqlitetestfuzzer_Init(interp);
- Sqlitetestwholenumber_Init(interp);
#if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
Sqlitetestfts3_Init(interp);
diff --git a/src/test1.c b/src/test1.c
index c3dac06..a638e48 100644
--- a/src/test1.c
+++ b/src/test1.c
@@ -113,64 +113,8 @@ int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
return TCL_OK;
}
-
-const char *sqlite3TestErrorName(int rc){
- const char *zName = 0;
- switch( rc ){
- case SQLITE_OK: zName = "SQLITE_OK"; break;
- case SQLITE_ERROR: zName = "SQLITE_ERROR"; break;
- case SQLITE_INTERNAL: zName = "SQLITE_INTERNAL"; break;
- case SQLITE_PERM: zName = "SQLITE_PERM"; break;
- case SQLITE_ABORT: zName = "SQLITE_ABORT"; break;
- case SQLITE_BUSY: zName = "SQLITE_BUSY"; break;
- case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break;
- case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break;
- case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break;
- case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
- case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
- case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
- case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
- case SQLITE_NOTFOUND: zName = "SQLITE_NOTFOUND"; break;
- case SQLITE_FULL: zName = "SQLITE_FULL"; break;
- case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
- case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
- case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
- case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break;
- case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break;
- case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
- case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break;
- case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break;
- case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break;
- case SQLITE_AUTH: zName = "SQLITE_AUTH"; break;
- case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break;
- case SQLITE_RANGE: zName = "SQLITE_RANGE"; break;
- case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break;
- case SQLITE_ROW: zName = "SQLITE_ROW"; break;
- case SQLITE_DONE: zName = "SQLITE_DONE"; break;
- case SQLITE_IOERR_READ: zName = "SQLITE_IOERR_READ"; break;
- case SQLITE_IOERR_SHORT_READ: zName = "SQLITE_IOERR_SHORT_READ"; break;
- case SQLITE_IOERR_WRITE: zName = "SQLITE_IOERR_WRITE"; break;
- case SQLITE_IOERR_FSYNC: zName = "SQLITE_IOERR_FSYNC"; break;
- case SQLITE_IOERR_DIR_FSYNC: zName = "SQLITE_IOERR_DIR_FSYNC"; break;
- case SQLITE_IOERR_TRUNCATE: zName = "SQLITE_IOERR_TRUNCATE"; break;
- case SQLITE_IOERR_FSTAT: zName = "SQLITE_IOERR_FSTAT"; break;
- case SQLITE_IOERR_UNLOCK: zName = "SQLITE_IOERR_UNLOCK"; break;
- case SQLITE_IOERR_RDLOCK: zName = "SQLITE_IOERR_RDLOCK"; break;
- case SQLITE_IOERR_DELETE: zName = "SQLITE_IOERR_DELETE"; break;
- case SQLITE_IOERR_BLOCKED: zName = "SQLITE_IOERR_BLOCKED"; break;
- case SQLITE_IOERR_NOMEM: zName = "SQLITE_IOERR_NOMEM"; break;
- case SQLITE_IOERR_ACCESS: zName = "SQLITE_IOERR_ACCESS"; break;
- case SQLITE_IOERR_CHECKRESERVEDLOCK:
- zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
- case SQLITE_IOERR_LOCK: zName = "SQLITE_IOERR_LOCK"; break;
- case SQLITE_CORRUPT_VTAB: zName = "SQLITE_CORRUPT_VTAB"; break;
- case SQLITE_READONLY_RECOVERY: zName = "SQLITE_READONLY_RECOVERY"; break;
- case SQLITE_READONLY_CANTLOCK: zName = "SQLITE_READONLY_CANTLOCK"; break;
- default: zName = "SQLITE_Unknown"; break;
- }
- return zName;
-}
-#define t1ErrorName sqlite3TestErrorName
+extern const char *sqlite3ErrName(int);
+#define t1ErrorName sqlite3ErrName
/*
** Convert an sqlite3_stmt* into an sqlite3*. This depends on the
@@ -738,6 +682,30 @@ static int sqlite_test_close(
}
/*
+** Usage: sqlite3_close_v2 DB
+**
+** Closes the database opened by sqlite3_open.
+*/
+static int sqlite_test_close_v2(
+ void *NotUsed,
+ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
+ int argc, /* Number of arguments */
+ char **argv /* Text of each argument */
+){
+ sqlite3 *db;
+ int rc;
+ if( argc!=2 ){
+ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
+ " FILENAME\"", 0);
+ return TCL_ERROR;
+ }
+ if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
+ rc = sqlite3_close_v2(db);
+ Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
+ return TCL_OK;
+}
+
+/*
** Implementation of the x_coalesce() function.
** Return the first argument non-NULL argument.
*/
@@ -1715,7 +1683,7 @@ static int test_blob_read(
if( rc==SQLITE_OK ){
Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zBuf, nByte));
}else{
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
}
Tcl_Free((char *)zBuf);
@@ -1765,7 +1733,7 @@ static int test_blob_write(
}
rc = sqlite3_blob_write(pBlob, zBuf, nBuf, iOffset);
if( rc!=SQLITE_OK ){
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
}
return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
@@ -1791,7 +1759,7 @@ static int test_blob_reopen(
rc = sqlite3_blob_reopen(pBlob, iRowid);
if( rc!=SQLITE_OK ){
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
}
return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
@@ -2001,7 +1969,7 @@ static int test_create_function_v2(
);
if( rc!=SQLITE_OK ){
Tcl_ResetResult(interp);
- Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
return TCL_OK;
@@ -2677,7 +2645,7 @@ static int test_collate(
if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
if( rc!=SQLITE_OK ){
- Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
return TCL_OK;
@@ -3067,7 +3035,7 @@ static int test_bind_int64(
){
sqlite3_stmt *pStmt;
int idx;
- i64 value;
+ Tcl_WideInt value;
int rc;
if( objc!=4 ){
@@ -3235,7 +3203,7 @@ static int test_bind_text(
rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT);
if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
if( rc!=SQLITE_OK ){
- Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
@@ -3283,7 +3251,7 @@ static int test_bind_text16(
rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel);
if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
if( rc!=SQLITE_OK ){
- Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
@@ -4557,7 +4525,7 @@ static int test_busy_timeout(
if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR;
rc = sqlite3_busy_timeout(db, ms);
- Tcl_AppendResult(interp, sqlite3TestErrorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_OK;
}
@@ -4703,7 +4671,7 @@ static int test_soft_heap_limit(
Tcl_Obj *CONST objv[]
){
sqlite3_int64 amt;
- sqlite3_int64 N = -1;
+ Tcl_WideInt N = -1;
if( objc!=1 && objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "?N?");
return TCL_ERROR;
@@ -5078,7 +5046,7 @@ static int file_control_chunksize_test(
rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_CHUNK_SIZE, (void *)&nSize);
if( rc ){
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
return TCL_ERROR;
}
return TCL_OK;
@@ -5096,7 +5064,7 @@ static int file_control_sizehint_test(
int objc, /* Number of arguments */
Tcl_Obj *CONST objv[] /* Command arguments */
){
- sqlite3_int64 nSize; /* Hinted size */
+ Tcl_WideInt nSize; /* Hinted size */
char *zDb; /* Db name ("main", "temp" etc.) */
sqlite3 *db; /* Database handle */
int rc; /* file_control() return code */
@@ -5115,7 +5083,7 @@ static int file_control_sizehint_test(
rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_SIZE_HINT, (void *)&nSize);
if( rc ){
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
return TCL_ERROR;
}
return TCL_OK;
@@ -5321,6 +5289,38 @@ static int file_control_vfsname(
return TCL_OK;
}
+/*
+** tclcmd: file_control_tempfilename DB ?AUXDB?
+**
+** Return a string that is a temporary filename
+*/
+static int file_control_tempfilename(
+ ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
+ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
+ int objc, /* Number of arguments */
+ Tcl_Obj *CONST objv[] /* Command arguments */
+){
+ sqlite3 *db;
+ const char *zDbName = "main";
+ char *zTName = 0;
+
+ if( objc!=2 && objc!=3 ){
+ Tcl_AppendResult(interp, "wrong # args: should be \"",
+ Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0);
+ return TCL_ERROR;
+ }
+ if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
+ return TCL_ERROR;
+ }
+ if( objc==3 ){
+ zDbName = Tcl_GetString(objv[2]);
+ }
+ sqlite3_file_control(db, zDbName, SQLITE_FCNTL_TEMPFILENAME, (void*)&zTName);
+ Tcl_AppendResult(interp, zTName, (char*)0);
+ sqlite3_free(zTName);
+ return TCL_OK;
+}
+
/*
** tclcmd: sqlite3_vfs_list
@@ -5627,7 +5627,7 @@ static void xLogcallback(void *unused, int err, char *zMsg){
Tcl_Obj *pNew = Tcl_DuplicateObj(logcallback.pObj);
Tcl_IncrRefCount(pNew);
Tcl_ListObjAppendElement(
- 0, pNew, Tcl_NewStringObj(sqlite3TestErrorName(err), -1)
+ 0, pNew, Tcl_NewStringObj(sqlite3ErrName(err), -1)
);
Tcl_ListObjAppendElement(0, pNew, Tcl_NewStringObj(zMsg, -1));
Tcl_EvalObjEx(logcallback.pInterp, pNew, TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT);
@@ -5799,6 +5799,31 @@ static int test_test_control(
return TCL_OK;
}
+#if SQLITE_OS_UNIX
+#include <sys/time.h>
+#include <sys/resource.h>
+
+static int test_getrusage(
+ void * clientData,
+ Tcl_Interp *interp,
+ int objc,
+ Tcl_Obj *CONST objv[]
+){
+ char buf[1024];
+ struct rusage r;
+ memset(&r, 0, sizeof(r));
+ getrusage(RUSAGE_SELF, &r);
+
+ sprintf(buf, "ru_utime=%d.%06d ru_stime=%d.%06d ru_minflt=%d ru_majflt=%d",
+ (int)r.ru_utime.tv_sec, (int)r.ru_utime.tv_usec,
+ (int)r.ru_stime.tv_sec, (int)r.ru_stime.tv_usec,
+ (int)r.ru_minflt, (int)r.ru_majflt
+ );
+ Tcl_SetObjResult(interp, Tcl_NewStringObj(buf, -1));
+ return TCL_OK;
+}
+#endif
+
#if SQLITE_OS_WIN
/*
** Information passed from the main thread into the windows file locker
@@ -5933,15 +5958,15 @@ static int optimization_control(
const char *zOptName;
int mask;
} aOpt[] = {
- { "all", SQLITE_OptMask },
+ { "all", SQLITE_AllOpts },
{ "query-flattener", SQLITE_QueryFlattener },
{ "column-cache", SQLITE_ColumnCache },
- { "index-sort", SQLITE_IndexSort },
- { "index-search", SQLITE_IndexSearch },
- { "index-cover", SQLITE_IndexCover },
{ "groupby-order", SQLITE_GroupByOrder },
{ "factor-constants", SQLITE_FactorOutConst },
{ "real-as-int", SQLITE_IdxRealAsInt },
+ { "distinct-opt", SQLITE_DistinctOpt },
+ { "cover-idx-scan", SQLITE_CoverIdxScan },
+ { "order-by-idx-join",SQLITE_OrderByIdxJoin },
};
if( objc!=4 ){
@@ -5970,6 +5995,69 @@ static int optimization_control(
return TCL_OK;
}
+typedef struct sqlite3_api_routines sqlite3_api_routines;
+/*
+** load_static_extension DB NAME ...
+**
+** Load one or more statically linked extensions.
+*/
+static int tclLoadStaticExtensionCmd(
+ void * clientData,
+ Tcl_Interp *interp,
+ int objc,
+ Tcl_Obj *CONST objv[]
+){
+ extern int sqlite3_amatch_init(sqlite3*,char**,const sqlite3_api_routines*);
+ extern int sqlite3_closure_init(sqlite3*,char**,const sqlite3_api_routines*);
+ extern int sqlite3_fuzzer_init(sqlite3*,char**,const sqlite3_api_routines*);
+ extern int sqlite3_ieee_init(sqlite3*,char**,const sqlite3_api_routines*);
+ extern int sqlite3_nextchar_init(sqlite3*,char**,const sqlite3_api_routines*);
+ extern int sqlite3_regexp_init(sqlite3*,char**,const sqlite3_api_routines*);
+ extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*);
+ extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*);
+ static const struct {
+ const char *zExtName;
+ int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*);
+ } aExtension[] = {
+ { "amatch", sqlite3_amatch_init },
+ { "closure", sqlite3_closure_init },
+ { "fuzzer", sqlite3_fuzzer_init },
+ { "ieee754", sqlite3_ieee_init },
+ { "nextchar", sqlite3_nextchar_init },
+ { "regexp", sqlite3_regexp_init },
+ { "spellfix", sqlite3_spellfix_init },
+ { "wholenumber", sqlite3_wholenumber_init },
+ };
+ sqlite3 *db;
+ const char *zName;
+ int i, j, rc;
+ char *zErrMsg = 0;
+ if( objc<3 ){
+ Tcl_WrongNumArgs(interp, 1, objv, "DB NAME ...");
+ return TCL_ERROR;
+ }
+ if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
+ for(j=2; j<objc; j++){
+ zName = Tcl_GetString(objv[j]);
+ for(i=0; i<ArraySize(aExtension); i++){
+ if( strcmp(zName, aExtension[i].zExtName)==0 ) break;
+ }
+ if( i>=ArraySize(aExtension) ){
+ Tcl_AppendResult(interp, "no such extension: ", zName, (char*)0);
+ return TCL_ERROR;
+ }
+ rc = aExtension[i].pInit(db, &zErrMsg, 0);
+ if( rc!=SQLITE_OK || zErrMsg ){
+ Tcl_AppendResult(interp, "initialization of ", zName, " failed: ", zErrMsg,
+ (char*)0);
+ sqlite3_free(zErrMsg);
+ return TCL_ERROR;
+ }
+ }
+ return TCL_OK;
+}
+
+
/*
** Register commands with the TCL interpreter.
*/
@@ -6013,6 +6101,7 @@ int Sqlitetest1_Init(Tcl_Interp *interp){
{ "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf },
#endif
{ "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close },
+ { "sqlite3_close_v2", (Tcl_CmdProc*)sqlite_test_close_v2 },
{ "sqlite3_create_function", (Tcl_CmdProc*)test_create_function },
{ "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate },
{ "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func },
@@ -6150,6 +6239,7 @@ int Sqlitetest1_Init(Tcl_Interp *interp){
{ "file_control_persist_wal", file_control_persist_wal, 0 },
{ "file_control_powersafe_overwrite",file_control_powersafe_overwrite,0},
{ "file_control_vfsname", file_control_vfsname, 0 },
+ { "file_control_tempfilename", file_control_tempfilename, 0 },
{ "sqlite3_vfs_list", vfs_list, 0 },
{ "sqlite3_create_function_v2", test_create_function_v2, 0 },
@@ -6187,6 +6277,10 @@ int Sqlitetest1_Init(Tcl_Interp *interp){
{ "print_explain_query_plan", test_print_eqp, 0 },
#endif
{ "sqlite3_test_control", test_test_control },
+#if SQLITE_OS_UNIX
+ { "getrusage", test_getrusage },
+#endif
+ { "load_static_extension", tclLoadStaticExtensionCmd },
};
static int bitmask_size = sizeof(Bitmask)*8;
int i;
@@ -6203,7 +6297,6 @@ int Sqlitetest1_Init(Tcl_Interp *interp){
#ifdef SQLITE_DEBUG
extern int sqlite3WhereTrace;
extern int sqlite3OSTrace;
- extern int sqlite3VdbeAddopTrace;
extern int sqlite3WalTrace;
#endif
#ifdef SQLITE_TEST
@@ -6266,8 +6359,6 @@ int Sqlitetest1_Init(Tcl_Interp *interp){
(char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
#endif
#ifdef SQLITE_DEBUG
- Tcl_LinkVar(interp, "sqlite_addop_trace",
- (char*)&sqlite3VdbeAddopTrace, TCL_LINK_INT);
Tcl_LinkVar(interp, "sqlite_where_trace",
(char*)&sqlite3WhereTrace, TCL_LINK_INT);
Tcl_LinkVar(interp, "sqlite_os_trace",
diff --git a/src/test2.c b/src/test2.c
index 8acdf6f..d130e9d 100644
--- a/src/test2.c
+++ b/src/test2.c
@@ -19,35 +19,7 @@
#include <string.h>
#include <ctype.h>
-/*
-** Interpret an SQLite error number
-*/
-static char *errorName(int rc){
- char *zName;
- switch( rc ){
- case SQLITE_OK: zName = "SQLITE_OK"; break;
- case SQLITE_ERROR: zName = "SQLITE_ERROR"; break;
- case SQLITE_PERM: zName = "SQLITE_PERM"; break;
- case SQLITE_ABORT: zName = "SQLITE_ABORT"; break;
- case SQLITE_BUSY: zName = "SQLITE_BUSY"; break;
- case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break;
- case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
- case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
- case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
- case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
- case SQLITE_FULL: zName = "SQLITE_FULL"; break;
- case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
- case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
- case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
- case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break;
- case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
- case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break;
- case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break;
- case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break;
- default: zName = "SQLITE_Unknown"; break;
- }
- return zName;
-}
+extern const char *sqlite3ErrName(int);
/*
** Page size and reserved size used for testing.
@@ -87,7 +59,7 @@ static int pager_open(
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB,
pager_test_reiniter);
if( rc!=SQLITE_OK ){
- Tcl_AppendResult(interp, errorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
sqlite3PagerSetCachesize(pPager, nPage);
@@ -119,7 +91,7 @@ static int pager_close(
pPager = sqlite3TestTextToPtr(argv[1]);
rc = sqlite3PagerClose(pPager);
if( rc!=SQLITE_OK ){
- Tcl_AppendResult(interp, errorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
return TCL_OK;
@@ -146,7 +118,7 @@ static int pager_rollback(
pPager = sqlite3TestTextToPtr(argv[1]);
rc = sqlite3PagerRollback(pPager);
if( rc!=SQLITE_OK ){
- Tcl_AppendResult(interp, errorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
return TCL_OK;
@@ -173,12 +145,12 @@ static int pager_commit(
pPager = sqlite3TestTextToPtr(argv[1]);
rc = sqlite3PagerCommitPhaseOne(pPager, 0, 0);
if( rc!=SQLITE_OK ){
- Tcl_AppendResult(interp, errorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
rc = sqlite3PagerCommitPhaseTwo(pPager);
if( rc!=SQLITE_OK ){
- Tcl_AppendResult(interp, errorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
return TCL_OK;
@@ -205,7 +177,7 @@ static int pager_stmt_begin(
pPager = sqlite3TestTextToPtr(argv[1]);
rc = sqlite3PagerOpenSavepoint(pPager, 1);
if( rc!=SQLITE_OK ){
- Tcl_AppendResult(interp, errorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
return TCL_OK;
@@ -233,7 +205,7 @@ static int pager_stmt_rollback(
rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, 0);
sqlite3PagerSavepoint(pPager, SAVEPOINT_RELEASE, 0);
if( rc!=SQLITE_OK ){
- Tcl_AppendResult(interp, errorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
return TCL_OK;
@@ -260,7 +232,7 @@ static int pager_stmt_commit(
pPager = sqlite3TestTextToPtr(argv[1]);
rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_RELEASE, 0);
if( rc!=SQLITE_OK ){
- Tcl_AppendResult(interp, errorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
return TCL_OK;
@@ -353,7 +325,7 @@ static int page_get(
rc = sqlite3PagerGet(pPager, pgno, &pPage);
}
if( rc!=SQLITE_OK ){
- Tcl_AppendResult(interp, errorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
sqlite3_snprintf(sizeof(zBuf),zBuf,"%p",pPage);
@@ -507,7 +479,7 @@ static int page_write(
pPage = (DbPage *)sqlite3TestTextToPtr(argv[1]);
rc = sqlite3PagerWrite(pPage);
if( rc!=SQLITE_OK ){
- Tcl_AppendResult(interp, errorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
pData = sqlite3PagerGetData(pPage);
@@ -556,7 +528,7 @@ static int fake_big_file(
(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB), 0
);
if( rc ){
- Tcl_AppendResult(interp, "open failed: ", errorName(rc), 0);
+ Tcl_AppendResult(interp, "open failed: ", sqlite3ErrName(rc), 0);
sqlite3_free(zFile);
return TCL_ERROR;
}
@@ -566,7 +538,7 @@ static int fake_big_file(
sqlite3OsCloseFree(fd);
sqlite3_free(zFile);
if( rc ){
- Tcl_AppendResult(interp, "write failed: ", errorName(rc), 0);
+ Tcl_AppendResult(interp, "write failed: ", sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
return TCL_OK;
diff --git a/src/test3.c b/src/test3.c
index e460c42..e3ed310 100644
--- a/src/test3.c
+++ b/src/test3.c
@@ -19,31 +19,7 @@
#include <stdlib.h>
#include <string.h>
-/*
-** Interpret an SQLite error number
-*/
-static char *errorName(int rc){
- char *zName;
- switch( rc ){
- case SQLITE_OK: zName = "SQLITE_OK"; break;
- case SQLITE_ERROR: zName = "SQLITE_ERROR"; break;
- case SQLITE_PERM: zName = "SQLITE_PERM"; break;
- case SQLITE_ABORT: zName = "SQLITE_ABORT"; break;
- case SQLITE_BUSY: zName = "SQLITE_BUSY"; break;
- case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break;
- case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
- case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
- case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
- case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
- case SQLITE_FULL: zName = "SQLITE_FULL"; break;
- case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
- case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
- case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
- case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break;
- default: zName = "SQLITE_Unknown"; break;
- }
- return zName;
-}
+extern const char *sqlite3ErrName(int);
/*
** A bogus sqlite3 connection structure for use in the btree
@@ -89,7 +65,7 @@ static int btree_open(
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB);
sqlite3_free(zFilename);
if( rc!=SQLITE_OK ){
- Tcl_AppendResult(interp, errorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
sqlite3BtreeSetCacheSize(pBt, nCache);
@@ -119,7 +95,7 @@ static int btree_close(
pBt = sqlite3TestTextToPtr(argv[1]);
rc = sqlite3BtreeClose(pBt);
if( rc!=SQLITE_OK ){
- Tcl_AppendResult(interp, errorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
nRefSqlite3--;
@@ -156,7 +132,7 @@ static int btree_begin_transaction(
rc = sqlite3BtreeBeginTrans(pBt, 1);
sqlite3BtreeLeave(pBt);
if( rc!=SQLITE_OK ){
- Tcl_AppendResult(interp, errorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
return TCL_OK;
@@ -250,7 +226,7 @@ static int btree_cursor(
sqlite3BtreeLeave(pBt);
if( rc ){
ckfree((char *)pCur);
- Tcl_AppendResult(interp, errorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
sqlite3_snprintf(sizeof(zBuf), zBuf,"%p", pCur);
@@ -285,7 +261,7 @@ static int btree_close_cursor(
sqlite3BtreeLeave(pBt);
ckfree((char *)pCur);
if( rc ){
- Tcl_AppendResult(interp, errorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
return SQLITE_OK;
@@ -319,7 +295,7 @@ static int btree_next(
rc = sqlite3BtreeNext(pCur, &res);
sqlite3BtreeLeave(pCur->pBtree);
if( rc ){
- Tcl_AppendResult(interp, errorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",res);
@@ -354,7 +330,7 @@ static int btree_first(
rc = sqlite3BtreeFirst(pCur, &res);
sqlite3BtreeLeave(pCur->pBtree);
if( rc ){
- Tcl_AppendResult(interp, errorName(rc), 0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
return TCL_ERROR;
}
sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",res);
diff --git a/src/test4.c b/src/test4.c
index 5b4c5a1..a6375c7 100644
--- a/src/test4.c
+++ b/src/test4.c
@@ -20,6 +20,8 @@
#include <sched.h>
#include <ctype.h>
+extern const char *sqlite3ErrName(int);
+
/*
** Each thread is controlled by an instance of the following
** structure.
@@ -372,34 +374,7 @@ static int tcl_thread_result(
return TCL_ERROR;
}
thread_wait(&threadset[i]);
- switch( threadset[i].rc ){
- case SQLITE_OK: zName = "SQLITE_OK"; break;
- case SQLITE_ERROR: zName = "SQLITE_ERROR"; break;
- case SQLITE_PERM: zName = "SQLITE_PERM"; break;
- case SQLITE_ABORT: zName = "SQLITE_ABORT"; break;
- case SQLITE_BUSY: zName = "SQLITE_BUSY"; break;
- case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break;
- case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break;
- case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
- case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
- case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
- case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
- case SQLITE_FULL: zName = "SQLITE_FULL"; break;
- case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
- case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
- case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
- case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break;
- case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
- case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break;
- case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break;
- case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break;
- case SQLITE_AUTH: zName = "SQLITE_AUTH"; break;
- case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break;
- case SQLITE_RANGE: zName = "SQLITE_RANGE"; break;
- case SQLITE_ROW: zName = "SQLITE_ROW"; break;
- case SQLITE_DONE: zName = "SQLITE_DONE"; break;
- default: zName = "SQLITE_Unknown"; break;
- }
+ zName = sqlite3ErrName(threadset[i].rc);
Tcl_AppendResult(interp, zName, 0);
return TCL_OK;
}
diff --git a/src/test6.c b/src/test6.c
index bae6b65..c151ea4 100644
--- a/src/test6.c
+++ b/src/test6.c
@@ -87,7 +87,7 @@ typedef struct WriteBuffer WriteBuffer;
** an aligned write() of an integer number of 512 byte regions, then
** option (3) above is never selected. Instead, each 512 byte region
** is either correctly written or left completely untouched. Similar
-** logic governs the behaviour if any of the other ATOMICXXX flags
+** logic governs the behavior if any of the other ATOMICXXX flags
** is set.
**
** If either the IOCAP_SAFEAPPEND or IOCAP_SEQUENTIAL flags are set
@@ -312,8 +312,8 @@ static int writeListSync(CrashFile *pFile, int isCrash){
assert(pWrite->zBuf);
#ifdef TRACE_CRASHTEST
- printf("Trashing %d sectors @ sector %d (%s)\n",
- 1+iLast-iFirst, iFirst, pWrite->pFile->zName
+ printf("Trashing %d sectors @ %lld (sector %d) (%s)\n",
+ 1+iLast-iFirst, pWrite->iOffset, iFirst, pWrite->pFile->zName
);
#endif
@@ -628,18 +628,19 @@ static int cfOpen(
** to read data from the 512-byte locking region of a file opened
** with the SQLITE_OPEN_MAIN_DB flag. This region of a database file
** never contains valid data anyhow. So avoid doing such a read here.
+ **
+ ** UPDATE: It also contains an assert() verifying that each call
+ ** to the xRead() method reads less than 128KB of data.
*/
const int isDb = (flags&SQLITE_OPEN_MAIN_DB);
- i64 iChunk = pWrapper->iSize;
- if( iChunk>PENDING_BYTE && isDb ){
- iChunk = PENDING_BYTE;
- }
+ i64 iOff;
+
memset(pWrapper->zData, 0, pWrapper->nData);
- rc = sqlite3OsRead(pReal, pWrapper->zData, (int)iChunk, 0);
- if( SQLITE_OK==rc && pWrapper->iSize>(PENDING_BYTE+512) && isDb ){
- i64 iOff = PENDING_BYTE+512;
- iChunk = pWrapper->iSize - iOff;
- rc = sqlite3OsRead(pReal, &pWrapper->zData[iOff], (int)iChunk, iOff);
+ for(iOff=0; iOff<pWrapper->iSize; iOff += 512){
+ int nRead = pWrapper->iSize - (int)iOff;
+ if( nRead>512 ) nRead = 512;
+ if( isDb && iOff==PENDING_BYTE ) continue;
+ rc = sqlite3OsRead(pReal, &pWrapper->zData[iOff], nRead, iOff);
}
}else{
rc = SQLITE_NOMEM;
diff --git a/src/test7.c b/src/test7.c
index 852cd1d..3cd4a22 100644
--- a/src/test7.c
+++ b/src/test7.c
@@ -376,6 +376,8 @@ static int tcl_client_colname(
return TCL_OK;
}
+extern const char *sqlite3ErrName(int);
+
/*
** Usage: client_result ID
**
@@ -403,34 +405,7 @@ static int tcl_client_result(
return TCL_ERROR;
}
client_wait(&threadset[i]);
- switch( threadset[i].rc ){
- case SQLITE_OK: zName = "SQLITE_OK"; break;
- case SQLITE_ERROR: zName = "SQLITE_ERROR"; break;
- case SQLITE_PERM: zName = "SQLITE_PERM"; break;
- case SQLITE_ABORT: zName = "SQLITE_ABORT"; break;
- case SQLITE_BUSY: zName = "SQLITE_BUSY"; break;
- case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break;
- case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break;
- case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
- case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
- case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
- case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
- case SQLITE_FULL: zName = "SQLITE_FULL"; break;
- case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
- case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
- case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
- case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break;
- case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
- case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break;
- case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break;
- case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break;
- case SQLITE_AUTH: zName = "SQLITE_AUTH"; break;
- case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break;
- case SQLITE_RANGE: zName = "SQLITE_RANGE"; break;
- case SQLITE_ROW: zName = "SQLITE_ROW"; break;
- case SQLITE_DONE: zName = "SQLITE_DONE"; break;
- default: zName = "SQLITE_Unknown"; break;
- }
+ zName = sqlite3ErrName(threadset[i].rc);
Tcl_AppendResult(interp, zName, 0);
return TCL_OK;
}
diff --git a/src/test8.c b/src/test8.c
index 53cb149..c573933 100644
--- a/src/test8.c
+++ b/src/test8.c
@@ -1300,7 +1300,7 @@ static sqlite3_module echoModuleV2 = {
** Decode a pointer to an sqlite3 object.
*/
extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
-extern const char *sqlite3TestErrorName(int rc);
+extern const char *sqlite3ErrName(int);
static void moduleDestroy(void *p){
sqlite3_free(p);
@@ -1340,7 +1340,7 @@ static int register_echo_module(
);
}
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
return TCL_OK;
}
@@ -1370,29 +1370,6 @@ static int declare_vtab(
return TCL_OK;
}
-#include "test_spellfix.c"
-
-/*
-** Register the spellfix virtual table module.
-*/
-static int register_spellfix_module(
- ClientData clientData,
- Tcl_Interp *interp,
- int objc,
- Tcl_Obj *CONST objv[]
-){
- sqlite3 *db;
-
- if( objc!=2 ){
- Tcl_WrongNumArgs(interp, 1, objv, "DB");
- return TCL_ERROR;
- }
- if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
-
- sqlite3Spellfix1Register(db);
- return TCL_OK;
-}
-
#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
/*
@@ -1406,7 +1383,6 @@ int Sqlitetest8_Init(Tcl_Interp *interp){
void *clientData;
} aObjCmd[] = {
{ "register_echo_module", register_echo_module, 0 },
- { "register_spellfix_module", register_spellfix_module, 0 },
{ "sqlite3_declare_vtab", declare_vtab, 0 },
};
int i;
diff --git a/src/test_async.c b/src/test_async.c
index c760eea..b0b9431 100644
--- a/src/test_async.c
+++ b/src/test_async.c
@@ -23,8 +23,8 @@
#include "sqlite3.h"
#include <assert.h>
-/* From test1.c */
-const char *sqlite3TestErrorName(int);
+/* From main.c */
+extern const char *sqlite3ErrName(int);
struct TestAsyncGlobal {
@@ -60,7 +60,7 @@ static int testAsyncInit(
rc = sqlite3async_initialize(zParent, isDefault);
if( rc!=SQLITE_OK ){
- Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3TestErrorName(rc), -1));
+ Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
return TCL_ERROR;
}
return TCL_OK;
@@ -208,7 +208,7 @@ static int testAsyncControl(
}
if( rc!=SQLITE_OK ){
- Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3TestErrorName(rc), -1));
+ Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
return TCL_ERROR;
}
diff --git a/src/test_autoext.c b/src/test_autoext.c
index 6b1e297..b5013f3 100644
--- a/src/test_autoext.c
+++ b/src/test_autoext.c
@@ -15,7 +15,7 @@
#include "sqlite3ext.h"
#ifndef SQLITE_OMIT_LOAD_EXTENSION
-static SQLITE_EXTENSION_INIT1
+SQLITE_EXTENSION_INIT1
/*
** The sqr() SQL function returns the square of its input value.
diff --git a/src/test_backup.c b/src/test_backup.c
index 2727137..e967424 100644
--- a/src/test_backup.c
+++ b/src/test_backup.c
@@ -17,9 +17,11 @@
#include <sqlite3.h>
#include <assert.h>
+/* These functions are implemented in main.c. */
+extern const char *sqlite3ErrName(int);
+
/* These functions are implemented in test1.c. */
-int getDbPointer(Tcl_Interp *, const char *, sqlite3 **);
-const char *sqlite3TestErrorName(int);
+extern int getDbPointer(Tcl_Interp *, const char *, sqlite3 **);
static int backupTestCmd(
ClientData clientData,
@@ -70,7 +72,7 @@ static int backupTestCmd(
Tcl_DeleteCommand(interp, zCmdName);
rc = sqlite3_backup_finish(p);
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
break;
}
@@ -80,7 +82,7 @@ static int backupTestCmd(
return TCL_ERROR;
}
rc = sqlite3_backup_step(p, nPage);
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
break;
}
diff --git a/src/test_config.c b/src/test_config.c
index f79b455..534727a 100644
--- a/src/test_config.c
+++ b/src/test_config.c
@@ -57,7 +57,7 @@ static void set_options(Tcl_Interp *interp){
Tcl_SetVar2(interp, "sqlite_options","casesensitivelike","0",TCL_GLOBAL_ONLY);
#endif
-#ifdef SQLITE_CURDIR
+#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
Tcl_SetVar2(interp, "sqlite_options", "curdir", "1", TCL_GLOBAL_ONLY);
#else
Tcl_SetVar2(interp, "sqlite_options", "curdir", "0", TCL_GLOBAL_ONLY);
@@ -87,6 +87,12 @@ static void set_options(Tcl_Interp *interp){
Tcl_SetVar2(interp, "sqlite_options", "lfs", "1", TCL_GLOBAL_ONLY);
#endif
+#if SQLITE_MAX_MMAP_SIZE>0
+ Tcl_SetVar2(interp, "sqlite_options", "mmap", "1", TCL_GLOBAL_ONLY);
+#else
+ Tcl_SetVar2(interp, "sqlite_options", "mmap", "0", TCL_GLOBAL_ONLY);
+#endif
+
#if 1 /* def SQLITE_MEMDEBUG */
Tcl_SetVar2(interp, "sqlite_options", "memdebug", "1", TCL_GLOBAL_ONLY);
#else
@@ -395,11 +401,7 @@ Tcl_SetVar2(interp, "sqlite_options", "long_double",
Tcl_SetVar2(interp, "sqlite_options", "memorymanage", "0", TCL_GLOBAL_ONLY);
#endif
-#ifdef SQLITE_OMIT_MERGE_SORT
- Tcl_SetVar2(interp, "sqlite_options", "mergesort", "0", TCL_GLOBAL_ONLY);
-#else
- Tcl_SetVar2(interp, "sqlite_options", "mergesort", "1", TCL_GLOBAL_ONLY);
-#endif
+Tcl_SetVar2(interp, "sqlite_options", "mergesort", "1", TCL_GLOBAL_ONLY);
#ifdef SQLITE_OMIT_OR_OPTIMIZATION
Tcl_SetVar2(interp, "sqlite_options", "or_opt", "0", TCL_GLOBAL_ONLY);
diff --git a/src/test_fs.c b/src/test_fs.c
new file mode 100644
index 0000000..478cad8
--- /dev/null
+++ b/src/test_fs.c
@@ -0,0 +1,333 @@
+/*
+** 2013 Jan 11
+**
+** 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.
+**
+*************************************************************************
+** Code for testing the virtual table interfaces. This code
+** is not included in the SQLite library. It is used for automated
+** testing of the SQLite library.
+**
+** The FS virtual table is created as follows:
+**
+** CREATE VIRTUAL TABLE tbl USING fs(idx);
+**
+** where idx is the name of a table in the db with 2 columns. The virtual
+** table also has two columns - file path and file contents.
+**
+** The first column of table idx must be an IPK, and the second contains file
+** paths. For example:
+**
+** CREATE TABLE idx(id INTEGER PRIMARY KEY, path TEXT);
+** INSERT INTO idx VALUES(4, '/etc/passwd');
+**
+** Adding the row to the idx table automatically creates a row in the
+** virtual table with rowid=4, path=/etc/passwd and a text field that
+** contains data read from file /etc/passwd on disk.
+*/
+#include "sqliteInt.h"
+#include "tcl.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#if SQLITE_OS_UNIX
+# include <unistd.h>
+#endif
+#if SQLITE_OS_WIN
+# include <io.h>
+#endif
+
+#ifndef SQLITE_OMIT_VIRTUALTABLE
+
+typedef struct fs_vtab fs_vtab;
+typedef struct fs_cursor fs_cursor;
+
+/*
+** A fs virtual-table object
+*/
+struct fs_vtab {
+ sqlite3_vtab base;
+ sqlite3 *db;
+ char *zDb; /* Name of db containing zTbl */
+ char *zTbl; /* Name of docid->file map table */
+};
+
+/* A fs cursor object */
+struct fs_cursor {
+ sqlite3_vtab_cursor base;
+ sqlite3_stmt *pStmt;
+ char *zBuf;
+ int nBuf;
+ int nAlloc;
+};
+
+/*
+** This function is the implementation of both the xConnect and xCreate
+** methods of the fs virtual table.
+**
+** The argv[] array contains the following:
+**
+** argv[0] -> module name ("fs")
+** argv[1] -> database name
+** argv[2] -> table name
+** argv[...] -> other module argument fields.
+*/
+static int fsConnect(
+ sqlite3 *db,
+ void *pAux,
+ int argc, const char *const*argv,
+ sqlite3_vtab **ppVtab,
+ char **pzErr
+){
+ fs_vtab *pVtab;
+ int nByte;
+ const char *zTbl;
+ const char *zDb = argv[1];
+
+ if( argc!=4 ){
+ *pzErr = sqlite3_mprintf("wrong number of arguments");
+ return SQLITE_ERROR;
+ }
+ zTbl = argv[3];
+
+ nByte = sizeof(fs_vtab) + (int)strlen(zTbl) + 1 + (int)strlen(zDb) + 1;
+ pVtab = (fs_vtab *)sqlite3MallocZero( nByte );
+ if( !pVtab ) return SQLITE_NOMEM;
+
+ pVtab->zTbl = (char *)&pVtab[1];
+ pVtab->zDb = &pVtab->zTbl[strlen(zTbl)+1];
+ pVtab->db = db;
+ memcpy(pVtab->zTbl, zTbl, strlen(zTbl));
+ memcpy(pVtab->zDb, zDb, strlen(zDb));
+ *ppVtab = &pVtab->base;
+ sqlite3_declare_vtab(db, "CREATE TABLE xyz(path TEXT, data TEXT)");
+
+ return SQLITE_OK;
+}
+/* Note that for this virtual table, the xCreate and xConnect
+** methods are identical. */
+
+static int fsDisconnect(sqlite3_vtab *pVtab){
+ sqlite3_free(pVtab);
+ return SQLITE_OK;
+}
+/* The xDisconnect and xDestroy methods are also the same */
+
+/*
+** Open a new fs cursor.
+*/
+static int fsOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
+ fs_cursor *pCur;
+ pCur = sqlite3MallocZero(sizeof(fs_cursor));
+ *ppCursor = &pCur->base;
+ return SQLITE_OK;
+}
+
+/*
+** Close a fs cursor.
+*/
+static int fsClose(sqlite3_vtab_cursor *cur){
+ fs_cursor *pCur = (fs_cursor *)cur;
+ sqlite3_finalize(pCur->pStmt);
+ sqlite3_free(pCur->zBuf);
+ sqlite3_free(pCur);
+ return SQLITE_OK;
+}
+
+static int fsNext(sqlite3_vtab_cursor *cur){
+ fs_cursor *pCur = (fs_cursor *)cur;
+ int rc;
+
+ rc = sqlite3_step(pCur->pStmt);
+ if( rc==SQLITE_ROW || rc==SQLITE_DONE ) rc = SQLITE_OK;
+
+ return rc;
+}
+
+static int fsFilter(
+ sqlite3_vtab_cursor *pVtabCursor,
+ int idxNum, const char *idxStr,
+ int argc, sqlite3_value **argv
+){
+ int rc;
+ fs_cursor *pCur = (fs_cursor *)pVtabCursor;
+ fs_vtab *p = (fs_vtab *)(pVtabCursor->pVtab);
+
+ assert( (idxNum==0 && argc==0) || (idxNum==1 && argc==1) );
+ if( idxNum==1 ){
+ char *zStmt = sqlite3_mprintf(
+ "SELECT * FROM %Q.%Q WHERE rowid=?", p->zDb, p->zTbl);
+ if( !zStmt ) return SQLITE_NOMEM;
+ rc = sqlite3_prepare_v2(p->db, zStmt, -1, &pCur->pStmt, 0);
+ sqlite3_free(zStmt);
+ if( rc==SQLITE_OK ){
+ sqlite3_bind_value(pCur->pStmt, 1, argv[0]);
+ }
+ }else{
+ char *zStmt = sqlite3_mprintf("SELECT * FROM %Q.%Q", p->zDb, p->zTbl);
+ if( !zStmt ) return SQLITE_NOMEM;
+ rc = sqlite3_prepare_v2(p->db, zStmt, -1, &pCur->pStmt, 0);
+ sqlite3_free(zStmt);
+ }
+
+ if( rc==SQLITE_OK ){
+ rc = fsNext(pVtabCursor);
+ }
+ return rc;
+}
+
+static int fsColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
+ fs_cursor *pCur = (fs_cursor*)cur;
+
+ assert( i==0 || i==1 );
+ if( i==0 ){
+ sqlite3_result_value(ctx, sqlite3_column_value(pCur->pStmt, 0));
+ }else{
+ const char *zFile = (const char *)sqlite3_column_text(pCur->pStmt, 1);
+ struct stat sbuf;
+ int fd;
+
+ fd = open(zFile, O_RDONLY);
+ if( fd<0 ) return SQLITE_IOERR;
+ fstat(fd, &sbuf);
+
+ if( sbuf.st_size>=pCur->nAlloc ){
+ int nNew = sbuf.st_size*2;
+ char *zNew;
+ if( nNew<1024 ) nNew = 1024;
+
+ zNew = sqlite3Realloc(pCur->zBuf, nNew);
+ if( zNew==0 ){
+ close(fd);
+ return SQLITE_NOMEM;
+ }
+ pCur->zBuf = zNew;
+ pCur->nAlloc = nNew;
+ }
+
+ read(fd, pCur->zBuf, sbuf.st_size);
+ close(fd);
+ pCur->nBuf = sbuf.st_size;
+ pCur->zBuf[pCur->nBuf] = '\0';
+
+ sqlite3_result_text(ctx, pCur->zBuf, -1, SQLITE_TRANSIENT);
+ }
+ return SQLITE_OK;
+}
+
+static int fsRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
+ fs_cursor *pCur = (fs_cursor*)cur;
+ *pRowid = sqlite3_column_int64(pCur->pStmt, 0);
+ return SQLITE_OK;
+}
+
+static int fsEof(sqlite3_vtab_cursor *cur){
+ fs_cursor *pCur = (fs_cursor*)cur;
+ return (sqlite3_data_count(pCur->pStmt)==0);
+}
+
+static int fsBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
+ int ii;
+
+ for(ii=0; ii<pIdxInfo->nConstraint; ii++){
+ struct sqlite3_index_constraint const *pCons = &pIdxInfo->aConstraint[ii];
+ if( pCons->iColumn<0 && pCons->usable
+ && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
+ struct sqlite3_index_constraint_usage *pUsage;
+ pUsage = &pIdxInfo->aConstraintUsage[ii];
+ pUsage->omit = 0;
+ pUsage->argvIndex = 1;
+ pIdxInfo->idxNum = 1;
+ pIdxInfo->estimatedCost = 1.0;
+ break;
+ }
+ }
+
+ return SQLITE_OK;
+}
+
+/*
+** A virtual table module that provides read-only access to a
+** Tcl global variable namespace.
+*/
+static sqlite3_module fsModule = {
+ 0, /* iVersion */
+ fsConnect,
+ fsConnect,
+ fsBestIndex,
+ fsDisconnect,
+ fsDisconnect,
+ fsOpen, /* xOpen - open a cursor */
+ fsClose, /* xClose - close a cursor */
+ fsFilter, /* xFilter - configure scan constraints */
+ fsNext, /* xNext - advance a cursor */
+ fsEof, /* xEof - check for end of scan */
+ fsColumn, /* xColumn - read data */
+ fsRowid, /* xRowid - read data */
+ 0, /* xUpdate */
+ 0, /* xBegin */
+ 0, /* xSync */
+ 0, /* xCommit */
+ 0, /* xRollback */
+ 0, /* xFindMethod */
+ 0, /* xRename */
+};
+
+/*
+** Decode a pointer to an sqlite3 object.
+*/
+extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
+
+/*
+** Register the echo virtual table module.
+*/
+static int register_fs_module(
+ ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
+ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
+ int objc, /* Number of arguments */
+ Tcl_Obj *CONST objv[] /* Command arguments */
+){
+ sqlite3 *db;
+ if( objc!=2 ){
+ Tcl_WrongNumArgs(interp, 1, objv, "DB");
+ return TCL_ERROR;
+ }
+ if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
+#ifndef SQLITE_OMIT_VIRTUALTABLE
+ sqlite3_create_module(db, "fs", &fsModule, (void *)interp);
+#endif
+ return TCL_OK;
+}
+
+#endif
+
+
+/*
+** Register commands with the TCL interpreter.
+*/
+int Sqlitetestfs_Init(Tcl_Interp *interp){
+#ifndef SQLITE_OMIT_VIRTUALTABLE
+ static struct {
+ char *zName;
+ Tcl_ObjCmdProc *xProc;
+ void *clientData;
+ } aObjCmd[] = {
+ { "register_fs_module", register_fs_module, 0 },
+ };
+ int i;
+ for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
+ Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
+ aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
+ }
+#endif
+ return TCL_OK;
+}
diff --git a/src/test_intarray.c b/src/test_intarray.c
index 8651d01..f5c3d9e 100644
--- a/src/test_intarray.c
+++ b/src/test_intarray.c
@@ -278,7 +278,7 @@ int sqlite3_intarray_bind(
extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
extern void *sqlite3TestTextToPtr(const char*);
extern int sqlite3TestMakePointerStr(Tcl_Interp*, char *zPtr, void*);
-extern const char *sqlite3TestErrorName(int);
+extern const char *sqlite3ErrName(int);
/*
** sqlite3_intarray_create DB NAME
@@ -309,7 +309,7 @@ static int test_intarray_create(
#endif
if( rc!=SQLITE_OK ){
assert( pArray==0 );
- Tcl_AppendResult(interp, sqlite3TestErrorName(rc), (char*)0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), (char*)0);
return TCL_ERROR;
}
sqlite3TestMakePointerStr(interp, zPtr, pArray);
@@ -346,12 +346,13 @@ static int test_intarray_bind(
return TCL_ERROR;
}
for(i=0; i<n; i++){
- a[i] = 0;
- Tcl_GetWideIntFromObj(0, objv[i+2], &a[i]);
+ Tcl_WideInt x = 0;
+ Tcl_GetWideIntFromObj(0, objv[i+2], &x);
+ a[i] = x;
}
rc = sqlite3_intarray_bind(pArray, n, a, sqlite3_free);
if( rc!=SQLITE_OK ){
- Tcl_AppendResult(interp, sqlite3TestErrorName(rc), (char*)0);
+ Tcl_AppendResult(interp, sqlite3ErrName(rc), (char*)0);
return TCL_ERROR;
}
#endif
diff --git a/src/test_intarray.h b/src/test_intarray.h
index e994367..691337d 100644
--- a/src/test_intarray.h
+++ b/src/test_intarray.h
@@ -77,6 +77,13 @@
#include "sqlite3.h"
/*
+** Make sure we can call this stuff from C++.
+*/
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
** An sqlite3_intarray is an abstract type to stores an instance of
** an integer array.
*/
@@ -112,3 +119,7 @@ int sqlite3_intarray_bind(
sqlite3_int64 *aElements, /* Content of the intarray */
void (*xFree)(void*) /* How to dispose of the intarray when done */
);
+
+#ifdef __cplusplus
+} /* End of the 'extern "C"' block */
+#endif
diff --git a/src/test_malloc.c b/src/test_malloc.c
index 09b8f73..cf98a8f 100644
--- a/src/test_malloc.c
+++ b/src/test_malloc.c
@@ -234,14 +234,14 @@ static int faultsimInstall(int install){
#ifdef SQLITE_TEST
/*
-** This function is implemented in test1.c. Returns a pointer to a static
+** This function is implemented in main.c. Returns a pointer to a static
** buffer containing the symbolic SQLite error code that corresponds to
** the least-significant 8-bits of the integer passed as an argument.
** For example:
**
-** sqlite3TestErrorName(1) -> "SQLITE_ERROR"
+** sqlite3ErrName(1) -> "SQLITE_ERROR"
*/
-const char *sqlite3TestErrorName(int);
+extern const char *sqlite3ErrName(int);
/*
** Transform pointers to text and back again
@@ -720,8 +720,8 @@ static int test_memdebug_settitle(
#ifdef SQLITE_MEMDEBUG
{
const char *zTitle;
- zTitle = Tcl_GetString(objv[1]);
extern int sqlite3MemdebugSettitle(const char*);
+ zTitle = Tcl_GetString(objv[1]);
sqlite3MemdebugSettitle(zTitle);
}
#endif
@@ -1072,7 +1072,7 @@ static int test_db_config_lookaside(
sqlite3 *db;
int bufid;
static char azBuf[2][10000];
- int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
+ extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
if( objc!=5 ){
Tcl_WrongNumArgs(interp, 1, objv, "BUFID SIZE COUNT");
return TCL_ERROR;
@@ -1094,9 +1094,7 @@ static int test_db_config_lookaside(
}
/*
-** Usage:
-**
-** sqlite3_config_heap NBYTE NMINALLOC
+** Usage: sqlite3_config_heap NBYTE NMINALLOC
*/
static int test_config_heap(
void * clientData,
@@ -1128,12 +1126,12 @@ static int test_config_heap(
rc = sqlite3_config(SQLITE_CONFIG_HEAP, zBuf, nByte, nMinAlloc);
}
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
return TCL_OK;
}
/*
-** tclcmd: sqlite3_config_error [DB]
+** Usage: sqlite3_config_error [DB]
**
** Invoke sqlite3_config() or sqlite3_db_config() with invalid
** opcodes and verify that they return errors.
@@ -1145,7 +1143,7 @@ static int test_config_error(
Tcl_Obj *CONST objv[]
){
sqlite3 *db;
- int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
+ extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
if( objc!=2 && objc!=1 ){
Tcl_WrongNumArgs(interp, 1, objv, "[DB]");
@@ -1171,10 +1169,10 @@ static int test_config_error(
}
/*
-** tclcmd: sqlite3_config_uri BOOLEAN
+** Usage: sqlite3_config_uri BOOLEAN
**
-** Invoke sqlite3_config() or sqlite3_db_config() with invalid
-** opcodes and verify that they return errors.
+** Enables or disables interpretation of URI parameters by default using
+** SQLITE_CONFIG_URI.
*/
static int test_config_uri(
void * clientData,
@@ -1194,16 +1192,43 @@ static int test_config_uri(
}
rc = sqlite3_config(SQLITE_CONFIG_URI, bOpenUri);
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
return TCL_OK;
}
/*
-** Usage:
+** Usage: sqlite3_config_cis BOOLEAN
**
-** sqlite3_dump_memsys3 FILENAME
-** sqlite3_dump_memsys5 FILENAME
+** Enables or disables the use of the covering-index scan optimization.
+** SQLITE_CONFIG_COVERING_INDEX_SCAN.
+*/
+static int test_config_cis(
+ void * clientData,
+ Tcl_Interp *interp,
+ int objc,
+ Tcl_Obj *CONST objv[]
+){
+ int rc;
+ int bUseCis;
+
+ if( objc!=2 ){
+ Tcl_WrongNumArgs(interp, 1, objv, "BOOL");
+ return TCL_ERROR;
+ }
+ if( Tcl_GetBooleanFromObj(interp, objv[1], &bUseCis) ){
+ return TCL_ERROR;
+ }
+
+ rc = sqlite3_config(SQLITE_CONFIG_COVERING_INDEX_SCAN, bUseCis);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
+
+ return TCL_OK;
+}
+
+/*
+** Usage: sqlite3_dump_memsys3 FILENAME
+** sqlite3_dump_memsys5 FILENAME
**
** Write a summary of unfreed memsys3 allocations to FILENAME.
*/
@@ -1310,7 +1335,7 @@ static int test_db_status(
int i, op, resetFlag;
const char *zOpName;
sqlite3 *db;
- int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
+ extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
static const struct {
const char *zName;
int op;
@@ -1376,7 +1401,7 @@ static int test_install_malloc_faultsim(
return TCL_ERROR;
}
rc = faultsimInstall(isInstall);
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
return TCL_OK;
}
@@ -1394,7 +1419,7 @@ static int test_install_memsys3(
const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
rc = sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetMemsys3());
#endif
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
return TCL_OK;
}
@@ -1451,6 +1476,7 @@ int Sqlitetest_malloc_Init(Tcl_Interp *interp){
{ "sqlite3_config_lookaside", test_config_lookaside ,0 },
{ "sqlite3_config_error", test_config_error ,0 },
{ "sqlite3_config_uri", test_config_uri ,0 },
+ { "sqlite3_config_cis", test_config_cis ,0 },
{ "sqlite3_db_config_lookaside",test_db_config_lookaside ,0 },
{ "sqlite3_dump_memsys3", test_dump_memsys3 ,3 },
{ "sqlite3_dump_memsys5", test_dump_memsys3 ,5 },
diff --git a/src/test_multiplex.c b/src/test_multiplex.c
index 23df347..624541b 100644
--- a/src/test_multiplex.c
+++ b/src/test_multiplex.c
@@ -60,7 +60,7 @@
/*
** These should be defined to be the same as the values in
-** sqliteInt.h. They are defined seperately here so that
+** sqliteInt.h. They are defined separately here so that
** the multiplex VFS shim can be built as a loadable
** module.
*/
@@ -1183,7 +1183,7 @@ int sqlite3_multiplex_shutdown(void){
/***************************** Test Code ***********************************/
#ifdef SQLITE_TEST
#include <tcl.h>
-extern const char *sqlite3TestErrorName(int);
+extern const char *sqlite3ErrName(int);
/*
@@ -1212,7 +1212,7 @@ static int test_multiplex_initialize(
/* Call sqlite3_multiplex_initialize() */
rc = sqlite3_multiplex_initialize(zName, makeDefault);
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
return TCL_OK;
}
@@ -1237,7 +1237,7 @@ static int test_multiplex_shutdown(
/* Call sqlite3_multiplex_shutdown() */
rc = sqlite3_multiplex_shutdown();
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
return TCL_OK;
}
@@ -1355,7 +1355,7 @@ static int test_multiplex_control(
}
rc = sqlite3_file_control(db, Tcl_GetString(objv[2]), aSub[idx].op, pArg);
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
return (rc==SQLITE_OK) ? TCL_OK : TCL_ERROR;
}
diff --git a/src/test_multiplex.h b/src/test_multiplex.h
index ec1ba9b..b7e1afe 100644
--- a/src/test_multiplex.h
+++ b/src/test_multiplex.h
@@ -46,6 +46,10 @@
#define MULTIPLEX_CTRL_SET_CHUNK_SIZE 214015
#define MULTIPLEX_CTRL_SET_MAX_CHUNKS 214016
+#ifdef __cplusplus
+extern "C" {
+#endif
+
/*
** CAPI: Initialize the multiplex VFS shim - sqlite3_multiplex_initialize()
**
@@ -88,4 +92,8 @@ extern int sqlite3_multiplex_initialize(const char *zOrigVfsName, int makeDefaul
*/
extern int sqlite3_multiplex_shutdown(void);
+#ifdef __cplusplus
+} /* End of the 'extern "C"' block */
+#endif
+
#endif
diff --git a/src/test_mutex.c b/src/test_mutex.c
index 0bb7437..c9b4a29 100644
--- a/src/test_mutex.c
+++ b/src/test_mutex.c
@@ -19,8 +19,8 @@
#include <assert.h>
#include <string.h>
-/* defined in test1.c */
-const char *sqlite3TestErrorName(int);
+/* defined in main.c */
+extern const char *sqlite3ErrName(int);
/* A countable mutex */
struct sqlite3_mutex {
@@ -148,7 +148,7 @@ static int test_shutdown(
}
rc = sqlite3_shutdown();
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
return TCL_OK;
}
@@ -169,7 +169,7 @@ static int test_initialize(
}
rc = sqlite3_initialize();
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
return TCL_OK;
}
@@ -230,7 +230,7 @@ static int test_install_mutex_counters(
g.isInstalled = isInstall;
}
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
return TCL_OK;
}
@@ -354,7 +354,7 @@ static int test_config(
}
rc = sqlite3_config(i);
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
return TCL_OK;
}
diff --git a/src/test_quota.c b/src/test_quota.c
index 2ce46ac..e590996 100644
--- a/src/test_quota.c
+++ b/src/test_quota.c
@@ -1073,7 +1073,7 @@ size_t sqlite3_quota_fwrite(
/* If the write was incomplete, adjust the file size and group size
** downward */
if( rc<nmemb && pFile ){
- size_t nWritten = rc>=0 ? rc : 0;
+ size_t nWritten = rc;
sqlite3_int64 iNewEnd = iOfst + size*nWritten;
if( iNewEnd<iEnd ) iNewEnd = iEnd;
quotaEnter();
@@ -1179,7 +1179,13 @@ int sqlite3_quota_ftruncate(quota_FILE *p, sqlite3_int64 szNew){
rc = ftruncate(fileno(p->f), szNew);
#endif
#if SQLITE_OS_WIN
- rc = _chsize_s(_fileno(p->f), szNew);
+# if defined(__MINGW32__) && defined(SQLITE_TEST)
+ /* _chsize_s() is missing from MingW (as of 2012-11-06). Use
+ ** _chsize() as a work-around for testing purposes. */
+ rc = _chsize(_fileno(p->f), (long)szNew);
+# else
+ rc = _chsize_s(_fileno(p->f), szNew);
+# endif
#endif
if( pFile && rc==0 ){
quotaGroup *pGroup = pFile->pGroup;
@@ -1289,7 +1295,7 @@ int sqlite3_quota_remove(const char *zFilename){
if( pGroup ){
for(pFile=pGroup->pFiles; pFile && rc==SQLITE_OK; pFile=pNextFile){
pNextFile = pFile->pNext;
- diff = memcmp(zFull, pFile->zFilename, nFull);
+ diff = strncmp(zFull, pFile->zFilename, nFull);
if( diff==0 && ((c = pFile->zFilename[nFull])==0 || c=='/' || c=='\\') ){
if( pFile->nRef ){
pFile->deleteOnClose = 1;
@@ -1319,7 +1325,7 @@ struct TclQuotaCallback {
Tcl_Obj *pScript; /* Script to be run */
};
-extern const char *sqlite3TestErrorName(int);
+extern const char *sqlite3ErrName(int);
/*
@@ -1354,8 +1360,10 @@ static void tclQuotaCallback(
rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL);
if( rc==TCL_OK ){
+ Tcl_WideInt x;
Tcl_Obj *pLimit = Tcl_ObjGetVar2(p->interp, pVarname, 0, 0);
- rc = Tcl_GetWideIntFromObj(p->interp, pLimit, piLimit);
+ rc = Tcl_GetWideIntFromObj(p->interp, pLimit, &x);
+ *piLimit = x;
Tcl_UnsetVar(p->interp, Tcl_GetString(pVarname), 0);
}
@@ -1399,7 +1407,7 @@ static int test_quota_initialize(
/* Call sqlite3_quota_initialize() */
rc = sqlite3_quota_initialize(zName, makeDefault);
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
return TCL_OK;
}
@@ -1422,7 +1430,7 @@ static int test_quota_shutdown(
/* Call sqlite3_quota_shutdown() */
rc = sqlite3_quota_shutdown();
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
return TCL_OK;
}
@@ -1437,7 +1445,7 @@ static int test_quota_set(
Tcl_Obj *CONST objv[]
){
const char *zPattern; /* File pattern to configure */
- sqlite3_int64 iLimit; /* Initial quota in bytes */
+ Tcl_WideInt iLimit; /* Initial quota in bytes */
Tcl_Obj *pScript; /* Tcl script to invoke to increase quota */
int rc; /* Value returned by quota_set() */
TclQuotaCallback *p; /* Callback object */
@@ -1477,7 +1485,7 @@ static int test_quota_set(
/* Invoke sqlite3_quota_set() */
rc = sqlite3_quota_set(zPattern, iLimit, xCallback, (void*)p, xDestroy);
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
return TCL_OK;
}
@@ -1503,7 +1511,7 @@ static int test_quota_file(
/* Invoke sqlite3_quota_file() */
rc = sqlite3_quota_file(zFilename);
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
return TCL_OK;
}
@@ -1613,7 +1621,6 @@ static int test_quota_fread(
return TCL_ERROR;
}
got = sqlite3_quota_fread(zBuf, sz, nElem, p);
- if( got<0 ) got = 0;
zBuf[got*sz] = 0;
Tcl_SetResult(interp, zBuf, TCL_VOLATILE);
sqlite3_free(zBuf);
diff --git a/src/test_rtree.c b/src/test_rtree.c
index d3c9e0c..f54ae9b 100644
--- a/src/test_rtree.c
+++ b/src/test_rtree.c
@@ -254,7 +254,7 @@ static int register_cube_geom(
UNUSED_PARAMETER(objv);
#else
extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
- extern const char *sqlite3TestErrorName(int);
+ extern const char *sqlite3ErrName(int);
sqlite3 *db;
int rc;
@@ -264,7 +264,7 @@ static int register_cube_geom(
}
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
rc = sqlite3_rtree_geometry_callback(db, "cube", cube_geom, (void *)&gHere);
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
#endif
return TCL_OK;
}
@@ -282,7 +282,7 @@ static int register_circle_geom(
UNUSED_PARAMETER(objv);
#else
extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
- extern const char *sqlite3TestErrorName(int);
+ extern const char *sqlite3ErrName(int);
sqlite3 *db;
int rc;
@@ -292,7 +292,7 @@ static int register_circle_geom(
}
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
rc = sqlite3_rtree_geometry_callback(db, "circle", circle_geom, 0);
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
#endif
return TCL_OK;
}
diff --git a/src/test_sqllog.c b/src/test_sqllog.c
new file mode 100644
index 0000000..4aa68b7
--- /dev/null
+++ b/src/test_sqllog.c
@@ -0,0 +1,507 @@
+/*
+** 2012 November 26
+**
+** 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.
+**
+*************************************************************************
+**
+** OVERVIEW
+**
+** This file contains experimental code used to record data from live
+** SQLite applications that may be useful for offline analysis.
+** Specifically, this module can be used to capture the following
+** information:
+**
+** 1) The initial contents of all database files opened by the
+** application, and
+**
+** 2) All SQL statements executed by the application.
+**
+** The captured information can then be used to run (for example)
+** performance analysis looking for slow queries or to look for
+** optimization opportunities in either the application or in SQLite
+** itself.
+**
+** USAGE
+**
+** To use this module, SQLite must be compiled with the SQLITE_ENABLE_SQLLOG
+** pre-processor symbol defined and this file linked into the application.
+** One way to link this file into the application is to append the content
+** of this file onto the end of the "sqlite3.c" amalgamation and then
+** recompile the application as normal except with the addition of the
+** -DSQLITE_ENABLE_SQLLOG option.
+**
+** At runtime, logging is enabled by setting environment variable
+** SQLITE_SQLLOG_DIR to the name of a directory in which to store logged
+** data. The logging directory must already exist.
+**
+** Usually, if the application opens the same database file more than once
+** (either by attaching it or by using more than one database handle), only
+** a single copy is made. This behavior may be overridden (so that a
+** separate copy is taken each time the database file is opened or attached)
+** by setting the environment variable SQLITE_SQLLOG_REUSE_FILES to 0.
+**
+** OUTPUT:
+**
+** The SQLITE_SQLLOG_DIR is populated with three types of files:
+**
+** sqllog_N.db - Copies of database files. N may be any integer.
+**
+** sqllog_N.sql - A list of SQL statements executed by a single
+** connection. N may be any integer.
+**
+** sqllog.idx - An index mapping from integer N to a database
+** file name - indicating the full path of the
+** database from which sqllog_N.db was copied.
+**
+** ERROR HANDLING:
+**
+** This module attempts to make a best effort to continue logging if an
+** IO or other error is encountered. For example, if a log file cannot
+** be opened logs are not collected for that connection, but other
+** logging proceeds as expected. Errors are logged by calling sqlite3_log().
+*/
+
+#ifndef _SQLITE3_H_
+#include "sqlite3.h"
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#include <sys/types.h>
+#include <unistd.h>
+static int getProcessId(void){
+#if SQLITE_OS_WIN
+ return (int)_getpid();
+#else
+ return (int)getpid();
+#endif
+}
+
+/* Names of environment variables to be used */
+#define ENVIRONMENT_VARIABLE1_NAME "SQLITE_SQLLOG_DIR"
+#define ENVIRONMENT_VARIABLE2_NAME "SQLITE_SQLLOG_REUSE_FILES"
+
+/* Assume that all database and database file names are shorted than this. */
+#define SQLLOG_NAMESZ 512
+
+/* Maximum number of simultaneous database connections the process may
+** open (if any more are opened an error is logged using sqlite3_log()
+** and processing is halted).
+*/
+#define MAX_CONNECTIONS 256
+
+/* There is one instance of this object for each SQLite database connection
+** that is being logged.
+*/
+struct SLConn {
+ int isErr; /* True if an error has occurred */
+ sqlite3 *db; /* Connection handle */
+ int iLog; /* First integer value used in file names */
+ FILE *fd; /* File descriptor for log file */
+};
+
+/* This object is a singleton that keeps track of all data loggers.
+*/
+static struct SLGlobal {
+ /* Protected by MUTEX_STATIC_MASTER */
+ sqlite3_mutex *mutex; /* Recursive mutex */
+ int nConn; /* Size of aConn[] array */
+
+ /* Protected by SLGlobal.mutex */
+ int bReuse; /* True to avoid extra copies of db files */
+ char zPrefix[SQLLOG_NAMESZ]; /* Prefix for all created files */
+ char zIdx[SQLLOG_NAMESZ]; /* Full path to *.idx file */
+ int iNextLog; /* Used to allocate file names */
+ int iNextDb; /* Used to allocate database file names */
+ int bRec; /* True if testSqllog() is called rec. */
+ int iClock; /* Clock value */
+ struct SLConn aConn[MAX_CONNECTIONS];
+} sqllogglobal;
+
+/*
+** Return true if c is an ASCII whitespace character.
+*/
+static int sqllog_isspace(char c){
+ return (c==' ' || c=='\t' || c=='\n' || c=='\v' || c=='\f' || c=='\r');
+}
+
+/*
+** The first argument points to a nul-terminated string containing an SQL
+** command. Before returning, this function sets *pz to point to the start
+** of the first token in this command, and *pn to the number of bytes in
+** the token. This is used to check if the SQL command is an "ATTACH" or
+** not.
+*/
+static void sqllogTokenize(const char *z, const char **pz, int *pn){
+ const char *p = z;
+ int n;
+
+ /* Skip past any whitespace */
+ while( sqllog_isspace(*p) ){
+ p++;
+ }
+
+ /* Figure out how long the first token is */
+ *pz = p;
+ n = 0;
+ while( (p[n]>='a' && p[n]<='z') || (p[n]>='A' && p[n]<='Z') ) n++;
+ *pn = n;
+}
+
+/*
+** Check if the logs directory already contains a copy of database file
+** zFile. If so, return a pointer to the full path of the copy. Otherwise,
+** return NULL.
+**
+** If a non-NULL value is returned, then the caller must arrange to
+** eventually free it using sqlite3_free().
+*/
+static char *sqllogFindFile(const char *zFile){
+ char *zRet = 0;
+ FILE *fd = 0;
+
+ /* Open the index file for reading */
+ fd = fopen(sqllogglobal.zIdx, "r");
+ if( fd==0 ){
+ sqlite3_log(SQLITE_IOERR, "sqllogFindFile(): error in fopen()");
+ return 0;
+ }
+
+ /* Loop through each entry in the index file. If zFile is not NULL and the
+ ** entry is a match, then set zRet to point to the filename of the existing
+ ** copy and break out of the loop. */
+ while( feof(fd)==0 ){
+ char zLine[SQLLOG_NAMESZ*2+5];
+ if( fgets(zLine, sizeof(zLine), fd) ){
+ int n;
+ char *z;
+
+ zLine[sizeof(zLine)-1] = '\0';
+ z = zLine;
+ while( *z>='0' && *z<='9' ) z++;
+ while( *z==' ' ) z++;
+
+ n = strlen(z);
+ while( n>0 && sqllog_isspace(z[n-1]) ) n--;
+
+ if( n==strlen(zFile) && 0==memcmp(zFile, z, n) ){
+ char zBuf[16];
+ memset(zBuf, 0, sizeof(zBuf));
+ z = zLine;
+ while( *z>='0' && *z<='9' ){
+ zBuf[z-zLine] = *z;
+ z++;
+ }
+ zRet = sqlite3_mprintf("%s_%s.db", sqllogglobal.zPrefix, zBuf);
+ break;
+ }
+ }
+ }
+
+ if( ferror(fd) ){
+ sqlite3_log(SQLITE_IOERR, "sqllogFindFile(): error reading index file");
+ }
+
+ fclose(fd);
+ return zRet;
+}
+
+static int sqllogFindAttached(
+ struct SLConn *p, /* Database connection */
+ const char *zSearch, /* Name to search for (or NULL) */
+ char *zName, /* OUT: Name of attached database */
+ char *zFile /* OUT: Name of attached file */
+){
+ sqlite3_stmt *pStmt;
+ int rc;
+
+ /* The "PRAGMA database_list" command returns a list of databases in the
+ ** order that they were attached. So a newly attached database is
+ ** described by the last row returned. */
+ assert( sqllogglobal.bRec==0 );
+ sqllogglobal.bRec = 1;
+ rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
+ if( rc==SQLITE_OK ){
+ while( SQLITE_ROW==sqlite3_step(pStmt) ){
+ const char *zVal1; int nVal1;
+ const char *zVal2; int nVal2;
+
+ zVal1 = (const char*)sqlite3_column_text(pStmt, 1);
+ nVal1 = sqlite3_column_bytes(pStmt, 1);
+ memcpy(zName, zVal1, nVal1+1);
+
+ zVal2 = (const char*)sqlite3_column_text(pStmt, 2);
+ nVal2 = sqlite3_column_bytes(pStmt, 2);
+ memcpy(zFile, zVal2, nVal2+1);
+
+ if( zSearch && strlen(zSearch)==nVal1
+ && 0==sqlite3_strnicmp(zSearch, zVal1, nVal1)
+ ){
+ break;
+ }
+ }
+ rc = sqlite3_finalize(pStmt);
+ }
+ sqllogglobal.bRec = 0;
+
+ if( rc!=SQLITE_OK ){
+ sqlite3_log(rc, "sqllogFindAttached(): error in \"PRAGMA database_list\"");
+ }
+ return rc;
+}
+
+
+/*
+** Parameter zSearch is the name of a database attached to the database
+** connection associated with the first argument. This function creates
+** a backup of this database in the logs directory.
+**
+** The name used for the backup file is automatically generated. Call
+** it zFile.
+**
+** If the bLog parameter is true, then a statement of the following form
+** is written to the log file associated with *p:
+**
+** ATTACH 'zFile' AS 'zName';
+**
+** Otherwise, if bLog is false, a comment is added to the log file:
+**
+** -- Main database file is 'zFile'
+**
+** The SLGlobal.mutex mutex is always held when this function is called.
+*/
+static void sqllogCopydb(struct SLConn *p, const char *zSearch, int bLog){
+ char zName[SQLLOG_NAMESZ]; /* Attached database name */
+ char zFile[SQLLOG_NAMESZ]; /* Database file name */
+ char *zFree;
+ char *zInit = 0;
+ int rc;
+
+ rc = sqllogFindAttached(p, zSearch, zName, zFile);
+ if( rc!=SQLITE_OK ) return;
+
+ if( zFile[0]=='\0' ){
+ zInit = sqlite3_mprintf("");
+ }else{
+ if( sqllogglobal.bReuse ){
+ zInit = sqllogFindFile(zFile);
+ }else{
+ zInit = 0;
+ }
+ if( zInit==0 ){
+ int rc;
+ sqlite3 *copy = 0;
+ int iDb;
+
+ /* Generate a file-name to use for the copy of this database */
+ iDb = sqllogglobal.iNextDb++;
+ zInit = sqlite3_mprintf("%s_%d.db", sqllogglobal.zPrefix, iDb);
+
+ /* Create the backup */
+ assert( sqllogglobal.bRec==0 );
+ sqllogglobal.bRec = 1;
+ rc = sqlite3_open(zInit, &copy);
+ if( rc==SQLITE_OK ){
+ sqlite3_backup *pBak;
+ sqlite3_exec(copy, "PRAGMA synchronous = 0", 0, 0, 0);
+ pBak = sqlite3_backup_init(copy, "main", p->db, zName);
+ if( pBak ){
+ sqlite3_backup_step(pBak, -1);
+ rc = sqlite3_backup_finish(pBak);
+ }else{
+ rc = sqlite3_errcode(copy);
+ }
+ sqlite3_close(copy);
+ }
+ sqllogglobal.bRec = 0;
+
+ if( rc==SQLITE_OK ){
+ /* Write an entry into the database index file */
+ FILE *fd = fopen(sqllogglobal.zIdx, "a");
+ if( fd ){
+ fprintf(fd, "%d %s\n", iDb, zFile);
+ fclose(fd);
+ }
+ }else{
+ sqlite3_log(rc, "sqllogCopydb(): error backing up database");
+ }
+ }
+ }
+
+ if( bLog ){
+ zFree = sqlite3_mprintf("ATTACH '%q' AS '%q'; -- clock=%d\n",
+ zInit, zName, sqllogglobal.iClock++
+ );
+ }else{
+ zFree = sqlite3_mprintf("-- Main database is '%q'\n", zInit);
+ }
+ fprintf(p->fd, "%s", zFree);
+ sqlite3_free(zFree);
+
+ sqlite3_free(zInit);
+}
+
+/*
+** If it is not already open, open the log file for connection *p.
+**
+** The SLGlobal.mutex mutex is always held when this function is called.
+*/
+static void sqllogOpenlog(struct SLConn *p){
+ /* If the log file has not yet been opened, open it now. */
+ if( p->fd==0 ){
+ char *zLog;
+
+ /* If it is still NULL, have global.zPrefix point to a copy of
+ ** environment variable $ENVIRONMENT_VARIABLE1_NAME. */
+ if( sqllogglobal.zPrefix[0]==0 ){
+ FILE *fd;
+ char *zVar = getenv(ENVIRONMENT_VARIABLE1_NAME);
+ if( zVar==0 || strlen(zVar)+10>=(sizeof(sqllogglobal.zPrefix)) ) return;
+ sprintf(sqllogglobal.zPrefix, "%s/sqllog_%d", zVar, getProcessId());
+ sprintf(sqllogglobal.zIdx, "%s.idx", sqllogglobal.zPrefix);
+ if( getenv(ENVIRONMENT_VARIABLE2_NAME) ){
+ sqllogglobal.bReuse = atoi(getenv(ENVIRONMENT_VARIABLE2_NAME));
+ }
+ fd = fopen(sqllogglobal.zIdx, "w");
+ if( fd ) fclose(fd);
+ }
+
+ /* Open the log file */
+ zLog = sqlite3_mprintf("%s_%d.sql", sqllogglobal.zPrefix, p->iLog);
+ p->fd = fopen(zLog, "w");
+ sqlite3_free(zLog);
+ if( p->fd==0 ){
+ sqlite3_log(SQLITE_IOERR, "sqllogOpenlog(): Failed to open log file");
+ }
+ }
+}
+
+/*
+** This function is called if the SQLLOG callback is invoked to report
+** execution of an SQL statement. Parameter p is the connection the statement
+** was executed by and parameter zSql is the text of the statement itself.
+*/
+static void testSqllogStmt(struct SLConn *p, const char *zSql){
+ const char *zFirst; /* Pointer to first token in zSql */
+ int nFirst; /* Size of token zFirst in bytes */
+
+ sqllogTokenize(zSql, &zFirst, &nFirst);
+ if( nFirst!=6 || 0!=sqlite3_strnicmp("ATTACH", zFirst, 6) ){
+ /* Not an ATTACH statement. Write this directly to the log. */
+ fprintf(p->fd, "%s; -- clock=%d\n", zSql, sqllogglobal.iClock++);
+ }else{
+ /* This is an ATTACH statement. Copy the database. */
+ sqllogCopydb(p, 0, 1);
+ }
+}
+
+/*
+** The SQLITE_CONFIG_SQLLOG callback registered by sqlite3_init_sqllog().
+**
+** The eType parameter has the following values:
+**
+** 0: Opening a new database connection. zSql is the name of the
+** file being opened. db is a pointer to the newly created database
+** connection.
+**
+** 1: An SQL statement has run to completion. zSql is the text of the
+** SQL statement with all parameters expanded to their actual values.
+**
+** 2: Closing a database connection. zSql is NULL. The db pointer to
+** the database connection being closed has already been shut down
+** and cannot be used for any further SQL.
+**
+** The pCtx parameter is a copy of the pointer that was originally passed
+** into the sqlite3_config(SQLITE_CONFIG_SQLLOG) statement. In this
+** particular implementation, pCtx is always a pointer to the
+** sqllogglobal global variable define above.
+*/
+static void testSqllog(void *pCtx, sqlite3 *db, const char *zSql, int eType){
+ struct SLConn *p = 0;
+ sqlite3_mutex *master = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
+
+ assert( eType==0 || eType==1 || eType==2 );
+ assert( (eType==2)==(zSql==0) );
+
+ /* This is a database open command. */
+ if( eType==0 ){
+ sqlite3_mutex_enter(master);
+ if( sqllogglobal.mutex==0 ){
+ sqllogglobal.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
+ }
+ p = &sqllogglobal.aConn[sqllogglobal.nConn++];
+ p->fd = 0;
+ p->db = db;
+ p->iLog = sqllogglobal.iNextLog++;
+ sqlite3_mutex_leave(master);
+
+ /* Open the log and take a copy of the main database file */
+ sqlite3_mutex_enter(sqllogglobal.mutex);
+ if( sqllogglobal.bRec==0 ){
+ sqllogOpenlog(p);
+ if( p->fd ) sqllogCopydb(p, "main", 0);
+ }
+ sqlite3_mutex_leave(sqllogglobal.mutex);
+ }
+
+ else{
+
+ int i;
+ for(i=0; i<sqllogglobal.nConn; i++){
+ p = &sqllogglobal.aConn[i];
+ if( p->db==db ) break;
+ }
+ if( i==sqllogglobal.nConn ) return;
+
+ /* A database handle close command */
+ if( eType==2 ){
+ sqlite3_mutex_enter(master);
+ if( p->fd ) fclose(p->fd);
+ p->db = 0;
+ p->fd = 0;
+
+ sqllogglobal.nConn--;
+ if( sqllogglobal.nConn==0 ){
+ sqlite3_mutex_free(sqllogglobal.mutex);
+ sqllogglobal.mutex = 0;
+ }else{
+ int nShift = &sqllogglobal.aConn[sqllogglobal.nConn] - p;
+ if( nShift>0 ){
+ memmove(p, &p[1], nShift*sizeof(struct SLConn));
+ }
+ }
+ sqlite3_mutex_leave(master);
+
+ /* An ordinary SQL command. */
+ }else if( p->fd ){
+ sqlite3_mutex_enter(sqllogglobal.mutex);
+ if( sqllogglobal.bRec==0 ){
+ testSqllogStmt(p, zSql);
+ }
+ sqlite3_mutex_leave(sqllogglobal.mutex);
+ }
+ }
+}
+
+/*
+** This function is called either before sqlite3_initialized() or by it.
+** It checks if the SQLITE_SQLLOG_DIR variable is defined, and if so
+** registers an SQLITE_CONFIG_SQLLOG callback to record the applications
+** database activity.
+*/
+void sqlite3_init_sqllog(void){
+ if( getenv(ENVIRONMENT_VARIABLE1_NAME) ){
+ if( SQLITE_OK==sqlite3_config(SQLITE_CONFIG_SQLLOG, testSqllog, 0) ){
+ memset(&sqllogglobal, 0, sizeof(sqllogglobal));
+ sqllogglobal.bReuse = 1;
+ }
+ }
+}
diff --git a/src/test_syscall.c b/src/test_syscall.c
index d484f22..7c0873c 100644
--- a/src/test_syscall.c
+++ b/src/test_syscall.c
@@ -23,7 +23,7 @@
**
** open close access getcwd stat fstat
** ftruncate fcntl read pread pread64 write
-** pwrite pwrite64 fchmod fallocate
+** pwrite pwrite64 fchmod fallocate mmap
**
** test_syscall uninstall
** Uninstall all wrapper functions.
@@ -69,18 +69,19 @@
** the xNextSystemCall() VFS method.
*/
+#include "sqliteInt.h"
#include "sqlite3.h"
#include "tcl.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
-#include "sqliteInt.h"
#if SQLITE_OS_UNIX
-/* From test1.c */
-extern const char *sqlite3TestErrorName(int);
+/* From main.c */
+extern const char *sqlite3ErrName(int);
+#include <sys/mman.h>
#include <sys/types.h>
#include <errno.h>
@@ -106,7 +107,8 @@ static int ts_pwrite(int fd, const void *aBuf, size_t nBuf, off_t off);
static int ts_pwrite64(int fd, const void *aBuf, size_t nBuf, off_t off);
static int ts_fchmod(int fd, mode_t mode);
static int ts_fallocate(int fd, off_t off, off_t len);
-
+static void *ts_mmap(void *, size_t, int, int, int, off_t);
+static void *ts_mremap(void*, size_t, size_t, int, ...);
struct TestSyscallArray {
const char *zName;
@@ -131,6 +133,8 @@ struct TestSyscallArray {
/* 13 */ { "pwrite64", (sqlite3_syscall_ptr)ts_pwrite64, 0, 0, 0 },
/* 14 */ { "fchmod", (sqlite3_syscall_ptr)ts_fchmod, 0, 0, 0 },
/* 15 */ { "fallocate", (sqlite3_syscall_ptr)ts_fallocate, 0, 0, 0 },
+ /* 16 */ { "mmap", (sqlite3_syscall_ptr)ts_mmap, 0, 0, 0 },
+ /* 17 */ { "mremap", (sqlite3_syscall_ptr)ts_mremap, 0, 0, 0 },
{ 0, 0, 0, 0, 0 }
};
@@ -152,6 +156,8 @@ struct TestSyscallArray {
aSyscall[13].xOrig)
#define orig_fchmod ((int(*)(int,mode_t))aSyscall[14].xOrig)
#define orig_fallocate ((int(*)(int,off_t,off_t))aSyscall[15].xOrig)
+#define orig_mmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[16].xOrig)
+#define orig_mremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[17].xOrig)
/*
** This function is called exactly once from within each invocation of a
@@ -377,6 +383,31 @@ static int ts_fallocate(int fd, off_t off, off_t len){
return orig_fallocate(fd, off, len);
}
+static void *ts_mmap(
+ void *pAddr,
+ size_t nByte,
+ int prot,
+ int flags,
+ int fd,
+ off_t iOff
+){
+ if( tsIsFailErrno("mmap") ){
+ return MAP_FAILED;
+ }
+ return orig_mmap(pAddr, nByte, prot, flags, fd, iOff);
+}
+
+static void *ts_mremap(void *a, size_t b, size_t c, int d, ...){
+ va_list ap;
+ void *pArg;
+ if( tsIsFailErrno("mremap") ){
+ return MAP_FAILED;
+ }
+ va_start(ap, d);
+ pArg = va_arg(ap, void *);
+ return orig_mremap(a, b, c, d, pArg);
+}
+
static int test_syscall_install(
void * clientData,
Tcl_Interp *interp,
@@ -467,7 +498,7 @@ static int test_syscall_reset(
}
}
if( rc!=SQLITE_OK ){
- Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3TestErrorName(rc), -1));
+ Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
return TCL_ERROR;
}
diff --git a/src/test_thread.c b/src/test_thread.c
index ae62de8..2f9363b 100644
--- a/src/test_thread.c
+++ b/src/test_thread.c
@@ -60,12 +60,14 @@ static Tcl_ObjCmdProc blocking_prepare_v2_proc;
int Sqlitetest1_Init(Tcl_Interp *);
int Sqlite3_Init(Tcl_Interp *);
+/* Functions from main.c */
+extern const char *sqlite3ErrName(int);
+
/* Functions from test1.c */
-void *sqlite3TestTextToPtr(const char *);
-const char *sqlite3TestErrorName(int);
-int getDbPointer(Tcl_Interp *, const char *, sqlite3 **);
-int sqlite3TestMakePointerStr(Tcl_Interp *, char *, void *);
-int sqlite3TestErrCode(Tcl_Interp *, sqlite3 *, int);
+extern void *sqlite3TestTextToPtr(const char *);
+extern int getDbPointer(Tcl_Interp *, const char *, sqlite3 **);
+extern int sqlite3TestMakePointerStr(Tcl_Interp *, char *, void *);
+extern int sqlite3TestErrCode(Tcl_Interp *, sqlite3 *, int);
/*
** Handler for events of type EvalEvent.
@@ -559,7 +561,7 @@ static int blocking_step_proc(
pStmt = (sqlite3_stmt*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
rc = sqlite3_blocking_step(pStmt);
- Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), 0);
+ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), 0);
return TCL_OK;
}
@@ -606,7 +608,7 @@ static int blocking_prepare_v2_proc(
}
if( rc!=SQLITE_OK ){
assert( pStmt==0 );
- sprintf(zBuf, "%s ", (char *)sqlite3TestErrorName(rc));
+ sprintf(zBuf, "%s ", (char *)sqlite3ErrName(rc));
Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
return TCL_ERROR;
}
diff --git a/src/test_vfs.c b/src/test_vfs.c
index 93c556b..fcd5774 100644
--- a/src/test_vfs.c
+++ b/src/test_vfs.c
@@ -125,8 +125,9 @@ struct Testvfs {
#define TESTVFS_ACCESS_MASK 0x00004000
#define TESTVFS_FULLPATHNAME_MASK 0x00008000
#define TESTVFS_READ_MASK 0x00010000
+#define TESTVFS_UNLOCK_MASK 0x00020000
-#define TESTVFS_ALL_MASK 0x0001FFFF
+#define TESTVFS_ALL_MASK 0x0003FFFF
#define TESTVFS_MAX_PAGES 1024
@@ -265,7 +266,8 @@ static void tvfsExecTcl(
const char *zMethod,
Tcl_Obj *arg1,
Tcl_Obj *arg2,
- Tcl_Obj *arg3
+ Tcl_Obj *arg3,
+ Tcl_Obj *arg4
){
int rc; /* Return code from Tcl_EvalObj() */
Tcl_Obj *pEval;
@@ -282,6 +284,7 @@ static void tvfsExecTcl(
if( arg1 ) Tcl_ListObjAppendElement(p->interp, pEval, arg1);
if( arg2 ) Tcl_ListObjAppendElement(p->interp, pEval, arg2);
if( arg3 ) Tcl_ListObjAppendElement(p->interp, pEval, arg3);
+ if( arg4 ) Tcl_ListObjAppendElement(p->interp, pEval, arg4);
rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL);
if( rc!=TCL_OK ){
@@ -302,7 +305,7 @@ static int tvfsClose(sqlite3_file *pFile){
if( p->pScript && p->mask&TESTVFS_CLOSE_MASK ){
tvfsExecTcl(p, "xClose",
- Tcl_NewStringObj(pFd->zFilename, -1), pFd->pShmId, 0
+ Tcl_NewStringObj(pFd->zFilename, -1), pFd->pShmId, 0, 0
);
}
@@ -333,7 +336,7 @@ static int tvfsRead(
Testvfs *p = (Testvfs *)pFd->pVfs->pAppData;
if( p->pScript && p->mask&TESTVFS_READ_MASK ){
tvfsExecTcl(p, "xRead",
- Tcl_NewStringObj(pFd->zFilename, -1), pFd->pShmId, 0
+ Tcl_NewStringObj(pFd->zFilename, -1), pFd->pShmId, 0, 0
);
tvfsResultCode(p, &rc);
}
@@ -362,7 +365,7 @@ static int tvfsWrite(
if( p->pScript && p->mask&TESTVFS_WRITE_MASK ){
tvfsExecTcl(p, "xWrite",
Tcl_NewStringObj(pFd->zFilename, -1), pFd->pShmId,
- Tcl_NewWideIntObj(iOfst)
+ Tcl_NewWideIntObj(iOfst), Tcl_NewIntObj(iAmt)
);
tvfsResultCode(p, &rc);
}
@@ -390,7 +393,7 @@ static int tvfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
if( p->pScript && p->mask&TESTVFS_TRUNCATE_MASK ){
tvfsExecTcl(p, "xTruncate",
- Tcl_NewStringObj(pFd->zFilename, -1), pFd->pShmId, 0
+ Tcl_NewStringObj(pFd->zFilename, -1), pFd->pShmId, 0, 0
);
tvfsResultCode(p, &rc);
}
@@ -431,7 +434,7 @@ static int tvfsSync(sqlite3_file *pFile, int flags){
tvfsExecTcl(p, "xSync",
Tcl_NewStringObj(pFd->zFilename, -1), pFd->pShmId,
- Tcl_NewStringObj(zFlags, -1)
+ Tcl_NewStringObj(zFlags, -1), 0
);
tvfsResultCode(p, &rc);
}
@@ -465,8 +468,12 @@ static int tvfsLock(sqlite3_file *pFile, int eLock){
** Unlock an tvfs-file.
*/
static int tvfsUnlock(sqlite3_file *pFile, int eLock){
- TestvfsFd *p = tvfsGetFd(pFile);
- return sqlite3OsUnlock(p->pReal, eLock);
+ TestvfsFd *pFd = tvfsGetFd(pFile);
+ Testvfs *p = (Testvfs *)pFd->pVfs->pAppData;
+ if( p->mask&TESTVFS_WRITE_MASK && tvfsInjectIoerr(p) ){
+ return SQLITE_IOERR_UNLOCK;
+ }
+ return sqlite3OsUnlock(pFd->pReal, eLock);
}
/*
@@ -578,7 +585,7 @@ static int tvfsOpen(
z += strlen(z) + 1;
}
}
- tvfsExecTcl(p, "xOpen", Tcl_NewStringObj(pFd->zFilename, -1), pArg, 0);
+ tvfsExecTcl(p, "xOpen", Tcl_NewStringObj(pFd->zFilename, -1), pArg, 0, 0);
Tcl_DecrRefCount(pArg);
if( tvfsResultCode(p, &rc) ){
if( rc!=SQLITE_OK ) return rc;
@@ -635,7 +642,7 @@ static int tvfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
if( p->pScript && p->mask&TESTVFS_DELETE_MASK ){
tvfsExecTcl(p, "xDelete",
- Tcl_NewStringObj(zPath, -1), Tcl_NewIntObj(dirSync), 0
+ Tcl_NewStringObj(zPath, -1), Tcl_NewIntObj(dirSync), 0, 0
);
tvfsResultCode(p, &rc);
}
@@ -663,7 +670,7 @@ static int tvfsAccess(
if( flags==SQLITE_ACCESS_READWRITE ) zArg = "SQLITE_ACCESS_READWRITE";
if( flags==SQLITE_ACCESS_READ ) zArg = "SQLITE_ACCESS_READ";
tvfsExecTcl(p, "xAccess",
- Tcl_NewStringObj(zPath, -1), Tcl_NewStringObj(zArg, -1), 0
+ Tcl_NewStringObj(zPath, -1), Tcl_NewStringObj(zArg, -1), 0, 0
);
if( tvfsResultCode(p, &rc) ){
if( rc!=SQLITE_OK ) return rc;
@@ -691,7 +698,7 @@ static int tvfsFullPathname(
Testvfs *p = (Testvfs *)pVfs->pAppData;
if( p->pScript && p->mask&TESTVFS_FULLPATHNAME_MASK ){
int rc;
- tvfsExecTcl(p, "xFullPathname", Tcl_NewStringObj(zPath, -1), 0, 0);
+ tvfsExecTcl(p, "xFullPathname", Tcl_NewStringObj(zPath, -1), 0, 0, 0);
if( tvfsResultCode(p, &rc) ){
if( rc!=SQLITE_OK ) return rc;
}
@@ -771,7 +778,7 @@ static int tvfsShmOpen(sqlite3_file *pFile){
*/
Tcl_ResetResult(p->interp);
if( p->pScript && p->mask&TESTVFS_SHMOPEN_MASK ){
- tvfsExecTcl(p, "xShmOpen", Tcl_NewStringObj(pFd->zFilename, -1), 0, 0);
+ tvfsExecTcl(p, "xShmOpen", Tcl_NewStringObj(pFd->zFilename, -1), 0, 0, 0);
if( tvfsResultCode(p, &rc) ){
if( rc!=SQLITE_OK ) return rc;
}
@@ -841,7 +848,7 @@ static int tvfsShmMap(
Tcl_ListObjAppendElement(p->interp, pArg, Tcl_NewIntObj(pgsz));
Tcl_ListObjAppendElement(p->interp, pArg, Tcl_NewIntObj(isWrite));
tvfsExecTcl(p, "xShmMap",
- Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, pArg
+ Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, pArg, 0
);
tvfsResultCode(p, &rc);
Tcl_DecrRefCount(pArg);
@@ -891,7 +898,7 @@ static int tvfsShmLock(
}
tvfsExecTcl(p, "xShmLock",
Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId,
- Tcl_NewStringObj(zLock, -1)
+ Tcl_NewStringObj(zLock, -1), 0
);
tvfsResultCode(p, &rc);
}
@@ -937,7 +944,7 @@ static void tvfsShmBarrier(sqlite3_file *pFile){
if( p->pScript && p->mask&TESTVFS_SHMBARRIER_MASK ){
tvfsExecTcl(p, "xShmBarrier",
- Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0
+ Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0, 0
);
}
}
@@ -961,7 +968,7 @@ static int tvfsShmUnmap(
if( p->pScript && p->mask&TESTVFS_SHMCLOSE_MASK ){
tvfsExecTcl(p, "xShmUnmap",
- Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0
+ Tcl_NewStringObj(pFd->pShm->zFile, -1), pFd->pShmId, 0, 0
);
tvfsResultCode(p, &rc);
}
@@ -1099,6 +1106,7 @@ static int testvfs_obj_cmd(
{ "xClose", TESTVFS_CLOSE_MASK },
{ "xAccess", TESTVFS_ACCESS_MASK },
{ "xFullPathname", TESTVFS_FULLPATHNAME_MASK },
+ { "xUnlock", TESTVFS_UNLOCK_MASK },
};
Tcl_Obj **apElem = 0;
int nElem = 0;
diff --git a/src/test_vfstrace.c b/src/test_vfstrace.c
index d2f7455..0aacc01 100644
--- a/src/test_vfstrace.c
+++ b/src/test_vfstrace.c
@@ -475,6 +475,7 @@ static int vfstraceFileControl(sqlite3_file *pFile, int op, void *pArg){
case SQLITE_FCNTL_PERSIST_WAL: zOp = "PERSIST_WAL"; break;
case SQLITE_FCNTL_OVERWRITE: zOp = "OVERWRITE"; break;
case SQLITE_FCNTL_VFSNAME: zOp = "VFSNAME"; break;
+ case SQLITE_FCNTL_TEMPFILENAME: zOp = "TEMPFILENAME"; break;
case 0xca093fa0: zOp = "DB_UNCHANGED"; break;
case SQLITE_FCNTL_PRAGMA: {
const char *const* a = (const char*const*)pArg;
@@ -496,7 +497,8 @@ static int vfstraceFileControl(sqlite3_file *pFile, int op, void *pArg){
*(char**)pArg = sqlite3_mprintf("vfstrace.%s/%z",
pInfo->zVfsName, *(char**)pArg);
}
- if( op==SQLITE_FCNTL_PRAGMA && rc==SQLITE_OK && *(char**)pArg ){
+ if( (op==SQLITE_FCNTL_PRAGMA || op==SQLITE_FCNTL_TEMPFILENAME)
+ && rc==SQLITE_OK && *(char**)pArg ){
vfstrace_printf(pInfo, "%s.xFileControl(%s,%s) returns %s",
pInfo->zVfsName, p->zFName, zOp, *(char**)pArg);
}
diff --git a/src/trigger.c b/src/trigger.c
index 8985ec6..f1ff766 100644
--- a/src/trigger.c
+++ b/src/trigger.c
@@ -729,6 +729,15 @@ static int codeTriggerProgram(
*/
pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
+ /* Clear the cookieGoto flag. When coding triggers, the cookieGoto
+ ** variable is used as a flag to indicate to sqlite3ExprCodeConstants()
+ ** that it is not safe to refactor constants (this happens after the
+ ** start of the first loop in the SQL statement is coded - at that
+ ** point code may be conditionally executed, so it is no longer safe to
+ ** initialize constant register values). */
+ assert( pParse->cookieGoto==0 || pParse->cookieGoto==-1 );
+ pParse->cookieGoto = 0;
+
switch( pStep->op ){
case TK_UPDATE: {
sqlite3Update(pParse,
diff --git a/src/update.c b/src/update.c
index 96ba4df..3ab1ab2 100644
--- a/src/update.c
+++ b/src/update.c
@@ -208,6 +208,7 @@ void sqlite3Update(
}
if( j>=pTab->nCol ){
if( sqlite3IsRowid(pChanges->a[i].zName) ){
+ j = -1;
chngRowid = 1;
pRowidExpr = pChanges->a[i].pExpr;
}else{
@@ -220,7 +221,8 @@ void sqlite3Update(
{
int rc;
rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
- pTab->aCol[j].zName, db->aDb[iDb].zName);
+ j<0 ? "ROWID" : pTab->aCol[j].zName,
+ db->aDb[iDb].zName);
if( rc==SQLITE_DENY ){
goto update_cleanup;
}else if( rc==SQLITE_IGNORE ){
@@ -458,7 +460,7 @@ void sqlite3Update(
/* The row-trigger may have deleted the row being updated. In this
** case, jump to the next row. No updates or AFTER triggers are
- ** required. This behaviour - what happens when the row being updated
+ ** required. This behavior - what happens when the row being updated
** is deleted or renamed by a BEFORE trigger - is left undefined in the
** documentation.
*/
diff --git a/src/utf.c b/src/utf.c
index e94815b..6d5b1bf 100644
--- a/src/utf.c
+++ b/src/utf.c
@@ -164,25 +164,23 @@ static const unsigned char sqlite3Utf8Trans1[] = {
|| (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
}
u32 sqlite3Utf8Read(
- const unsigned char *zIn, /* First byte of UTF-8 character */
- const unsigned char **pzNext /* Write first byte past UTF-8 char here */
+ const unsigned char **pz /* Pointer to string from which to read char */
){
unsigned int c;
/* Same as READ_UTF8() above but without the zTerm parameter.
** For this routine, we assume the UTF8 string is always zero-terminated.
*/
- c = *(zIn++);
+ c = *((*pz)++);
if( c>=0xc0 ){
c = sqlite3Utf8Trans1[c-0xc0];
- while( (*zIn & 0xc0)==0x80 ){
- c = (c<<6) + (0x3f & *(zIn++));
+ while( (*(*pz) & 0xc0)==0x80 ){
+ c = (c<<6) + (0x3f & *((*pz)++));
}
if( c<0x80
|| (c&0xFFFFF800)==0xD800
|| (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; }
}
- *pzNext = zIn;
return c;
}
@@ -283,7 +281,6 @@ int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
if( desiredEnc==SQLITE_UTF16LE ){
/* UTF-8 -> UTF-16 Little-endian */
while( zIn<zTerm ){
- /* c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); */
READ_UTF8(zIn, zTerm, c);
WRITE_UTF16LE(z, c);
}
@@ -291,7 +288,6 @@ int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
assert( desiredEnc==SQLITE_UTF16BE );
/* UTF-8 -> UTF-16 Big-endian */
while( zIn<zTerm ){
- /* c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); */
READ_UTF8(zIn, zTerm, c);
WRITE_UTF16BE(z, c);
}
@@ -419,7 +415,7 @@ int sqlite3Utf8To8(unsigned char *zIn){
u32 c;
while( zIn[0] && zOut<=zIn ){
- c = sqlite3Utf8Read(zIn, (const u8**)&zIn);
+ c = sqlite3Utf8Read((const u8**)&zIn);
if( c!=0xfffd ){
WRITE_UTF8(zOut, c);
}
@@ -524,7 +520,7 @@ void sqlite3UtfSelfTest(void){
assert( n>0 && n<=4 );
z[0] = 0;
z = zBuf;
- c = sqlite3Utf8Read(z, (const u8**)&z);
+ c = sqlite3Utf8Read((const u8**)&z);
t = i;
if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
diff --git a/src/util.c b/src/util.c
index 5cf8eba..d83a630 100644
--- a/src/util.c
+++ b/src/util.c
@@ -261,7 +261,7 @@ int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
*/
int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
#ifndef SQLITE_OMIT_FLOATING_POINT
- int incr = (enc==SQLITE_UTF8?1:2);
+ int incr;
const char *zEnd = z + length;
/* sign * significand * (10 ^ (esign * exponent)) */
int sign = 1; /* sign of significand */
@@ -272,10 +272,22 @@ int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
int eValid = 1; /* True exponent is either not used or is well-formed */
double result;
int nDigits = 0;
+ int nonNum = 0;
+ assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
*pResult = 0.0; /* Default return value, in case of an error */
- if( enc==SQLITE_UTF16BE ) z++;
+ if( enc==SQLITE_UTF8 ){
+ incr = 1;
+ }else{
+ int i;
+ incr = 2;
+ assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
+ for(i=3-enc; i<length && z[i]==0; i+=2){}
+ nonNum = i<length;
+ zEnd = z+i+enc-3;
+ z += (enc&1);
+ }
/* skip leading spaces */
while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
@@ -408,7 +420,7 @@ do_atof_calc:
*pResult = result;
/* return true if number and no extra non-whitespace chracters after */
- return z>=zEnd && nDigits>0 && eValid;
+ return z>=zEnd && nDigits>0 && eValid && nonNum==0;
#else
return !sqlite3Atoi64(z, pResult, length, enc);
#endif /* SQLITE_OMIT_FLOATING_POINT */
@@ -457,21 +469,33 @@ static int compare2pow63(const char *zNum, int incr){
** signed 64-bit integer, its negative -9223372036854665808 can be.
**
** If zNum is too big for a 64-bit integer and is not
-** 9223372036854665808 then return 1.
+** 9223372036854665808 or if zNum contains any non-numeric text,
+** then return 1.
**
** length is the number of bytes in the string (bytes, not characters).
** The string is not necessarily zero-terminated. The encoding is
** given by enc.
*/
int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
- int incr = (enc==SQLITE_UTF8?1:2);
+ int incr;
u64 u = 0;
int neg = 0; /* assume positive */
int i;
int c = 0;
+ int nonNum = 0;
const char *zStart;
const char *zEnd = zNum + length;
- if( enc==SQLITE_UTF16BE ) zNum++;
+ assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
+ if( enc==SQLITE_UTF8 ){
+ incr = 1;
+ }else{
+ incr = 2;
+ assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
+ for(i=3-enc; i<length && zNum[i]==0; i+=2){}
+ nonNum = i<length;
+ zEnd = zNum+i+enc-3;
+ zNum += (enc&1);
+ }
while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
if( zNum<zEnd ){
if( *zNum=='-' ){
@@ -496,7 +520,7 @@ int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
testcase( i==18 );
testcase( i==19 );
testcase( i==20 );
- if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr ){
+ if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr || nonNum ){
/* zNum is empty or contains non-numeric text or is longer
** than 19 digits (thus guaranteeing that it is too large) */
return 1;
diff --git a/src/vacuum.c b/src/vacuum.c
index 401d41d..4afb2cc 100644
--- a/src/vacuum.c
+++ b/src/vacuum.c
@@ -85,6 +85,7 @@ void sqlite3Vacuum(Parse *pParse){
Vdbe *v = sqlite3GetVdbe(pParse);
if( v ){
sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0);
+ sqlite3VdbeUsesBtree(v, 0);
}
return;
}
@@ -288,6 +289,7 @@ int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
BTREE_DEFAULT_CACHE_SIZE, 0, /* Preserve the default page cache size */
BTREE_TEXT_ENCODING, 0, /* Preserve the text encoding */
BTREE_USER_VERSION, 0, /* Preserve the user version */
+ BTREE_APPLICATION_ID, 0, /* Preserve the application id */
};
assert( 1==sqlite3BtreeIsInTrans(pTemp) );
diff --git a/src/vdbe.c b/src/vdbe.c
index 1a3c412..f343e13 100644
--- a/src/vdbe.c
+++ b/src/vdbe.c
@@ -152,11 +152,7 @@ int sqlite3_found_count = 0;
&& sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
-#ifdef SQLITE_OMIT_MERGE_SORT
-# define isSorter(x) 0
-#else
# define isSorter(x) ((x)->pSorter!=0)
-#endif
/*
** Argument pMem points at a register that will be passed to a
@@ -422,7 +418,9 @@ void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
** Print the value of a register for tracing purposes:
*/
static void memTracePrint(FILE *out, Mem *p){
- if( p->flags & MEM_Null ){
+ if( p->flags & MEM_Invalid ){
+ fprintf(out, " undefined");
+ }else if( p->flags & MEM_Null ){
fprintf(out, " NULL");
}else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
fprintf(out, " si:%lld", p->u.i);
@@ -867,7 +865,7 @@ case OP_Halt: {
if( rc==SQLITE_BUSY ){
p->rc = rc = SQLITE_BUSY;
}else{
- assert( rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT );
+ assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
assert( rc==SQLITE_OK || db->nDeferredCons>0 );
rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
}
@@ -956,23 +954,28 @@ case OP_String: { /* out2-prerelease */
break;
}
-/* Opcode: Null * P2 P3 * *
+/* Opcode: Null P1 P2 P3 * *
**
** Write a NULL into registers P2. If P3 greater than P2, then also write
-** NULL into register P3 and ever register in between P2 and P3. If P3
+** NULL into register P3 and every register in between P2 and P3. If P3
** is less than P2 (typically P3 is zero) then only register P2 is
-** set to NULL
+** set to NULL.
+**
+** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
+** NULL values will not compare equal even if SQLITE_NULLEQ is set on
+** OP_Ne or OP_Eq.
*/
case OP_Null: { /* out2-prerelease */
int cnt;
+ u16 nullFlag;
cnt = pOp->p3-pOp->p2;
assert( pOp->p3<=p->nMem );
- pOut->flags = MEM_Null;
+ pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
while( cnt>0 ){
pOut++;
memAboutToChange(p, pOut);
VdbeMemRelease(pOut);
- pOut->flags = MEM_Null;
+ pOut->flags = nullFlag;
cnt--;
}
break;
@@ -1015,10 +1018,10 @@ case OP_Variable: { /* out2-prerelease */
/* Opcode: Move P1 P2 P3 * *
**
-** Move the values in register P1..P1+P3-1 over into
-** registers P2..P2+P3-1. Registers P1..P1+P1-1 are
+** Move the values in register P1..P1+P3 over into
+** registers P2..P2+P3. Registers P1..P1+P3 are
** left holding a NULL. It is an error for register ranges
-** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
+** P1..P1+P3 and P2..P2+P3 to overlap.
*/
case OP_Move: {
char *zMalloc; /* Holding variable for allocated memory */
@@ -1026,7 +1029,7 @@ case OP_Move: {
int p1; /* Register to copy from */
int p2; /* Register to copy to */
- n = pOp->p3;
+ n = pOp->p3 + 1;
p1 = pOp->p1;
p2 = pOp->p2;
assert( n>0 && p1>0 && p2>0 );
@@ -1055,20 +1058,31 @@ case OP_Move: {
break;
}
-/* Opcode: Copy P1 P2 * * *
+/* Opcode: Copy P1 P2 P3 * *
**
-** Make a copy of register P1 into register P2.
+** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
**
** This instruction makes a deep copy of the value. A duplicate
** is made of any string or blob constant. See also OP_SCopy.
*/
-case OP_Copy: { /* in1, out2 */
+case OP_Copy: {
+ int n;
+
+ n = pOp->p3;
pIn1 = &aMem[pOp->p1];
pOut = &aMem[pOp->p2];
assert( pOut!=pIn1 );
- sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
- Deephemeralize(pOut);
- REGISTER_TRACE(pOp->p2, pOut);
+ while( 1 ){
+ sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
+ Deephemeralize(pOut);
+#ifdef SQLITE_DEBUG
+ pOut->pScopyFrom = 0;
+#endif
+ REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
+ if( (n--)==0 ) break;
+ pOut++;
+ pIn1++;
+ }
break;
}
@@ -1252,6 +1266,7 @@ case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
+ char bIntint; /* Started out as two integer operands */
int flags; /* Combined MEM_* flags from both inputs */
i64 iA; /* Integer value of left operand */
i64 iB; /* Integer value of right operand */
@@ -1268,6 +1283,7 @@ case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
iA = pIn1->u.i;
iB = pIn2->u.i;
+ bIntint = 1;
switch( pOp->opcode ){
case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
@@ -1288,6 +1304,7 @@ case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
pOut->u.i = iB;
MemSetTypeFlag(pOut, MEM_Int);
}else{
+ bIntint = 0;
fp_math:
rA = sqlite3VdbeRealValue(pIn1);
rB = sqlite3VdbeRealValue(pIn2);
@@ -1319,7 +1336,7 @@ fp_math:
}
pOut->r = rB;
MemSetTypeFlag(pOut, MEM_Real);
- if( (flags & MEM_Real)==0 ){
+ if( (flags & MEM_Real)==0 && !bIntint ){
sqlite3VdbeIntegerAffinity(pOut);
}
#endif
@@ -1737,6 +1754,10 @@ case OP_ToReal: { /* same as TK_TO_REAL, in1 */
**
** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
** store a boolean result (either 0, or 1, or NULL) in register P2.
+**
+** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
+** equal to one another, provided that they do not have their MEM_Cleared
+** bit set.
*/
/* Opcode: Ne P1 P2 P3 P4 P5
**
@@ -1803,7 +1824,15 @@ case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
** or not both operands are null.
*/
assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
- res = (flags1 & flags3 & MEM_Null)==0;
+ assert( (flags1 & MEM_Cleared)==0 );
+ if( (flags1&MEM_Null)!=0
+ && (flags3&MEM_Null)!=0
+ && (flags3&MEM_Cleared)==0
+ ){
+ res = 0; /* Results are equal */
+ }else{
+ res = 1; /* Results are not equal */
+ }
}else{
/* SQLITE_NULLEQ is clear and at least one operand is NULL,
** then the result is always NULL.
@@ -1862,9 +1891,9 @@ case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
** Set the permutation used by the OP_Compare operator to be the array
** of integers in P4.
**
-** The permutation is only valid until the next OP_Permutation, OP_Compare,
-** OP_Halt, or OP_ResultRow. Typically the OP_Permutation should occur
-** immediately prior to the OP_Compare.
+** The permutation is only valid until the next OP_Compare that has
+** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
+** occur immediately prior to the OP_Compare.
*/
case OP_Permutation: {
assert( pOp->p4type==P4_INTARRAY );
@@ -1873,12 +1902,17 @@ case OP_Permutation: {
break;
}
-/* Opcode: Compare P1 P2 P3 P4 *
+/* Opcode: Compare P1 P2 P3 P4 P5
**
** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
** the comparison for use by the next OP_Jump instruct.
**
+** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
+** determined by the most recent OP_Permutation operator. If the
+** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
+** order.
+**
** P4 is a KeyInfo structure that defines collating sequences and sort
** orders for the comparison. The permutation applies to registers
** only. The KeyInfo elements are used sequentially.
@@ -1897,6 +1931,7 @@ case OP_Compare: {
CollSeq *pColl; /* Collating sequence to use on this term */
int bRev; /* True for DESCENDING sort order */
+ if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
n = pOp->p3;
pKeyInfo = pOp->p4.pKeyInfo;
assert( n>0 );
@@ -2040,8 +2075,6 @@ case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
**
** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
** set the flag and fall through to the next instruction.
-**
-** See also: JumpOnce
*/
case OP_Once: { /* jump */
assert( pOp->p1<p->nOnceFlag );
@@ -2212,6 +2245,11 @@ case OP_Column: {
}
}else if( ALWAYS(pC->pseudoTableReg>0) ){
pReg = &aMem[pC->pseudoTableReg];
+ if( pC->multiPseudo ){
+ sqlite3VdbeMemShallowCopy(pDest, pReg+p2, MEM_Ephem);
+ Deephemeralize(pDest);
+ goto op_column_out;
+ }
assert( pReg->flags & MEM_Blob );
assert( memIsValid(pReg) );
payloadSize = pReg->n;
@@ -3270,7 +3308,7 @@ case OP_OpenEphemeral: {
break;
}
-/* Opcode: OpenSorter P1 P2 * P4 *
+/* Opcode: SorterOpen P1 P2 * P4 *
**
** This opcode works like OP_OpenEphemeral except that it opens
** a transient index that is specifically designed to sort large
@@ -3278,26 +3316,23 @@ case OP_OpenEphemeral: {
*/
case OP_SorterOpen: {
VdbeCursor *pCx;
-#ifndef SQLITE_OMIT_MERGE_SORT
+
pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
if( pCx==0 ) goto no_mem;
pCx->pKeyInfo = pOp->p4.pKeyInfo;
pCx->pKeyInfo->enc = ENC(p->db);
pCx->isSorter = 1;
rc = sqlite3VdbeSorterInit(db, pCx);
-#else
- pOp->opcode = OP_OpenEphemeral;
- pc--;
-#endif
break;
}
-/* Opcode: OpenPseudo P1 P2 P3 * *
+/* Opcode: OpenPseudo P1 P2 P3 * P5
**
** Open a new cursor that points to a fake table that contains a single
** row of data. The content of that one row in the content of memory
-** register P2. In other words, cursor P1 becomes an alias for the
-** MEM_Blob content contained in register P2.
+** register P2 when P5==0. In other words, cursor P1 becomes an alias for the
+** MEM_Blob content contained in register P2. When P5==1, then the
+** row is represented by P3 consecutive registers beginning with P2.
**
** A pseudo-table created by this opcode is used to hold a single
** row output from the sorter so that the row can be decomposed into
@@ -3317,6 +3352,7 @@ case OP_OpenPseudo: {
pCx->pseudoTableReg = pOp->p2;
pCx->isTable = 1;
pCx->isIndex = 0;
+ pCx->multiPseudo = pOp->p5;
break;
}
@@ -3479,7 +3515,7 @@ case OP_SeekGt: { /* jump, in3 */
** r.flags = 0;
** }
*/
- r.flags = (u16)(UNPACKED_INCRKEY * (1 & (oc - OP_SeekLt)));
+ r.flags = (u8)(UNPACKED_INCRKEY * (1 & (oc - OP_SeekLt)));
assert( oc!=OP_SeekGt || r.flags==UNPACKED_INCRKEY );
assert( oc!=OP_SeekLe || r.flags==UNPACKED_INCRKEY );
assert( oc!=OP_SeekGe || r.flags==0 );
@@ -4168,15 +4204,11 @@ case OP_SorterCompare: {
*/
case OP_SorterData: {
VdbeCursor *pC;
-#ifndef SQLITE_OMIT_MERGE_SORT
+
pOut = &aMem[pOp->p2];
pC = p->apCsr[pOp->p1];
assert( pC->isSorter );
rc = sqlite3VdbeSorterRowkey(pC, pOut);
-#else
- pOp->opcode = OP_RowKey;
- pc--;
-#endif
break;
}
@@ -4280,7 +4312,7 @@ case OP_Rowid: { /* out2-prerelease */
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
pC = p->apCsr[pOp->p1];
assert( pC!=0 );
- assert( pC->pseudoTableReg==0 );
+ assert( pC->pseudoTableReg==0 || pC->nullRow );
if( pC->nullRow ){
pOut->flags = MEM_Null;
break;
@@ -4375,9 +4407,6 @@ case OP_Last: { /* jump */
** correctly optimizing out sorts.
*/
case OP_SorterSort: /* jump */
-#ifdef SQLITE_OMIT_MERGE_SORT
- pOp->opcode = OP_Sort;
-#endif
case OP_Sort: { /* jump */
#ifdef SQLITE_TEST
sqlite3_sort_count++;
@@ -4456,9 +4485,6 @@ case OP_Rewind: { /* jump */
** number P5-1 in the prepared statement is incremented.
*/
case OP_SorterNext: /* jump */
-#ifdef SQLITE_OMIT_MERGE_SORT
- pOp->opcode = OP_Next;
-#endif
case OP_Prev: /* jump */
case OP_Next: { /* jump */
VdbeCursor *pC;
@@ -4509,9 +4535,6 @@ case OP_Next: { /* jump */
** for tables is OP_Insert.
*/
case OP_SorterInsert: /* in2 */
-#ifdef SQLITE_OMIT_MERGE_SORT
- pOp->opcode = OP_IdxInsert;
-#endif
case OP_IdxInsert: { /* in2 */
VdbeCursor *pC;
BtCursor *pCrsr;
@@ -4706,6 +4729,7 @@ case OP_Destroy: { /* out2-prerelease */
int iCnt;
Vdbe *pVdbe;
int iDb;
+
#ifndef SQLITE_OMIT_VIRTUALTABLE
iCnt = 0;
for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){
@@ -5495,7 +5519,9 @@ case OP_JournalMode: { /* out2-prerelease */
Pager *pPager; /* Pager associated with pBt */
int eNew; /* New journal mode */
int eOld; /* The old journal mode */
+#ifndef SQLITE_OMIT_WAL
const char *zFilename; /* Name of database file for pPager */
+#endif
eNew = pOp->p3;
assert( eNew==PAGER_JOURNALMODE_DELETE
@@ -5735,7 +5761,7 @@ case OP_VOpen: {
/* Initialize sqlite3_vtab_cursor base class */
pVtabCursor->pVtab = pVtab;
- /* Initialise vdbe cursor object */
+ /* Initialize vdbe cursor object */
pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
if( pCur ){
pCur->pVtabCursor = pVtabCursor;
@@ -6014,7 +6040,7 @@ case OP_VUpdate: {
assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
db->lastRowid = lastRowid = rowid;
}
- if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
+ if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
if( pOp->p5==OE_Ignore ){
rc = SQLITE_OK;
}else{
@@ -6075,7 +6101,10 @@ case OP_Trace: {
char *zTrace;
char *z;
- if( db->xTrace && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){
+ if( db->xTrace
+ && !p->doingRerun
+ && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
+ ){
z = sqlite3VdbeExpandSql(p, zTrace);
db->xTrace(db->pTraceArg, z);
sqlite3DbFree(db, z);
diff --git a/src/vdbe.h b/src/vdbe.h
index 90a43ce..fa7b31b 100644
--- a/src/vdbe.h
+++ b/src/vdbe.h
@@ -188,7 +188,7 @@ VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
int sqlite3VdbeMakeLabel(Vdbe*);
void sqlite3VdbeRunOnlyOnce(Vdbe*);
void sqlite3VdbeDelete(Vdbe*);
-void sqlite3VdbeDeleteObject(sqlite3*,Vdbe*);
+void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
void sqlite3VdbeMakeReady(Vdbe*,Parse*);
int sqlite3VdbeFinalize(Vdbe*);
void sqlite3VdbeResolveLabel(Vdbe*, int);
diff --git a/src/vdbeInt.h b/src/vdbeInt.h
index 1f5694a..3a5b402 100644
--- a/src/vdbeInt.h
+++ b/src/vdbeInt.h
@@ -19,6 +19,14 @@
#define _VDBEINT_H_
/*
+** The maximum number of times that a statement will try to reparse
+** itself before giving up and returning SQLITE_SCHEMA.
+*/
+#ifndef SQLITE_MAX_SCHEMA_RETRY
+# define SQLITE_MAX_SCHEMA_RETRY 50
+#endif
+
+/*
** SQL is translated into a sequence of instructions to be
** executed by a virtual machine. Each instruction is an instance
** of the following structure.
@@ -63,6 +71,7 @@ struct VdbeCursor {
Bool isIndex; /* True if an index containing keys only - no data */
Bool isOrdered; /* True if the underlying table is BTREE_UNORDERED */
Bool isSorter; /* True if a new-style sorter */
+ Bool multiPseudo; /* Multi-register pseudo-cursor */
sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */
const sqlite3_module *pModule; /* Module for cursor pVtabCursor */
i64 seqCount; /* Sequence counter */
@@ -122,7 +131,7 @@ struct VdbeFrame {
VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */
void *token; /* Copy of SubProgram.token */
i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */
- u16 nCursor; /* Number of entries in apCsr */
+ int nCursor; /* Number of entries in apCsr */
int pc; /* Program Counter in parent (calling) frame */
int nOp; /* Size of aOp array */
int nMem; /* Number of entries in aMem */
@@ -187,7 +196,9 @@ struct Mem {
#define MEM_RowSet 0x0020 /* Value is a RowSet object */
#define MEM_Frame 0x0040 /* Value is a VdbeFrame object */
#define MEM_Invalid 0x0080 /* Value is undefined */
-#define MEM_TypeMask 0x00ff /* Mask of type bits */
+#define MEM_Cleared 0x0100 /* NULL set by OP_Null, not from data */
+#define MEM_TypeMask 0x01ff /* Mask of type bits */
+
/* Whenever Mem contains a valid string or blob representation, one of
** the following flags must be set to determine the memory management
@@ -273,6 +284,11 @@ struct Explain {
char zBase[100]; /* Initial space */
};
+/* A bitfield type for use inside of structures. Always follow with :N where
+** N is the number of bits.
+*/
+typedef unsigned bft; /* Bit Field Type */
+
/*
** An instance of the virtual machine. This structure contains the complete
** state of the virtual machine.
@@ -301,7 +317,7 @@ struct Vdbe {
int nLabel; /* Number of labels used */
int *aLabel; /* Space to hold the labels */
u16 nResColumn; /* Number of columns in one row of the result set */
- u16 nCursor; /* Number of slots in apCsr[] */
+ int nCursor; /* Number of slots in apCsr[] */
u32 magic; /* Magic number for sanity checking */
char *zErrMsg; /* Error message written here */
Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */
@@ -314,15 +330,16 @@ struct Vdbe {
int pc; /* The program counter */
int rc; /* Value to return */
u8 errorAction; /* Recovery action to do in case of an error */
- u8 explain; /* True if EXPLAIN present on SQL command */
- u8 changeCntOn; /* True to update the change-counter */
- u8 expired; /* True if the VM needs to be recompiled */
- u8 runOnlyOnce; /* Automatically expire on reset */
u8 minWriteFileFormat; /* Minimum file format for writable database files */
- u8 inVtabMethod; /* See comments above */
- u8 usesStmtJournal; /* True if uses a statement journal */
- u8 readOnly; /* True for read-only statements */
- u8 isPrepareV2; /* True if prepared with prepare_v2() */
+ bft explain:2; /* True if EXPLAIN present on SQL command */
+ bft inVtabMethod:2; /* See comments above */
+ bft changeCntOn:1; /* True to update the change-counter */
+ bft expired:1; /* True if the VM needs to be recompiled */
+ bft runOnlyOnce:1; /* Automatically expire on reset */
+ bft usesStmtJournal:1; /* True if uses a statement journal */
+ bft readOnly:1; /* True for read-only statements */
+ bft isPrepareV2:1; /* True if prepared with prepare_v2() */
+ bft doingRerun:1; /* True if rerunning after an auto-reprepare */
int nChange; /* Number of db changes made since last reset */
yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
yDbMask lockMask; /* Subset of btreeMask that requires a lock */
@@ -420,15 +437,6 @@ int sqlite3VdbeFrameRestore(VdbeFrame *);
void sqlite3VdbeMemStoreType(Mem *pMem);
int sqlite3VdbeTransferError(Vdbe *p);
-#ifdef SQLITE_OMIT_MERGE_SORT
-# define sqlite3VdbeSorterInit(Y,Z) SQLITE_OK
-# define sqlite3VdbeSorterWrite(X,Y,Z) SQLITE_OK
-# define sqlite3VdbeSorterClose(Y,Z)
-# define sqlite3VdbeSorterRowkey(Y,Z) SQLITE_OK
-# define sqlite3VdbeSorterRewind(X,Y,Z) SQLITE_OK
-# define sqlite3VdbeSorterNext(X,Y,Z) SQLITE_OK
-# define sqlite3VdbeSorterCompare(X,Y,Z) SQLITE_OK
-#else
int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
@@ -436,7 +444,6 @@ int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *, int *);
int sqlite3VdbeSorterRewind(sqlite3 *, const VdbeCursor *, int *);
int sqlite3VdbeSorterWrite(sqlite3 *, const VdbeCursor *, Mem *);
int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int *);
-#endif
#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
void sqlite3VdbeEnter(Vdbe*);
diff --git a/src/vdbeapi.c b/src/vdbeapi.c
index b9a88a6..7c861e2 100644
--- a/src/vdbeapi.c
+++ b/src/vdbeapi.c
@@ -445,7 +445,7 @@ end_of_step:
assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE );
if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
/* If this statement was prepared using sqlite3_prepare_v2(), and an
- ** error has occured, then return the error code in p->rc to the
+ ** error has occurred, then return the error code in p->rc to the
** caller. Set the error code in the database handle to the same value.
*/
rc = sqlite3VdbeTransferError(p);
@@ -454,14 +454,6 @@ end_of_step:
}
/*
-** The maximum number of times that a statement will try to reparse
-** itself before giving up and returning SQLITE_SCHEMA.
-*/
-#ifndef SQLITE_MAX_SCHEMA_RETRY
-# define SQLITE_MAX_SCHEMA_RETRY 5
-#endif
-
-/*
** This is the top-level implementation of sqlite3_step(). Call
** sqlite3Step() to do most of the work. If a schema error occurs,
** call sqlite3Reprepare() and try again.
@@ -478,10 +470,12 @@ int sqlite3_step(sqlite3_stmt *pStmt){
}
db = v->db;
sqlite3_mutex_enter(db->mutex);
+ v->doingRerun = 0;
while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
&& cnt++ < SQLITE_MAX_SCHEMA_RETRY
&& (rc2 = rc = sqlite3Reprepare(v))==SQLITE_OK ){
sqlite3_reset(pStmt);
+ v->doingRerun = 1;
assert( v->expired==0 );
}
if( rc2!=SQLITE_OK && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
@@ -1196,7 +1190,7 @@ int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
if( zName ){
for(i=0; i<p->nzVar; i++){
const char *z = p->azVar[i];
- if( z && memcmp(z,zName,nName)==0 && z[nName]==0 ){
+ if( z && strncmp(z,zName,nName)==0 && z[nName]==0 ){
return i+1;
}
}
diff --git a/src/vdbeaux.c b/src/vdbeaux.c
index d4f9864..2c4269a 100644
--- a/src/vdbeaux.c
+++ b/src/vdbeaux.c
@@ -17,18 +17,6 @@
#include "sqliteInt.h"
#include "vdbeInt.h"
-
-
-/*
-** When debugging the code generator in a symbolic debugger, one can
-** set the sqlite3VdbeAddopTrace to 1 and all opcodes will be printed
-** as they are added to the instruction stream.
-*/
-#ifdef SQLITE_DEBUG
-int sqlite3VdbeAddopTrace = 0;
-#endif
-
-
/*
** Create a new virtual database engine.
*/
@@ -53,7 +41,7 @@ Vdbe *sqlite3VdbeCreate(sqlite3 *db){
void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
assert( isPrepareV2==1 || isPrepareV2==0 );
if( p==0 ) return;
-#ifdef SQLITE_OMIT_TRACE
+#if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
if( !isPrepareV2 ) return;
#endif
assert( p->zSql==0 );
@@ -158,7 +146,9 @@ int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
pOp->p4type = P4_NOTUSED;
#ifdef SQLITE_DEBUG
pOp->zComment = 0;
- if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
+ if( p->db->flags & SQLITE_VdbeAddopTrace ){
+ sqlite3VdbePrintOp(0, i, &p->aOp[i]);
+ }
#endif
#ifdef VDBE_PROFILE
pOp->cycles = 0;
@@ -377,7 +367,7 @@ int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
|| (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1)
#endif
|| ((opcode==OP_Halt || opcode==OP_HaltIfNull)
- && (pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
+ && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
){
hasAbort = 1;
break;
@@ -385,7 +375,7 @@ int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
}
sqlite3DbFree(v->db, sIter.apSub);
- /* Return true if hasAbort==mayAbort. Or if a malloc failure occured.
+ /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
** If malloc failed, then the while() loop above may not have iterated
** through all opcodes and hasAbort may be set incorrectly. Return
** true for this case to prevent the assert() in the callers frame
@@ -512,7 +502,7 @@ int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
pOut->p5 = 0;
#ifdef SQLITE_DEBUG
pOut->zComment = 0;
- if( sqlite3VdbeAddopTrace ){
+ if( p->db->flags & SQLITE_VdbeAddopTrace ){
sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
}
#endif
@@ -723,6 +713,7 @@ void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
addr = p->nOp - 1;
}
pOp = &p->aOp[addr];
+ assert( pOp->p4type==P4_NOTUSED || pOp->p4type==P4_INT32 );
freeP4(db, pOp->p4type, pOp->p4.p);
pOp->p4.p = 0;
if( n==P4_INT32 ){
@@ -745,10 +736,9 @@ void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
u8 *aSortOrder;
memcpy((char*)pKeyInfo, zP4, nByte - nField);
aSortOrder = pKeyInfo->aSortOrder;
- if( aSortOrder ){
- pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
- memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
- }
+ assert( aSortOrder!=0 );
+ pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
+ memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
pOp->p4type = P4_KEYINFO;
}else{
p->db->mallocFailed = 1;
@@ -861,26 +851,23 @@ static char *displayP4(Op *pOp, char *zTemp, int nTemp){
case P4_KEYINFO: {
int i, j;
KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
+ assert( pKeyInfo->aSortOrder!=0 );
sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
i = sqlite3Strlen30(zTemp);
for(j=0; j<pKeyInfo->nField; j++){
CollSeq *pColl = pKeyInfo->aColl[j];
- if( pColl ){
- int n = sqlite3Strlen30(pColl->zName);
- if( i+n>nTemp-6 ){
- memcpy(&zTemp[i],",...",4);
- break;
- }
- zTemp[i++] = ',';
- if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
- zTemp[i++] = '-';
- }
- memcpy(&zTemp[i], pColl->zName,n+1);
- i += n;
- }else if( i+4<nTemp-6 ){
- memcpy(&zTemp[i],",nil",4);
- i += 4;
+ const char *zColl = pColl ? pColl->zName : "nil";
+ int n = sqlite3Strlen30(zColl);
+ if( i+n>nTemp-6 ){
+ memcpy(&zTemp[i],",...",4);
+ break;
+ }
+ zTemp[i++] = ',';
+ if( pKeyInfo->aSortOrder[j] ){
+ zTemp[i++] = '-';
}
+ memcpy(&zTemp[i], zColl, n+1);
+ i += n;
}
zTemp[i++] = ')';
zTemp[i] = 0;
@@ -1541,7 +1528,7 @@ void sqlite3VdbeMakeReady(
zEnd = &zCsr[nByte];
}while( nByte && !db->mallocFailed );
- p->nCursor = (u16)nCursor;
+ p->nCursor = nCursor;
p->nOnceFlag = nOnce;
if( p->aVar ){
p->nVar = (ynVar)nVar;
@@ -1770,7 +1757,9 @@ static int vdbeCommit(sqlite3 *db, Vdbe *p){
if( sqlite3BtreeIsInTrans(pBt) ){
needXcommit = 1;
if( i!=1 ) nTrans++;
+ sqlite3BtreeEnter(pBt);
rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
+ sqlite3BtreeLeave(pBt);
}
}
if( rc!=SQLITE_OK ){
@@ -1781,7 +1770,7 @@ static int vdbeCommit(sqlite3 *db, Vdbe *p){
if( needXcommit && db->xCommitCallback ){
rc = db->xCommitCallback(db->pCommitArg);
if( rc ){
- return SQLITE_CONSTRAINT;
+ return SQLITE_CONSTRAINT_COMMITHOOK;
}
}
@@ -2018,7 +2007,7 @@ int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
/* If p->iStatement is greater than zero, then this Vdbe opened a
** statement transaction that should be closed here. The only exception
- ** is that an IO error may have occured, causing an emergency rollback.
+ ** is that an IO error may have occurred, causing an emergency rollback.
** In this case (db->nStatement==0), and there is nothing to do.
*/
if( db->nStatement && p->iStatement ){
@@ -2073,14 +2062,14 @@ int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
**
** If there are outstanding FK violations and this function returns
-** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT and write
-** an error message to it. Then return SQLITE_ERROR.
+** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
+** and write an error message to it. Then return SQLITE_ERROR.
*/
#ifndef SQLITE_OMIT_FOREIGN_KEY
int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
sqlite3 *db = p->db;
if( (deferred && db->nDeferredCons>0) || (!deferred && p->nFkConstraint>0) ){
- p->rc = SQLITE_CONSTRAINT;
+ p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
p->errorAction = OE_Abort;
sqlite3SetString(&p->zErrMsg, db, "foreign key constraint failed");
return SQLITE_ERROR;
@@ -2154,7 +2143,7 @@ int sqlite3VdbeHalt(Vdbe *p){
**
** Even if the statement is read-only, it is important to perform
** a statement or transaction rollback operation. If the error
- ** occured while writing to the journal, sub-journal or database
+ ** occurred while writing to the journal, sub-journal or database
** file as part of an effort to free up cache space (see function
** pagerStress() in pager.c), the rollback is required to restore
** the pager to a consistent state.
@@ -2195,7 +2184,7 @@ int sqlite3VdbeHalt(Vdbe *p){
sqlite3VdbeLeave(p);
return SQLITE_ERROR;
}
- rc = SQLITE_CONSTRAINT;
+ rc = SQLITE_CONSTRAINT_FOREIGNKEY;
}else{
/* The auto-commit flag is true, the vdbe program was successful
** or hit an 'OR FAIL' constraint and there are no deferred foreign
@@ -2238,7 +2227,7 @@ int sqlite3VdbeHalt(Vdbe *p){
if( eStatementOp ){
rc = sqlite3VdbeCloseStatement(p, eStatementOp);
if( rc ){
- if( p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT ){
+ if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
p->rc = rc;
sqlite3DbFree(db, p->zErrMsg);
p->zErrMsg = 0;
@@ -2324,6 +2313,27 @@ int sqlite3VdbeTransferError(Vdbe *p){
return rc;
}
+#ifdef SQLITE_ENABLE_SQLLOG
+/*
+** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
+** invoke it.
+*/
+static void vdbeInvokeSqllog(Vdbe *v){
+ if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
+ char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
+ assert( v->db->init.busy==0 );
+ if( zExpanded ){
+ sqlite3GlobalConfig.xSqllog(
+ sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
+ );
+ sqlite3DbFree(v->db, zExpanded);
+ }
+ }
+}
+#else
+# define vdbeInvokeSqllog(x)
+#endif
+
/*
** Clean up a VDBE after execution but do not delete the VDBE just yet.
** Write any error messages into *pzErrMsg. Return the result code.
@@ -2351,6 +2361,7 @@ int sqlite3VdbeReset(Vdbe *p){
** instructions yet, leave the main database error information unchanged.
*/
if( p->pc>=0 ){
+ vdbeInvokeSqllog(p);
sqlite3VdbeTransferError(p);
sqlite3DbFree(db, p->zErrMsg);
p->zErrMsg = 0;
@@ -2432,12 +2443,14 @@ void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
}
/*
-** Free all memory associated with the Vdbe passed as the second argument.
+** Free all memory associated with the Vdbe passed as the second argument,
+** except for object itself, which is preserved.
+**
** The difference between this function and sqlite3VdbeDelete() is that
** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
-** the database connection.
+** the database connection and frees the object itself.
*/
-void sqlite3VdbeDeleteObject(sqlite3 *db, Vdbe *p){
+void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
SubProgram *pSub, *pNext;
int i;
assert( p->db==0 || p->db==db );
@@ -2458,7 +2471,6 @@ void sqlite3VdbeDeleteObject(sqlite3 *db, Vdbe *p){
sqlite3DbFree(db, p->zExplain);
sqlite3DbFree(db, p->pExplain);
#endif
- sqlite3DbFree(db, p);
}
/*
@@ -2470,6 +2482,7 @@ void sqlite3VdbeDelete(Vdbe *p){
if( NEVER(p==0) ) return;
db = p->db;
assert( sqlite3_mutex_held(db->mutex) );
+ sqlite3VdbeClearObject(db, p);
if( p->pPrev ){
p->pPrev->pNext = p->pNext;
}else{
@@ -2481,7 +2494,7 @@ void sqlite3VdbeDelete(Vdbe *p){
}
p->magic = VDBE_MAGIC_DEAD;
p->db = 0;
- sqlite3VdbeDeleteObject(db, p);
+ sqlite3DbFree(db, p);
}
/*
@@ -2544,7 +2557,7 @@ int sqlite3VdbeCursorMoveto(VdbeCursor *p){
** the blob of data that it corresponds to. In a table record, all serial
** types are stored at the start of the record, and the blobs of data at
** the end. Hence these functions allow the caller to handle the
-** serial-type and data blob seperately.
+** serial-type and data blob separately.
**
** The following table describes the various storage classes for data:
**
@@ -2583,9 +2596,6 @@ u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
# define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
i64 i = pMem->u.i;
u64 u;
- if( file_format>=4 && (i&1)==i ){
- return 8+(u32)i;
- }
if( i<0 ){
if( i<(-MAX_6BYTE) ) return 6;
/* Previous test prevents: u = -(-9223372036854775808) */
@@ -2593,7 +2603,9 @@ u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
}else{
u = i;
}
- if( u<=127 ) return 1;
+ if( u<=127 ){
+ return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1;
+ }
if( u<=32767 ) return 2;
if( u<=8388607 ) return 3;
if( u<=2147483647 ) return 4;
@@ -2878,6 +2890,7 @@ UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
}
p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
+ assert( pKeyInfo->aSortOrder!=0 );
p->pKeyInfo = pKeyInfo;
p->nField = pKeyInfo->nField + 1;
return p;
@@ -2971,6 +2984,7 @@ int sqlite3VdbeRecordCompare(
idx1 = getVarint32(aKey1, szHdr1);
d1 = szHdr1;
nField = pKeyInfo->nField;
+ assert( pKeyInfo->aSortOrder!=0 );
while( idx1<szHdr1 && i<pPKey2->nField ){
u32 serial_type1;
@@ -2990,7 +3004,7 @@ int sqlite3VdbeRecordCompare(
assert( mem1.zMalloc==0 ); /* See comment below */
/* Invert the result if we are using DESC sort order. */
- if( pKeyInfo->aSortOrder && i<nField && pKeyInfo->aSortOrder[i] ){
+ if( i<nField && pKeyInfo->aSortOrder[i] ){
rc = -rc;
}
diff --git a/src/vdbeblob.c b/src/vdbeblob.c
index ae77a47..2e8fd8e 100644
--- a/src/vdbeblob.c
+++ b/src/vdbeblob.c
@@ -313,7 +313,7 @@ int sqlite3_blob_open(
}
sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
rc = blobSeekToRow(pBlob, iRow, &zErr);
- } while( (++nAttempt)<5 && rc==SQLITE_SCHEMA );
+ } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
blob_open_out:
if( rc==SQLITE_OK && db->mallocFailed==0 ){
diff --git a/src/vdbemem.c b/src/vdbemem.c
index fd964de..8fc222e 100644
--- a/src/vdbemem.c
+++ b/src/vdbemem.c
@@ -32,7 +32,9 @@
** between formats.
*/
int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
+#ifndef SQLITE_OMIT_UTF16
int rc;
+#endif
assert( (pMem->flags&MEM_RowSet)==0 );
assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
|| desiredEnc==SQLITE_UTF16BE );
diff --git a/src/vdbesort.c b/src/vdbesort.c
index ba1e9f0..fdfc4a7 100644
--- a/src/vdbesort.c
+++ b/src/vdbesort.c
@@ -18,7 +18,6 @@
#include "sqliteInt.h"
#include "vdbeInt.h"
-#ifndef SQLITE_OMIT_MERGE_SORT
typedef struct VdbeSorterIter VdbeSorterIter;
typedef struct SorterRecord SorterRecord;
@@ -195,8 +194,11 @@ static int vdbeSorterIterRead(
int rc; /* sqlite3OsRead() return code */
/* Determine how many bytes of data to read. */
- nRead = (int)(p->iEof - p->iReadOff);
- if( nRead>p->nBuffer ) nRead = p->nBuffer;
+ if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
+ nRead = p->nBuffer;
+ }else{
+ nRead = (int)(p->iEof - p->iReadOff);
+ }
assert( nRead>0 );
/* Read data from the file. Return early if an error occurs. */
@@ -1034,5 +1036,3 @@ int sqlite3VdbeSorterCompare(
vdbeSorterCompare(pCsr, 1, pVal->z, pVal->n, pKey, nKey, pRes);
return SQLITE_OK;
}
-
-#endif /* #ifndef SQLITE_OMIT_MERGE_SORT */
diff --git a/src/vdbetrace.c b/src/vdbetrace.c
index 35825c8..356277e 100644
--- a/src/vdbetrace.c
+++ b/src/vdbetrace.c
@@ -53,6 +53,11 @@ static int findNextHostParameter(const char *zSql, int *pnToken){
** then the returned string holds a copy of zRawSql with "-- " prepended
** to each line of text.
**
+** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
+** then long strings and blobs are truncated to that many bytes. This
+** can be used to prevent unreasonably large trace strings when dealing
+** with large (multi-megabyte) strings and blobs.
+**
** The calling function is responsible for making sure the memory returned
** is eventually freed.
**
@@ -123,30 +128,49 @@ char *sqlite3VdbeExpandSql(
}else if( pVar->flags & MEM_Real ){
sqlite3XPrintf(&out, "%!.15g", pVar->r);
}else if( pVar->flags & MEM_Str ){
+ int nOut; /* Number of bytes of the string text to include in output */
#ifndef SQLITE_OMIT_UTF16
u8 enc = ENC(db);
+ Mem utf8;
if( enc!=SQLITE_UTF8 ){
- Mem utf8;
memset(&utf8, 0, sizeof(utf8));
utf8.db = db;
sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
- sqlite3XPrintf(&out, "'%.*q'", utf8.n, utf8.z);
- sqlite3VdbeMemRelease(&utf8);
- }else
+ pVar = &utf8;
+ }
#endif
- {
- sqlite3XPrintf(&out, "'%.*q'", pVar->n, pVar->z);
+ nOut = pVar->n;
+#ifdef SQLITE_TRACE_SIZE_LIMIT
+ if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
+ nOut = SQLITE_TRACE_SIZE_LIMIT;
+ while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
}
+#endif
+ sqlite3XPrintf(&out, "'%.*q'", nOut, pVar->z);
+#ifdef SQLITE_TRACE_SIZE_LIMIT
+ if( nOut<pVar->n ) sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut);
+#endif
+#ifndef SQLITE_OMIT_UTF16
+ if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
+#endif
}else if( pVar->flags & MEM_Zero ){
sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
}else{
+ int nOut; /* Number of bytes of the blob to include in output */
assert( pVar->flags & MEM_Blob );
sqlite3StrAccumAppend(&out, "x'", 2);
- for(i=0; i<pVar->n; i++){
+ nOut = pVar->n;
+#ifdef SQLITE_TRACE_SIZE_LIMIT
+ if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
+#endif
+ for(i=0; i<nOut; i++){
sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
}
sqlite3StrAccumAppend(&out, "'", 1);
+#ifdef SQLITE_TRACE_SIZE_LIMIT
+ if( nOut<pVar->n ) sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut);
+#endif
}
}
}
diff --git a/src/vtab.c b/src/vtab.c
index 50d576f..958202c 100644
--- a/src/vtab.c
+++ b/src/vtab.c
@@ -264,7 +264,7 @@ void sqlite3VtabClear(sqlite3 *db, Table *p){
if( p->azModuleArg ){
int i;
for(i=0; i<p->nModuleArg; i++){
- sqlite3DbFree(db, p->azModuleArg[i]);
+ if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]);
}
sqlite3DbFree(db, p->azModuleArg);
}
@@ -324,7 +324,7 @@ void sqlite3VtabBeginParse(
pTable->tabFlags |= TF_Virtual;
pTable->nModuleArg = 0;
addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
- addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName));
+ addModuleArgument(db, pTable, 0);
addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z);
@@ -481,6 +481,7 @@ static int vtabCallConstructor(
int nArg = pTab->nModuleArg;
char *zErr = 0;
char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
+ int iDb;
if( !zModuleName ){
return SQLITE_NOMEM;
@@ -494,6 +495,9 @@ static int vtabCallConstructor(
pVTable->db = db;
pVTable->pMod = pMod;
+ iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
+ pTab->azModuleArg[1] = db->aDb[iDb].zName;
+
/* Invoke the virtual table constructor */
assert( &db->pVtabCtx );
assert( xConstruct );
@@ -528,7 +532,7 @@ static int vtabCallConstructor(
/* If everything went according to plan, link the new VTable structure
** into the linked list headed by pTab->pVTable. Then loop through the
** columns of the table to see if any of them contain the token "hidden".
- ** If so, set the Column.isHidden flag and remove the token from
+ ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
** the type string. */
pVTable->pNext = pTab->pVTable;
pTab->pVTable = pVTable;
@@ -559,7 +563,7 @@ static int vtabCallConstructor(
assert(zType[i-1]==' ');
zType[i-1] = '\0';
}
- pTab->aCol[iCol].isHidden = 1;
+ pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
}
}
}
diff --git a/src/wal.c b/src/wal.c
index cc166ba..e642ea2 100644
--- a/src/wal.c
+++ b/src/wal.c
@@ -1207,8 +1207,9 @@ finished:
** checkpointing the log file.
*/
if( pWal->hdr.nPage ){
- sqlite3_log(SQLITE_OK, "Recovered %d frames from WAL file %s",
- pWal->hdr.nPage, pWal->zWalName
+ sqlite3_log(SQLITE_NOTICE_RECOVER_WAL,
+ "recovered %d frames from WAL file %s",
+ pWal->hdr.mxFrame, pWal->zWalName
);
}
}
@@ -1722,8 +1723,8 @@ static int walCheckpoint(
rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
}
- /* If the database file may grow as a result of this checkpoint, hint
- ** about the eventual size of the db file to the VFS layer.
+ /* If the database may grow as a result of this checkpoint, hint
+ ** about the eventual size of the db file to the VFS layer.
*/
if( rc==SQLITE_OK ){
i64 nReq = ((i64)mxPage * szPage);
@@ -1733,6 +1734,7 @@ static int walCheckpoint(
}
}
+
/* Iterate through the contents of the WAL, copying data to the db file. */
while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
i64 iOffset;
@@ -2287,19 +2289,17 @@ void sqlite3WalEndReadTransaction(Wal *pWal){
}
/*
-** Read a page from the WAL, if it is present in the WAL and if the
-** current read transaction is configured to use the WAL.
+** Search the wal file for page pgno. If found, set *piRead to the frame that
+** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
+** to zero.
**
-** The *pInWal is set to 1 if the requested page is in the WAL and
-** has been loaded. Or *pInWal is set to 0 if the page was not in
-** the WAL and needs to be read out of the database.
+** Return SQLITE_OK if successful, or an error code if an error occurs. If an
+** error does occur, the final value of *piRead is undefined.
*/
-int sqlite3WalRead(
+int sqlite3WalFindFrame(
Wal *pWal, /* WAL handle */
Pgno pgno, /* Database page number to read data for */
- int *pInWal, /* OUT: True if data is read from WAL */
- int nOut, /* Size of buffer pOut in bytes */
- u8 *pOut /* Buffer to write page data to */
+ u32 *piRead /* OUT: Frame number (or zero) */
){
u32 iRead = 0; /* If !=0, WAL frame to return data from */
u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
@@ -2315,7 +2315,7 @@ int sqlite3WalRead(
** WAL were empty.
*/
if( iLast==0 || pWal->readLock==0 ){
- *pInWal = 0;
+ *piRead = 0;
return SQLITE_OK;
}
@@ -2386,26 +2386,31 @@ int sqlite3WalRead(
}
#endif
- /* If iRead is non-zero, then it is the log frame number that contains the
- ** required page. Read and return data from the log file.
- */
- if( iRead ){
- int sz;
- i64 iOffset;
- sz = pWal->hdr.szPage;
- sz = (sz&0xfe00) + ((sz&0x0001)<<16);
- testcase( sz<=32768 );
- testcase( sz>=65536 );
- iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
- *pInWal = 1;
- /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
- return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
- }
-
- *pInWal = 0;
+ *piRead = iRead;
return SQLITE_OK;
}
+/*
+** Read the contents of frame iRead from the wal file into buffer pOut
+** (which is nOut bytes in size). Return SQLITE_OK if successful, or an
+** error code otherwise.
+*/
+int sqlite3WalReadFrame(
+ Wal *pWal, /* WAL handle */
+ u32 iRead, /* Frame to read */
+ int nOut, /* Size of buffer pOut in bytes */
+ u8 *pOut /* Buffer to write page data to */
+){
+ int sz;
+ i64 iOffset;
+ sz = pWal->hdr.szPage;
+ sz = (sz&0xfe00) + ((sz&0x0001)<<16);
+ testcase( sz<=32768 );
+ testcase( sz>=65536 );
+ iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
+ /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
+ return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
+}
/*
** Return the size of the database in pages (or zero, if unknown).
@@ -2518,7 +2523,7 @@ int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
assert( walFramePgno(pWal, iFrame)!=1 );
rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
}
- walCleanupHash(pWal);
+ if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
}
assert( rc==SQLITE_OK );
return rc;
@@ -2828,7 +2833,7 @@ int sqlite3WalFrames(
*/
if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){
if( pWal->padToSectorBoundary ){
- int sectorSize = sqlite3OsSectorSize(pWal->pWalFd);
+ int sectorSize = sqlite3SectorSize(pWal->pWalFd);
w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
while( iOffset<w.iSyncPoint ){
rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
@@ -2952,6 +2957,9 @@ int sqlite3WalCheckpoint(
/* Read the wal-index header. */
if( rc==SQLITE_OK ){
rc = walIndexReadHdr(pWal, &isChanged);
+ if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
+ sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
+ }
}
/* Copy data from the log to the database file. */
diff --git a/src/wal.h b/src/wal.h
index a848de1..0925463 100644
--- a/src/wal.h
+++ b/src/wal.h
@@ -31,7 +31,6 @@
# define sqlite3WalClose(w,x,y,z) 0
# define sqlite3WalBeginReadTransaction(y,z) 0
# define sqlite3WalEndReadTransaction(z)
-# define sqlite3WalRead(v,w,x,y,z) 0
# define sqlite3WalDbsize(y) 0
# define sqlite3WalBeginWriteTransaction(y) 0
# define sqlite3WalEndWriteTransaction(x) 0
@@ -44,6 +43,7 @@
# define sqlite3WalExclusiveMode(y,z) 0
# define sqlite3WalHeapMemory(z) 0
# define sqlite3WalFramesize(z) 0
+# define sqlite3WalFindFrame(x,y,z) 0
#else
#define WAL_SAVEPOINT_NDATA 4
@@ -71,7 +71,8 @@ int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
void sqlite3WalEndReadTransaction(Wal *pWal);
/* Read a page from the write-ahead log, if it is present. */
-int sqlite3WalRead(Wal *pWal, Pgno pgno, int *pInWal, int nOut, u8 *pOut);
+int sqlite3WalFindFrame(Wal *, Pgno, u32 *);
+int sqlite3WalReadFrame(Wal *, u32, int, u8 *);
/* If the WAL is not empty, return the size of the database. */
Pgno sqlite3WalDbsize(Wal *pWal);
diff --git a/src/walker.c b/src/walker.c
index eab96ea..e71ed2a 100644
--- a/src/walker.c
+++ b/src/walker.c
@@ -113,7 +113,9 @@ int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
/*
** Call sqlite3WalkExpr() for every expression in Select statement p.
** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
-** on the compound select chain, p->pPrior.
+** on the compound select chain, p->pPrior. Invoke the xSelectCallback()
+** either before or after the walk of expressions and FROM clause, depending
+** on whether pWalker->bSelectDepthFirst is false or true, respectively.
**
** Return WRC_Continue under normal conditions. Return WRC_Abort if
** there is an abort request.
@@ -127,14 +129,23 @@ int sqlite3WalkSelect(Walker *pWalker, Select *p){
rc = WRC_Continue;
pWalker->walkerDepth++;
while( p ){
- rc = pWalker->xSelectCallback(pWalker, p);
- if( rc ) break;
+ if( !pWalker->bSelectDepthFirst ){
+ rc = pWalker->xSelectCallback(pWalker, p);
+ if( rc ) break;
+ }
if( sqlite3WalkSelectExpr(pWalker, p)
|| sqlite3WalkSelectFrom(pWalker, p)
){
pWalker->walkerDepth--;
return WRC_Abort;
}
+ if( pWalker->bSelectDepthFirst ){
+ rc = pWalker->xSelectCallback(pWalker, p);
+ /* Depth-first search is currently only used for
+ ** selectAddSubqueryTypeInfo() and that routine always returns
+ ** WRC_Continue (0). So the following branch is never taken. */
+ if( NEVER(rc) ) break;
+ }
p = p->pPrior;
}
pWalker->walkerDepth--;
diff --git a/src/where.c b/src/where.c
index 216a47f..e614f4a 100644
--- a/src/where.c
+++ b/src/where.c
@@ -23,9 +23,10 @@
** Trace output macros
*/
#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
-int sqlite3WhereTrace = 0;
+/***/ int sqlite3WhereTrace = 0;
#endif
-#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
+#if defined(SQLITE_DEBUG) \
+ && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
# define WHERETRACE(X) if(sqlite3WhereTrace) sqlite3DebugPrintf X
#else
# define WHERETRACE(X)
@@ -97,8 +98,8 @@ struct WhereTerm {
int leftCursor; /* Cursor number of X in "X <op> <expr>" */
union {
int leftColumn; /* Column number of X in "X <op> <expr>" */
- WhereOrInfo *pOrInfo; /* Extra information if eOperator==WO_OR */
- WhereAndInfo *pAndInfo; /* Extra information if eOperator==WO_AND */
+ WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */
+ WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
} u;
u16 eOperator; /* A WO_xx value describing <op> */
u8 wtFlags; /* TERM_xxx bit flags. See below */
@@ -139,7 +140,6 @@ struct WhereTerm {
struct WhereClause {
Parse *pParse; /* The parser context */
WhereMaskSet *pMaskSet; /* Mapping of table cursor numbers to bitmasks */
- Bitmask vmask; /* Bitmask identifying virtual table cursors */
WhereClause *pOuter; /* Outer conjunction */
u8 op; /* Split operator. TK_AND or TK_OR */
u16 wctrlFlags; /* Might include WHERE_AND_ONLY */
@@ -226,6 +226,7 @@ struct WhereCost {
#define WO_ISNULL 0x080
#define WO_OR 0x100 /* Two or more OR-connected terms */
#define WO_AND 0x200 /* Two or more AND-connected terms */
+#define WO_EQUIV 0x400 /* Of the form A==B, both columns */
#define WO_NOOP 0x800 /* This term does not restrict search space */
#define WO_ALL 0xfff /* Mask of all possible WO_* values */
@@ -252,18 +253,55 @@ struct WhereCost {
#define WHERE_COLUMN_NULL 0x00080000 /* x IS NULL */
#define WHERE_INDEXED 0x000f0000 /* Anything that uses an index */
#define WHERE_NOT_FULLSCAN 0x100f3000 /* Does not do a full table scan */
-#define WHERE_IN_ABLE 0x000f1000 /* Able to support an IN operator */
+#define WHERE_IN_ABLE 0x080f1000 /* Able to support an IN operator */
#define WHERE_TOP_LIMIT 0x00100000 /* x<EXPR or x<=EXPR constraint */
#define WHERE_BTM_LIMIT 0x00200000 /* x>EXPR or x>=EXPR constraint */
#define WHERE_BOTH_LIMIT 0x00300000 /* Both x>EXPR and x<EXPR */
-#define WHERE_IDX_ONLY 0x00800000 /* Use index only - omit table */
-#define WHERE_ORDERBY 0x01000000 /* Output will appear in correct order */
-#define WHERE_REVERSE 0x02000000 /* Scan in reverse order */
-#define WHERE_UNIQUE 0x04000000 /* Selects no more than one row */
+#define WHERE_IDX_ONLY 0x00400000 /* Use index only - omit table */
+#define WHERE_ORDERED 0x00800000 /* Output will appear in correct order */
+#define WHERE_REVERSE 0x01000000 /* Scan in reverse order */
+#define WHERE_UNIQUE 0x02000000 /* Selects no more than one row */
+#define WHERE_ALL_UNIQUE 0x04000000 /* This and all prior have one row */
+#define WHERE_OB_UNIQUE 0x00004000 /* Values in ORDER BY columns are
+ ** different for every output row */
#define WHERE_VIRTUALTABLE 0x08000000 /* Use virtual-table processing */
#define WHERE_MULTI_OR 0x10000000 /* OR using multiple indices */
#define WHERE_TEMP_INDEX 0x20000000 /* Uses an ephemeral index */
#define WHERE_DISTINCT 0x40000000 /* Correct order for DISTINCT */
+#define WHERE_COVER_SCAN 0x80000000 /* Full scan of a covering index */
+
+/*
+** This module contains many separate subroutines that work together to
+** find the best indices to use for accessing a particular table in a query.
+** An instance of the following structure holds context information about the
+** index search so that it can be more easily passed between the various
+** routines.
+*/
+typedef struct WhereBestIdx WhereBestIdx;
+struct WhereBestIdx {
+ Parse *pParse; /* Parser context */
+ WhereClause *pWC; /* The WHERE clause */
+ struct SrcList_item *pSrc; /* The FROM clause term to search */
+ Bitmask notReady; /* Mask of cursors not available */
+ Bitmask notValid; /* Cursors not available for any purpose */
+ ExprList *pOrderBy; /* The ORDER BY clause */
+ ExprList *pDistinct; /* The select-list if query is DISTINCT */
+ sqlite3_index_info **ppIdxInfo; /* Index information passed to xBestIndex */
+ int i, n; /* Which loop is being coded; # of loops */
+ WhereLevel *aLevel; /* Info about outer loops */
+ WhereCost cost; /* Lowest cost query plan */
+};
+
+/*
+** Return TRUE if the probe cost is less than the baseline cost
+*/
+static int compareCost(const WhereCost *pProbe, const WhereCost *pBaseline){
+ if( pProbe->rCost<pBaseline->rCost ) return 1;
+ if( pProbe->rCost>pBaseline->rCost ) return 0;
+ if( pProbe->plan.nOBSat>pBaseline->plan.nOBSat ) return 1;
+ if( pProbe->plan.nRow<pBaseline->plan.nRow ) return 1;
+ return 0;
+}
/*
** Initialize a preallocated WhereClause structure.
@@ -280,7 +318,6 @@ static void whereClauseInit(
pWC->nTerm = 0;
pWC->nSlot = ArraySize(pWC->aStatic);
pWC->a = pWC->aStatic;
- pWC->vmask = 0;
pWC->wctrlFlags = wctrlFlags;
}
@@ -367,7 +404,7 @@ static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){
pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
}
pTerm = &pWC->a[idx = pWC->nTerm++];
- pTerm->pExpr = p;
+ pTerm->pExpr = sqlite3ExprSkipCollate(p);
pTerm->wtFlags = wtFlags;
pTerm->pWC = pWC;
pTerm->iParent = -1;
@@ -527,23 +564,32 @@ static int allowedOp(int op){
** Commute a comparison operator. Expressions of the form "X op Y"
** are converted into "Y op X".
**
-** If a collation sequence is associated with either the left or right
+** If left/right precedence rules come into play when determining the
+** collating
** side of the comparison, it remains associated with the same side after
** the commutation. So "Y collate NOCASE op X" becomes
-** "X collate NOCASE op Y". This is because any collation sequence on
+** "X op Y". This is because any collation sequence on
** the left hand side of a comparison overrides any collation sequence
-** attached to the right. For the same reason the EP_ExpCollate flag
+** attached to the right. For the same reason the EP_Collate flag
** is not commuted.
*/
static void exprCommute(Parse *pParse, Expr *pExpr){
- u16 expRight = (pExpr->pRight->flags & EP_ExpCollate);
- u16 expLeft = (pExpr->pLeft->flags & EP_ExpCollate);
+ u16 expRight = (pExpr->pRight->flags & EP_Collate);
+ u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
- pExpr->pRight->pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
- pExpr->pLeft->pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
- SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl);
- pExpr->pRight->flags = (pExpr->pRight->flags & ~EP_ExpCollate) | expLeft;
- pExpr->pLeft->flags = (pExpr->pLeft->flags & ~EP_ExpCollate) | expRight;
+ if( expRight==expLeft ){
+ /* Either X and Y both have COLLATE operator or neither do */
+ if( expRight ){
+ /* Both X and Y have COLLATE operators. Make sure X is always
+ ** used by clearing the EP_Collate flag from Y. */
+ pExpr->pRight->flags &= ~EP_Collate;
+ }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
+ /* Neither X nor Y have COLLATE operators, but X has a non-default
+ ** collating sequence. So add the EP_Collate marker on X to cause
+ ** it to be searched first. */
+ pExpr->pLeft->flags |= EP_Collate;
+ }
+ }
SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
if( pExpr->op>=TK_GT ){
assert( TK_LT==TK_GT+2 );
@@ -584,6 +630,23 @@ static u16 operatorMask(int op){
** where X is a reference to the iColumn of table iCur and <op> is one of
** the WO_xx operator codes specified by the op parameter.
** Return a pointer to the term. Return 0 if not found.
+**
+** The term returned might by Y=<expr> if there is another constraint in
+** the WHERE clause that specifies that X=Y. Any such constraints will be
+** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
+** aEquiv[] array holds X and all its equivalents, with each SQL variable
+** taking up two slots in aEquiv[]. The first slot is for the cursor number
+** and the second is for the column number. There are 22 slots in aEquiv[]
+** so that means we can look for X plus up to 10 other equivalent values.
+** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3
+** and ... and A9=A10 and A10=<expr>.
+**
+** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
+** then try for the one with no dependencies on <expr> - in other words where
+** <expr> is a constant expression of some kind. Only return entries of
+** the form "X <op> Y" where Y is a column in another table if no terms of
+** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
+** exist, try to return a term that does not use WO_EQUIV.
*/
static WhereTerm *findTerm(
WhereClause *pWC, /* The WHERE clause to be searched */
@@ -593,45 +656,85 @@ static WhereTerm *findTerm(
u32 op, /* Mask of WO_xx values describing operator */
Index *pIdx /* Must be compatible with this index, if not NULL */
){
- WhereTerm *pTerm;
- int k;
+ WhereTerm *pTerm; /* Term being examined as possible result */
+ WhereTerm *pResult = 0; /* The answer to return */
+ WhereClause *pWCOrig = pWC; /* Original pWC value */
+ int j, k; /* Loop counters */
+ Expr *pX; /* Pointer to an expression */
+ Parse *pParse; /* Parsing context */
+ int iOrigCol = iColumn; /* Original value of iColumn */
+ int nEquiv = 2; /* Number of entires in aEquiv[] */
+ int iEquiv = 2; /* Number of entries of aEquiv[] processed so far */
+ int aEquiv[22]; /* iCur,iColumn and up to 10 other equivalents */
+
assert( iCur>=0 );
- op &= WO_ALL;
- for(; pWC; pWC=pWC->pOuter){
- for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
- if( pTerm->leftCursor==iCur
- && (pTerm->prereqRight & notReady)==0
- && pTerm->u.leftColumn==iColumn
- && (pTerm->eOperator & op)!=0
- ){
- if( iColumn>=0 && pIdx && pTerm->eOperator!=WO_ISNULL ){
- Expr *pX = pTerm->pExpr;
- CollSeq *pColl;
- char idxaff;
- int j;
- Parse *pParse = pWC->pParse;
-
- idxaff = pIdx->pTable->aCol[iColumn].affinity;
- if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
-
- /* Figure out the collation sequence required from an index for
- ** it to be useful for optimising expression pX. Store this
- ** value in variable pColl.
- */
- assert(pX->pLeft);
- pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
- assert(pColl || pParse->nErr);
-
- for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
- if( NEVER(j>=pIdx->nColumn) ) return 0;
+ aEquiv[0] = iCur;
+ aEquiv[1] = iColumn;
+ for(;;){
+ for(pWC=pWCOrig; pWC; pWC=pWC->pOuter){
+ for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
+ if( pTerm->leftCursor==iCur
+ && pTerm->u.leftColumn==iColumn
+ ){
+ if( (pTerm->prereqRight & notReady)==0
+ && (pTerm->eOperator & op & WO_ALL)!=0
+ ){
+ if( iOrigCol>=0 && pIdx && (pTerm->eOperator & WO_ISNULL)==0 ){
+ CollSeq *pColl;
+ char idxaff;
+
+ pX = pTerm->pExpr;
+ pParse = pWC->pParse;
+ idxaff = pIdx->pTable->aCol[iOrigCol].affinity;
+ if( !sqlite3IndexAffinityOk(pX, idxaff) ){
+ continue;
+ }
+
+ /* Figure out the collation sequence required from an index for
+ ** it to be useful for optimising expression pX. Store this
+ ** value in variable pColl.
+ */
+ assert(pX->pLeft);
+ pColl = sqlite3BinaryCompareCollSeq(pParse,pX->pLeft,pX->pRight);
+ if( pColl==0 ) pColl = pParse->db->pDfltColl;
+
+ for(j=0; pIdx->aiColumn[j]!=iOrigCol; j++){
+ if( NEVER(j>=pIdx->nColumn) ) return 0;
+ }
+ if( sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ){
+ continue;
+ }
+ }
+ if( pTerm->prereqRight==0 && (pTerm->eOperator&WO_EQ)!=0 ){
+ pResult = pTerm;
+ goto findTerm_success;
+ }else if( pResult==0 ){
+ pResult = pTerm;
+ }
+ }
+ if( (pTerm->eOperator & WO_EQUIV)!=0
+ && nEquiv<ArraySize(aEquiv)
+ ){
+ pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight);
+ assert( pX->op==TK_COLUMN );
+ for(j=0; j<nEquiv; j+=2){
+ if( aEquiv[j]==pX->iTable && aEquiv[j+1]==pX->iColumn ) break;
+ }
+ if( j==nEquiv ){
+ aEquiv[j] = pX->iTable;
+ aEquiv[j+1] = pX->iColumn;
+ nEquiv += 2;
+ }
}
- if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
}
- return pTerm;
}
}
+ if( iEquiv>=nEquiv ) break;
+ iCur = aEquiv[iEquiv++];
+ iColumn = aEquiv[iEquiv++];
}
- return 0;
+findTerm_success:
+ return pResult;
}
/* Forward reference */
@@ -817,7 +920,7 @@ static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
**
** CASE 1:
**
-** If all subterms are of the form T.C=expr for some single column of C
+** If all subterms are of the form T.C=expr for some single column of C and
** a single table T (as shown in example B above) then create a new virtual
** term that is an equivalent IN expression. In other words, if the term
** being analyzed is:
@@ -905,11 +1008,10 @@ static void exprAnalyzeOrTerm(
** Compute the set of tables that might satisfy cases 1 or 2.
*/
indexable = ~(Bitmask)0;
- chngToIN = ~(pWC->vmask);
+ chngToIN = ~(Bitmask)0;
for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
WhereAndInfo *pAndInfo;
- assert( pOrTerm->eOperator==0 );
assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
chngToIN = 0;
pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
@@ -948,7 +1050,7 @@ static void exprAnalyzeOrTerm(
b |= getMask(pMaskSet, pOther->leftCursor);
}
indexable &= b;
- if( pOrTerm->eOperator!=WO_EQ ){
+ if( (pOrTerm->eOperator & WO_EQ)==0 ){
chngToIN = 0;
}else{
chngToIN &= b;
@@ -999,7 +1101,7 @@ static void exprAnalyzeOrTerm(
for(j=0; j<2 && !okToChngToIN; j++){
pOrTerm = pOrWc->a;
for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
- assert( pOrTerm->eOperator==WO_EQ );
+ assert( pOrTerm->eOperator & WO_EQ );
pOrTerm->wtFlags &= ~TERM_OR_OK;
if( pOrTerm->leftCursor==iCursor ){
/* This is the 2-bit case and we are on the second iteration and
@@ -1025,7 +1127,7 @@ static void exprAnalyzeOrTerm(
/* No candidate table+column was found. This can only occur
** on the second iteration */
assert( j==1 );
- assert( (chngToIN&(chngToIN-1))==0 );
+ assert( IsPowerOfTwo(chngToIN) );
assert( chngToIN==getMask(pMaskSet, iCursor) );
break;
}
@@ -1035,7 +1137,7 @@ static void exprAnalyzeOrTerm(
** table and column is common to every term in the OR clause */
okToChngToIN = 1;
for(; i>=0 && okToChngToIN; i--, pOrTerm++){
- assert( pOrTerm->eOperator==WO_EQ );
+ assert( pOrTerm->eOperator & WO_EQ );
if( pOrTerm->leftCursor!=iCursor ){
pOrTerm->wtFlags &= ~TERM_OR_OK;
}else if( pOrTerm->u.leftColumn!=iColumn ){
@@ -1071,7 +1173,7 @@ static void exprAnalyzeOrTerm(
for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
- assert( pOrTerm->eOperator==WO_EQ );
+ assert( pOrTerm->eOperator & WO_EQ );
assert( pOrTerm->leftCursor==iCursor );
assert( pOrTerm->u.leftColumn==iColumn );
pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
@@ -1101,7 +1203,6 @@ static void exprAnalyzeOrTerm(
}
#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
-
/*
** The input to this routine is an WhereTerm structure with only the
** "pExpr" field filled in. The job of this routine is to analyze the
@@ -1144,6 +1245,7 @@ static void exprAnalyze(
pTerm = &pWC->a[idxTerm];
pMaskSet = pWC->pMaskSet;
pExpr = pTerm->pExpr;
+ assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
op = pExpr->op;
if( op==TK_IN ){
@@ -1169,17 +1271,19 @@ static void exprAnalyze(
pTerm->leftCursor = -1;
pTerm->iParent = -1;
pTerm->eOperator = 0;
- if( allowedOp(op) && (pTerm->prereqRight & prereqLeft)==0 ){
- Expr *pLeft = pExpr->pLeft;
- Expr *pRight = pExpr->pRight;
+ if( allowedOp(op) ){
+ Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
+ Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
+ u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
if( pLeft->op==TK_COLUMN ){
pTerm->leftCursor = pLeft->iTable;
pTerm->u.leftColumn = pLeft->iColumn;
- pTerm->eOperator = operatorMask(op);
+ pTerm->eOperator = operatorMask(op) & opMask;
}
if( pRight && pRight->op==TK_COLUMN ){
WhereTerm *pNew;
Expr *pDup;
+ u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
if( pTerm->leftCursor>=0 ){
int idxNew;
pDup = sqlite3ExprDup(db, pExpr, 0);
@@ -1194,18 +1298,25 @@ static void exprAnalyze(
pTerm = &pWC->a[idxTerm];
pTerm->nChild = 1;
pTerm->wtFlags |= TERM_COPIED;
+ if( pExpr->op==TK_EQ
+ && !ExprHasProperty(pExpr, EP_FromJoin)
+ && OptimizationEnabled(db, SQLITE_Transitive)
+ ){
+ pTerm->eOperator |= WO_EQUIV;
+ eExtraOp = WO_EQUIV;
+ }
}else{
pDup = pExpr;
pNew = pTerm;
}
exprCommute(pParse, pDup);
- pLeft = pDup->pLeft;
+ pLeft = sqlite3ExprSkipCollate(pDup->pLeft);
pNew->leftCursor = pLeft->iTable;
pNew->u.leftColumn = pLeft->iColumn;
testcase( (prereqLeft | extraRight) != prereqLeft );
pNew->prereqRight = prereqLeft | extraRight;
pNew->prereqAll = prereqAll;
- pNew->eOperator = operatorMask(pDup->op);
+ pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
}
}
@@ -1278,7 +1389,7 @@ static void exprAnalyze(
Expr *pNewExpr2;
int idxNew1;
int idxNew2;
- CollSeq *pColl; /* Collating sequence to use */
+ Token sCollSeqName; /* Name of collating sequence */
pLeft = pExpr->x.pList->a[1].pExpr;
pStr2 = sqlite3ExprDup(db, pStr1, 0);
@@ -1300,16 +1411,19 @@ static void exprAnalyze(
}
*pC = c + 1;
}
- pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, noCase ? "NOCASE" : "BINARY",0);
+ sCollSeqName.z = noCase ? "NOCASE" : "BINARY";
+ sCollSeqName.n = 6;
+ pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
- sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl),
- pStr1, 0);
+ sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName),
+ pStr1, 0);
idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
testcase( idxNew1==0 );
exprAnalyze(pSrc, pWC, idxNew1);
+ pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
- sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl),
- pStr2, 0);
+ sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName),
+ pStr2, 0);
idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
testcase( idxNew2==0 );
exprAnalyze(pSrc, pWC, idxNew2);
@@ -1407,25 +1521,6 @@ static void exprAnalyze(
}
/*
-** Return TRUE if any of the expressions in pList->a[iFirst...] contain
-** a reference to any table other than the iBase table.
-*/
-static int referencesOtherTables(
- ExprList *pList, /* Search expressions in ths list */
- WhereMaskSet *pMaskSet, /* Mapping from tables to bitmaps */
- int iFirst, /* Be searching with the iFirst-th expression */
- int iBase /* Ignore references to this table */
-){
- Bitmask allowed = ~getMask(pMaskSet, iBase);
- while( iFirst<pList->nExpr ){
- if( (exprTableUsage(pMaskSet, pList->a[iFirst++].pExpr)&allowed)!=0 ){
- return 1;
- }
- }
- return 0;
-}
-
-/*
** This function searches the expression list passed as the second argument
** for an expression of type TK_COLUMN that refers to the same column and
** uses the same collation sequence as the iCol'th column of index pIdx.
@@ -1446,12 +1541,12 @@ static int findIndexCol(
const char *zColl = pIdx->azColl[iCol];
for(i=0; i<pList->nExpr; i++){
- Expr *p = pList->a[i].pExpr;
+ Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr);
if( p->op==TK_COLUMN
&& p->iColumn==pIdx->aiColumn[iCol]
&& p->iTable==iBase
){
- CollSeq *pColl = sqlite3ExprCollSeq(pParse, p);
+ CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){
return i;
}
@@ -1484,7 +1579,8 @@ static int isDistinctIndex(
Bitmask mask = 0; /* Mask of unaccounted for pDistinct exprs */
int i; /* Iterator variable */
- if( pIdx->zName==0 || pDistinct==0 || pDistinct->nExpr>=BMS ) return 0;
+ assert( pDistinct!=0 );
+ if( pIdx->zName==0 || pDistinct->nExpr>=BMS ) return 0;
testcase( pDistinct->nExpr==BMS-1 );
/* Loop through all the expressions in the distinct list. If any of them
@@ -1497,7 +1593,7 @@ static int isDistinctIndex(
*/
for(i=0; i<pDistinct->nExpr; i++){
WhereTerm *pTerm;
- Expr *p = pDistinct->a[i].pExpr;
+ Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
if( p->op!=TK_COLUMN ) return 0;
pTerm = findTerm(pWC, p->iTable, p->iColumn, ~(Bitmask)0, WO_EQ, 0);
if( pTerm ){
@@ -1549,7 +1645,7 @@ static int isDistinctRedundant(
** current SELECT is a correlated sub-query.
*/
for(i=0; i<pDistinct->nExpr; i++){
- Expr *p = pDistinct->a[i].pExpr;
+ Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
}
@@ -1587,165 +1683,6 @@ static int isDistinctRedundant(
}
/*
-** This routine decides if pIdx can be used to satisfy the ORDER BY
-** clause. If it can, it returns 1. If pIdx cannot satisfy the
-** ORDER BY clause, this routine returns 0.
-**
-** pOrderBy is an ORDER BY clause from a SELECT statement. pTab is the
-** left-most table in the FROM clause of that same SELECT statement and
-** the table has a cursor number of "base". pIdx is an index on pTab.
-**
-** nEqCol is the number of columns of pIdx that are used as equality
-** constraints. Any of these columns may be missing from the ORDER BY
-** clause and the match can still be a success.
-**
-** All terms of the ORDER BY that match against the index must be either
-** ASC or DESC. (Terms of the ORDER BY clause past the end of a UNIQUE
-** index do not need to satisfy this constraint.) The *pbRev value is
-** set to 1 if the ORDER BY clause is all DESC and it is set to 0 if
-** the ORDER BY clause is all ASC.
-*/
-static int isSortingIndex(
- Parse *pParse, /* Parsing context */
- WhereMaskSet *pMaskSet, /* Mapping from table cursor numbers to bitmaps */
- Index *pIdx, /* The index we are testing */
- int base, /* Cursor number for the table to be sorted */
- ExprList *pOrderBy, /* The ORDER BY clause */
- int nEqCol, /* Number of index columns with == constraints */
- int wsFlags, /* Index usages flags */
- int *pbRev /* Set to 1 if ORDER BY is DESC */
-){
- int i, j; /* Loop counters */
- int sortOrder = 0; /* XOR of index and ORDER BY sort direction */
- int nTerm; /* Number of ORDER BY terms */
- struct ExprList_item *pTerm; /* A term of the ORDER BY clause */
- sqlite3 *db = pParse->db;
-
- if( !pOrderBy ) return 0;
- if( wsFlags & WHERE_COLUMN_IN ) return 0;
- if( pIdx->bUnordered ) return 0;
-
- nTerm = pOrderBy->nExpr;
- assert( nTerm>0 );
-
- /* Argument pIdx must either point to a 'real' named index structure,
- ** or an index structure allocated on the stack by bestBtreeIndex() to
- ** represent the rowid index that is part of every table. */
- assert( pIdx->zName || (pIdx->nColumn==1 && pIdx->aiColumn[0]==-1) );
-
- /* Match terms of the ORDER BY clause against columns of
- ** the index.
- **
- ** Note that indices have pIdx->nColumn regular columns plus
- ** one additional column containing the rowid. The rowid column
- ** of the index is also allowed to match against the ORDER BY
- ** clause.
- */
- for(i=j=0, pTerm=pOrderBy->a; j<nTerm && i<=pIdx->nColumn; i++){
- Expr *pExpr; /* The expression of the ORDER BY pTerm */
- CollSeq *pColl; /* The collating sequence of pExpr */
- int termSortOrder; /* Sort order for this term */
- int iColumn; /* The i-th column of the index. -1 for rowid */
- int iSortOrder; /* 1 for DESC, 0 for ASC on the i-th index term */
- const char *zColl; /* Name of the collating sequence for i-th index term */
-
- pExpr = pTerm->pExpr;
- if( pExpr->op!=TK_COLUMN || pExpr->iTable!=base ){
- /* Can not use an index sort on anything that is not a column in the
- ** left-most table of the FROM clause */
- break;
- }
- pColl = sqlite3ExprCollSeq(pParse, pExpr);
- if( !pColl ){
- pColl = db->pDfltColl;
- }
- if( pIdx->zName && i<pIdx->nColumn ){
- iColumn = pIdx->aiColumn[i];
- if( iColumn==pIdx->pTable->iPKey ){
- iColumn = -1;
- }
- iSortOrder = pIdx->aSortOrder[i];
- zColl = pIdx->azColl[i];
- }else{
- iColumn = -1;
- iSortOrder = 0;
- zColl = pColl->zName;
- }
- if( pExpr->iColumn!=iColumn || sqlite3StrICmp(pColl->zName, zColl) ){
- /* Term j of the ORDER BY clause does not match column i of the index */
- if( i<nEqCol ){
- /* If an index column that is constrained by == fails to match an
- ** ORDER BY term, that is OK. Just ignore that column of the index
- */
- continue;
- }else if( i==pIdx->nColumn ){
- /* Index column i is the rowid. All other terms match. */
- break;
- }else{
- /* If an index column fails to match and is not constrained by ==
- ** then the index cannot satisfy the ORDER BY constraint.
- */
- return 0;
- }
- }
- assert( pIdx->aSortOrder!=0 || iColumn==-1 );
- assert( pTerm->sortOrder==0 || pTerm->sortOrder==1 );
- assert( iSortOrder==0 || iSortOrder==1 );
- termSortOrder = iSortOrder ^ pTerm->sortOrder;
- if( i>nEqCol ){
- if( termSortOrder!=sortOrder ){
- /* Indices can only be used if all ORDER BY terms past the
- ** equality constraints are all either DESC or ASC. */
- return 0;
- }
- }else{
- sortOrder = termSortOrder;
- }
- j++;
- pTerm++;
- if( iColumn<0 && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
- /* If the indexed column is the primary key and everything matches
- ** so far and none of the ORDER BY terms to the right reference other
- ** tables in the join, then we are assured that the index can be used
- ** to sort because the primary key is unique and so none of the other
- ** columns will make any difference
- */
- j = nTerm;
- }
- }
-
- *pbRev = sortOrder!=0;
- if( j>=nTerm ){
- /* All terms of the ORDER BY clause are covered by this index so
- ** this index can be used for sorting. */
- return 1;
- }
- if( pIdx->onError!=OE_None && i==pIdx->nColumn
- && (wsFlags & WHERE_COLUMN_NULL)==0
- && !referencesOtherTables(pOrderBy, pMaskSet, j, base)
- ){
- Column *aCol = pIdx->pTable->aCol;
-
- /* All terms of this index match some prefix of the ORDER BY clause,
- ** the index is UNIQUE, and no terms on the tail of the ORDER BY
- ** refer to other tables in a join. So, assuming that the index entries
- ** visited contain no NULL values, then this index delivers rows in
- ** the required order.
- **
- ** It is not possible for any of the first nEqCol index fields to be
- ** NULL (since the corresponding "=" operator in the WHERE clause would
- ** not be true). So if all remaining index columns have NOT NULL
- ** constaints attached to them, we can be confident that the visited
- ** index entries are free of NULLs. */
- for(i=nEqCol; i<pIdx->nColumn; i++){
- if( aCol[pIdx->aiColumn[i]].notNull==0 ) break;
- }
- return (i==pIdx->nColumn);
- }
- return 0;
-}
-
-/*
** Prepare a crude estimate of the logarithm of the input value.
** The results need not be exact. This is only used for estimating
** the total cost of performing operations with O(logN) or O(NlogN)
@@ -1809,9 +1746,7 @@ static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){
/*
** Required because bestIndex() is called by bestOrClauseIndex()
*/
-static void bestIndex(
- Parse*, WhereClause*, struct SrcList_item*,
- Bitmask, Bitmask, ExprList*, WhereCost*);
+static void bestIndex(WhereBestIdx*);
/*
** This routine attempts to find an scanning strategy that can be used
@@ -1820,20 +1755,14 @@ static void bestIndex(
** The table associated with FROM clause term pSrc may be either a
** regular B-Tree table or a virtual table.
*/
-static void bestOrClauseIndex(
- Parse *pParse, /* The parsing context */
- WhereClause *pWC, /* The WHERE clause */
- struct SrcList_item *pSrc, /* The FROM clause term to search */
- Bitmask notReady, /* Mask of cursors not available for indexing */
- Bitmask notValid, /* Cursors not available for any purpose */
- ExprList *pOrderBy, /* The ORDER BY clause */
- WhereCost *pCost /* Lowest cost query plan */
-){
+static void bestOrClauseIndex(WhereBestIdx *p){
#ifndef SQLITE_OMIT_OR_OPTIMIZATION
- const int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */
+ WhereClause *pWC = p->pWC; /* The WHERE clause */
+ struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
+ const int iCur = pSrc->iCursor; /* The cursor of the table */
const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur); /* Bitmask for pSrc */
WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm]; /* End of pWC->a[] */
- WhereTerm *pTerm; /* A single term of the WHERE clause */
+ WhereTerm *pTerm; /* A single term of the WHERE clause */
/* The OR-clause optimization is disallowed if the INDEXED BY or
** NOT INDEXED clauses are used or if the WHERE_AND_ONLY bit is set. */
@@ -1846,8 +1775,8 @@ static void bestOrClauseIndex(
/* Search the WHERE clause terms for a usable WO_OR term. */
for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
- if( pTerm->eOperator==WO_OR
- && ((pTerm->prereqAll & ~maskSrc) & notReady)==0
+ if( (pTerm->eOperator & WO_OR)!=0
+ && ((pTerm->prereqAll & ~maskSrc) & p->notReady)==0
&& (pTerm->u.pOrInfo->indexable & maskSrc)!=0
){
WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
@@ -1857,15 +1786,19 @@ static void bestOrClauseIndex(
double rTotal = 0;
double nRow = 0;
Bitmask used = 0;
+ WhereBestIdx sBOI;
+ sBOI = *p;
+ sBOI.pOrderBy = 0;
+ sBOI.pDistinct = 0;
+ sBOI.ppIdxInfo = 0;
for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
- WhereCost sTermCost;
WHERETRACE(("... Multi-index OR testing for term %d of %d....\n",
(pOrTerm - pOrWC->a), (pTerm - pWC->a)
));
- if( pOrTerm->eOperator==WO_AND ){
- WhereClause *pAndWC = &pOrTerm->u.pAndInfo->wc;
- bestIndex(pParse, pAndWC, pSrc, notReady, notValid, 0, &sTermCost);
+ if( (pOrTerm->eOperator& WO_AND)!=0 ){
+ sBOI.pWC = &pOrTerm->u.pAndInfo->wc;
+ bestIndex(&sBOI);
}else if( pOrTerm->leftCursor==iCur ){
WhereClause tempWC;
tempWC.pParse = pWC->pParse;
@@ -1875,19 +1808,20 @@ static void bestOrClauseIndex(
tempWC.a = pOrTerm;
tempWC.wctrlFlags = 0;
tempWC.nTerm = 1;
- bestIndex(pParse, &tempWC, pSrc, notReady, notValid, 0, &sTermCost);
+ sBOI.pWC = &tempWC;
+ bestIndex(&sBOI);
}else{
continue;
}
- rTotal += sTermCost.rCost;
- nRow += sTermCost.plan.nRow;
- used |= sTermCost.used;
- if( rTotal>=pCost->rCost ) break;
+ rTotal += sBOI.cost.rCost;
+ nRow += sBOI.cost.plan.nRow;
+ used |= sBOI.cost.used;
+ if( rTotal>=p->cost.rCost ) break;
}
/* If there is an ORDER BY clause, increase the scan cost to account
** for the cost of the sort. */
- if( pOrderBy!=0 ){
+ if( p->pOrderBy!=0 ){
WHERETRACE(("... sorting increases OR cost %.9g to %.9g\n",
rTotal, rTotal+nRow*estLog(nRow)));
rTotal += nRow*estLog(nRow);
@@ -1897,12 +1831,13 @@ static void bestOrClauseIndex(
** less than the current cost stored in pCost, replace the contents
** of pCost. */
WHERETRACE(("... multi-index OR cost=%.9g nrow=%.9g\n", rTotal, nRow));
- if( rTotal<pCost->rCost ){
- pCost->rCost = rTotal;
- pCost->used = used;
- pCost->plan.nRow = nRow;
- pCost->plan.wsFlags = flags;
- pCost->plan.u.pTerm = pTerm;
+ if( rTotal<p->cost.rCost ){
+ p->cost.rCost = rTotal;
+ p->cost.used = used;
+ p->cost.plan.nRow = nRow;
+ p->cost.plan.nOBSat = p->i ? p->aLevel[p->i-1].plan.nOBSat : 0;
+ p->cost.plan.wsFlags = flags;
+ p->cost.plan.u.pTerm = pTerm;
}
}
}
@@ -1922,7 +1857,7 @@ static int termCanDriveIndex(
){
char aff;
if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
- if( pTerm->eOperator!=WO_EQ ) return 0;
+ if( (pTerm->eOperator & WO_EQ)==0 ) return 0;
if( (pTerm->prereqRight & notReady)!=0 ) return 0;
aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
@@ -1939,15 +1874,12 @@ static int termCanDriveIndex(
** is taken into account, then alter the query plan to use the
** transient index.
*/
-static void bestAutomaticIndex(
- Parse *pParse, /* The parsing context */
- WhereClause *pWC, /* The WHERE clause */
- struct SrcList_item *pSrc, /* The FROM clause term to search */
- Bitmask notReady, /* Mask of cursors that are not available */
- WhereCost *pCost /* Lowest cost query plan */
-){
- double nTableRow; /* Rows in the input table */
- double logN; /* log(nTableRow) */
+static void bestAutomaticIndex(WhereBestIdx *p){
+ Parse *pParse = p->pParse; /* The parsing context */
+ WhereClause *pWC = p->pWC; /* The WHERE clause */
+ struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
+ double nTableRow; /* Rows in the input table */
+ double logN; /* log(nTableRow) */
double costTempIdx; /* per-query cost of the transient index */
WhereTerm *pTerm; /* A single term of the WHERE clause */
WhereTerm *pWCEnd; /* End of pWC->a[] */
@@ -1961,10 +1893,16 @@ static void bestAutomaticIndex(
/* Automatic indices are disabled at run-time */
return;
}
- if( (pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)!=0 ){
+ if( (p->cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0
+ && (p->cost.plan.wsFlags & WHERE_COVER_SCAN)==0
+ ){
/* We already have some kind of index in use for this query. */
return;
}
+ if( pSrc->viaCoroutine ){
+ /* Cannot index a co-routine */
+ return;
+ }
if( pSrc->notIndexed ){
/* The NOT INDEXED clause appears in the SQL. */
return;
@@ -1979,7 +1917,7 @@ static void bestAutomaticIndex(
nTableRow = pTable->nRowEst;
logN = estLog(nTableRow);
costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1);
- if( costTempIdx>=pCost->rCost ){
+ if( costTempIdx>=p->cost.rCost ){
/* The cost of creating the transient table would be greater than
** doing the full table scan */
return;
@@ -1988,19 +1926,19 @@ static void bestAutomaticIndex(
/* Search for any equality comparison term */
pWCEnd = &pWC->a[pWC->nTerm];
for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
- if( termCanDriveIndex(pTerm, pSrc, notReady) ){
+ if( termCanDriveIndex(pTerm, pSrc, p->notReady) ){
WHERETRACE(("auto-index reduces cost from %.1f to %.1f\n",
- pCost->rCost, costTempIdx));
- pCost->rCost = costTempIdx;
- pCost->plan.nRow = logN + 1;
- pCost->plan.wsFlags = WHERE_TEMP_INDEX;
- pCost->used = pTerm->prereqRight;
+ p->cost.rCost, costTempIdx));
+ p->cost.rCost = costTempIdx;
+ p->cost.plan.nRow = logN + 1;
+ p->cost.plan.wsFlags = WHERE_TEMP_INDEX;
+ p->cost.used = pTerm->prereqRight;
break;
}
}
}
#else
-# define bestAutomaticIndex(A,B,C,D,E) /* no-op */
+# define bestAutomaticIndex(A) /* no-op */
#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
@@ -2161,12 +2099,11 @@ static void constructAutomaticIndex(
** responsibility of the caller to eventually release the structure
** by passing the pointer returned by this function to sqlite3_free().
*/
-static sqlite3_index_info *allocateIndexInfo(
- Parse *pParse,
- WhereClause *pWC,
- struct SrcList_item *pSrc,
- ExprList *pOrderBy
-){
+static sqlite3_index_info *allocateIndexInfo(WhereBestIdx *p){
+ Parse *pParse = p->pParse;
+ WhereClause *pWC = p->pWC;
+ struct SrcList_item *pSrc = p->pSrc;
+ ExprList *pOrderBy = p->pOrderBy;
int i, j;
int nTerm;
struct sqlite3_index_constraint *pIdxCons;
@@ -2182,10 +2119,10 @@ static sqlite3_index_info *allocateIndexInfo(
** to this virtual table */
for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
if( pTerm->leftCursor != pSrc->iCursor ) continue;
- assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
- testcase( pTerm->eOperator==WO_IN );
- testcase( pTerm->eOperator==WO_ISNULL );
- if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
+ assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
+ testcase( pTerm->eOperator & WO_IN );
+ testcase( pTerm->eOperator & WO_ISNULL );
+ if( pTerm->eOperator & (WO_ISNULL) ) continue;
if( pTerm->wtFlags & TERM_VNULL ) continue;
nTerm++;
}
@@ -2196,12 +2133,13 @@ static sqlite3_index_info *allocateIndexInfo(
*/
nOrderBy = 0;
if( pOrderBy ){
- for(i=0; i<pOrderBy->nExpr; i++){
+ int n = pOrderBy->nExpr;
+ for(i=0; i<n; i++){
Expr *pExpr = pOrderBy->a[i].pExpr;
if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
}
- if( i==pOrderBy->nExpr ){
- nOrderBy = pOrderBy->nExpr;
+ if( i==n){
+ nOrderBy = n;
}
}
@@ -2232,15 +2170,18 @@ static sqlite3_index_info *allocateIndexInfo(
pUsage;
for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
+ u8 op;
if( pTerm->leftCursor != pSrc->iCursor ) continue;
- assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
- testcase( pTerm->eOperator==WO_IN );
- testcase( pTerm->eOperator==WO_ISNULL );
- if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
+ assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
+ testcase( pTerm->eOperator & WO_IN );
+ testcase( pTerm->eOperator & WO_ISNULL );
+ if( pTerm->eOperator & (WO_ISNULL) ) continue;
if( pTerm->wtFlags & TERM_VNULL ) continue;
pIdxCons[j].iColumn = pTerm->u.leftColumn;
pIdxCons[j].iTermOffset = i;
- pIdxCons[j].op = (u8)pTerm->eOperator;
+ op = (u8)pTerm->eOperator & WO_ALL;
+ if( op==WO_IN ) op = WO_EQ;
+ pIdxCons[j].op = op;
/* The direct assignment in the previous line is possible only because
** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The
** following asserts verify this fact. */
@@ -2250,7 +2191,7 @@ static sqlite3_index_info *allocateIndexInfo(
assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
- assert( pTerm->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
+ assert( pTerm->eOperator & (WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
j++;
}
for(i=0; i<nOrderBy; i++){
@@ -2325,16 +2266,10 @@ static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
** routine takes care of freeing the sqlite3_index_info structure after
** everybody has finished with it.
*/
-static void bestVirtualIndex(
- Parse *pParse, /* The parsing context */
- WhereClause *pWC, /* The WHERE clause */
- struct SrcList_item *pSrc, /* The FROM clause term to search */
- Bitmask notReady, /* Mask of cursors not available for index */
- Bitmask notValid, /* Cursors not valid for any purpose */
- ExprList *pOrderBy, /* The order by clause */
- WhereCost *pCost, /* Lowest cost query plan */
- sqlite3_index_info **ppIdxInfo /* Index information passed to xBestIndex */
-){
+static void bestVirtualIndex(WhereBestIdx *p){
+ Parse *pParse = p->pParse; /* The parsing context */
+ WhereClause *pWC = p->pWC; /* The WHERE clause */
+ struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
Table *pTab = pSrc->pTab;
sqlite3_index_info *pIdxInfo;
struct sqlite3_index_constraint *pIdxCons;
@@ -2342,21 +2277,22 @@ static void bestVirtualIndex(
WhereTerm *pTerm;
int i, j;
int nOrderBy;
+ int bAllowIN; /* Allow IN optimizations */
double rCost;
/* Make sure wsFlags is initialized to some sane value. Otherwise, if the
** malloc in allocateIndexInfo() fails and this function returns leaving
** wsFlags in an uninitialized state, the caller may behave unpredictably.
*/
- memset(pCost, 0, sizeof(*pCost));
- pCost->plan.wsFlags = WHERE_VIRTUALTABLE;
+ memset(&p->cost, 0, sizeof(p->cost));
+ p->cost.plan.wsFlags = WHERE_VIRTUALTABLE;
/* If the sqlite3_index_info structure has not been previously
** allocated and initialized, then allocate and initialize it now.
*/
- pIdxInfo = *ppIdxInfo;
+ pIdxInfo = *p->ppIdxInfo;
if( pIdxInfo==0 ){
- *ppIdxInfo = pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pOrderBy);
+ *p->ppIdxInfo = pIdxInfo = allocateIndexInfo(p);
}
if( pIdxInfo==0 ){
return;
@@ -2376,65 +2312,112 @@ static void bestVirtualIndex(
assert( pTab->azModuleArg && pTab->azModuleArg[0] );
assert( sqlite3GetVTable(pParse->db, pTab) );
- /* Set the aConstraint[].usable fields and initialize all
- ** output variables to zero.
- **
- ** aConstraint[].usable is true for constraints where the right-hand
- ** side contains only references to tables to the left of the current
- ** table. In other words, if the constraint is of the form:
- **
- ** column = expr
- **
- ** and we are evaluating a join, then the constraint on column is
- ** only valid if all tables referenced in expr occur to the left
- ** of the table containing column.
- **
- ** The aConstraints[] array contains entries for all constraints
- ** on the current table. That way we only have to compute it once
- ** even though we might try to pick the best index multiple times.
- ** For each attempt at picking an index, the order of tables in the
- ** join might be different so we have to recompute the usable flag
- ** each time.
+ /* Try once or twice. On the first attempt, allow IN optimizations.
+ ** If an IN optimization is accepted by the virtual table xBestIndex
+ ** method, but the pInfo->aConstrainUsage.omit flag is not set, then
+ ** the query will not work because it might allow duplicate rows in
+ ** output. In that case, run the xBestIndex method a second time
+ ** without the IN constraints. Usually this loop only runs once.
+ ** The loop will exit using a "break" statement.
*/
- pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
- pUsage = pIdxInfo->aConstraintUsage;
- for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
- j = pIdxCons->iTermOffset;
- pTerm = &pWC->a[j];
- pIdxCons->usable = (pTerm->prereqRight&notReady) ? 0 : 1;
- }
- memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
- if( pIdxInfo->needToFreeIdxStr ){
- sqlite3_free(pIdxInfo->idxStr);
- }
- pIdxInfo->idxStr = 0;
- pIdxInfo->idxNum = 0;
- pIdxInfo->needToFreeIdxStr = 0;
- pIdxInfo->orderByConsumed = 0;
- /* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */
- pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((double)2);
- nOrderBy = pIdxInfo->nOrderBy;
- if( !pOrderBy ){
- pIdxInfo->nOrderBy = 0;
- }
-
- if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
- return;
- }
+ for(bAllowIN=1; 1; bAllowIN--){
+ assert( bAllowIN==0 || bAllowIN==1 );
- pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
- for(i=0; i<pIdxInfo->nConstraint; i++){
- if( pUsage[i].argvIndex>0 ){
- pCost->used |= pWC->a[pIdxCons[i].iTermOffset].prereqRight;
+ /* Set the aConstraint[].usable fields and initialize all
+ ** output variables to zero.
+ **
+ ** aConstraint[].usable is true for constraints where the right-hand
+ ** side contains only references to tables to the left of the current
+ ** table. In other words, if the constraint is of the form:
+ **
+ ** column = expr
+ **
+ ** and we are evaluating a join, then the constraint on column is
+ ** only valid if all tables referenced in expr occur to the left
+ ** of the table containing column.
+ **
+ ** The aConstraints[] array contains entries for all constraints
+ ** on the current table. That way we only have to compute it once
+ ** even though we might try to pick the best index multiple times.
+ ** For each attempt at picking an index, the order of tables in the
+ ** join might be different so we have to recompute the usable flag
+ ** each time.
+ */
+ pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
+ pUsage = pIdxInfo->aConstraintUsage;
+ for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
+ j = pIdxCons->iTermOffset;
+ pTerm = &pWC->a[j];
+ if( (pTerm->prereqRight&p->notReady)==0
+ && (bAllowIN || (pTerm->eOperator & WO_IN)==0)
+ ){
+ pIdxCons->usable = 1;
+ }else{
+ pIdxCons->usable = 0;
+ }
+ }
+ memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
+ if( pIdxInfo->needToFreeIdxStr ){
+ sqlite3_free(pIdxInfo->idxStr);
+ }
+ pIdxInfo->idxStr = 0;
+ pIdxInfo->idxNum = 0;
+ pIdxInfo->needToFreeIdxStr = 0;
+ pIdxInfo->orderByConsumed = 0;
+ /* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */
+ pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((double)2);
+ nOrderBy = pIdxInfo->nOrderBy;
+ if( !p->pOrderBy ){
+ pIdxInfo->nOrderBy = 0;
}
+
+ if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
+ return;
+ }
+
+ pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
+ for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
+ if( pUsage[i].argvIndex>0 ){
+ j = pIdxCons->iTermOffset;
+ pTerm = &pWC->a[j];
+ p->cost.used |= pTerm->prereqRight;
+ if( (pTerm->eOperator & WO_IN)!=0 ){
+ if( pUsage[i].omit==0 ){
+ /* Do not attempt to use an IN constraint if the virtual table
+ ** says that the equivalent EQ constraint cannot be safely omitted.
+ ** If we do attempt to use such a constraint, some rows might be
+ ** repeated in the output. */
+ break;
+ }
+ /* A virtual table that is constrained by an IN clause may not
+ ** consume the ORDER BY clause because (1) the order of IN terms
+ ** is not necessarily related to the order of output terms and
+ ** (2) Multiple outputs from a single IN value will not merge
+ ** together. */
+ pIdxInfo->orderByConsumed = 0;
+ }
+ }
+ }
+ if( i>=pIdxInfo->nConstraint ) break;
}
+ /* The orderByConsumed signal is only valid if all outer loops collectively
+ ** generate just a single row of output.
+ */
+ if( pIdxInfo->orderByConsumed ){
+ for(i=0; i<p->i; i++){
+ if( (p->aLevel[i].plan.wsFlags & WHERE_UNIQUE)==0 ){
+ pIdxInfo->orderByConsumed = 0;
+ }
+ }
+ }
+
/* If there is an ORDER BY clause, and the selected virtual table index
** does not satisfy it, increase the cost of the scan accordingly. This
** matches the processing for non-virtual tables in bestBtreeIndex().
*/
rCost = pIdxInfo->estimatedCost;
- if( pOrderBy && pIdxInfo->orderByConsumed==0 ){
+ if( p->pOrderBy && pIdxInfo->orderByConsumed==0 ){
rCost += estLog(rCost)*rCost;
}
@@ -2446,21 +2429,24 @@ static void bestVirtualIndex(
** is defined.
*/
if( (SQLITE_BIG_DBL/((double)2))<rCost ){
- pCost->rCost = (SQLITE_BIG_DBL/((double)2));
+ p->cost.rCost = (SQLITE_BIG_DBL/((double)2));
}else{
- pCost->rCost = rCost;
+ p->cost.rCost = rCost;
}
- pCost->plan.u.pVtabIdx = pIdxInfo;
+ p->cost.plan.u.pVtabIdx = pIdxInfo;
if( pIdxInfo->orderByConsumed ){
- pCost->plan.wsFlags |= WHERE_ORDERBY;
+ p->cost.plan.wsFlags |= WHERE_ORDERED;
+ p->cost.plan.nOBSat = nOrderBy;
+ }else{
+ p->cost.plan.nOBSat = p->i ? p->aLevel[p->i-1].plan.nOBSat : 0;
}
- pCost->plan.nEq = 0;
+ p->cost.plan.nEq = 0;
pIdxInfo->nOrderBy = nOrderBy;
/* Try to find a more efficient access pattern by using multiple indexes
** to optimize an OR expression within the WHERE clause.
*/
- bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
+ bestOrClauseIndex(p);
}
#endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -2548,10 +2534,8 @@ static int whereKeyStats(
pColl = db->pDfltColl;
assert( pColl->enc==SQLITE_UTF8 );
}else{
- pColl = sqlite3GetCollSeq(db, SQLITE_UTF8, 0, *pIdx->azColl);
+ pColl = sqlite3GetCollSeq(pParse, SQLITE_UTF8, 0, *pIdx->azColl);
if( pColl==0 ){
- sqlite3ErrorMsg(pParse, "no such collation sequence: %s",
- *pIdx->azColl);
return SQLITE_ERROR;
}
z = (const u8 *)sqlite3ValueText(pVal, pColl->enc);
@@ -2722,24 +2706,24 @@ static int whereRangeScanEst(
if( pLower ){
Expr *pExpr = pLower->pExpr->pRight;
rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
- assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
+ assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 );
if( rc==SQLITE_OK
&& whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK
){
iLower = a[0];
- if( pLower->eOperator==WO_GT ) iLower += a[1];
+ if( (pLower->eOperator & WO_GT)!=0 ) iLower += a[1];
}
sqlite3ValueFree(pRangeVal);
}
if( rc==SQLITE_OK && pUpper ){
Expr *pExpr = pUpper->pExpr->pRight;
rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
- assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
+ assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
if( rc==SQLITE_OK
&& whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK
){
iUpper = a[0];
- if( pUpper->eOperator==WO_LE ) iUpper += a[1];
+ if( (pUpper->eOperator & WO_LE)!=0 ) iUpper += a[1];
}
sqlite3ValueFree(pRangeVal);
}
@@ -2859,11 +2843,295 @@ static int whereInScanEst(
}
#endif /* defined(SQLITE_ENABLE_STAT3) */
+/*
+** Check to see if column iCol of the table with cursor iTab will appear
+** in sorted order according to the current query plan.
+**
+** Return values:
+**
+** 0 iCol is not ordered
+** 1 iCol has only a single value
+** 2 iCol is in ASC order
+** 3 iCol is in DESC order
+*/
+static int isOrderedColumn(
+ WhereBestIdx *p,
+ int iTab,
+ int iCol
+){
+ int i, j;
+ WhereLevel *pLevel = &p->aLevel[p->i-1];
+ Index *pIdx;
+ u8 sortOrder;
+ for(i=p->i-1; i>=0; i--, pLevel--){
+ if( pLevel->iTabCur!=iTab ) continue;
+ if( (pLevel->plan.wsFlags & WHERE_ALL_UNIQUE)!=0 ){
+ return 1;
+ }
+ assert( (pLevel->plan.wsFlags & WHERE_ORDERED)!=0 );
+ if( (pIdx = pLevel->plan.u.pIdx)!=0 ){
+ if( iCol<0 ){
+ sortOrder = 0;
+ testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
+ }else{
+ int n = pIdx->nColumn;
+ for(j=0; j<n; j++){
+ if( iCol==pIdx->aiColumn[j] ) break;
+ }
+ if( j>=n ) return 0;
+ sortOrder = pIdx->aSortOrder[j];
+ testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
+ }
+ }else{
+ if( iCol!=(-1) ) return 0;
+ sortOrder = 0;
+ testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
+ }
+ if( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 ){
+ assert( sortOrder==0 || sortOrder==1 );
+ testcase( sortOrder==1 );
+ sortOrder = 1 - sortOrder;
+ }
+ return sortOrder+2;
+ }
+ return 0;
+}
+
+/*
+** This routine decides if pIdx can be used to satisfy the ORDER BY
+** clause, either in whole or in part. The return value is the
+** cumulative number of terms in the ORDER BY clause that are satisfied
+** by the index pIdx and other indices in outer loops.
+**
+** The table being queried has a cursor number of "base". pIdx is the
+** index that is postulated for use to access the table.
+**
+** The *pbRev value is set to 0 order 1 depending on whether or not
+** pIdx should be run in the forward order or in reverse order.
+*/
+static int isSortingIndex(
+ WhereBestIdx *p, /* Best index search context */
+ Index *pIdx, /* The index we are testing */
+ int base, /* Cursor number for the table to be sorted */
+ int *pbRev, /* Set to 1 for reverse-order scan of pIdx */
+ int *pbObUnique /* ORDER BY column values will different in every row */
+){
+ int i; /* Number of pIdx terms used */
+ int j; /* Number of ORDER BY terms satisfied */
+ int sortOrder = 2; /* 0: forward. 1: backward. 2: unknown */
+ int nTerm; /* Number of ORDER BY terms */
+ struct ExprList_item *pOBItem;/* A term of the ORDER BY clause */
+ Table *pTab = pIdx->pTable; /* Table that owns index pIdx */
+ ExprList *pOrderBy; /* The ORDER BY clause */
+ Parse *pParse = p->pParse; /* Parser context */
+ sqlite3 *db = pParse->db; /* Database connection */
+ int nPriorSat; /* ORDER BY terms satisfied by outer loops */
+ int seenRowid = 0; /* True if an ORDER BY rowid term is seen */
+ int uniqueNotNull; /* pIdx is UNIQUE with all terms are NOT NULL */
+ int outerObUnique; /* Outer loops generate different values in
+ ** every row for the ORDER BY columns */
+
+ if( p->i==0 ){
+ nPriorSat = 0;
+ outerObUnique = 1;
+ }else{
+ u32 wsFlags = p->aLevel[p->i-1].plan.wsFlags;
+ nPriorSat = p->aLevel[p->i-1].plan.nOBSat;
+ if( (wsFlags & WHERE_ORDERED)==0 ){
+ /* This loop cannot be ordered unless the next outer loop is
+ ** also ordered */
+ return nPriorSat;
+ }
+ if( OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ){
+ /* Only look at the outer-most loop if the OrderByIdxJoin
+ ** optimization is disabled */
+ return nPriorSat;
+ }
+ testcase( wsFlags & WHERE_OB_UNIQUE );
+ testcase( wsFlags & WHERE_ALL_UNIQUE );
+ outerObUnique = (wsFlags & (WHERE_OB_UNIQUE|WHERE_ALL_UNIQUE))!=0;
+ }
+ pOrderBy = p->pOrderBy;
+ assert( pOrderBy!=0 );
+ if( pIdx->bUnordered ){
+ /* Hash indices (indicated by the "unordered" tag on sqlite_stat1) cannot
+ ** be used for sorting */
+ return nPriorSat;
+ }
+ nTerm = pOrderBy->nExpr;
+ uniqueNotNull = pIdx->onError!=OE_None;
+ assert( nTerm>0 );
+
+ /* Argument pIdx must either point to a 'real' named index structure,
+ ** or an index structure allocated on the stack by bestBtreeIndex() to
+ ** represent the rowid index that is part of every table. */
+ assert( pIdx->zName || (pIdx->nColumn==1 && pIdx->aiColumn[0]==-1) );
+
+ /* Match terms of the ORDER BY clause against columns of
+ ** the index.
+ **
+ ** Note that indices have pIdx->nColumn regular columns plus
+ ** one additional column containing the rowid. The rowid column
+ ** of the index is also allowed to match against the ORDER BY
+ ** clause.
+ */
+ j = nPriorSat;
+ for(i=0,pOBItem=&pOrderBy->a[j]; j<nTerm && i<=pIdx->nColumn; i++){
+ Expr *pOBExpr; /* The expression of the ORDER BY pOBItem */
+ CollSeq *pColl; /* The collating sequence of pOBExpr */
+ int termSortOrder; /* Sort order for this term */
+ int iColumn; /* The i-th column of the index. -1 for rowid */
+ int iSortOrder; /* 1 for DESC, 0 for ASC on the i-th index term */
+ int isEq; /* Subject to an == or IS NULL constraint */
+ int isMatch; /* ORDER BY term matches the index term */
+ const char *zColl; /* Name of collating sequence for i-th index term */
+ WhereTerm *pConstraint; /* A constraint in the WHERE clause */
+
+ /* If the next term of the ORDER BY clause refers to anything other than
+ ** a column in the "base" table, then this index will not be of any
+ ** further use in handling the ORDER BY. */
+ pOBExpr = sqlite3ExprSkipCollate(pOBItem->pExpr);
+ if( pOBExpr->op!=TK_COLUMN || pOBExpr->iTable!=base ){
+ break;
+ }
+
+ /* Find column number and collating sequence for the next entry
+ ** in the index */
+ if( pIdx->zName && i<pIdx->nColumn ){
+ iColumn = pIdx->aiColumn[i];
+ if( iColumn==pIdx->pTable->iPKey ){
+ iColumn = -1;
+ }
+ iSortOrder = pIdx->aSortOrder[i];
+ zColl = pIdx->azColl[i];
+ assert( zColl!=0 );
+ }else{
+ iColumn = -1;
+ iSortOrder = 0;
+ zColl = 0;
+ }
+
+ /* Check to see if the column number and collating sequence of the
+ ** index match the column number and collating sequence of the ORDER BY
+ ** clause entry. Set isMatch to 1 if they both match. */
+ if( pOBExpr->iColumn==iColumn ){
+ if( zColl ){
+ pColl = sqlite3ExprCollSeq(pParse, pOBItem->pExpr);
+ if( !pColl ) pColl = db->pDfltColl;
+ isMatch = sqlite3StrICmp(pColl->zName, zColl)==0;
+ }else{
+ isMatch = 1;
+ }
+ }else{
+ isMatch = 0;
+ }
+
+ /* termSortOrder is 0 or 1 for whether or not the access loop should
+ ** run forward or backwards (respectively) in order to satisfy this
+ ** term of the ORDER BY clause. */
+ assert( pOBItem->sortOrder==0 || pOBItem->sortOrder==1 );
+ assert( iSortOrder==0 || iSortOrder==1 );
+ termSortOrder = iSortOrder ^ pOBItem->sortOrder;
+
+ /* If X is the column in the index and ORDER BY clause, check to see
+ ** if there are any X= or X IS NULL constraints in the WHERE clause. */
+ pConstraint = findTerm(p->pWC, base, iColumn, p->notReady,
+ WO_EQ|WO_ISNULL|WO_IN, pIdx);
+ if( pConstraint==0 ){
+ isEq = 0;
+ }else if( (pConstraint->eOperator & WO_IN)!=0 ){
+ isEq = 0;
+ }else if( (pConstraint->eOperator & WO_ISNULL)!=0 ){
+ uniqueNotNull = 0;
+ isEq = 1; /* "X IS NULL" means X has only a single value */
+ }else if( pConstraint->prereqRight==0 ){
+ isEq = 1; /* Constraint "X=constant" means X has only a single value */
+ }else{
+ Expr *pRight = pConstraint->pExpr->pRight;
+ if( pRight->op==TK_COLUMN ){
+ WHERETRACE((" .. isOrderedColumn(tab=%d,col=%d)",
+ pRight->iTable, pRight->iColumn));
+ isEq = isOrderedColumn(p, pRight->iTable, pRight->iColumn);
+ WHERETRACE((" -> isEq=%d\n", isEq));
+
+ /* If the constraint is of the form X=Y where Y is an ordered value
+ ** in an outer loop, then make sure the sort order of Y matches the
+ ** sort order required for X. */
+ if( isMatch && isEq>=2 && isEq!=pOBItem->sortOrder+2 ){
+ testcase( isEq==2 );
+ testcase( isEq==3 );
+ break;
+ }
+ }else{
+ isEq = 0; /* "X=expr" places no ordering constraints on X */
+ }
+ }
+ if( !isMatch ){
+ if( isEq==0 ){
+ break;
+ }else{
+ continue;
+ }
+ }else if( isEq!=1 ){
+ if( sortOrder==2 ){
+ sortOrder = termSortOrder;
+ }else if( termSortOrder!=sortOrder ){
+ break;
+ }
+ }
+ j++;
+ pOBItem++;
+ if( iColumn<0 ){
+ seenRowid = 1;
+ break;
+ }else if( pTab->aCol[iColumn].notNull==0 && isEq!=1 ){
+ testcase( isEq==0 );
+ testcase( isEq==2 );
+ testcase( isEq==3 );
+ uniqueNotNull = 0;
+ }
+ }
+ if( seenRowid ){
+ uniqueNotNull = 1;
+ }else if( uniqueNotNull==0 || i<pIdx->nColumn ){
+ uniqueNotNull = 0;
+ }
+
+ /* If we have not found at least one ORDER BY term that matches the
+ ** index, then show no progress. */
+ if( pOBItem==&pOrderBy->a[nPriorSat] ) return nPriorSat;
+
+ /* Either the outer queries must generate rows where there are no two
+ ** rows with the same values in all ORDER BY columns, or else this
+ ** loop must generate just a single row of output. Example: Suppose
+ ** the outer loops generate A=1 and A=1, and this loop generates B=3
+ ** and B=4. Then without the following test, ORDER BY A,B would
+ ** generate the wrong order output: 1,3 1,4 1,3 1,4
+ */
+ if( outerObUnique==0 && uniqueNotNull==0 ) return nPriorSat;
+ *pbObUnique = uniqueNotNull;
+
+ /* Return the necessary scan order back to the caller */
+ *pbRev = sortOrder & 1;
+
+ /* If there was an "ORDER BY rowid" term that matched, or it is only
+ ** possible for a single row from this table to match, then skip over
+ ** any additional ORDER BY terms dealing with this table.
+ */
+ if( uniqueNotNull ){
+ /* Advance j over additional ORDER BY terms associated with base */
+ WhereMaskSet *pMS = p->pWC->pMaskSet;
+ Bitmask m = ~getMask(pMS, base);
+ while( j<nTerm && (exprTableUsage(pMS, pOrderBy->a[j].pExpr)&m)==0 ){
+ j++;
+ }
+ }
+ return j;
+}
/*
** Find the best query plan for accessing a particular table. Write the
-** best query plan and its cost into the WhereCost object supplied as the
-** last parameter.
+** best query plan and its cost into the p->cost.
**
** The lowest cost plan wins. The cost is an estimate of the amount of
** CPU and disk I/O needed to process the requested result.
@@ -2883,21 +3151,15 @@ static int whereInScanEst(
** SQLITE_BIG_DBL. If a plan is found that uses the named index,
** then the cost is calculated in the usual way.
**
-** If a NOT INDEXED clause (pSrc->notIndexed!=0) was attached to the table
+** If a NOT INDEXED clause was attached to the table
** in the SELECT statement, then no indexes are considered. However, the
** selected plan may still take advantage of the built-in rowid primary key
** index.
*/
-static void bestBtreeIndex(
- Parse *pParse, /* The parsing context */
- WhereClause *pWC, /* The WHERE clause */
- struct SrcList_item *pSrc, /* The FROM clause term to search */
- Bitmask notReady, /* Mask of cursors not available for indexing */
- Bitmask notValid, /* Cursors not available for any purpose */
- ExprList *pOrderBy, /* The ORDER BY clause */
- ExprList *pDistinct, /* The select-list if query is DISTINCT */
- WhereCost *pCost /* Lowest cost query plan */
-){
+static void bestBtreeIndex(WhereBestIdx *p){
+ Parse *pParse = p->pParse; /* The parsing context */
+ WhereClause *pWC = p->pWC; /* The WHERE clause */
+ struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */
Index *pProbe; /* An index we are evaluating */
Index *pIdx; /* Copy of pProbe, or zero for IPK index */
@@ -2906,11 +3168,16 @@ static void bestBtreeIndex(
Index sPk; /* A fake index object for the primary key */
tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
int aiColumnPk = -1; /* The aColumn[] value for the sPk index */
- int wsFlagMask; /* Allowed flags in pCost->plan.wsFlag */
+ int wsFlagMask; /* Allowed flags in p->cost.plan.wsFlag */
+ int nPriorSat; /* ORDER BY terms satisfied by outer loops */
+ int nOrderBy; /* Number of ORDER BY terms */
+ char bSortInit; /* Initializer for bSort in inner loop */
+ char bDistInit; /* Initializer for bDist in inner loop */
+
/* Initialize the cost to a worst-case value */
- memset(pCost, 0, sizeof(*pCost));
- pCost->rCost = SQLITE_BIG_DBL;
+ memset(&p->cost, 0, sizeof(p->cost));
+ p->cost.rCost = SQLITE_BIG_DBL;
/* If the pSrc table is the right table of a LEFT JOIN then we may not
** use an index to satisfy IS NULL constraints on that table. This is
@@ -2956,22 +3223,29 @@ static void bestBtreeIndex(
pIdx = 0;
}
+ nOrderBy = p->pOrderBy ? p->pOrderBy->nExpr : 0;
+ if( p->i ){
+ nPriorSat = p->aLevel[p->i-1].plan.nOBSat;
+ bSortInit = nPriorSat<nOrderBy;
+ bDistInit = 0;
+ }else{
+ nPriorSat = 0;
+ bSortInit = nOrderBy>0;
+ bDistInit = p->pDistinct!=0;
+ }
+
/* Loop over all indices looking for the best one to use
*/
for(; pProbe; pIdx=pProbe=pProbe->pNext){
const tRowcnt * const aiRowEst = pProbe->aiRowEst;
- double cost; /* Cost of using pProbe */
- double nRow; /* Estimated number of rows in result set */
+ WhereCost pc; /* Cost of using pProbe */
double log10N = (double)1; /* base-10 logarithm of nRow (inexact) */
- int rev; /* True to scan in reverse order */
- int wsFlags = 0;
- Bitmask used = 0;
/* The following variables are populated based on the properties of
** index being evaluated. They are then used to determine the expected
** cost and number of rows returned.
**
- ** nEq:
+ ** pc.plan.nEq:
** Number of equality terms that can be implemented using the index.
** In other words, the number of initial fields in the index that
** are used in == or IN or NOT NULL constraints of the WHERE clause.
@@ -3014,6 +3288,10 @@ static void bestBtreeIndex(
** external sort (i.e. scanning the index being evaluated will not
** correctly order records).
**
+ ** bDist:
+ ** Boolean. True if there is a DISTINCT clause that will require an
+ ** external btree.
+ **
** bLookup:
** Boolean. True if a table lookup is required for each index entry
** visited. In other words, true if this is not a covering index.
@@ -3029,29 +3307,35 @@ static void bestBtreeIndex(
** SELECT a, b FROM tbl WHERE a = 1;
** SELECT a, b, c FROM tbl WHERE a = 1;
*/
- int nEq; /* Number of == or IN terms matching index */
int bInEst = 0; /* True if "x IN (SELECT...)" seen */
int nInMul = 1; /* Number of distinct equalities to lookup */
double rangeDiv = (double)1; /* Estimated reduction in search space */
int nBound = 0; /* Number of range constraints seen */
- int bSort = !!pOrderBy; /* True if external sort required */
- int bDist = !!pDistinct; /* True if index cannot help with DISTINCT */
- int bLookup = 0; /* True if not a covering index */
+ char bSort = bSortInit; /* True if external sort required */
+ char bDist = bDistInit; /* True if index cannot help with DISTINCT */
+ char bLookup = 0; /* True if not a covering index */
WhereTerm *pTerm; /* A single term of the WHERE clause */
#ifdef SQLITE_ENABLE_STAT3
WhereTerm *pFirstTerm = 0; /* First term matching the index */
#endif
- /* Determine the values of nEq and nInMul */
- for(nEq=0; nEq<pProbe->nColumn; nEq++){
- int j = pProbe->aiColumn[nEq];
- pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
+ WHERETRACE((
+ " %s(%s):\n",
+ pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk")
+ ));
+ memset(&pc, 0, sizeof(pc));
+ pc.plan.nOBSat = nPriorSat;
+
+ /* Determine the values of pc.plan.nEq and nInMul */
+ for(pc.plan.nEq=0; pc.plan.nEq<pProbe->nColumn; pc.plan.nEq++){
+ int j = pProbe->aiColumn[pc.plan.nEq];
+ pTerm = findTerm(pWC, iCur, j, p->notReady, eqTermMask, pIdx);
if( pTerm==0 ) break;
- wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
+ pc.plan.wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
testcase( pTerm->pWC!=pWC );
if( pTerm->eOperator & WO_IN ){
Expr *pExpr = pTerm->pExpr;
- wsFlags |= WHERE_COLUMN_IN;
+ pc.plan.wsFlags |= WHERE_COLUMN_IN;
if( ExprHasProperty(pExpr, EP_xIsSelect) ){
/* "x IN (SELECT ...)": Assume the SELECT returns 25 rows */
nInMul *= 25;
@@ -3061,12 +3345,12 @@ static void bestBtreeIndex(
nInMul *= pExpr->x.pList->nExpr;
}
}else if( pTerm->eOperator & WO_ISNULL ){
- wsFlags |= WHERE_COLUMN_NULL;
+ pc.plan.wsFlags |= WHERE_COLUMN_NULL;
}
#ifdef SQLITE_ENABLE_STAT3
- if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
+ if( pc.plan.nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
#endif
- used |= pTerm->prereqRight;
+ pc.used |= pTerm->prereqRight;
}
/* If the index being considered is UNIQUE, and there is an equality
@@ -3075,65 +3359,82 @@ static void bestBtreeIndex(
** indicate this to the caller.
**
** Otherwise, if the search may find more than one row, test to see if
- ** there is a range constraint on indexed column (nEq+1) that can be
- ** optimized using the index.
+ ** there is a range constraint on indexed column (pc.plan.nEq+1) that
+ ** can be optimized using the index.
*/
- if( nEq==pProbe->nColumn && pProbe->onError!=OE_None ){
- testcase( wsFlags & WHERE_COLUMN_IN );
- testcase( wsFlags & WHERE_COLUMN_NULL );
- if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
- wsFlags |= WHERE_UNIQUE;
+ if( pc.plan.nEq==pProbe->nColumn && pProbe->onError!=OE_None ){
+ testcase( pc.plan.wsFlags & WHERE_COLUMN_IN );
+ testcase( pc.plan.wsFlags & WHERE_COLUMN_NULL );
+ if( (pc.plan.wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
+ pc.plan.wsFlags |= WHERE_UNIQUE;
+ if( p->i==0 || (p->aLevel[p->i-1].plan.wsFlags & WHERE_ALL_UNIQUE)!=0 ){
+ pc.plan.wsFlags |= WHERE_ALL_UNIQUE;
+ }
}
}else if( pProbe->bUnordered==0 ){
- int j = (nEq==pProbe->nColumn ? -1 : pProbe->aiColumn[nEq]);
- if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
- WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
- WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
- whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
+ int j;
+ j = (pc.plan.nEq==pProbe->nColumn ? -1 : pProbe->aiColumn[pc.plan.nEq]);
+ if( findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
+ WhereTerm *pTop, *pBtm;
+ pTop = findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE, pIdx);
+ pBtm = findTerm(pWC, iCur, j, p->notReady, WO_GT|WO_GE, pIdx);
+ whereRangeScanEst(pParse, pProbe, pc.plan.nEq, pBtm, pTop, &rangeDiv);
if( pTop ){
nBound = 1;
- wsFlags |= WHERE_TOP_LIMIT;
- used |= pTop->prereqRight;
+ pc.plan.wsFlags |= WHERE_TOP_LIMIT;
+ pc.used |= pTop->prereqRight;
testcase( pTop->pWC!=pWC );
}
if( pBtm ){
nBound++;
- wsFlags |= WHERE_BTM_LIMIT;
- used |= pBtm->prereqRight;
+ pc.plan.wsFlags |= WHERE_BTM_LIMIT;
+ pc.used |= pBtm->prereqRight;
testcase( pBtm->pWC!=pWC );
}
- wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
+ pc.plan.wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
}
}
/* If there is an ORDER BY clause and the index being considered will
** naturally scan rows in the required order, set the appropriate flags
- ** in wsFlags. Otherwise, if there is an ORDER BY clause but the index
- ** will scan rows in a different order, set the bSort variable. */
- if( isSortingIndex(
- pParse, pWC->pMaskSet, pProbe, iCur, pOrderBy, nEq, wsFlags, &rev)
- ){
- bSort = 0;
- wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY;
- wsFlags |= (rev ? WHERE_REVERSE : 0);
+ ** in pc.plan.wsFlags. Otherwise, if there is an ORDER BY clause but
+ ** the index will scan rows in a different order, set the bSort
+ ** variable. */
+ if( bSort && (pSrc->jointype & JT_LEFT)==0 ){
+ int bRev = 2;
+ int bObUnique = 0;
+ WHERETRACE((" --> before isSortIndex: nPriorSat=%d\n",nPriorSat));
+ pc.plan.nOBSat = isSortingIndex(p, pProbe, iCur, &bRev, &bObUnique);
+ WHERETRACE((" --> after isSortIndex: bRev=%d bObU=%d nOBSat=%d\n",
+ bRev, bObUnique, pc.plan.nOBSat));
+ if( nPriorSat<pc.plan.nOBSat || (pc.plan.wsFlags & WHERE_ALL_UNIQUE)!=0 ){
+ pc.plan.wsFlags |= WHERE_ORDERED;
+ if( bObUnique ) pc.plan.wsFlags |= WHERE_OB_UNIQUE;
+ }
+ if( nOrderBy==pc.plan.nOBSat ){
+ bSort = 0;
+ pc.plan.wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE;
+ }
+ if( bRev & 1 ) pc.plan.wsFlags |= WHERE_REVERSE;
}
/* If there is a DISTINCT qualifier and this index will scan rows in
** order of the DISTINCT expressions, clear bDist and set the appropriate
- ** flags in wsFlags. */
- if( isDistinctIndex(pParse, pWC, pProbe, iCur, pDistinct, nEq)
- && (wsFlags & WHERE_COLUMN_IN)==0
+ ** flags in pc.plan.wsFlags. */
+ if( bDist
+ && isDistinctIndex(pParse, pWC, pProbe, iCur, p->pDistinct, pc.plan.nEq)
+ && (pc.plan.wsFlags & WHERE_COLUMN_IN)==0
){
bDist = 0;
- wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_DISTINCT;
+ pc.plan.wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_DISTINCT;
}
/* If currently calculating the cost of using an index (not the IPK
** index), determine if all required column data may be obtained without
** using the main table (i.e. if the index is a covering
** index for this query). If it is, set the WHERE_IDX_ONLY flag in
- ** wsFlags. Otherwise, set the bLookup variable to true. */
- if( pIdx && wsFlags ){
+ ** pc.plan.wsFlags. Otherwise, set the bLookup variable to true. */
+ if( pIdx ){
Bitmask m = pSrc->colUsed;
int j;
for(j=0; j<pIdx->nColumn; j++){
@@ -3143,7 +3444,7 @@ static void bestBtreeIndex(
}
}
if( m==0 ){
- wsFlags |= WHERE_IDX_ONLY;
+ pc.plan.wsFlags |= WHERE_IDX_ONLY;
}else{
bLookup = 1;
}
@@ -3153,10 +3454,10 @@ static void bestBtreeIndex(
** Estimate the number of rows of output. For an "x IN (SELECT...)"
** constraint, do not let the estimate exceed half the rows in the table.
*/
- nRow = (double)(aiRowEst[nEq] * nInMul);
- if( bInEst && nRow*2>aiRowEst[0] ){
- nRow = aiRowEst[0]/2;
- nInMul = (int)(nRow / aiRowEst[nEq]);
+ pc.plan.nRow = (double)(aiRowEst[pc.plan.nEq] * nInMul);
+ if( bInEst && pc.plan.nRow*2>aiRowEst[0] ){
+ pc.plan.nRow = aiRowEst[0]/2;
+ nInMul = (int)(pc.plan.nRow / aiRowEst[pc.plan.nEq]);
}
#ifdef SQLITE_ENABLE_STAT3
@@ -3166,15 +3467,19 @@ static void bestBtreeIndex(
** to get a better estimate on the number of rows based on
** VALUE and how common that value is according to the histogram.
*/
- if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 && aiRowEst[1]>1 ){
+ if( pc.plan.nRow>(double)1 && pc.plan.nEq==1
+ && pFirstTerm!=0 && aiRowEst[1]>1 ){
assert( (pFirstTerm->eOperator & (WO_EQ|WO_ISNULL|WO_IN))!=0 );
if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
- testcase( pFirstTerm->eOperator==WO_EQ );
- testcase( pFirstTerm->eOperator==WO_ISNULL );
- whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
+ testcase( pFirstTerm->eOperator & WO_EQ );
+ testcase( pFirstTerm->eOperator & WO_EQUIV );
+ testcase( pFirstTerm->eOperator & WO_ISNULL );
+ whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight,
+ &pc.plan.nRow);
}else if( bInEst==0 ){
- assert( pFirstTerm->eOperator==WO_IN );
- whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
+ assert( pFirstTerm->eOperator & WO_IN );
+ whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList,
+ &pc.plan.nRow);
}
}
#endif /* SQLITE_ENABLE_STAT3 */
@@ -3182,8 +3487,8 @@ static void bestBtreeIndex(
/* Adjust the number of output rows and downward to reflect rows
** that are excluded by range constraints.
*/
- nRow = nRow/rangeDiv;
- if( nRow<1 ) nRow = 1;
+ pc.plan.nRow = pc.plan.nRow/rangeDiv;
+ if( pc.plan.nRow<1 ) pc.plan.nRow = 1;
/* Experiments run on real SQLite databases show that the time needed
** to do a binary search to locate a row in a table or index is roughly
@@ -3198,7 +3503,19 @@ static void bestBtreeIndex(
** So this computation assumes table records are about twice as big
** as index records
*/
- if( (wsFlags & WHERE_NOT_FULLSCAN)==0 ){
+ if( (pc.plan.wsFlags&~(WHERE_REVERSE|WHERE_ORDERED|WHERE_OB_UNIQUE))
+ ==WHERE_IDX_ONLY
+ && (pWC->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
+ && sqlite3GlobalConfig.bUseCis
+ && OptimizationEnabled(pParse->db, SQLITE_CoverIdxScan)
+ ){
+ /* This index is not useful for indexing, but it is a covering index.
+ ** A full-scan of the index might be a little faster than a full-scan
+ ** of the table, so give this case a cost slightly less than a table
+ ** scan. */
+ pc.rCost = aiRowEst[0]*3 + pProbe->nColumn;
+ pc.plan.wsFlags |= WHERE_COVER_SCAN|WHERE_COLUMN_RANGE;
+ }else if( (pc.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
/* The cost of a full table scan is a number of move operations equal
** to the number of rows in the table.
**
@@ -3208,10 +3525,15 @@ static void bestBtreeIndex(
** decision and one which we expect to revisit in the future. But
** it seems to be working well enough at the moment.
*/
- cost = aiRowEst[0]*4;
+ pc.rCost = aiRowEst[0]*4;
+ pc.plan.wsFlags &= ~WHERE_IDX_ONLY;
+ if( pIdx ){
+ pc.plan.wsFlags &= ~WHERE_ORDERED;
+ pc.plan.nOBSat = nPriorSat;
+ }
}else{
log10N = estLog(aiRowEst[0]);
- cost = nRow;
+ pc.rCost = pc.plan.nRow;
if( pIdx ){
if( bLookup ){
/* For an index lookup followed by a table lookup:
@@ -3219,20 +3541,20 @@ static void bestBtreeIndex(
** + nRow steps through the index
** + nRow table searches to lookup the table entry using the rowid
*/
- cost += (nInMul + nRow)*log10N;
+ pc.rCost += (nInMul + pc.plan.nRow)*log10N;
}else{
/* For a covering index:
** nInMul index searches to find the initial entry
** + nRow steps through the index
*/
- cost += nInMul*log10N;
+ pc.rCost += nInMul*log10N;
}
}else{
/* For a rowid primary key lookup:
** nInMult table searches to find the initial entry for each range
** + nRow steps through the table
*/
- cost += nInMul*log10N;
+ pc.rCost += nInMul*log10N;
}
}
@@ -3243,10 +3565,12 @@ static void bestBtreeIndex(
** difference and select C of 3.0.
*/
if( bSort ){
- cost += nRow*estLog(nRow)*3;
+ double m = estLog(pc.plan.nRow*(nOrderBy - pc.plan.nOBSat)/nOrderBy);
+ m *= (double)(pc.plan.nOBSat ? 2 : 3);
+ pc.rCost += pc.plan.nRow*m;
}
if( bDist ){
- cost += nRow*estLog(nRow)*3;
+ pc.rCost += pc.plan.nRow*estLog(pc.plan.nRow)*3;
}
/**** Cost of using this index has now been computed ****/
@@ -3267,25 +3591,25 @@ static void bestBtreeIndex(
** might be selected even when there exists an optimal index that has
** no such dependency.
*/
- if( nRow>2 && cost<=pCost->rCost ){
+ if( pc.plan.nRow>2 && pc.rCost<=p->cost.rCost ){
int k; /* Loop counter */
- int nSkipEq = nEq; /* Number of == constraints to skip */
+ int nSkipEq = pc.plan.nEq; /* Number of == constraints to skip */
int nSkipRange = nBound; /* Number of < constraints to skip */
Bitmask thisTab; /* Bitmap for pSrc */
thisTab = getMask(pWC->pMaskSet, iCur);
- for(pTerm=pWC->a, k=pWC->nTerm; nRow>2 && k; k--, pTerm++){
+ for(pTerm=pWC->a, k=pWC->nTerm; pc.plan.nRow>2 && k; k--, pTerm++){
if( pTerm->wtFlags & TERM_VIRTUAL ) continue;
- if( (pTerm->prereqAll & notValid)!=thisTab ) continue;
+ if( (pTerm->prereqAll & p->notValid)!=thisTab ) continue;
if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){
if( nSkipEq ){
- /* Ignore the first nEq equality matches since the index
+ /* Ignore the first pc.plan.nEq equality matches since the index
** has already accounted for these */
nSkipEq--;
}else{
/* Assume each additional equality match reduces the result
** set size by a factor of 10 */
- nRow /= 10;
+ pc.plan.nRow /= 10;
}
}else if( pTerm->eOperator & (WO_LT|WO_LE|WO_GT|WO_GE) ){
if( nSkipRange ){
@@ -3299,37 +3623,33 @@ static void bestBtreeIndex(
** more selective intentionally because of the subjective
** observation that indexed range constraints really are more
** selective in practice, on average. */
- nRow /= 3;
+ pc.plan.nRow /= 3;
}
- }else if( pTerm->eOperator!=WO_NOOP ){
+ }else if( (pTerm->eOperator & WO_NOOP)==0 ){
/* Any other expression lowers the output row count by half */
- nRow /= 2;
+ pc.plan.nRow /= 2;
}
}
- if( nRow<2 ) nRow = 2;
+ if( pc.plan.nRow<2 ) pc.plan.nRow = 2;
}
WHERETRACE((
- "%s(%s): nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
- " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
- pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
- nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags,
- notReady, log10N, nRow, cost, used
+ " nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%08x\n"
+ " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f\n"
+ " used=0x%llx nOBSat=%d\n",
+ pc.plan.nEq, nInMul, (int)rangeDiv, bSort, bLookup, pc.plan.wsFlags,
+ p->notReady, log10N, pc.plan.nRow, pc.rCost, pc.used,
+ pc.plan.nOBSat
));
/* If this index is the best we have seen so far, then record this
- ** index and its cost in the pCost structure.
+ ** index and its cost in the p->cost structure.
*/
- if( (!pIdx || wsFlags)
- && (cost<pCost->rCost || (cost<=pCost->rCost && nRow<pCost->plan.nRow))
- ){
- pCost->rCost = cost;
- pCost->used = used;
- pCost->plan.nRow = nRow;
- pCost->plan.wsFlags = (wsFlags&wsFlagMask);
- pCost->plan.nEq = nEq;
- pCost->plan.u.pIdx = pIdx;
+ if( (!pIdx || pc.plan.wsFlags) && compareCost(&pc, &p->cost) ){
+ p->cost = pc;
+ p->cost.plan.wsFlags &= wsFlagMask;
+ p->cost.plan.u.pIdx = pIdx;
}
/* If there was an INDEXED BY clause, then only that one index is
@@ -3344,27 +3664,26 @@ static void bestBtreeIndex(
/* If there is no ORDER BY clause and the SQLITE_ReverseOrder flag
** is set, then reverse the order that the index will be scanned
** in. This is used for application testing, to help find cases
- ** where application behaviour depends on the (undefined) order that
+ ** where application behavior depends on the (undefined) order that
** SQLite outputs rows in in the absence of an ORDER BY clause. */
- if( !pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
- pCost->plan.wsFlags |= WHERE_REVERSE;
+ if( !p->pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
+ p->cost.plan.wsFlags |= WHERE_REVERSE;
}
- assert( pOrderBy || (pCost->plan.wsFlags&WHERE_ORDERBY)==0 );
- assert( pCost->plan.u.pIdx==0 || (pCost->plan.wsFlags&WHERE_ROWID_EQ)==0 );
+ assert( p->pOrderBy || (p->cost.plan.wsFlags&WHERE_ORDERED)==0 );
+ assert( p->cost.plan.u.pIdx==0 || (p->cost.plan.wsFlags&WHERE_ROWID_EQ)==0 );
assert( pSrc->pIndex==0
- || pCost->plan.u.pIdx==0
- || pCost->plan.u.pIdx==pSrc->pIndex
+ || p->cost.plan.u.pIdx==0
+ || p->cost.plan.u.pIdx==pSrc->pIndex
);
- WHERETRACE(("best index is: %s\n",
- ((pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ? "none" :
- pCost->plan.u.pIdx ? pCost->plan.u.pIdx->zName : "ipk")
- ));
+ WHERETRACE((" best index is %s cost=%.1f\n",
+ p->cost.plan.u.pIdx ? p->cost.plan.u.pIdx->zName : "ipk",
+ p->cost.rCost));
- bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
- bestAutomaticIndex(pParse, pWC, pSrc, notReady, pCost);
- pCost->plan.wsFlags |= eqTermMask;
+ bestOrClauseIndex(p);
+ bestAutomaticIndex(p);
+ p->cost.plan.wsFlags |= eqTermMask;
}
/*
@@ -3372,28 +3691,28 @@ static void bestBtreeIndex(
** best query plan and its cost into the WhereCost object supplied
** as the last parameter. This function may calculate the cost of
** both real and virtual table scans.
+**
+** This function does not take ORDER BY or DISTINCT into account. Nor
+** does it remember the virtual table query plan. All it does is compute
+** the cost while determining if an OR optimization is applicable. The
+** details will be reconsidered later if the optimization is found to be
+** applicable.
*/
-static void bestIndex(
- Parse *pParse, /* The parsing context */
- WhereClause *pWC, /* The WHERE clause */
- struct SrcList_item *pSrc, /* The FROM clause term to search */
- Bitmask notReady, /* Mask of cursors not available for indexing */
- Bitmask notValid, /* Cursors not available for any purpose */
- ExprList *pOrderBy, /* The ORDER BY clause */
- WhereCost *pCost /* Lowest cost query plan */
-){
+static void bestIndex(WhereBestIdx *p){
#ifndef SQLITE_OMIT_VIRTUALTABLE
- if( IsVirtual(pSrc->pTab) ){
- sqlite3_index_info *p = 0;
- bestVirtualIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost,&p);
- if( p->needToFreeIdxStr ){
- sqlite3_free(p->idxStr);
+ if( IsVirtual(p->pSrc->pTab) ){
+ sqlite3_index_info *pIdxInfo = 0;
+ p->ppIdxInfo = &pIdxInfo;
+ bestVirtualIndex(p);
+ assert( pIdxInfo!=0 || p->pParse->db->mallocFailed );
+ if( pIdxInfo && pIdxInfo->needToFreeIdxStr ){
+ sqlite3_free(pIdxInfo->idxStr);
}
- sqlite3DbFree(pParse->db, p);
+ sqlite3DbFree(p->pParse->db, pIdxInfo);
}else
#endif
{
- bestBtreeIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, 0, pCost);
+ bestBtreeIndex(p);
}
}
@@ -3492,7 +3811,8 @@ static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
static int codeEqualityTerm(
Parse *pParse, /* The parsing context */
WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
- WhereLevel *pLevel, /* When level of the FROM clause we are working on */
+ WhereLevel *pLevel, /* The level of the FROM clause we are working on */
+ int iEq, /* Index of the equality term within this level */
int iTarget /* Attempt to leave results in this register */
){
Expr *pX = pTerm->pExpr;
@@ -3510,12 +3830,26 @@ static int codeEqualityTerm(
int eType;
int iTab;
struct InLoop *pIn;
+ u8 bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0;
+ if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0
+ && pLevel->plan.u.pIdx->aSortOrder[iEq]
+ ){
+ testcase( iEq==0 );
+ testcase( iEq==pLevel->plan.u.pIdx->nColumn-1 );
+ testcase( iEq>0 && iEq+1<pLevel->plan.u.pIdx->nColumn );
+ testcase( bRev );
+ bRev = !bRev;
+ }
assert( pX->op==TK_IN );
iReg = iTarget;
eType = sqlite3FindInIndex(pParse, pX, 0);
+ if( eType==IN_INDEX_INDEX_DESC ){
+ testcase( bRev );
+ bRev = !bRev;
+ }
iTab = pX->iTable;
- sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
+ sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
assert( pLevel->plan.wsFlags & WHERE_IN_ABLE );
if( pLevel->u.in.nIn==0 ){
pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
@@ -3533,6 +3867,7 @@ static int codeEqualityTerm(
}else{
pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
}
+ pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next;
sqlite3VdbeAddOp1(v, OP_IsNull, iReg);
}else{
pLevel->u.in.nIn = 0;
@@ -3627,7 +3962,7 @@ static int codeAllEqualityTerms(
** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
- r1 = codeEqualityTerm(pParse, pTerm, pLevel, regBase+j);
+ r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, regBase+j);
if( r1!=regBase+j ){
if( nReg==1 ){
sqlite3ReleaseTempReg(pParse, regBase);
@@ -3837,6 +4172,7 @@ static Bitmask codeOneLoopStart(
int addrCont; /* Jump here to continue with next cycle */
int iRowidReg = 0; /* Rowid is stored in this register, if not zero */
int iReleaseReg = 0; /* Temp register to free before returning */
+ Bitmask newNotReady; /* Return value */
pParse = pWInfo->pParse;
v = pParse->pVdbe;
@@ -3847,6 +4183,7 @@ static Bitmask codeOneLoopStart(
bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0;
omitTable = (pLevel->plan.wsFlags & WHERE_IDX_ONLY)!=0
&& (wctrlFlags & WHERE_FORCE_TABLE)==0;
+ VdbeNoopComment((v, "Begin Join Loop %d", iLevel));
/* Create labels for the "break" and "continue" instructions
** for the current loop. Jump to addrBrk to break out of a loop.
@@ -3871,12 +4208,23 @@ static Bitmask codeOneLoopStart(
VdbeComment((v, "init LEFT JOIN no-match flag"));
}
+ /* Special case of a FROM clause subquery implemented as a co-routine */
+ if( pTabItem->viaCoroutine ){
+ int regYield = pTabItem->regReturn;
+ sqlite3VdbeAddOp2(v, OP_Integer, pTabItem->addrFillSub-1, regYield);
+ pLevel->p2 = sqlite3VdbeAddOp1(v, OP_Yield, regYield);
+ VdbeComment((v, "next row of co-routine %s", pTabItem->pTab->zName));
+ sqlite3VdbeAddOp2(v, OP_If, regYield+1, addrBrk);
+ pLevel->op = OP_Goto;
+ }else
+
#ifndef SQLITE_OMIT_VIRTUALTABLE
if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
/* Case 0: The table is a virtual-table. Use the VFilter and VNext
** to access the data.
*/
int iReg; /* P3 Value for OP_VFilter */
+ int addrNotFound;
sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
int nConstraint = pVtabIdx->nConstraint;
struct sqlite3_index_constraint_usage *aUsage =
@@ -3886,11 +4234,18 @@ static Bitmask codeOneLoopStart(
sqlite3ExprCachePush(pParse);
iReg = sqlite3GetTempRange(pParse, nConstraint+2);
+ addrNotFound = pLevel->addrBrk;
for(j=1; j<=nConstraint; j++){
for(k=0; k<nConstraint; k++){
if( aUsage[k].argvIndex==j ){
- int iTerm = aConstraint[k].iTermOffset;
- sqlite3ExprCode(pParse, pWC->a[iTerm].pExpr->pRight, iReg+j+1);
+ int iTarget = iReg+j+1;
+ pTerm = &pWC->a[aConstraint[k].iTermOffset];
+ if( pTerm->eOperator & WO_IN ){
+ codeEqualityTerm(pParse, pTerm, pLevel, k, iTarget);
+ addrNotFound = pLevel->addrNxt;
+ }else{
+ sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
+ }
break;
}
}
@@ -3898,7 +4253,7 @@ static Bitmask codeOneLoopStart(
}
sqlite3VdbeAddOp2(v, OP_Integer, pVtabIdx->idxNum, iReg);
sqlite3VdbeAddOp2(v, OP_Integer, j-1, iReg+1);
- sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrBrk, iReg, pVtabIdx->idxStr,
+ sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, pVtabIdx->idxStr,
pVtabIdx->needToFreeIdxStr ? P4_MPRINTF : P4_STATIC);
pVtabIdx->needToFreeIdxStr = 0;
for(j=0; j<nConstraint; j++){
@@ -3925,13 +4280,13 @@ static Bitmask codeOneLoopStart(
pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0);
assert( pTerm!=0 );
assert( pTerm->pExpr!=0 );
- assert( pTerm->leftCursor==iCur );
assert( omitTable==0 );
testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
- iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, iReleaseReg);
+ iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, iReleaseReg);
addrNxt = pLevel->addrNxt;
sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt);
sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
+ sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1);
sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
VdbeComment((v, "pk"));
pLevel->op = OP_Noop;
@@ -4089,7 +4444,7 @@ static Bitmask codeOneLoopStart(
** this requires some special handling.
*/
if( (wctrlFlags&WHERE_ORDERBY_MIN)!=0
- && (pLevel->plan.wsFlags&WHERE_ORDERBY)
+ && (pLevel->plan.wsFlags&WHERE_ORDERED)
&& (pIdx->nColumn>nEq)
){
/* assert( pOrderBy->nExpr==1 ); */
@@ -4252,6 +4607,11 @@ static Bitmask codeOneLoopStart(
pLevel->op = OP_Next;
}
pLevel->p1 = iIdxCur;
+ if( pLevel->plan.wsFlags & WHERE_COVER_SCAN ){
+ pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
+ }else{
+ assert( pLevel->p5==0 );
+ }
}else
#ifndef SQLITE_OMIT_OR_OPTIMIZATION
@@ -4311,7 +4671,7 @@ static Bitmask codeOneLoopStart(
pTerm = pLevel->plan.u.pTerm;
assert( pTerm!=0 );
- assert( pTerm->eOperator==WO_OR );
+ assert( pTerm->eOperator & WO_OR );
assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
pOrWc = &pTerm->u.pOrInfo->wc;
pLevel->op = OP_Return;
@@ -4366,6 +4726,10 @@ static Bitmask codeOneLoopStart(
** the "interesting" terms of z - terms that did not originate in the
** ON or USING clause of a LEFT JOIN, and terms that are usable as
** indices.
+ **
+ ** This optimization also only applies if the (x1 OR x2 OR ...) term
+ ** is not contained in the ON clause of a LEFT JOIN.
+ ** See ticket http://www.sqlite.org/src/info/f2369304e4
*/
if( pWC->nTerm>1 ){
int iTerm;
@@ -4384,10 +4748,10 @@ static Bitmask codeOneLoopStart(
for(ii=0; ii<pOrWc->nTerm; ii++){
WhereTerm *pOrTerm = &pOrWc->a[ii];
- if( pOrTerm->leftCursor==iCur || pOrTerm->eOperator==WO_AND ){
+ if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
WhereInfo *pSubWInfo; /* Info for single OR-term scan */
Expr *pOrExpr = pOrTerm->pExpr;
- if( pAndExpr ){
+ if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
pAndExpr->pLeft = pOrExpr;
pOrExpr = pAndExpr;
}
@@ -4474,7 +4838,7 @@ static Bitmask codeOneLoopStart(
pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
}
- notReady &= ~getMask(pWC->pMaskSet, iCur);
+ newNotReady = notReady & ~getMask(pWC->pMaskSet, iCur);
/* Insert code to test every subexpression that can be completely
** computed using the current set of tables.
@@ -4488,7 +4852,7 @@ static Bitmask codeOneLoopStart(
testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */
testcase( pTerm->wtFlags & TERM_CODED );
if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
- if( (pTerm->prereqAll & notReady)!=0 ){
+ if( (pTerm->prereqAll & newNotReady)!=0 ){
testcase( pWInfo->untestedTerms==0
&& (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
pWInfo->untestedTerms = 1;
@@ -4503,6 +4867,33 @@ static Bitmask codeOneLoopStart(
pTerm->wtFlags |= TERM_CODED;
}
+ /* Insert code to test for implied constraints based on transitivity
+ ** of the "==" operator.
+ **
+ ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
+ ** and we are coding the t1 loop and the t2 loop has not yet coded,
+ ** then we cannot use the "t1.a=t2.b" constraint, but we can code
+ ** the implied "t1.a=123" constraint.
+ */
+ for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
+ Expr *pE;
+ WhereTerm *pAlt;
+ Expr sEq;
+ if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
+ if( pTerm->eOperator!=(WO_EQUIV|WO_EQ) ) continue;
+ if( pTerm->leftCursor!=iCur ) continue;
+ pE = pTerm->pExpr;
+ assert( !ExprHasProperty(pE, EP_FromJoin) );
+ assert( (pTerm->prereqRight & newNotReady)!=0 );
+ pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0);
+ if( pAlt==0 ) continue;
+ if( pAlt->wtFlags & (TERM_CODED) ) continue;
+ VdbeNoopComment((v, "begin transitive constraint"));
+ sEq = *pAlt->pExpr;
+ sEq.pLeft = pE->pLeft;
+ sqlite3ExprIfFalse(pParse, &sEq, addrCont, SQLITE_JUMPIFNULL);
+ }
+
/* For a LEFT OUTER JOIN, generate code that will record the fact that
** at least one row of the right table has matched the left table.
*/
@@ -4515,7 +4906,7 @@ static Bitmask codeOneLoopStart(
testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */
testcase( pTerm->wtFlags & TERM_CODED );
if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
- if( (pTerm->prereqAll & notReady)!=0 ){
+ if( (pTerm->prereqAll & newNotReady)!=0 ){
assert( pWInfo->untestedTerms );
continue;
}
@@ -4526,7 +4917,7 @@ static Bitmask codeOneLoopStart(
}
sqlite3ReleaseTempReg(pParse, iReleaseReg);
- return notReady;
+ return newNotReady;
}
#if defined(SQLITE_TEST)
@@ -4646,42 +5037,46 @@ static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
**
** ORDER BY CLAUSE PROCESSING
**
-** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
+** pOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
** if there is one. If there is no ORDER BY clause or if this routine
-** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
+** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
**
** If an index can be used so that the natural output order of the table
** scan is correct for the ORDER BY clause, then that index is used and
-** *ppOrderBy is set to NULL. This is an optimization that prevents an
-** unnecessary sort of the result set if an index appropriate for the
-** ORDER BY clause already exists.
+** the returned WhereInfo.nOBSat field is set to pOrderBy->nExpr. This
+** is an optimization that prevents an unnecessary sort of the result set
+** if an index appropriate for the ORDER BY clause already exists.
**
** If the where clause loops cannot be arranged to provide the correct
-** output order, then the *ppOrderBy is unchanged.
+** output order, then WhereInfo.nOBSat is 0.
*/
WhereInfo *sqlite3WhereBegin(
Parse *pParse, /* The parser context */
SrcList *pTabList, /* A list of all tables to be scanned */
Expr *pWhere, /* The WHERE clause */
- ExprList **ppOrderBy, /* An ORDER BY clause, or NULL */
+ ExprList *pOrderBy, /* An ORDER BY clause, or NULL */
ExprList *pDistinct, /* The select-list for DISTINCT queries - or NULL */
u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */
int iIdxCur /* If WHERE_ONETABLE_ONLY is set, index cursor number */
){
- int i; /* Loop counter */
int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
int nTabList; /* Number of elements in pTabList */
WhereInfo *pWInfo; /* Will become the return value of this function */
Vdbe *v = pParse->pVdbe; /* The virtual database engine */
Bitmask notReady; /* Cursors that are not yet positioned */
+ WhereBestIdx sWBI; /* Best index search context */
WhereMaskSet *pMaskSet; /* The expression mask set */
- WhereClause *pWC; /* Decomposition of the WHERE clause */
- struct SrcList_item *pTabItem; /* A single entry from pTabList */
- WhereLevel *pLevel; /* A single level in the pWInfo list */
- int iFrom; /* First unused FROM clause element */
+ WhereLevel *pLevel; /* A single level in pWInfo->a[] */
+ int iFrom; /* First unused FROM clause element */
int andFlags; /* AND-ed combination of all pWC->a[].wtFlags */
+ int ii; /* Loop counter */
sqlite3 *db; /* Database connection */
+
+ /* Variable initialization */
+ memset(&sWBI, 0, sizeof(sWBI));
+ sWBI.pParse = pParse;
+
/* The number of tables in the FROM clause is limited by the number of
** bits in a Bitmask
*/
@@ -4721,22 +5116,23 @@ WhereInfo *sqlite3WhereBegin(
pWInfo->pParse = pParse;
pWInfo->pTabList = pTabList;
pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
- pWInfo->pWC = pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo];
+ pWInfo->pWC = sWBI.pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo];
pWInfo->wctrlFlags = wctrlFlags;
pWInfo->savedNQueryLoop = pParse->nQueryLoop;
- pMaskSet = (WhereMaskSet*)&pWC[1];
+ pMaskSet = (WhereMaskSet*)&sWBI.pWC[1];
+ sWBI.aLevel = pWInfo->a;
/* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
- if( db->flags & SQLITE_DistinctOpt ) pDistinct = 0;
+ if( OptimizationDisabled(db, SQLITE_DistinctOpt) ) pDistinct = 0;
/* Split the WHERE clause into separate subexpressions where each
** subexpression is separated by an AND operator.
*/
initMaskSet(pMaskSet);
- whereClauseInit(pWC, pParse, pMaskSet, wctrlFlags);
+ whereClauseInit(sWBI.pWC, pParse, pMaskSet, wctrlFlags);
sqlite3ExprCodeConstants(pParse, pWhere);
- whereSplit(pWC, pWhere, TK_AND); /* IMP: R-15842-53296 */
+ whereSplit(sWBI.pWC, pWhere, TK_AND); /* IMP: R-15842-53296 */
/* Special case: a WHERE clause that is constant. Evaluate the
** expression and either jump over all of the code or fall thru.
@@ -4757,30 +5153,19 @@ WhereInfo *sqlite3WhereBegin(
** bitmask for all tables to the left of the join. Knowing the bitmask
** for all tables to the left of a left join is important. Ticket #3015.
**
- ** Configure the WhereClause.vmask variable so that bits that correspond
- ** to virtual table cursors are set. This is used to selectively disable
- ** the OR-to-IN transformation in exprAnalyzeOrTerm(). It is not helpful
- ** with virtual tables.
- **
** Note that bitmasks are created for all pTabList->nSrc tables in
** pTabList, not just the first nTabList tables. nTabList is normally
** equal to pTabList->nSrc but might be shortened to 1 if the
** WHERE_ONETABLE_ONLY flag is set.
*/
- assert( pWC->vmask==0 && pMaskSet->n==0 );
- for(i=0; i<pTabList->nSrc; i++){
- createMask(pMaskSet, pTabList->a[i].iCursor);
-#ifndef SQLITE_OMIT_VIRTUALTABLE
- if( ALWAYS(pTabList->a[i].pTab) && IsVirtual(pTabList->a[i].pTab) ){
- pWC->vmask |= ((Bitmask)1 << i);
- }
-#endif
+ for(ii=0; ii<pTabList->nSrc; ii++){
+ createMask(pMaskSet, pTabList->a[ii].iCursor);
}
#ifndef NDEBUG
{
Bitmask toTheLeft = 0;
- for(i=0; i<pTabList->nSrc; i++){
- Bitmask m = getMask(pMaskSet, pTabList->a[i].iCursor);
+ for(ii=0; ii<pTabList->nSrc; ii++){
+ Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor);
assert( (m-1)==toTheLeft );
toTheLeft |= m;
}
@@ -4792,7 +5177,7 @@ WhereInfo *sqlite3WhereBegin(
** want to analyze these virtual terms, so start analyzing at the end
** and work forward so that the added virtual terms are never processed.
*/
- exprAnalyzeAll(pTabList, pWC);
+ exprAnalyzeAll(pTabList, sWBI.pWC);
if( db->mallocFailed ){
goto whereBeginError;
}
@@ -4801,7 +5186,7 @@ WhereInfo *sqlite3WhereBegin(
** If it is, then set pDistinct to NULL and WhereInfo.eDistinct to
** WHERE_DISTINCT_UNIQUE to tell the caller to ignore the DISTINCT.
*/
- if( pDistinct && isDistinctRedundant(pParse, pTabList, pWC, pDistinct) ){
+ if( pDistinct && isDistinctRedundant(pParse, pTabList, sWBI.pWC, pDistinct) ){
pDistinct = 0;
pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
}
@@ -4821,22 +5206,26 @@ WhereInfo *sqlite3WhereBegin(
** This loop also figures out the nesting order of tables in the FROM
** clause.
*/
- notReady = ~(Bitmask)0;
+ sWBI.notValid = ~(Bitmask)0;
+ sWBI.pOrderBy = pOrderBy;
+ sWBI.n = nTabList;
+ sWBI.pDistinct = pDistinct;
andFlags = ~0;
WHERETRACE(("*** Optimizer Start ***\n"));
- for(i=iFrom=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){
+ for(sWBI.i=iFrom=0, pLevel=pWInfo->a; sWBI.i<nTabList; sWBI.i++, pLevel++){
WhereCost bestPlan; /* Most efficient plan seen so far */
Index *pIdx; /* Index for FROM table at pTabItem */
int j; /* For looping over FROM tables */
int bestJ = -1; /* The value of j */
Bitmask m; /* Bitmask value for j or bestJ */
int isOptimal; /* Iterator for optimal/non-optimal search */
+ int ckOptimal; /* Do the optimal scan check */
int nUnconstrained; /* Number tables without INDEXED BY */
Bitmask notIndexed; /* Mask of tables that cannot use an index */
memset(&bestPlan, 0, sizeof(bestPlan));
bestPlan.rCost = SQLITE_BIG_DBL;
- WHERETRACE(("*** Begin search for loop %d ***\n", i));
+ WHERETRACE(("*** Begin search for loop %d ***\n", sWBI.i));
/* Loop through the remaining entries in the FROM clause to find the
** next nested loop. The loop tests all FROM clause entries
@@ -4852,8 +5241,8 @@ WhereInfo *sqlite3WhereBegin(
** by waiting for other tables to run first. This "optimal" test works
** by first assuming that the FROM clause is on the inner loop and finding
** its query plan, then checking to see if that query plan uses any
- ** other FROM clause terms that are notReady. If no notReady terms are
- ** used then the "optimal" query plan works.
+ ** other FROM clause terms that are sWBI.notValid. If no notValid terms
+ ** are used then the "optimal" query plan works.
**
** Note that the WhereCost.nRow parameter for an optimal scan might
** not be as small as it would be if the table really were the innermost
@@ -4865,10 +5254,8 @@ WhereInfo *sqlite3WhereBegin(
** strategies were found by the first iteration. This second iteration
** is used to search for the lowest cost scan overall.
**
- ** Previous versions of SQLite performed only the second iteration -
- ** the next outermost loop was always that with the lowest overall
- ** cost. However, this meant that SQLite could select the wrong plan
- ** for scripts such as the following:
+ ** Without the optimal scan step (the first iteration) a suboptimal
+ ** plan might be chosen for queries like this:
**
** CREATE TABLE t1(a, b);
** CREATE TABLE t2(c, d);
@@ -4883,59 +5270,89 @@ WhereInfo *sqlite3WhereBegin(
*/
nUnconstrained = 0;
notIndexed = 0;
- for(isOptimal=(iFrom<nTabList-1); isOptimal>=0 && bestJ<0; isOptimal--){
- Bitmask mask; /* Mask of tables not yet ready */
- for(j=iFrom, pTabItem=&pTabList->a[j]; j<nTabList; j++, pTabItem++){
- int doNotReorder; /* True if this table should not be reordered */
- WhereCost sCost; /* Cost information from best[Virtual]Index() */
- ExprList *pOrderBy; /* ORDER BY clause for index to optimize */
- ExprList *pDist; /* DISTINCT clause for index to optimize */
-
- doNotReorder = (pTabItem->jointype & (JT_LEFT|JT_CROSS))!=0;
- if( j!=iFrom && doNotReorder ) break;
- m = getMask(pMaskSet, pTabItem->iCursor);
- if( (m & notReady)==0 ){
+
+ /* The optimal scan check only occurs if there are two or more tables
+ ** available to be reordered */
+ if( iFrom==nTabList-1 ){
+ ckOptimal = 0; /* Common case of just one table in the FROM clause */
+ }else{
+ ckOptimal = -1;
+ for(j=iFrom, sWBI.pSrc=&pTabList->a[j]; j<nTabList; j++, sWBI.pSrc++){
+ m = getMask(pMaskSet, sWBI.pSrc->iCursor);
+ if( (m & sWBI.notValid)==0 ){
if( j==iFrom ) iFrom++;
continue;
}
- mask = (isOptimal ? m : notReady);
- pOrderBy = ((i==0 && ppOrderBy )?*ppOrderBy:0);
- pDist = (i==0 ? pDistinct : 0);
- if( pTabItem->pIndex==0 ) nUnconstrained++;
+ if( j>iFrom && (sWBI.pSrc->jointype & (JT_LEFT|JT_CROSS))!=0 ) break;
+ if( ++ckOptimal ) break;
+ if( (sWBI.pSrc->jointype & JT_LEFT)!=0 ) break;
+ }
+ }
+ assert( ckOptimal==0 || ckOptimal==1 );
+
+ for(isOptimal=ckOptimal; isOptimal>=0 && bestJ<0; isOptimal--){
+ for(j=iFrom, sWBI.pSrc=&pTabList->a[j]; j<nTabList; j++, sWBI.pSrc++){
+ if( j>iFrom && (sWBI.pSrc->jointype & (JT_LEFT|JT_CROSS))!=0 ){
+ /* This break and one like it in the ckOptimal computation loop
+ ** above prevent table reordering across LEFT and CROSS JOINs.
+ ** The LEFT JOIN case is necessary for correctness. The prohibition
+ ** against reordering across a CROSS JOIN is an SQLite feature that
+ ** allows the developer to control table reordering */
+ break;
+ }
+ m = getMask(pMaskSet, sWBI.pSrc->iCursor);
+ if( (m & sWBI.notValid)==0 ){
+ assert( j>iFrom );
+ continue;
+ }
+ sWBI.notReady = (isOptimal ? m : sWBI.notValid);
+ if( sWBI.pSrc->pIndex==0 ) nUnconstrained++;
- WHERETRACE(("=== trying table %d with isOptimal=%d ===\n",
- j, isOptimal));
- assert( pTabItem->pTab );
+ WHERETRACE((" === trying table %d (%s) with isOptimal=%d ===\n",
+ j, sWBI.pSrc->pTab->zName, isOptimal));
+ assert( sWBI.pSrc->pTab );
#ifndef SQLITE_OMIT_VIRTUALTABLE
- if( IsVirtual(pTabItem->pTab) ){
- sqlite3_index_info **pp = &pWInfo->a[j].pIdxInfo;
- bestVirtualIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
- &sCost, pp);
+ if( IsVirtual(sWBI.pSrc->pTab) ){
+ sWBI.ppIdxInfo = &pWInfo->a[j].pIdxInfo;
+ bestVirtualIndex(&sWBI);
}else
#endif
{
- bestBtreeIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
- pDist, &sCost);
+ bestBtreeIndex(&sWBI);
}
- assert( isOptimal || (sCost.used&notReady)==0 );
+ assert( isOptimal || (sWBI.cost.used&sWBI.notValid)==0 );
/* If an INDEXED BY clause is present, then the plan must use that
** index if it uses any index at all */
- assert( pTabItem->pIndex==0
- || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
- || sCost.plan.u.pIdx==pTabItem->pIndex );
+ assert( sWBI.pSrc->pIndex==0
+ || (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
+ || sWBI.cost.plan.u.pIdx==sWBI.pSrc->pIndex );
- if( isOptimal && (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
+ if( isOptimal && (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
notIndexed |= m;
}
+ if( isOptimal ){
+ pWInfo->a[j].rOptCost = sWBI.cost.rCost;
+ }else if( ckOptimal ){
+ /* If two or more tables have nearly the same outer loop cost, but
+ ** very different inner loop (optimal) cost, we want to choose
+ ** for the outer loop that table which benefits the least from
+ ** being in the inner loop. The following code scales the
+ ** outer loop cost estimate to accomplish that. */
+ WHERETRACE((" scaling cost from %.1f to %.1f\n",
+ sWBI.cost.rCost,
+ sWBI.cost.rCost/pWInfo->a[j].rOptCost));
+ sWBI.cost.rCost /= pWInfo->a[j].rOptCost;
+ }
/* Conditions under which this table becomes the best so far:
**
** (1) The table must not depend on other tables that have not
- ** yet run.
+ ** yet run. (In other words, it must not depend on tables
+ ** in inner loops.)
**
- ** (2) A full-table-scan plan cannot supercede indexed plan unless
- ** the full-table-scan is an "optimal" plan as defined above.
+ ** (2) (This rule was removed on 2012-11-09. The scaling of the
+ ** cost using the optimal scan cost made this rule obsolete.)
**
** (3) All tables have an INDEXED BY clause or this table lacks an
** INDEXED BY clause or this table uses the specific
@@ -4946,43 +5363,47 @@ WhereInfo *sqlite3WhereBegin(
** The NEVER() comes about because rule (2) above prevents
** An indexable full-table-scan from reaching rule (3).
**
- ** (4) The plan cost must be lower than prior plans or else the
- ** cost must be the same and the number of rows must be lower.
+ ** (4) The plan cost must be lower than prior plans, where "cost"
+ ** is defined by the compareCost() function above.
*/
- if( (sCost.used&notReady)==0 /* (1) */
- && (bestJ<0 || (notIndexed&m)!=0 /* (2) */
- || (bestPlan.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
- || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0)
- && (nUnconstrained==0 || pTabItem->pIndex==0 /* (3) */
- || NEVER((sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
- && (bestJ<0 || sCost.rCost<bestPlan.rCost /* (4) */
- || (sCost.rCost<=bestPlan.rCost
- && sCost.plan.nRow<bestPlan.plan.nRow))
+ if( (sWBI.cost.used&sWBI.notValid)==0 /* (1) */
+ && (nUnconstrained==0 || sWBI.pSrc->pIndex==0 /* (3) */
+ || NEVER((sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
+ && (bestJ<0 || compareCost(&sWBI.cost, &bestPlan)) /* (4) */
){
- WHERETRACE(("=== table %d is best so far"
- " with cost=%g and nRow=%g\n",
- j, sCost.rCost, sCost.plan.nRow));
- bestPlan = sCost;
+ WHERETRACE((" === table %d (%s) is best so far\n"
+ " cost=%.1f, nRow=%.1f, nOBSat=%d, wsFlags=%08x\n",
+ j, sWBI.pSrc->pTab->zName,
+ sWBI.cost.rCost, sWBI.cost.plan.nRow,
+ sWBI.cost.plan.nOBSat, sWBI.cost.plan.wsFlags));
+ bestPlan = sWBI.cost;
bestJ = j;
}
- if( doNotReorder ) break;
+
+ /* In a join like "w JOIN x LEFT JOIN y JOIN z" make sure that
+ ** table y (and not table z) is always the next inner loop inside
+ ** of table x. */
+ if( (sWBI.pSrc->jointype & JT_LEFT)!=0 ) break;
}
}
assert( bestJ>=0 );
- assert( notReady & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
- WHERETRACE(("*** Optimizer selects table %d for loop %d"
- " with cost=%g and nRow=%g\n",
- bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow));
- /* The ALWAYS() that follows was added to hush up clang scan-build */
- if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 && ALWAYS(ppOrderBy) ){
- *ppOrderBy = 0;
- }
+ assert( sWBI.notValid & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
+ assert( bestJ==iFrom || (pTabList->a[iFrom].jointype & JT_LEFT)==0 );
+ testcase( bestJ>iFrom && (pTabList->a[iFrom].jointype & JT_CROSS)!=0 );
+ testcase( bestJ>iFrom && bestJ<nTabList-1
+ && (pTabList->a[bestJ+1].jointype & JT_LEFT)!=0 );
+ WHERETRACE(("*** Optimizer selects table %d (%s) for loop %d with:\n"
+ " cost=%.1f, nRow=%.1f, nOBSat=%d, wsFlags=0x%08x\n",
+ bestJ, pTabList->a[bestJ].pTab->zName,
+ pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow,
+ bestPlan.plan.nOBSat, bestPlan.plan.wsFlags));
if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
assert( pWInfo->eDistinct==0 );
pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
}
andFlags &= bestPlan.plan.wsFlags;
pLevel->plan = bestPlan.plan;
+ pLevel->iTabCur = pTabList->a[bestJ].iCursor;
testcase( bestPlan.plan.wsFlags & WHERE_INDEXED );
testcase( bestPlan.plan.wsFlags & WHERE_TEMP_INDEX );
if( bestPlan.plan.wsFlags & (WHERE_INDEXED|WHERE_TEMP_INDEX) ){
@@ -4996,7 +5417,7 @@ WhereInfo *sqlite3WhereBegin(
}else{
pLevel->iIdxCur = -1;
}
- notReady &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor);
+ sWBI.notValid &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor);
pLevel->iFrom = (u8)bestJ;
if( bestPlan.plan.nRow>=(double)1 ){
pParse->nQueryLoop *= bestPlan.plan.nRow;
@@ -5024,12 +5445,19 @@ WhereInfo *sqlite3WhereBegin(
if( pParse->nErr || db->mallocFailed ){
goto whereBeginError;
}
+ if( nTabList ){
+ pLevel--;
+ pWInfo->nOBSat = pLevel->plan.nOBSat;
+ }else{
+ pWInfo->nOBSat = 0;
+ }
/* If the total query only selects a single row, then the ORDER BY
** clause is irrelevant.
*/
- if( (andFlags & WHERE_UNIQUE)!=0 && ppOrderBy ){
- *ppOrderBy = 0;
+ if( (andFlags & WHERE_UNIQUE)!=0 && pOrderBy ){
+ assert( nTabList==0 || (pLevel->plan.wsFlags & WHERE_ALL_UNIQUE)!=0 );
+ pWInfo->nOBSat = pOrderBy->nExpr;
}
/* If the caller is an UPDATE or DELETE statement that is requesting
@@ -5049,13 +5477,13 @@ WhereInfo *sqlite3WhereBegin(
sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
notReady = ~(Bitmask)0;
pWInfo->nRowOut = (double)1;
- for(i=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){
+ for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
Table *pTab; /* Table to open */
int iDb; /* Index of database containing table/index */
+ struct SrcList_item *pTabItem;
pTabItem = &pTabList->a[pLevel->iFrom];
pTab = pTabItem->pTab;
- pLevel->iTabCur = pTabItem->iCursor;
pWInfo->nRowOut *= pLevel->plan.nRow;
iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
@@ -5066,6 +5494,8 @@ WhereInfo *sqlite3WhereBegin(
const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
int iCur = pTabItem->iCursor;
sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
+ }else if( IsVirtual(pTab) ){
+ /* noop */
}else
#endif
if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
@@ -5087,7 +5517,7 @@ WhereInfo *sqlite3WhereBegin(
}
#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){
- constructAutomaticIndex(pParse, pWC, pTabItem, notReady, pLevel);
+ constructAutomaticIndex(pParse, sWBI.pWC, pTabItem, notReady, pLevel);
}else
#endif
if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
@@ -5101,7 +5531,7 @@ WhereInfo *sqlite3WhereBegin(
VdbeComment((v, "%s", pIx->zName));
}
sqlite3CodeVerifySchema(pParse, iDb);
- notReady &= ~getMask(pWC->pMaskSet, pTabItem->iCursor);
+ notReady &= ~getMask(sWBI.pWC->pMaskSet, pTabItem->iCursor);
}
pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
if( db->mallocFailed ) goto whereBeginError;
@@ -5111,10 +5541,10 @@ WhereInfo *sqlite3WhereBegin(
** program.
*/
notReady = ~(Bitmask)0;
- for(i=0; i<nTabList; i++){
- pLevel = &pWInfo->a[i];
- explainOneScan(pParse, pTabList, pLevel, i, pLevel->iFrom, wctrlFlags);
- notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady);
+ for(ii=0; ii<nTabList; ii++){
+ pLevel = &pWInfo->a[ii];
+ explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags);
+ notReady = codeOneLoopStart(pWInfo, ii, wctrlFlags, notReady);
pWInfo->iContinue = pLevel->addrCont;
}
@@ -5125,16 +5555,20 @@ WhereInfo *sqlite3WhereBegin(
** the index is listed as "{}". If the primary key is used the
** index name is '*'.
*/
- for(i=0; i<nTabList; i++){
+ for(ii=0; ii<nTabList; ii++){
char *z;
int n;
- pLevel = &pWInfo->a[i];
+ int w;
+ struct SrcList_item *pTabItem;
+
+ pLevel = &pWInfo->a[ii];
+ w = pLevel->plan.wsFlags;
pTabItem = &pTabList->a[pLevel->iFrom];
z = pTabItem->zAlias;
if( z==0 ) z = pTabItem->pTab->zName;
n = sqlite3Strlen30(z);
if( n+nQPlan < sizeof(sqlite3_query_plan)-10 ){
- if( pLevel->plan.wsFlags & WHERE_IDX_ONLY ){
+ if( (w & WHERE_IDX_ONLY)!=0 && (w & WHERE_COVER_SCAN)==0 ){
memcpy(&sqlite3_query_plan[nQPlan], "{}", 2);
nQPlan += 2;
}else{
@@ -5143,12 +5577,12 @@ WhereInfo *sqlite3WhereBegin(
}
sqlite3_query_plan[nQPlan++] = ' ';
}
- testcase( pLevel->plan.wsFlags & WHERE_ROWID_EQ );
- testcase( pLevel->plan.wsFlags & WHERE_ROWID_RANGE );
- if( pLevel->plan.wsFlags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
+ testcase( w & WHERE_ROWID_EQ );
+ testcase( w & WHERE_ROWID_RANGE );
+ if( w & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
memcpy(&sqlite3_query_plan[nQPlan], "* ", 2);
nQPlan += 2;
- }else if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
+ }else if( (w & WHERE_INDEXED)!=0 && (w & WHERE_COVER_SCAN)==0 ){
n = sqlite3Strlen30(pLevel->plan.u.pIdx->zName);
if( n+nQPlan < sizeof(sqlite3_query_plan)-2 ){
memcpy(&sqlite3_query_plan[nQPlan], pLevel->plan.u.pIdx->zName, n);
@@ -5209,7 +5643,7 @@ void sqlite3WhereEnd(WhereInfo *pWInfo){
sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
- sqlite3VdbeAddOp2(v, OP_Next, pIn->iCur, pIn->addrInTop);
+ sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
}
sqlite3DbFree(db, pLevel->u.in.aInLoop);
diff --git a/test/8_3_names.test b/test/8_3_names.test
index b53e28a..1d63f5d 100644
--- a/test/8_3_names.test
+++ b/test/8_3_names.test
@@ -150,7 +150,7 @@ db close
forcedelete test.db
do_test 8_3_names-5.0 {
sqlite3 db file:./test.db?8_3_names=1
- register_wholenumber_module db
+ load_static_extension db wholenumber
db eval {
PRAGMA journal_mode=WAL;
CREATE TABLE t1(x);
@@ -160,7 +160,7 @@ do_test 8_3_names-5.0 {
UPDATE t1 SET x=x*2;
}
sqlite3 db2 file:./test.db?8_3_names=1
- register_wholenumber_module db2
+ load_static_extension db2 wholenumber
db2 eval {
BEGIN;
SELECT sum(x) FROM t1;
diff --git a/test/aggnested.test b/test/aggnested.test
index 22f0fb6..6e2fd65 100644
--- a/test/aggnested.test
+++ b/test/aggnested.test
@@ -68,4 +68,164 @@ do_test aggnested-2.0 {
} {A,B,B 3 33 333 3333}
db2 close
+##################### Test cases for ticket [bfbf38e5e9956ac69f] ############
+#
+# This first test case is the original problem report:
+do_test aggnested-3.0 {
+ db eval {
+ CREATE TABLE AAA (
+ aaa_id INTEGER PRIMARY KEY AUTOINCREMENT
+ );
+ CREATE TABLE RRR (
+ rrr_id INTEGER PRIMARY KEY AUTOINCREMENT,
+ rrr_date INTEGER NOT NULL,
+ rrr_aaa INTEGER
+ );
+ CREATE TABLE TTT (
+ ttt_id INTEGER PRIMARY KEY AUTOINCREMENT,
+ target_aaa INTEGER NOT NULL,
+ source_aaa INTEGER NOT NULL
+ );
+ insert into AAA (aaa_id) values (2);
+ insert into TTT (ttt_id, target_aaa, source_aaa)
+ values (4469, 2, 2);
+ insert into TTT (ttt_id, target_aaa, source_aaa)
+ values (4476, 2, 1);
+ insert into RRR (rrr_id, rrr_date, rrr_aaa)
+ values (0, 0, NULL);
+ insert into RRR (rrr_id, rrr_date, rrr_aaa)
+ values (2, 4312, 2);
+ SELECT i.aaa_id,
+ (SELECT sum(CASE WHEN (t.source_aaa == i.aaa_id) THEN 1 ELSE 0 END)
+ FROM TTT t
+ ) AS segfault
+ FROM
+ (SELECT curr.rrr_aaa as aaa_id
+ FROM RRR curr
+ -- you also can comment out the next line
+ -- it causes segfault to happen after one row is outputted
+ INNER JOIN AAA a ON (curr.rrr_aaa = aaa_id)
+ LEFT JOIN RRR r ON (r.rrr_id <> 0 AND r.rrr_date < curr.rrr_date)
+ GROUP BY curr.rrr_id
+ HAVING r.rrr_date IS NULL
+ ) i;
+ }
+} {2 1}
+
+# Further variants of the test case, as found in the ticket
+#
+do_test aggnested-3.1 {
+ db eval {
+ DROP TABLE IF EXISTS t1;
+ DROP TABLE IF EXISTS t2;
+ CREATE TABLE t1 (
+ id1 INTEGER PRIMARY KEY AUTOINCREMENT,
+ value1 INTEGER
+ );
+ INSERT INTO t1 VALUES(4469,2),(4476,1);
+ CREATE TABLE t2 (
+ id2 INTEGER PRIMARY KEY AUTOINCREMENT,
+ value2 INTEGER
+ );
+ INSERT INTO t2 VALUES(0,1),(2,2);
+ SELECT
+ (SELECT sum(value2==xyz) FROM t2)
+ FROM
+ (SELECT curr.value1 as xyz
+ FROM t1 AS curr LEFT JOIN t1 AS other
+ GROUP BY curr.id1);
+ }
+} {1 1}
+do_test aggnested-3.2 {
+ db eval {
+ DROP TABLE IF EXISTS t1;
+ DROP TABLE IF EXISTS t2;
+ CREATE TABLE t1 (
+ id1 INTEGER,
+ value1 INTEGER,
+ x1 INTEGER
+ );
+ INSERT INTO t1 VALUES(4469,2,98),(4469,1,99),(4469,3,97);
+ CREATE TABLE t2 (
+ value2 INTEGER
+ );
+ INSERT INTO t2 VALUES(1);
+ SELECT
+ (SELECT sum(value2==xyz) FROM t2)
+ FROM
+ (SELECT value1 as xyz, max(x1) AS pqr
+ FROM t1
+ GROUP BY id1);
+ }
+} {0}
+do_test aggnested-3.3 {
+ db eval {
+ DROP TABLE IF EXISTS t1;
+ DROP TABLE IF EXISTS t2;
+ CREATE TABLE t1(id1, value1);
+ INSERT INTO t1 VALUES(4469,2),(4469,1);
+ CREATE TABLE t2 (value2);
+ INSERT INTO t2 VALUES(1);
+ SELECT (SELECT sum(value2=value1) FROM t2), max(value1)
+ FROM t1
+ GROUP BY id1;
+ }
+} {0 2}
+
+# A batch of queries all doing approximately the same operation involving
+# two nested aggregate queries.
+#
+do_test aggnested-3.11 {
+ db eval {
+ DROP TABLE IF EXISTS t1;
+ DROP TABLE IF EXISTS t2;
+ CREATE TABLE t1(id1, value1);
+ INSERT INTO t1 VALUES(4469,12),(4469,11),(4470,34);
+ CREATE INDEX t1id1 ON t1(id1);
+ CREATE TABLE t2 (value2);
+ INSERT INTO t2 VALUES(12),(34),(34);
+ INSERT INTO t2 SELECT value2 FROM t2;
+
+ SELECT max(value1), (SELECT count(*) FROM t2 WHERE value2=max(value1))
+ FROM t1
+ GROUP BY id1;
+ }
+} {12 2 34 4}
+do_test aggnested-3.12 {
+ db eval {
+ SELECT max(value1), (SELECT count(*) FROM t2 WHERE value2=value1)
+ FROM t1
+ GROUP BY id1;
+ }
+} {12 2 34 4}
+do_test aggnested-3.13 {
+ db eval {
+ SELECT value1, (SELECT sum(value2=value1) FROM t2)
+ FROM t1;
+ }
+} {12 2 11 0 34 4}
+do_test aggnested-3.14 {
+ db eval {
+ SELECT value1, (SELECT sum(value2=value1) FROM t2)
+ FROM t1
+ WHERE value1 IN (SELECT max(value1) FROM t1 GROUP BY id1);
+ }
+} {12 2 34 4}
+do_test aggnested-3.15 {
+ # FIXME: If case 3.16 works, then this case really ought to work too...
+ catchsql {
+ SELECT max(value1), (SELECT sum(value2=max(value1)) FROM t2)
+ FROM t1
+ GROUP BY id1;
+ }
+} {1 {misuse of aggregate function max()}}
+do_test aggnested-3.16 {
+ db eval {
+ SELECT max(value1), (SELECT sum(value2=value1) FROM t2)
+ FROM t1
+ GROUP BY id1;
+ }
+} {12 2 34 4}
+
+
finish_test
diff --git a/test/analyze6.test b/test/analyze6.test
index 74b7ec7..eaa9d73 100644
--- a/test/analyze6.test
+++ b/test/analyze6.test
@@ -61,14 +61,14 @@ do_test analyze6-1.0 {
#
do_test analyze6-1.1 {
eqp {SELECT count(*) FROM ev, cat WHERE x=y}
-} {0 0 1 {SCAN TABLE cat (~16 rows)} 0 1 0 {SEARCH TABLE ev USING COVERING INDEX evy (y=?) (~32 rows)}}
+} {0 0 1 {SCAN TABLE cat USING COVERING INDEX catx (~16 rows)} 0 1 0 {SEARCH TABLE ev USING COVERING INDEX evy (y=?) (~32 rows)}}
# The same plan is chosen regardless of the order of the tables in the
# FROM clause.
#
do_test analyze6-1.2 {
eqp {SELECT count(*) FROM cat, ev WHERE x=y}
-} {0 0 0 {SCAN TABLE cat (~16 rows)} 0 1 1 {SEARCH TABLE ev USING COVERING INDEX evy (y=?) (~32 rows)}}
+} {0 0 0 {SCAN TABLE cat USING COVERING INDEX catx (~16 rows)} 0 1 1 {SEARCH TABLE ev USING COVERING INDEX evy (y=?) (~32 rows)}}
# Ticket [83ea97620bd3101645138b7b0e71c12c5498fe3d] 2011-03-30
diff --git a/test/analyze7.test b/test/analyze7.test
index 5bdb04d..46ec39e 100644
--- a/test/analyze7.test
+++ b/test/analyze7.test
@@ -26,7 +26,7 @@ ifcapable {!analyze||!vtab} {
# Generate some test data
#
do_test analyze7-1.0 {
- register_wholenumber_module db
+ load_static_extension db wholenumber
execsql {
CREATE TABLE t1(a,b,c,d);
CREATE INDEX t1a ON t1(a);
diff --git a/test/auth.test b/test/auth.test
index 211ae7e..fd402b1 100644
--- a/test/auth.test
+++ b/test/auth.test
@@ -2262,7 +2262,9 @@ do_test auth-4.3 {
SQLITE_SELECT {} {} {} v1 \
SQLITE_READ t2 a main v1 \
SQLITE_READ t2 b main v1 \
- SQLITE_SELECT {} {} {} {} \
+ SQLITE_READ v1 x main v1 \
+ SQLITE_READ v1 x main v1 \
+ SQLITE_SELECT {} {} {} v1 \
SQLITE_READ v1 x main v1 \
SQLITE_INSERT v1chng {} main r2 \
SQLITE_READ v1 x main r2 \
@@ -2288,7 +2290,9 @@ do_test auth-4.5 {
SQLITE_SELECT {} {} {} v1 \
SQLITE_READ t2 a main v1 \
SQLITE_READ t2 b main v1 \
- SQLITE_SELECT {} {} {} {} \
+ SQLITE_READ v1 x main v1 \
+ SQLITE_READ v1 x main v1 \
+ SQLITE_SELECT {} {} {} v1 \
SQLITE_READ v1 x main v1 \
SQLITE_INSERT v1chng {} main r3 \
SQLITE_READ v1 x main r3 \
@@ -2364,6 +2368,29 @@ ifcapable trigger {
} {1}
}
+# Ticket [0eb70d77cb05bb22720]: Invalid pointer passsed to the authorizer
+# callback when updating a ROWID.
+#
+do_test auth-6.1 {
+ execsql {
+ CREATE TABLE t6(a,b,c,d,e,f,g,h);
+ INSERT INTO t6 VALUES(1,2,3,4,5,6,7,8);
+ }
+} {}
+set ::authargs [list]
+proc auth {args} {
+ eval lappend ::authargs $args
+ return SQLITE_OK
+}
+do_test auth-6.2 {
+ execsql {UPDATE t6 SET rowID=rowID+100}
+ set ::authargs
+} [list SQLITE_READ t6 ROWID main {} \
+ SQLITE_UPDATE t6 ROWID main {} \
+]
+do_test auth-6.3 {
+ execsql {SELECT rowid, * FROM t6}
+} {101 1 2 3 4 5 6 7 8}
rename proc {}
rename proc_real proc
diff --git a/test/auth2.test b/test/auth2.test
index f5dba14..9343fd6 100644
--- a/test/auth2.test
+++ b/test/auth2.test
@@ -131,12 +131,12 @@ do_test auth2-2.3 {
}
set ::authargs
} {SQLITE_SELECT {} {} {} {}
-SQLITE_READ v2 a main {}
-SQLITE_READ v2 b main {}
SQLITE_READ t2 x main v2
SQLITE_READ t2 y main v2
SQLITE_READ t2 y main v2
SQLITE_READ t2 z main v2
+SQLITE_READ v2 a main {}
+SQLITE_READ v2 b main {}
SQLITE_SELECT {} {} {} v2
}
do_test auth2-2.4 {
@@ -149,20 +149,20 @@ do_test auth2-2.4 {
}
set ::authargs
} {SQLITE_SELECT {} {} {} {}
-SQLITE_READ v2 b main {}
-SQLITE_READ v2 a main {}
SQLITE_READ t2 x main v2
SQLITE_READ t2 y main v2
SQLITE_READ t2 y main v2
SQLITE_READ t2 z main v2
-SQLITE_SELECT {} {} {} v2
-SQLITE_SELECT {} {} {} {}
SQLITE_READ v2 b main {}
SQLITE_READ v2 a main {}
+SQLITE_SELECT {} {} {} v2
+SQLITE_SELECT {} {} {} {}
SQLITE_READ t2 x main v2
SQLITE_READ t2 y main v2
SQLITE_READ t2 y main v2
SQLITE_READ t2 z main v2
+SQLITE_READ v2 b main {}
+SQLITE_READ v2 a main {}
SQLITE_SELECT {} {} {} v2
}
db2 close
diff --git a/test/autoindex1.test b/test/autoindex1.test
index 6c8d54d..54ff82a 100644
--- a/test/autoindex1.test
+++ b/test/autoindex1.test
@@ -257,5 +257,129 @@ do_execsql_test autoindex1-700 {
0 0 0 {USE TEMP B-TREE FOR ORDER BY}
}
+# The following checks a performance issue reported on the sqlite-dev
+# mailing list on 2013-01-10
+#
+do_execsql_test autoindex1-800 {
+ CREATE TABLE accounts(
+ _id INTEGER PRIMARY KEY AUTOINCREMENT,
+ account_name TEXT,
+ account_type TEXT,
+ data_set TEXT
+ );
+ CREATE TABLE data(
+ _id INTEGER PRIMARY KEY AUTOINCREMENT,
+ package_id INTEGER REFERENCES package(_id),
+ mimetype_id INTEGER REFERENCES mimetype(_id) NOT NULL,
+ raw_contact_id INTEGER REFERENCES raw_contacts(_id) NOT NULL,
+ is_read_only INTEGER NOT NULL DEFAULT 0,
+ is_primary INTEGER NOT NULL DEFAULT 0,
+ is_super_primary INTEGER NOT NULL DEFAULT 0,
+ data_version INTEGER NOT NULL DEFAULT 0,
+ data1 TEXT,
+ data2 TEXT,
+ data3 TEXT,
+ data4 TEXT,
+ data5 TEXT,
+ data6 TEXT,
+ data7 TEXT,
+ data8 TEXT,
+ data9 TEXT,
+ data10 TEXT,
+ data11 TEXT,
+ data12 TEXT,
+ data13 TEXT,
+ data14 TEXT,
+ data15 TEXT,
+ data_sync1 TEXT,
+ data_sync2 TEXT,
+ data_sync3 TEXT,
+ data_sync4 TEXT
+ );
+ CREATE TABLE mimetypes(
+ _id INTEGER PRIMARY KEY AUTOINCREMENT,
+ mimetype TEXT NOT NULL
+ );
+ CREATE TABLE raw_contacts(
+ _id INTEGER PRIMARY KEY AUTOINCREMENT,
+ account_id INTEGER REFERENCES accounts(_id),
+ sourceid TEXT,
+ raw_contact_is_read_only INTEGER NOT NULL DEFAULT 0,
+ version INTEGER NOT NULL DEFAULT 1,
+ dirty INTEGER NOT NULL DEFAULT 0,
+ deleted INTEGER NOT NULL DEFAULT 0,
+ contact_id INTEGER REFERENCES contacts(_id),
+ aggregation_mode INTEGER NOT NULL DEFAULT 0,
+ aggregation_needed INTEGER NOT NULL DEFAULT 1,
+ custom_ringtone TEXT,
+ send_to_voicemail INTEGER NOT NULL DEFAULT 0,
+ times_contacted INTEGER NOT NULL DEFAULT 0,
+ last_time_contacted INTEGER,
+ starred INTEGER NOT NULL DEFAULT 0,
+ display_name TEXT,
+ display_name_alt TEXT,
+ display_name_source INTEGER NOT NULL DEFAULT 0,
+ phonetic_name TEXT,
+ phonetic_name_style TEXT,
+ sort_key TEXT,
+ sort_key_alt TEXT,
+ name_verified INTEGER NOT NULL DEFAULT 0,
+ sync1 TEXT,
+ sync2 TEXT,
+ sync3 TEXT,
+ sync4 TEXT,
+ sync_uid TEXT,
+ sync_version INTEGER NOT NULL DEFAULT 1,
+ has_calendar_event INTEGER NOT NULL DEFAULT 0,
+ modified_time INTEGER,
+ is_restricted INTEGER DEFAULT 0,
+ yp_source TEXT,
+ method_selected INTEGER DEFAULT 0,
+ custom_vibration_type INTEGER DEFAULT 0,
+ custom_ringtone_path TEXT,
+ message_notification TEXT,
+ message_notification_path TEXT
+ );
+ CREATE INDEX data_mimetype_data1_index ON data (mimetype_id,data1);
+ CREATE INDEX data_raw_contact_id ON data (raw_contact_id);
+ CREATE UNIQUE INDEX mime_type ON mimetypes (mimetype);
+ CREATE INDEX raw_contact_sort_key1_index ON raw_contacts (sort_key);
+ CREATE INDEX raw_contact_sort_key2_index ON raw_contacts (sort_key_alt);
+ CREATE INDEX raw_contacts_contact_id_index ON raw_contacts (contact_id);
+ CREATE INDEX raw_contacts_source_id_account_id_index
+ ON raw_contacts (sourceid, account_id);
+ ANALYZE sqlite_master;
+ INSERT INTO sqlite_stat1
+ VALUES('raw_contacts','raw_contact_sort_key2_index','1600 4');
+ INSERT INTO sqlite_stat1
+ VALUES('raw_contacts','raw_contact_sort_key1_index','1600 4');
+ INSERT INTO sqlite_stat1
+ VALUES('raw_contacts','raw_contacts_source_id_account_id_index',
+ '1600 1600 1600');
+ INSERT INTO sqlite_stat1
+ VALUES('raw_contacts','raw_contacts_contact_id_index','1600 1');
+ INSERT INTO sqlite_stat1 VALUES('mimetypes','mime_type','12 1');
+ INSERT INTO sqlite_stat1
+ VALUES('data','data_mimetype_data1_index','9819 2455 3');
+ INSERT INTO sqlite_stat1 VALUES('data','data_raw_contact_id','9819 7');
+ INSERT INTO sqlite_stat1 VALUES('accounts',NULL,'1');
+ DROP TABLE IF EXISTS sqlite_stat3;
+ ANALYZE sqlite_master;
+
+ EXPLAIN QUERY PLAN
+ SELECT * FROM
+ data JOIN mimetypes ON (data.mimetype_id=mimetypes._id)
+ JOIN raw_contacts ON (data.raw_contact_id=raw_contacts._id)
+ JOIN accounts ON (raw_contacts.account_id=accounts._id)
+ WHERE mimetype_id=10 AND data14 IS NOT NULL;
+} {/SEARCH TABLE data .*SEARCH TABLE raw_contacts/}
+do_execsql_test autoindex1-801 {
+ EXPLAIN QUERY PLAN
+ SELECT * FROM
+ data JOIN mimetypes ON (data.mimetype_id=mimetypes._id)
+ JOIN raw_contacts ON (data.raw_contact_id=raw_contacts._id)
+ JOIN accounts ON (raw_contacts.account_id=accounts._id)
+ WHERE mimetypes._id=10 AND data14 IS NOT NULL;
+} {/SEARCH TABLE data .*SEARCH TABLE raw_contacts/}
finish_test
diff --git a/test/autovacuum.test b/test/autovacuum.test
index 1aef18f..bba40e3 100644
--- a/test/autovacuum.test
+++ b/test/autovacuum.test
@@ -114,7 +114,7 @@ foreach delete_order $delete_orders {
}
do_test autovacuum-1.$tn.($delete).3 {
execsql {
- select a from av1
+ select a from av1 order by rowid
}
} $::tbl_data
}
diff --git a/test/backcompat.test b/test/backcompat.test
index 509dfe5..dd40fed 100644
--- a/test/backcompat.test
+++ b/test/backcompat.test
@@ -213,7 +213,9 @@ unset ::incompatible
#
do_allbackcompat_test {
if {[code1 {sqlite3 -version}] >= "3.7.0"
+ && [code1 {set ::sqlite_options(wal)}]
&& [code2 {sqlite3 -version}] >= "3.7.0"
+ && [code2 {set ::sqlite_options(wal)}]
} {
do_test backcompat-2.1.1 { sql1 {
diff --git a/test/backup4.test b/test/backup4.test
new file mode 100644
index 0000000..a1a7735
--- /dev/null
+++ b/test/backup4.test
@@ -0,0 +1,104 @@
+# 2012 October 13
+#
+# 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.
+#
+#***********************************************************************
+#
+# The tests in this file verify that if an empty database (zero bytes in
+# size) is used as the source of a backup operation, the final destination
+# database is one page in size.
+#
+# The destination must consist of at least one page as truncating a
+# database file to zero bytes is equivalent to resetting the database
+# schema cookie and change counter. Doing that could cause other clients
+# to become confused and continue using out-of-date cache data.
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set testprefix backup4
+
+#-------------------------------------------------------------------------
+# At one point this test was failing because [db] was using an out of
+# date schema in test case 1.2.
+#
+do_execsql_test 1.0 {
+ CREATE TABLE t1(x, y, UNIQUE(x, y));
+ INSERT INTO t1 VALUES('one', 'two');
+ SELECT * FROM t1 WHERE x='one';
+ PRAGMA integrity_check;
+} {one two ok}
+
+do_test 1.1 {
+ sqlite3 db1 :memory:
+ db1 backup test.db
+ sqlite3 db1 test.db
+ db1 eval {
+ CREATE TABLE t1(x, y);
+ INSERT INTO t1 VALUES('one', 'two');
+ }
+ db1 close
+} {}
+
+do_execsql_test 1.2 {
+ SELECT * FROM t1 WHERE x='one';
+ PRAGMA integrity_check;
+} {one two ok}
+
+db close
+forcedelete test.db
+forcedelete test.db2
+sqlite3 db test.db
+
+#-------------------------------------------------------------------------
+# Test that if the source is zero bytes, the destination database
+# consists of a single page only.
+#
+do_execsql_test 2.1 {
+ CREATE TABLE t1(a, b);
+ CREATE INDEX i1 ON t1(a, b);
+}
+do_test 2.2 { file size test.db } [expr $AUTOVACUUM ? 4096 : 3072]
+
+do_test 2.3 {
+ sqlite3 db1 test.db2
+ db1 backup test.db
+ db1 close
+ file size test.db
+} {1024}
+
+do_test 2.4 { file size test.db2 } 0
+
+db close
+forcedelete test.db
+forcedelete test.db2
+sqlite3 db test.db
+
+#-------------------------------------------------------------------------
+# Test that if the destination has a page-size larger than the implicit
+# page-size of the source, the final destination database still consists
+# of a single page.
+#
+do_execsql_test 3.1 {
+ PRAGMA page_size = 4096;
+ CREATE TABLE t1(a, b);
+ CREATE INDEX i1 ON t1(a, b);
+}
+do_test 3.2 { file size test.db } [expr $AUTOVACUUM ? 16384 : 12288]
+
+do_test 3.3 {
+ sqlite3 db1 test.db2
+ db1 backup test.db
+ db1 close
+ file size test.db
+} {1024}
+
+do_test 3.4 { file size test.db2 } 0
+
+finish_test
+
diff --git a/test/backup_ioerr.test b/test/backup_ioerr.test
index 313cff3..ca3fd32 100644
--- a/test/backup_ioerr.test
+++ b/test/backup_ioerr.test
@@ -115,7 +115,7 @@ proc clear_ioerr_simulation {} {
# reported, then the backup process is concluded with a call to
# backup_finish().
#
-# Test that if an IO error occurs, or if one occured while updating
+# Test that if an IO error occurs, or if one occurred while updating
# the backup database during step 4, then the conditions listed
# under step 3 are all true.
#
@@ -214,7 +214,7 @@ for {set iError 1} {$bStop == 0} {incr iError} {
set rc [catchsql { UPDATE t1 SET b = randstr(1000,1000) WHERE a < 50 } sdb]
if {[lindex $rc 0] && $::sqlite_io_error_persist==0} {
- # The IO error occured while updating the source database. In this
+ # The IO error occurred while updating the source database. In this
# case the backup should be able to continue.
set rc [B step 5000]
if { $rc != "SQLITE_IOERR_UNLOCK" } {
diff --git a/test/bigfile.test b/test/bigfile.test
index 59e9f18..31c6323 100644
--- a/test/bigfile.test
+++ b/test/bigfile.test
@@ -16,6 +16,7 @@
#
if {[file exists skip-big-file]} return
+if {$tcl_platform(os)=="Darwin"} return
set testdir [file dirname $argv0]
source $testdir/tester.tcl
diff --git a/test/bigfile2.test b/test/bigfile2.test
index 1f0ea85..b67cb34 100644
--- a/test/bigfile2.test
+++ b/test/bigfile2.test
@@ -14,6 +14,7 @@
#
if {[file exists skip-big-file]} return
+if {$tcl_platform(os)=="Darwin"} return
set testdir [file dirname $argv0]
source $testdir/tester.tcl
diff --git a/test/btreefault.test b/test/btreefault.test
new file mode 100644
index 0000000..9b42240
--- /dev/null
+++ b/test/btreefault.test
@@ -0,0 +1,58 @@
+# 2013 April 02
+#
+# 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 file contains fault injection tests designed to test the btree.c
+# module.
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+source $testdir/malloc_common.tcl
+set testprefix btreefault
+
+# This test will not work with an in-memory journal, as the database will
+# become corrupt if an error is injected into a transaction after it starts
+# writing data out to the db file.
+if {[permutation]=="inmemory_journal"} {
+ finish_test
+ return
+}
+
+do_test 1-pre1 {
+ execsql {
+ PRAGMA auto_vacuum = incremental;
+ PRAGMA journal_mode = DELETE;
+ CREATE TABLE t1(a PRIMARY KEY, b);
+ INSERT INTO t1 VALUES(randomblob(1000), randomblob(100));
+ INSERT INTO t1 SELECT randomblob(1000), randomblob(1000) FROM t1;
+ INSERT INTO t1 SELECT randomblob(1000), randomblob(1000) FROM t1;
+ INSERT INTO t1 SELECT randomblob(1000), randomblob(1000) FROM t1;
+ INSERT INTO t1 SELECT randomblob(1000), randomblob(1000) FROM t1;
+ DELETE FROM t1 WHERE rowid%2;
+ }
+ faultsim_save_and_close
+} {}
+
+do_faultsim_test 1 -prep {
+ faultsim_restore_and_reopen
+ set ::STMT [sqlite3_prepare db "SELECT * FROM t1 ORDER BY a" -1 DUMMY]
+ sqlite3_step $::STMT
+ sqlite3_step $::STMT
+} -body {
+ execsql { PRAGMA incremental_vacuum = 10 }
+} -test {
+ sqlite3_finalize $::STMT
+ faultsim_test_result {0 {}}
+ faultsim_integrity_check
+}
+
+finish_test
+
diff --git a/test/cache.test b/test/cache.test
index f81948b..ffc25c4 100644
--- a/test/cache.test
+++ b/test/cache.test
@@ -46,7 +46,7 @@ do_test cache-1.2 {
# leaked, but would not be reused until the pager-cache was full (i.e.
# 2000 pages by default).
#
-# This tests that once the pager-cache is initialised, it can be locked
+# This tests that once the pager-cache is initialized, it can be locked
# and unlocked repeatedly without internally allocating any new pages.
#
set cache_size [pager_cache_size db]
diff --git a/test/capi2.test b/test/capi2.test
index 8b36ac6..20ee340 100644
--- a/test/capi2.test
+++ b/test/capi2.test
@@ -235,8 +235,9 @@ do_test capi2-3.13 {
do_test capi2-3.13b {db changes} {0}
do_test capi2-3.14 {
- list [sqlite3_finalize $VM] [sqlite3_errmsg $DB]
-} {SQLITE_CONSTRAINT {column a is not unique}}
+ list [sqlite3_finalize $VM] [sqlite3_errmsg $DB] \
+ [sqlite3_extended_errcode $DB]
+} {SQLITE_CONSTRAINT {column a is not unique} SQLITE_CONSTRAINT_UNIQUE}
do_test capi2-3.15 {
set VM [sqlite3_prepare $DB {CREATE TABLE t2(a NOT NULL, b)} -1 TAIL]
set TAIL
@@ -258,8 +259,9 @@ do_test capi2-3.18 {
[get_column_names $VM]
} {SQLITE_ERROR 0 {} {}}
do_test capi2-3.19 {
- list [sqlite3_finalize $VM] [sqlite3_errmsg $DB]
-} {SQLITE_CONSTRAINT {t2.a may not be NULL}}
+ list [sqlite3_finalize $VM] [sqlite3_errmsg $DB] \
+ [sqlite3_extended_errcode $DB]
+} {SQLITE_CONSTRAINT {t2.a may not be NULL} SQLITE_CONSTRAINT_NOTNULL}
do_test capi2-3.20 {
execsql {
@@ -278,8 +280,8 @@ do_test capi2-3.23 {
sqlite3_finalize $VM
} {SQLITE_CONSTRAINT}
do_test capi2-3.24 {
- sqlite3_errcode $DB
-} {SQLITE_CONSTRAINT}
+ list [sqlite3_errcode $DB] [sqlite3_extended_errcode $DB]
+} {SQLITE_CONSTRAINT SQLITE_CONSTRAINT_UNIQUE}
# Two or more virtual machines exists at the same time.
#
diff --git a/test/check.test b/test/check.test
index bf0b770..99b72ac 100644
--- a/test/check.test
+++ b/test/check.test
@@ -15,6 +15,7 @@
set testdir [file dirname $argv0]
source $testdir/tester.tcl
+set ::testprefix check
# Only run these tests if the build includes support for CHECK constraints
ifcapable !check {
@@ -413,4 +414,42 @@ do_test check-6.15 {
}
+#--------------------------------------------------------------------------
+# If a connection opens a database that contains a CHECK constraint that
+# uses an unknown UDF, the schema should not be considered malformed.
+# Attempting to modify the table should fail (since the CHECK constraint
+# cannot be tested).
+#
+reset_db
+proc myfunc {x} {expr $x < 10}
+db func myfunc myfunc
+
+do_execsql_test 7.1 { CREATE TABLE t6(a CHECK (myfunc(a))) }
+do_execsql_test 7.2 { INSERT INTO t6 VALUES(9) }
+do_catchsql_test 7.3 { INSERT INTO t6 VALUES(11) } {1 {constraint failed}}
+
+do_test 7.4 {
+ sqlite3 db2 test.db
+ execsql { SELECT * FROM t6 } db2
+} {9}
+
+do_test 7.5 {
+ catchsql { INSERT INTO t6 VALUES(8) } db2
+} {1 {unknown function: myfunc()}}
+
+do_test 7.6 {
+ catchsql { CREATE TABLE t7(a CHECK (myfunc(a))) } db2
+} {1 {no such function: myfunc}}
+
+do_test 7.7 {
+ db2 func myfunc myfunc
+ execsql { INSERT INTO t6 VALUES(8) } db2
+} {}
+
+do_test 7.8 {
+ db2 func myfunc myfunc
+ catchsql { INSERT INTO t6 VALUES(12) } db2
+} {1 {constraint failed}}
+
+
finish_test
diff --git a/test/close.test b/test/close.test
new file mode 100644
index 0000000..355a886
--- /dev/null
+++ b/test/close.test
@@ -0,0 +1,79 @@
+# 2013 May 14
+#
+# 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.
+#
+#***********************************************************************
+#
+# Test some specific circumstances to do with shared cache mode.
+#
+
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set ::testprefix close
+
+do_execsql_test 1.0 {
+ CREATE TABLE t1(x);
+ INSERT INTO t1 VALUES('one');
+ INSERT INTO t1 VALUES('two');
+ INSERT INTO t1 VALUES('three');
+}
+db close
+
+do_test 1.1 {
+ set DB [sqlite3_open test.db]
+ sqlite3_close_v2 $DB
+} {SQLITE_OK}
+
+do_test 1.2.1 {
+ set DB [sqlite3_open test.db]
+ set STMT [sqlite3_prepare $DB "SELECT * FROM t1" -1 dummy]
+ sqlite3_close_v2 $DB
+} {SQLITE_OK}
+do_test 1.2.2 {
+ sqlite3_finalize $STMT
+} {SQLITE_OK}
+
+do_test 1.3.1 {
+ set DB [sqlite3_open test.db]
+ set STMT [sqlite3_prepare $DB "SELECT * FROM t1" -1 dummy]
+ sqlite3_step $STMT
+ sqlite3_close_v2 $DB
+} {SQLITE_OK}
+
+do_test 1.3.2 {
+ sqlite3_column_text $STMT 0
+} {one}
+
+do_test 1.3.3 {
+ sqlite3_finalize $STMT
+} {SQLITE_OK}
+
+do_test 1.4.1 {
+ set DB [sqlite3_open test.db]
+ set STMT [sqlite3_prepare $DB "SELECT * FROM t1" -1 dummy]
+ sqlite3_step $STMT
+ sqlite3_close_v2 $DB
+} {SQLITE_OK}
+
+do_test 1.4.2 {
+ list [sqlite3_step $STMT] [sqlite3_column_text $STMT 0]
+} {SQLITE_ROW two}
+
+do_test 1.4.3 {
+ list [catch {
+ sqlite3_prepare $DB "SELECT * FROM sqlite_master" -1 dummy
+ } msg] $msg
+} {1 {(21) library routine called out of sequence}}
+
+do_test 1.4.4 {
+ sqlite3_finalize $STMT
+} {SQLITE_OK}
+
+finish_test
+
diff --git a/test/closure01.test b/test/closure01.test
new file mode 100644
index 0000000..5dac87a
--- /dev/null
+++ b/test/closure01.test
@@ -0,0 +1,226 @@
+# 2013-04-25
+#
+# 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.
+#
+#***********************************************************************
+#
+# Test cases for transitive_closure virtual table.
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set testprefix closure01
+
+ifcapable !vtab { finish_test ; return }
+
+load_static_extension db closure
+
+do_execsql_test 1.0 {
+ BEGIN;
+ CREATE TABLE t1(x INTEGER PRIMARY KEY, y INTEGER);
+ CREATE INDEX t1y ON t1(y);
+ INSERT INTO t1(x) VALUES(1),(2);
+ INSERT INTO t1(x) SELECT x+2 FROM t1;
+ INSERT INTO t1(x) SELECT x+4 FROM t1;
+ INSERT INTO t1(x) SELECT x+8 FROM t1;
+ INSERT INTO t1(x) SELECT x+16 FROM t1;
+ INSERT INTO t1(x) SELECT x+32 FROM t1;
+ INSERT INTO t1(x) SELECT x+64 FROM t1;
+ INSERT INTO t1(x) SELECT x+128 FROM t1;
+ INSERT INTO t1(x) SELECT x+256 FROM t1;
+ INSERT INTO t1(x) SELECT x+512 FROM t1;
+ INSERT INTO t1(x) SELECT x+1024 FROM t1;
+ INSERT INTO t1(x) SELECT x+2048 FROM t1;
+ INSERT INTO t1(x) SELECT x+4096 FROM t1;
+ INSERT INTO t1(x) SELECT x+8192 FROM t1;
+ INSERT INTO t1(x) SELECT x+16384 FROM t1;
+ INSERT INTO t1(x) SELECT x+32768 FROM t1;
+ INSERT INTO t1(x) SELECT x+65536 FROM t1;
+ UPDATE t1 SET y=x/2 WHERE x>1;
+ COMMIT;
+ CREATE VIRTUAL TABLE cx
+ USING transitive_closure(tablename=t1, idcolumn=x, parentcolumn=y);
+} {}
+
+# The entire table
+do_execsql_test 1.1 {
+ SELECT count(*), depth FROM cx WHERE root=1 GROUP BY depth ORDER BY 1;
+} {/1 0 1 17 2 1 4 2 8 3 16 4 .* 65536 16/}
+
+# descendents of 32768
+do_execsql_test 1.2 {
+ SELECT * FROM cx WHERE root=32768 ORDER BY id;
+} {32768 0 65536 1 65537 1 131072 2}
+
+# descendents of 16384
+do_execsql_test 1.3 {
+ SELECT * FROM cx WHERE root=16384 AND depth<=2 ORDER BY id;
+} {16384 0 32768 1 32769 1 65536 2 65537 2 65538 2 65539 2}
+
+# children of 16384
+do_execsql_test 1.4 {
+ SELECT id, depth, root, tablename, idcolumn, parentcolumn FROM cx
+ WHERE root=16384
+ AND depth=1
+ ORDER BY id;
+} {32768 1 {} t1 x y 32769 1 {} t1 x y}
+
+# great-grandparent of 16384
+do_execsql_test 1.5 {
+ SELECT id, depth, root, tablename, idcolumn, parentcolumn FROM cx
+ WHERE root=16384
+ AND depth=3
+ AND idcolumn='Y'
+ AND parentcolumn='X';
+} {2048 3 {} t1 Y X}
+
+# depth<5
+do_execsql_test 1.6 {
+ SELECT count(*), depth FROM cx WHERE root=1 AND depth<5
+ GROUP BY depth ORDER BY 1;
+} {1 0 2 1 4 2 8 3 16 4}
+
+# depth<=5
+do_execsql_test 1.7 {
+ SELECT count(*), depth FROM cx WHERE root=1 AND depth<=5
+ GROUP BY depth ORDER BY 1;
+} {1 0 2 1 4 2 8 3 16 4 32 5}
+
+# depth==5
+do_execsql_test 1.8 {
+ SELECT count(*), depth FROM cx WHERE root=1 AND depth=5
+ GROUP BY depth ORDER BY 1;
+} {32 5}
+
+# depth BETWEEN 3 AND 5
+do_execsql_test 1.9 {
+ SELECT count(*), depth FROM cx WHERE root=1 AND depth BETWEEN 3 AND 5
+ GROUP BY depth ORDER BY 1;
+} {8 3 16 4 32 5}
+
+# depth==5 with min() and max()
+do_execsql_test 1.10 {
+ SELECT count(*), min(id), max(id) FROM cx WHERE root=1 AND depth=5;
+} {32 32 63}
+
+# Create a much smaller table t2 with only 32 elements
+db eval {
+ CREATE TABLE t2(x INTEGER PRIMARY KEY, y INTEGER);
+ INSERT INTO t2 SELECT x, y FROM t1 WHERE x<32;
+ CREATE INDEX t2y ON t2(y);
+ CREATE VIRTUAL TABLE c2
+ USING transitive_closure(tablename=t2, idcolumn=x, parentcolumn=y);
+}
+
+# t2 full-table
+do_execsql_test 2.1 {
+ SELECT count(*), min(id), max(id) FROM c2 WHERE root=1;
+} {31 1 31}
+# t2 root=10
+do_execsql_test 2.2 {
+ SELECT id FROM c2 WHERE root=10;
+} {10 20 21}
+# t2 root=11
+do_execsql_test 2.3 {
+ SELECT id FROM c2 WHERE root=12;
+} {12 24 25}
+# t2 root IN [10,12]
+do_execsql_test 2.4 {
+ SELECT id FROM c2 WHERE root IN (10,12) ORDER BY id;
+} {10 12 20 21 24 25}
+# t2 root IN [10,12] (sorted)
+do_execsql_test 2.5 {
+ SELECT id FROM c2 WHERE root IN (10,12) ORDER BY +id;
+} {10 12 20 21 24 25}
+
+# t2 c2up from 20
+do_execsql_test 3.0 {
+ CREATE VIRTUAL TABLE c2up USING transitive_closure(
+ tablename = t2,
+ idcolumn = y,
+ parentcolumn = x
+ );
+ SELECT id FROM c2up WHERE root=20;
+} {1 2 5 10 20}
+
+# cx as c2up
+do_execsql_test 3.1 {
+ SELECT id FROM cx
+ WHERE root=20
+ AND tablename='t2'
+ AND idcolumn='y'
+ AND parentcolumn='x';
+} {1 2 5 10 20}
+
+# t2 first cousins of 20
+do_execsql_test 3.2 {
+ SELECT DISTINCT id FROM c2
+ WHERE root IN (SELECT id FROM c2up
+ WHERE root=20 AND depth<=2)
+ ORDER BY id;
+} {5 10 11 20 21 22 23}
+
+# t2 first cousins of 20
+do_execsql_test 3.3 {
+ SELECT id FROM c2
+ WHERE root=(SELECT id FROM c2up
+ WHERE root=20 AND depth=2)
+ AND depth=2
+ EXCEPT
+ SELECT id FROM c2
+ WHERE root=(SELECT id FROM c2up
+ WHERE root=20 AND depth=1)
+ AND depth<=1
+ ORDER BY id;
+} {22 23}
+
+# missing tablename.
+do_test 4.1 {
+ catchsql {
+ SELECT id FROM cx
+ WHERE root=20
+ AND tablename='t3'
+ AND idcolumn='y'
+ AND parentcolumn='x';
+ }
+} {1 {no such table: t3}}
+
+# missing idcolumn
+do_test 4.2 {
+ catchsql {
+ SELECT id FROM cx
+ WHERE root=20
+ AND tablename='t2'
+ AND idcolumn='xyz'
+ AND parentcolumn='x';
+ }
+} {1 {no such column: t2.xyz}}
+
+# missing parentcolumn
+do_test 4.3 {
+ catchsql {
+ SELECT id FROM cx
+ WHERE root=20
+ AND tablename='t2'
+ AND idcolumn='x'
+ AND parentcolumn='pqr';
+ }
+} {1 {no such column: t2.pqr}}
+
+# generic closure
+do_execsql_test 5.1 {
+ CREATE VIRTUAL TABLE temp.closure USING transitive_closure;
+ SELECT id FROM closure
+ WHERE root=1
+ AND depth=3
+ AND tablename='t1'
+ AND idcolumn='x'
+ AND parentcolumn='y'
+ ORDER BY id;
+} {8 9 10 11 12 13 14 15}
+
+finish_test
diff --git a/test/collate1.test b/test/collate1.test
index ac2c75b..b493ee8 100644
--- a/test/collate1.test
+++ b/test/collate1.test
@@ -75,6 +75,7 @@ do_test collate1-1.1 {
}
} {{} 0x119 0x2D}
do_test collate1-1.2 {
+breakpoint
execsql {
SELECT c2 FROM collate1t1 ORDER BY 1 COLLATE hex;
}
diff --git a/test/collate3.test b/test/collate3.test
index c4dbfbe..2c051cb 100644
--- a/test/collate3.test
+++ b/test/collate3.test
@@ -55,6 +55,104 @@ execsql {
DROP TABLE collate3t1;
}
+proc caseless {a b} { string compare -nocase $a $b }
+do_test collate3-1.4 {
+ db collate caseless caseless
+ execsql {
+ CREATE TABLE t1(a COLLATE caseless);
+ INSERT INTO t1 VALUES('Abc2');
+ INSERT INTO t1 VALUES('abc1');
+ INSERT INTO t1 VALUES('aBc3');
+ }
+ execsql { SELECT * FROM t1 ORDER BY a }
+} {abc1 Abc2 aBc3}
+
+do_test collate3-1.5 {
+ db close
+ sqlite3 db test.db
+ catchsql { SELECT * FROM t1 ORDER BY a }
+} {1 {no such collation sequence: caseless}}
+
+do_test collate3-1.6.1 {
+ db collate caseless caseless
+ execsql { CREATE INDEX i1 ON t1(a) }
+ execsql { SELECT * FROM t1 ORDER BY a }
+} {abc1 Abc2 aBc3}
+
+do_test collate3-1.6.2 {
+ db close
+ sqlite3 db test.db
+ catchsql { SELECT * FROM t1 ORDER BY a }
+} {1 {no such collation sequence: caseless}}
+
+do_test collate3-1.6.3 {
+ db close
+ sqlite3 db test.db
+ catchsql { PRAGMA integrity_check }
+} {1 {no such collation sequence: caseless}}
+
+do_test collate3-1.6.4 {
+ db close
+ sqlite3 db test.db
+ catchsql { REINDEX }
+} {1 {no such collation sequence: caseless}}
+
+do_test collate3-1.7.1 {
+ db collate caseless caseless
+ execsql {
+ DROP TABLE t1;
+ CREATE TABLE t1(a);
+ CREATE INDEX i1 ON t1(a COLLATE caseless);
+ INSERT INTO t1 VALUES('Abc2');
+ INSERT INTO t1 VALUES('abc1');
+ INSERT INTO t1 VALUES('aBc3');
+ SELECT * FROM t1 ORDER BY a COLLATE caseless;
+ }
+} {abc1 Abc2 aBc3}
+
+do_test collate3-1.7.2 {
+ db close
+ sqlite3 db test.db
+ catchsql { SELECT * FROM t1 ORDER BY a COLLATE caseless}
+} {1 {no such collation sequence: caseless}}
+
+do_test collate3-1.7.4 {
+ db close
+ sqlite3 db test.db
+ catchsql { REINDEX }
+} {1 {no such collation sequence: caseless}}
+
+do_test collate3-1.7.3 {
+ db close
+ sqlite3 db test.db
+ catchsql { PRAGMA integrity_check }
+} {1 {no such collation sequence: caseless}}
+
+do_test collate3-1.7.4 {
+ db close
+ sqlite3 db test.db
+ catchsql { REINDEX }
+} {1 {no such collation sequence: caseless}}
+
+do_test collate3-1.7.5 {
+ db close
+ sqlite3 db test.db
+ db collate caseless caseless
+ catchsql { PRAGMA integrity_check }
+} {0 ok}
+
+proc needed {nm} { db collate caseless caseless }
+do_test collate3-1.7.6 {
+ db close
+ sqlite3 db test.db
+ db collation_needed needed
+ catchsql { PRAGMA integrity_check }
+} {0 ok}
+
+do_test collate3-1.8 {
+ execsql { DROP TABLE t1 }
+} {}
+
#
# Create a table with a default collation sequence, then close
# and re-open the database without re-registering the collation
diff --git a/test/collate4.test b/test/collate4.test
index 12bc16e..5ae1e7b 100644
--- a/test/collate4.test
+++ b/test/collate4.test
@@ -60,7 +60,7 @@ proc cksort {sql} {
#
# Because these tests also exercise all the different ways indices
# can be created, they also serve to verify that indices are correctly
-# initialised with user-defined collation sequences when they are
+# initialized with user-defined collation sequences when they are
# created.
#
# Tests named collate4-1.1.* use indices with a single column. Tests
@@ -94,7 +94,7 @@ do_test collate4-1.1.5 {
cksort {SELECT b FROM collate4t1 ORDER BY b COLLATE TEXT}
} {{} A B a b nosort}
do_test collate4-1.1.6 {
- cksort {SELECT b FROM collate4t1 ORDER BY b COLLATE NOCASE}
+ cksort {SELECT b FROM collate4t1 ORDER BY b COLLATE NOCASE, rowid}
} {{} a A b B sort}
do_test collate4-1.1.7 {
@@ -171,13 +171,13 @@ do_test collate4-1.1.21 {
}
} {}
do_test collate4-1.1.22 {
- cksort {SELECT a FROM collate4t4 ORDER BY a}
+ cksort {SELECT a FROM collate4t4 ORDER BY a, rowid}
} {{} a A b B sort}
do_test collate4-1.1.23 {
- cksort {SELECT a FROM collate4t4 ORDER BY a COLLATE NOCASE}
+ cksort {SELECT a FROM collate4t4 ORDER BY a COLLATE NOCASE, rowid}
} {{} a A b B sort}
do_test collate4-1.1.24 {
- cksort {SELECT a FROM collate4t4 ORDER BY a COLLATE TEXT}
+ cksort {SELECT a FROM collate4t4 ORDER BY a COLLATE TEXT, rowid}
} {{} A B a b nosort}
do_test collate4-1.1.25 {
cksort {SELECT b FROM collate4t4 ORDER BY b}
@@ -222,7 +222,7 @@ do_test collate4-1.2.4 {
cksort {SELECT a FROM collate4t1 ORDER BY a, b}
} {{} A a B b nosort}
do_test collate4-1.2.5 {
- cksort {SELECT a FROM collate4t1 ORDER BY a, b COLLATE nocase}
+ cksort {SELECT a FROM collate4t1 ORDER BY a, b COLLATE nocase, rowid}
} {{} a A b B sort}
do_test collate4-1.2.6 {
cksort {SELECT a FROM collate4t1 ORDER BY a, b COLLATE text}
@@ -271,10 +271,10 @@ do_test collate4-1.2.14 {
}
} {}
do_test collate4-1.2.15 {
- cksort {SELECT a FROM collate4t3 ORDER BY a}
+ cksort {SELECT a FROM collate4t3 ORDER BY a, rowid}
} {{} a A b B sort}
do_test collate4-1.2.16 {
- cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE nocase}
+ cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE nocase, rowid}
} {{} a A b B sort}
do_test collate4-1.2.17 {
cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE text}
@@ -364,7 +364,8 @@ do_test collate4-2.1.4 {
CREATE INDEX collate4i1 ON collate4t1(a COLLATE TEXT);
}
count {
- SELECT * FROM collate4t2, collate4t1 WHERE a = b;
+ SELECT * FROM collate4t2, collate4t1 WHERE a = b
+ ORDER BY collate4t2.rowid, collate4t1.rowid
}
} {A a A A 19}
do_test collate4-2.1.5 {
@@ -375,7 +376,8 @@ do_test collate4-2.1.5 {
ifcapable subquery {
do_test collate4-2.1.6 {
count {
- SELECT a FROM collate4t1 WHERE a IN (SELECT * FROM collate4t2);
+ SELECT a FROM collate4t1 WHERE a IN (SELECT * FROM collate4t2)
+ ORDER BY rowid
}
} {a A 10}
do_test collate4-2.1.7 {
@@ -384,7 +386,8 @@ ifcapable subquery {
CREATE INDEX collate4i1 ON collate4t1(a);
}
count {
- SELECT a FROM collate4t1 WHERE a IN (SELECT * FROM collate4t2);
+ SELECT a FROM collate4t1 WHERE a IN (SELECT * FROM collate4t2)
+ ORDER BY rowid
}
} {a A 6}
do_test collate4-2.1.8 {
@@ -398,7 +401,7 @@ ifcapable subquery {
CREATE INDEX collate4i1 ON collate4t1(a COLLATE TEXT);
}
count {
- SELECT a FROM collate4t1 WHERE a IN ('z', 'a');
+ SELECT a FROM collate4t1 WHERE a IN ('z', 'a') ORDER BY rowid;
}
} {a A 9}
}
diff --git a/test/collate5.test b/test/collate5.test
index e5bea7a..5f8697e 100644
--- a/test/collate5.test
+++ b/test/collate5.test
@@ -221,7 +221,7 @@ do_test collate5-3.0 {
execsql {
SELECT a FROM collate5t1 UNION ALL SELECT a FROM collate5t2 ORDER BY 1;
}
-} {a A a A b B b B n N}
+} {/[aA] [aA] [aA] [aA] [bB] [bB] [bB] [bB] [nN] [nN]/}
do_test collate5-3.1 {
execsql {
SELECT a FROM collate5t2 UNION ALL SELECT a FROM collate5t1 ORDER BY 1;
@@ -282,7 +282,7 @@ do_test collate5-4.2 {
execsql {
SELECT a, b, count(*) FROM collate5t1 GROUP BY a, b ORDER BY a, b;
}
-} {A 1.0 2 b 2 1 B 3 1}
+} {/[aA] 1(.0)? 2 [bB] 2 1 [bB] 3 1/}
do_test collate5-4.3 {
execsql {
DROP TABLE collate5t1;
diff --git a/test/conflict.test b/test/conflict.test
index 17c7263..6576959 100644
--- a/test/conflict.test
+++ b/test/conflict.test
@@ -580,6 +580,7 @@ do_test conflict-9.19 {
SELECT * FROM t2;
}
} {1 {column e is not unique}}
+verify_ex_errcode conflict-9.21b SQLITE_CONSTRAINT_UNIQUE
do_test conflict-9.20 {
catch {execsql {COMMIT}}
execsql {SELECT * FROM t3}
@@ -592,6 +593,7 @@ do_test conflict-9.21 {
SELECT * FROM t2;
}
} {1 {column e is not unique}}
+verify_ex_errcode conflict-9.21b SQLITE_CONSTRAINT_UNIQUE
do_test conflict-9.22 {
catch {execsql {COMMIT}}
execsql {SELECT * FROM t3}
@@ -781,6 +783,7 @@ do_test conflict-12.3 {
UPDATE t5 SET a=a+1 WHERE a=1;
}
} {1 {PRIMARY KEY must be unique}}
+verify_ex_errcode conflict-12.3b SQLITE_CONSTRAINT_PRIMARYKEY
do_test conflict-12.4 {
execsql {
UPDATE OR REPLACE t5 SET a=a+1 WHERE a=1;
@@ -802,6 +805,7 @@ do_test conflict-13.1 {
REPLACE INTO t13 VALUES(2);
}
} {1 {constraint failed}}
+verify_ex_errcode conflict-13.1b SQLITE_CONSTRAINT_CHECK
do_test conflict-13.2 {
execsql {
REPLACE INTO t13 VALUES(3);
diff --git a/test/corruptD.test b/test/corruptD.test
index 393d41e..2423cd4 100644
--- a/test/corruptD.test
+++ b/test/corruptD.test
@@ -107,12 +107,12 @@ proc restore_file {} {
do_test corruptD-1.1.1 {
incr_change_counter
hexio_write test.db [expr 1024+1] FFFF
- catchsql { SELECT * FROM t1 }
+ catchsql { SELECT * FROM t1 ORDER BY rowid }
} {1 {database disk image is malformed}}
do_test corruptD-1.1.2 {
incr_change_counter
hexio_write test.db [expr 1024+1] [hexio_render_int32 1021]
- catchsql { SELECT * FROM t1 }
+ catchsql { SELECT * FROM t1 ORDER BY rowid }
} {1 {database disk image is malformed}}
#-------------------------------------------------------------------------
diff --git a/test/corruptE.test b/test/corruptE.test
index 507721d..48292ab 100644
--- a/test/corruptE.test
+++ b/test/corruptE.test
@@ -49,7 +49,7 @@ do_test corruptE-1.1 {
INSERT OR IGNORE INTO t1 SELECT x*17,y FROM t1;
INSERT OR IGNORE INTO t1 SELECT x*19,y FROM t1;
CREATE INDEX t1i1 ON t1(x);
- CREATE TABLE t2 AS SELECT x,2 as y FROM t1 WHERE rowid%5!=0;
+ CREATE TABLE t2 AS SELECT x,2 as y FROM t1 WHERE rowid%5!=0 ORDER BY rowid;
COMMIT;
}
} {}
diff --git a/test/coveridxscan.test b/test/coveridxscan.test
new file mode 100644
index 0000000..7b3c0b0
--- /dev/null
+++ b/test/coveridxscan.test
@@ -0,0 +1,93 @@
+# 2012 September 17
+#
+# 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.
+#
+#***********************************************************************
+#
+# Tests for the optimization which attempts to use a covering index
+# for a full-table scan (under the theory that the index will be smaller
+# and require less I/O and hence will run faster.)
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+
+set testprefix coveridxscan
+
+do_test 1.1 {
+ db eval {
+ CREATE TABLE t1(a,b,c);
+ INSERT INTO t1 VALUES(5,4,3), (4,8,2), (3,2,1);
+ CREATE INDEX t1ab ON t1(a,b);
+ CREATE INDEX t1b ON t1(b);
+ SELECT a FROM t1;
+ }
+ # covering index used for the scan, hence values are increasing
+} {3 4 5}
+
+do_test 1.2 {
+ db eval {
+ SELECT a, c FROM t1;
+ }
+ # There is no covering index, hence the values are in rowid order
+} {5 3 4 2 3 1}
+
+do_test 1.3 {
+ db eval {
+ SELECT b FROM t1;
+ }
+ # Choice of two indices: use the one with fewest columns
+} {2 4 8}
+
+do_test 2.1 {
+ optimization_control db cover-idx-scan 0
+ db eval {SELECT a FROM t1}
+ # With the optimization turned off, output in rowid order
+} {5 4 3}
+do_test 2.2 {
+ db eval {SELECT a, c FROM t1}
+} {5 3 4 2 3 1}
+do_test 2.3 {
+ db eval {SELECT b FROM t1}
+} {4 8 2}
+
+db close
+sqlite3_shutdown
+sqlite3_config_cis 0
+sqlite3 db test.db
+
+do_test 3.1 {
+ db eval {SELECT a FROM t1}
+ # With the optimization configured off, output in rowid order
+} {5 4 3}
+do_test 3.2 {
+ db eval {SELECT a, c FROM t1}
+} {5 3 4 2 3 1}
+do_test 3.3 {
+ db eval {SELECT b FROM t1}
+} {4 8 2}
+
+db close
+sqlite3_shutdown
+sqlite3_config_cis 1
+sqlite3 db test.db
+
+# The CIS optimization is enabled again. Covering indices are once again
+# used for all table scans.
+do_test 4.1 {
+ db eval {SELECT a FROM t1}
+} {3 4 5}
+do_test 4.2 {
+ db eval {SELECT a, c FROM t1}
+} {5 3 4 2 3 1}
+do_test 4.3 {
+ db eval {SELECT b FROM t1}
+} {2 4 8}
+
+
+finish_test
diff --git a/test/crash5.test b/test/crash5.test
index a786712..83d1647 100644
--- a/test/crash5.test
+++ b/test/crash5.test
@@ -65,7 +65,7 @@ for {set ii 0} {$ii < 10} {incr ii} {
# puts "$n $msg ac=[sqlite3_get_autocommit db]"
# If the transaction is still active (it may not be if the malloc()
- # failure occured in the OS layer), write to the database. Make sure
+ # failure occurred in the OS layer), write to the database. Make sure
# page 4 is among those written.
#
if {![sqlite3_get_autocommit db]} {
diff --git a/test/crash7.test b/test/crash7.test
index 4554a2a..482999c 100644
--- a/test/crash7.test
+++ b/test/crash7.test
@@ -13,6 +13,7 @@
set testdir [file dirname $argv0]
source $testdir/tester.tcl
+set testprefix crash7
ifcapable !crashtest {
finish_test
@@ -79,4 +80,37 @@ foreach f [list test.db test.db-journal] {
}
}
+db close
+forcedelete test.db
+sqlite3 db test.db
+do_execsql_test 2.0 {
+ CREATE TABLE t1(a, b, UNIQUE(a, b));
+ INSERT INTO t1 VALUES(randomblob(100), randomblob(100));
+ INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM t1;
+ INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM t1;
+ INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM t1;
+ INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM t1;
+ INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM t1;
+ INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM t1;
+ INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM t1;
+ INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM t1;
+ INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM t1;
+ INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM t1;
+ INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM t1;
+ INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM t1;
+ INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM t1;
+ INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM t1;
+ DELETE FROM t1 WHERE rowid%2;
+}
+db_save_and_close
+
+for {set i 0} {$i < 20} {incr i} {
+ db_restore_and_reopen
+ do_test 2.[expr $i+1].1 {
+ crashsql -file test.db -seed $i {VACUUM}
+ } {1 {child process exited abnormally}}
+ do_execsql_test 2.[expr $i+1].2 { PRAGMA integrity_check } {ok}
+}
+
+
finish_test
diff --git a/test/crypto.test b/test/crypto.test
index e8bbe85..8a58fe0 100644
--- a/test/crypto.test
+++ b/test/crypto.test
@@ -62,6 +62,32 @@ proc setup {file key} {
db close
}
+proc get_cipher_provider {} {
+ sqlite_orig db test.db
+ return [execsql {
+ PRAGMA key = 'test';
+ PRAGMA cipher_provider;
+ }];
+}
+
+proc if_built_with_openssl {name cmd expected} {
+ if {[get_cipher_provider] == "openssl"} {
+ do_test $name $cmd $expected
+ }
+}
+
+proc if_built_with_libtomcrypt {name cmd expected} {
+ if {[get_cipher_provider] == "libtomcrypt"} {
+ do_test $name $cmd $expected
+ }
+}
+
+proc if_built_with_commoncrypto {name cmd expected} {
+ if {[get_cipher_provider] == "commoncrypto"} {
+ do_test $name $cmd $expected
+ }
+}
+
# The database is initially empty.
# set an hex key create some basic data
# create table and insert operations should work
@@ -1392,7 +1418,7 @@ do_test verify-pragma-cipher-version {
execsql {
PRAGMA cipher_version;
}
-} {2.1.1}
+} {2.2.1}
db close
file delete -force test.db
@@ -1735,7 +1761,7 @@ file delete -force test.db
# verify the pragma cipher
# reports the default value
-do_test verify-pragma-cipher-default {
+if_built_with_openssl verify-pragma-cipher-default {
sqlite_orig db test.db
execsql {
PRAGMA key = 'test';
@@ -1747,7 +1773,7 @@ file delete -force test.db
# verify the pragma cipher
# reports a change in value
-do_test verify-pragma-cipher-changed {
+if_built_with_openssl verify-pragma-cipher-changed {
sqlite_orig db test.db
execsql {
PRAGMA key = 'test';
@@ -1856,4 +1882,24 @@ do_test 2.0-beta-to-2.0-migration {
db close
file delete -force test.db
+if_built_with_libtomcrypt verify-default-cipher {
+ sqlite_orig db test.db
+ execsql {
+ PRAGMA key='test';
+ PRAGMA cipher;
+ }
+} {rijndael}
+db close
+file delete -force test.db
+
+if_built_with_commoncrypto verify-default-cipher {
+ sqlite_orig db test.db
+ execsql {
+ PRAGMA key='test';
+ PRAGMA cipher;
+ }
+} {aes-256-cbc}
+db close
+file delete -force test.db
+
finish_test
diff --git a/test/dbstatus2.test b/test/dbstatus2.test
index b2ec156..2541a1a 100644
--- a/test/dbstatus2.test
+++ b/test/dbstatus2.test
@@ -40,6 +40,7 @@ proc db_write {db {reset 0}} {
do_test 1.1 {
db close
sqlite3 db test.db
+ execsql { PRAGMA mmap_size = 0 }
expr {[file size test.db] / 1024}
} 6
@@ -85,10 +86,12 @@ do_test 2.3 { db_write db 1 } {0 4 0}
do_test 2.4 { db_write db 0 } {0 0 0}
do_test 2.5 { db_write db 1 } {0 0 0}
-do_test 2.6 {
- execsql { PRAGMA journal_mode = WAL }
- db_write db 1
-} {0 1 0}
+ifcapable wal {
+ do_test 2.6 {
+ execsql { PRAGMA journal_mode = WAL }
+ db_write db 1
+ } {0 1 0}
+}
do_test 2.7 {
execsql { INSERT INTO t1 VALUES(5, randomblob(600)) }
db_write db
diff --git a/test/descidx3.test b/test/descidx3.test
index 3cc87af..c375acc 100644
--- a/test/descidx3.test
+++ b/test/descidx3.test
@@ -132,11 +132,11 @@ ifcapable subquery {
# the IN(...) operator is not available. Hence these tests cannot be
# run.
do_test descidx3-4.1 {
- execsql {
+ lsort [execsql {
UPDATE t1 SET a=2 WHERE i<6;
SELECT i FROM t1 WHERE a IN (1,2) AND b>0 AND b<'zzz';
- }
- } {8 6 2 4 3}
+ }]
+ } {2 3 4 6 8}
do_test descidx3-4.2 {
execsql {
UPDATE t1 SET a=1;
diff --git a/test/distinct.test b/test/distinct.test
index 3a33544..fcbe4e6 100644
--- a/test/distinct.test
+++ b/test/distinct.test
@@ -168,17 +168,33 @@ foreach {tn sql temptables res} {
6 "b FROM t1" {hash} {b B}
7 "a FROM t1" {} {A a}
8 "b COLLATE nocase FROM t1" {} {b}
- 9 "b COLLATE nocase FROM t1 ORDER BY b COLLATE nocase" {} {B}
+ 9 "b COLLATE nocase FROM t1 ORDER BY b COLLATE nocase" {} {b}
} {
do_execsql_test 2.$tn.1 "SELECT DISTINCT $sql" $res
do_temptables_test 2.$tn.2 "SELECT DISTINCT $sql" $temptables
}
do_execsql_test 2.A {
- SELECT (SELECT DISTINCT o.a FROM t1 AS i) FROM t1 AS o;
+ SELECT (SELECT DISTINCT o.a FROM t1 AS i) FROM t1 AS o ORDER BY rowid;
} {a A a A}
-
-
+do_test 3.0 {
+ db eval {
+ CREATE TABLE t3(a INTEGER, b INTEGER, c, UNIQUE(a,b));
+ INSERT INTO t3 VALUES
+ (null, null, 1),
+ (null, null, 2),
+ (null, 3, 4),
+ (null, 3, 5),
+ (6, null, 7),
+ (6, null, 8);
+ SELECT DISTINCT a, b FROM t3 ORDER BY +a, +b;
+ }
+} {{} {} {} 3 6 {}}
+do_test 3.1 {
+ regexp {OpenEphemeral} [db eval {
+ EXPLAIN SELECT DISTINCT a, b FROM t3 ORDER BY +a, +b;
+ }]
+} {0}
finish_test
diff --git a/test/e_createtable.test b/test/e_createtable.test
index 8221828..351a0f7 100644
--- a/test/e_createtable.test
+++ b/test/e_createtable.test
@@ -1257,7 +1257,7 @@ do_createtable_tests 4.4 {
# SQLite allows NULL values in a PRIMARY KEY column.
#
# If the column is an integer primary key, attempting to insert a NULL
-# into the column triggers the auto-increment behaviour. Attempting
+# into the column triggers the auto-increment behavior. Attempting
# to use UPDATE to set an ipk column to a NULL value is an error.
#
do_createtable_tests 4.5.1 {
@@ -1591,7 +1591,7 @@ foreach {tn tbl res ac data} {
" $res
do_test e_createtable-4.17.$tn.3 { sqlite3_get_autocommit db } $ac
- do_execsql_test 4.17.$tn.4 "SELECT * FROM $tbl" $data
+ do_execsql_test 4.17.$tn.4 "SELECT * FROM $tbl ORDER BY rowid" $data
}
catchsql COMMIT
diff --git a/test/e_fkey.test b/test/e_fkey.test
index 5b27e03..001ba6c 100644
--- a/test/e_fkey.test
+++ b/test/e_fkey.test
@@ -627,7 +627,8 @@ proc test_efkey_57 {tn isError sql} {
execsql $sql
do_test e_fkey-18.$tn {
catchsql { INSERT INTO t2 VALUES(NULL) }
- } [lindex {{0 {}} {1 {foreign key mismatch}}} $isError]
+ } [lindex {{0 {}} {/1 {foreign key mismatch - ".*" referencing ".*"}/}} \
+ $isError]
}
test_efkey_57 2 0 { CREATE TABLE t1(x PRIMARY KEY) }
test_efkey_57 3 0 { CREATE TABLE t1(x UNIQUE) }
@@ -698,16 +699,16 @@ do_test e_fkey-19.2 {
} {}
do_test e_fkey-19.2 {
catchsql { INSERT INTO child4 VALUES('xxx', 5) }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "child4" referencing "parent"}}
do_test e_fkey-19.3 {
catchsql { INSERT INTO child5 VALUES('xxx', 6) }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "child5" referencing "parent"}}
do_test e_fkey-19.4 {
catchsql { INSERT INTO child6 VALUES(2, 3) }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "child6" referencing "parent"}}
do_test e_fkey-19.5 {
catchsql { INSERT INTO child7 VALUES(3) }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "child7" referencing "parent"}}
#-------------------------------------------------------------------------
# Test errors in the database schema that are detected while preparing
@@ -765,12 +766,12 @@ do_test e_fkey-20.1 {
foreach {tn tbl ptbl err} {
2 c1 {} "no such table: main.nosuchtable"
- 3 c2 p2 "foreign key mismatch"
- 4 c3 p3 "foreign key mismatch"
- 5 c4 p4 "foreign key mismatch"
- 6 c5 p5 "foreign key mismatch"
- 7 c6 p6 "foreign key mismatch"
- 8 c7 p7 "foreign key mismatch"
+ 3 c2 p2 "foreign key mismatch - \"c2\" referencing \"p2\""
+ 4 c3 p3 "foreign key mismatch - \"c3\" referencing \"p3\""
+ 5 c4 p4 "foreign key mismatch - \"c4\" referencing \"p4\""
+ 6 c5 p5 "foreign key mismatch - \"c5\" referencing \"p5\""
+ 7 c6 p6 "foreign key mismatch - \"c6\" referencing \"p6\""
+ 8 c7 p7 "foreign key mismatch - \"c7\" referencing \"p7\""
} {
do_test e_fkey-20.$tn.1 {
catchsql "INSERT INTO $tbl VALUES('a', 'b')"
@@ -820,22 +821,22 @@ do_test e_fkey-21.2 {
} {}
do_test e_fkey-21.3 {
catchsql { INSERT INTO child9 VALUES('I') }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "child9" referencing "parent2"}}
do_test e_fkey-21.4 {
catchsql { INSERT INTO child9 VALUES('II') }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "child9" referencing "parent2"}}
do_test e_fkey-21.5 {
catchsql { INSERT INTO child9 VALUES(NULL) }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "child9" referencing "parent2"}}
do_test e_fkey-21.6 {
catchsql { INSERT INTO child10 VALUES('I', 'II', 'III') }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "child10" referencing "parent2"}}
do_test e_fkey-21.7 {
catchsql { INSERT INTO child10 VALUES(1, 2, 3) }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "child10" referencing "parent2"}}
do_test e_fkey-21.8 {
catchsql { INSERT INTO child10 VALUES(NULL, NULL, NULL) }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "child10" referencing "parent2"}}
#-------------------------------------------------------------------------
# Test errors that are reported when creating the child table.
@@ -1151,7 +1152,7 @@ do_test e_fkey-28.8 {
CREATE TABLE c(a, b, FOREIGN KEY(a,b) REFERENCES p);
}
catchsql {DELETE FROM p}
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "c" referencing "p"}}
do_test e_fkey-28.9 {
drop_all_tables
execsql {
@@ -1159,7 +1160,7 @@ do_test e_fkey-28.9 {
CREATE TABLE c(a REFERENCES p);
}
catchsql {DELETE FROM p}
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "c" referencing "p"}}
#-------------------------------------------------------------------------
@@ -2060,7 +2061,7 @@ do_test e_fkey-45.1 {
do_test e_fkey-45.2 {
execsql {
DELETE FROM pA WHERE rowid = 3;
- SELECT quote(x) FROM pA;
+ SELECT quote(x) FROM pA ORDER BY rowid;
}
} {X'0000' X'9999' X'1234'}
do_test e_fkey-45.3 {
@@ -2069,7 +2070,7 @@ do_test e_fkey-45.3 {
do_test e_fkey-45.4 {
execsql {
UPDATE pA SET x = X'8765' WHERE rowid = 4;
- SELECT quote(x) FROM pA;
+ SELECT quote(x) FROM pA ORDER BY rowid;
}
} {X'0000' X'9999' X'8765'}
do_test e_fkey-45.5 {
@@ -2325,7 +2326,7 @@ do_test e_fkey-51.1 {
do_test e_fkey-51.2 {
execsql {
UPDATE parent SET x = 22;
- SELECT * FROM parent ; SELECT 'xxx' ; SELECT a FROM child;
+ SELECT * FROM parent ORDER BY rowid; SELECT 'xxx' ; SELECT a FROM child;
}
} {22 21 23 xxx 22}
do_test e_fkey-51.3 {
@@ -2335,7 +2336,7 @@ do_test e_fkey-51.3 {
INSERT INTO parent VALUES(-1);
INSERT INTO child VALUES(-1);
UPDATE parent SET x = 22;
- SELECT * FROM parent ; SELECT 'xxx' ; SELECT a FROM child;
+ SELECT * FROM parent ORDER BY rowid; SELECT 'xxx' ; SELECT a FROM child;
}
} {22 23 21 xxx 23}
@@ -2729,19 +2730,19 @@ do_test e_fkey-60.3 {
do_test e_fkey-60.4 {
execsql { CREATE TABLE nosuchtable(x PRIMARY KEY) }
catchsql { DELETE FROM p }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "c2" referencing "p"}}
do_test e_fkey-60.5 {
execsql { DROP TABLE c1 }
catchsql { DELETE FROM p }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "c2" referencing "p"}}
do_test e_fkey-60.6 {
execsql { DROP TABLE c2 }
execsql { DELETE FROM p }
} {}
#-------------------------------------------------------------------------
-# Test that the special behaviours of ALTER and DROP TABLE are only
-# activated when foreign keys are enabled. Special behaviours are:
+# Test that the special behaviors of ALTER and DROP TABLE are only
+# activated when foreign keys are enabled. Special behaviors are:
#
# 1. ADD COLUMN not allowing a REFERENCES clause with a non-NULL
# default value.
@@ -2836,7 +2837,7 @@ foreach zMatch [list SIMPLE PARTIAL FULL Simple parTIAL FuLL ] {
do_test e_fkey-62.$zMatch.2 {
execsql { INSERT INTO p VALUES(1, 2, 3) }
- # MATCH SIMPLE behaviour: Allow any child key that contains one or more
+ # MATCH SIMPLE behavior: Allow any child key that contains one or more
# NULL value to be inserted. Non-NULL values do not have to map to any
# parent key values, so long as at least one field of the child key is
# NULL.
diff --git a/test/e_insert.test b/test/e_insert.test
index ac4361f..951ae24 100644
--- a/test/e_insert.test
+++ b/test/e_insert.test
@@ -141,8 +141,8 @@ do_insert_tests e_insert-0 {
delete_all_data
-# EVIDENCE-OF: R-20288-20462 The first form (with the "VALUES" keyword)
-# creates a single new row in an existing table.
+# EVIDENCE-OF: R-21490-41092 The first form (with the "VALUES" keyword)
+# creates one or more new rows in an existing table.
#
do_insert_tests e_insert-1.1 {
0 "SELECT count(*) FROM a2" {0}
@@ -152,11 +152,14 @@ do_insert_tests e_insert-1.1 {
2a "INSERT INTO a2(a, b) VALUES(1, 2)" {}
2b "SELECT count(*) FROM a2" {2}
+
+ 3a "INSERT INTO a2(a) VALUES(3),(4)" {}
+ 3b "SELECT count(*) FROM a2" {4}
}
-# EVIDENCE-OF: R-36040-20870 If no column-list is specified then the
-# number of values must be the same as the number of columns in the
-# table.
+# EVIDENCE-OF: R-53616-44976 If no column-list is specified then the
+# number of values inserted into each row must be the same as the number
+# of columns in the table.
#
# A test in the block above verifies that if the VALUES list has the
# correct number of columns (for table a2, 3 columns) works. So these
@@ -171,9 +174,10 @@ do_insert_tests e_insert-1.2 -error {
4 "INSERT INTO a2 VALUES(1,2,3,4,5)" {a2 3 5}
}
-# EVIDENCE-OF: R-04006-57648 In this case the result of evaluating the
-# left-most expression in the VALUES list is inserted into the left-most
-# column of the new row, and so on.
+# EVIDENCE-OF: R-34231-22576 In this case the result of evaluating the
+# left-most expression in each term of the VALUES list is inserted into
+# the left-most column of the each new row, and forth for each
+# subsequent expression.
#
delete_all_data
do_insert_tests e_insert-1.3 {
@@ -187,8 +191,9 @@ do_insert_tests e_insert-1.3 {
3b "SELECT * FROM a2 WHERE oid=last_insert_rowid()" {2 x y}
}
-# EVIDENCE-OF: R-62524-00361 If a column-list is specified, then the
-# number of values must match the number of specified columns.
+# EVIDENCE-OF: R-44710-64652 If a column-list is specified, then the
+# number of values in each term of the VALUS list must match the number
+# of specified columns.
#
do_insert_tests e_insert-1.4 -error {
%d values for %d columns
diff --git a/test/e_select.test b/test/e_select.test
index e5949af..ea44aed 100644
--- a/test/e_select.test
+++ b/test/e_select.test
@@ -1023,10 +1023,10 @@ do_execsql_test e_select-4.9.0 {
#
do_select_tests e_select-4.9 {
1 "SELECT group_concat(one), two FROM b1 GROUP BY two" {
- 4,5 f 1 o 7,6 s 3,2 t
+ /#,# f 1 o #,# s #,# t/
}
2 "SELECT group_concat(one), sum(one) FROM b1 GROUP BY (one>4)" {
- 1,4,3,2 10 5,7,6 18
+ 1,2,3,4 10 5,6,7 18
}
3 "SELECT group_concat(one) FROM b1 GROUP BY (two>'o'), one%2" {
4 1,5 2,6 3,7
@@ -1040,7 +1040,7 @@ do_select_tests e_select-4.9 {
# values are considered equal.
#
do_select_tests e_select-4.10 {
- 1 "SELECT group_concat(y) FROM b2 GROUP BY x" {0,1 3 2,4}
+ 1 "SELECT group_concat(y) FROM b2 GROUP BY x" {/#,# 3 #,#/}
2 "SELECT count(*) FROM b2 GROUP BY CASE WHEN y<4 THEN NULL ELSE 0 END" {4 1}
}
@@ -1227,7 +1227,7 @@ do_select_tests e_select-5.1 {
# the entire set of result rows are returned by the SELECT.
#
# EVIDENCE-OF: R-47911-02086 If neither ALL or DISTINCT are present,
-# then the behaviour is as if ALL were specified.
+# then the behavior is as if ALL were specified.
#
# EVIDENCE-OF: R-14442-41305 If the simple SELECT is a SELECT DISTINCT,
# then duplicate rows are removed from the set of result rows before it
@@ -1745,12 +1745,12 @@ do_select_tests e_select-8.4 {
1 2 7 1 2 8 1 4 93 1 5 -1
}
8 "SELECT z, x FROM d1 ORDER BY 2" {
- 3 1 8 1 7 1 -20 1
- 93 1 -1 1 -1 2 93 2
+ /# 1 # 1 # 1 # 1
+ # 1 # 1 # 2 # 2/
}
9 "SELECT z, x FROM d1 ORDER BY 1" {
- -20 1 -1 2 -1 1 3 1
- 7 1 8 1 93 2 93 1
+ /-20 1 -1 # -1 # 3 1
+ 7 1 8 1 93 # 93 #/
}
}
@@ -1766,10 +1766,10 @@ do_select_tests e_select-8.5 {
94 94 9 8 4 0 0 -19
}
3 "SELECT z AS x, x AS z FROM d1 ORDER BY z" {
- 3 1 8 1 7 1 -20 1 93 1 -1 1 -1 2 93 2
+ /# 1 # 1 # 1 # 1 # 1 # 1 # 2 # 2/
}
4 "SELECT z AS x, x AS z FROM d1 ORDER BY x" {
- -20 1 -1 2 -1 1 3 1 7 1 8 1 93 2 93 1
+ /-20 1 -1 # -1 # 3 1 7 1 8 1 93 # 93 #/
}
}
diff --git a/test/e_uri.test b/test/e_uri.test
index 8c9949e..ac34ed4 100644
--- a/test/e_uri.test
+++ b/test/e_uri.test
@@ -261,9 +261,9 @@ foreach {tn uri error} "
}
-# EVIDENCE-OF: R-09651-31805 If "ro" is specified, then the database is
+# EVIDENCE-OF: R-43036-46756 If "ro" is specified, then the database is
# opened for read-only access, just as if the SQLITE_OPEN_READONLY flag
-# had been set in the third argument to sqlite3_prepare_v2().
+# had been set in the third argument to sqlite3_open_v2().
#
# EVIDENCE-OF: R-40137-26050 If the mode option is set to "rw", then the
# database is opened for read-write (but not create) access, as if
@@ -361,7 +361,7 @@ foreach {tn uri error} "
#
# EVIDENCE-OF: R-19510-48080 If sqlite3_open_v2() is used and the
# "cache" parameter is present in a URI filename, its value overrides
-# any behaviour requested by setting SQLITE_OPEN_PRIVATECACHE or
+# any behavior requested by setting SQLITE_OPEN_PRIVATECACHE or
# SQLITE_OPEN_SHAREDCACHE flag.
#
set orig [sqlite3_enable_shared_cache]
diff --git a/test/enc2.test b/test/enc2.test
index 415bc0f..3eb3aa2 100644
--- a/test/enc2.test
+++ b/test/enc2.test
@@ -32,7 +32,7 @@ ifcapable {!utf16} {
# enc2.3.*: Simple tests with a UTF-16BE db.
# enc2.4.*: Test that attached databases must have the same text encoding
# as the main database.
-# enc2.5.*: Test the behaviour of the library when a collation sequence is
+# enc2.5.*: Test the behavior of the library when a collation sequence is
# not available for the most desirable text encoding.
# enc2.6.*: Similar test for user functions.
# enc2.7.*: Test that the VerifyCookie opcode protects against assuming the
diff --git a/test/eqp.test b/test/eqp.test
index 0e663f0..454f2af 100644
--- a/test/eqp.test
+++ b/test/eqp.test
@@ -62,7 +62,7 @@ do_eqp_test 1.3 {
do_eqp_test 1.4 {
SELECT a FROM t1 ORDER BY +a
} {
- 0 0 0 {SCAN TABLE t1 (~1000000 rows)}
+ 0 0 0 {SCAN TABLE t1 USING COVERING INDEX i1 (~1000000 rows)}
0 0 0 {USE TEMP B-TREE FOR ORDER BY}
}
do_eqp_test 1.5 {
@@ -166,7 +166,7 @@ det 2.3.2 "SELECT min(x) FROM t2" {
0 0 0 {SEARCH TABLE t2 USING COVERING INDEX t2i1 (~1 rows)}
}
det 2.3.3 "SELECT min(x), max(x) FROM t2" {
- 0 0 0 {SCAN TABLE t2 (~1000000 rows)}
+ 0 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1 (~1000000 rows)}
}
det 2.4.1 "SELECT * FROM t1 WHERE rowid=?" {
@@ -339,7 +339,7 @@ do_eqp_test 4.3.1 {
SELECT x FROM t1 UNION SELECT x FROM t2
} {
1 0 0 {SCAN TABLE t1 (~1000000 rows)}
- 2 0 0 {SCAN TABLE t2 (~1000000 rows)}
+ 2 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1 (~1000000 rows)}
0 0 0 {COMPOUND SUBQUERIES 1 AND 2 USING TEMP B-TREE (UNION)}
}
@@ -347,7 +347,7 @@ do_eqp_test 4.3.2 {
SELECT x FROM t1 UNION SELECT x FROM t2 UNION SELECT x FROM t1
} {
2 0 0 {SCAN TABLE t1 (~1000000 rows)}
- 3 0 0 {SCAN TABLE t2 (~1000000 rows)}
+ 3 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1 (~1000000 rows)}
1 0 0 {COMPOUND SUBQUERIES 2 AND 3 USING TEMP B-TREE (UNION)}
4 0 0 {SCAN TABLE t1 (~1000000 rows)}
0 0 0 {COMPOUND SUBQUERIES 1 AND 4 USING TEMP B-TREE (UNION)}
@@ -447,7 +447,7 @@ det 5.8.1 "SELECT c, d FROM t2 ORDER BY c" {
det 5.9 {
SELECT (SELECT b FROM t1 WHERE a=0), (SELECT a FROM t1 WHERE b=t2.c) FROM t2
} {
- 0 0 0 {SCAN TABLE t2 (~1000000 rows)}
+ 0 0 0 {SCAN TABLE t2 USING COVERING INDEX i4 (~1000000 rows)}
0 0 0 {EXECUTE SCALAR SUBQUERY 1}
1 0 0 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=?) (~10 rows)}
0 0 0 {EXECUTE CORRELATED SCALAR SUBQUERY 2}
@@ -471,7 +471,7 @@ det 5.10 {
# (c=?) (~10 rows) 0|1|1|SCAN TABLE t1 (~1000000 rows)
det 5.11 "SELECT * FROM (SELECT * FROM t2 WHERE c=1), t1" {
0 0 0 {SEARCH TABLE t2 USING INDEX i4 (c=?) (~10 rows)}
- 0 1 1 {SCAN TABLE t1 (~1000000 rows)}
+ 0 1 1 {SCAN TABLE t1 USING COVERING INDEX i2 (~1000000 rows)}
}
# EVIDENCE-OF: R-40701-42164 sqlite> EXPLAIN QUERY PLAN SELECT a FROM
@@ -479,8 +479,8 @@ det 5.11 "SELECT * FROM (SELECT * FROM t2 WHERE c=1), t1" {
# 2|0|0|SCAN TABLE t2 (~1000000 rows) 0|0|0|COMPOUND SUBQUERIES 1 AND 2
# USING TEMP B-TREE (UNION)
det 5.12 "SELECT a FROM t1 UNION SELECT c FROM t2" {
- 1 0 0 {SCAN TABLE t1 (~1000000 rows)}
- 2 0 0 {SCAN TABLE t2 (~1000000 rows)}
+ 1 0 0 {SCAN TABLE t1 USING COVERING INDEX i1 (~1000000 rows)}
+ 2 0 0 {SCAN TABLE t2 USING COVERING INDEX i4 (~1000000 rows)}
0 0 0 {COMPOUND SUBQUERIES 1 AND 2 USING TEMP B-TREE (UNION)}
}
diff --git a/test/errmsg.test b/test/errmsg.test
index 9f8409b..6b3f3b7 100644
--- a/test/errmsg.test
+++ b/test/errmsg.test
@@ -80,12 +80,14 @@ do_test 2.2 {
SQLITE_ERROR {SQL logic error or missing database}
SQLITE_CONSTRAINT {column b is not unique}
}]
+verify_ex_errcode 2.2b SQLITE_CONSTRAINT_UNIQUE
do_test 2.3 {
error_messages_v2 "INSERT INTO t1 VALUES('ghi', 'def')"
} [list {*}{
SQLITE_CONSTRAINT {column b is not unique}
SQLITE_CONSTRAINT {column b is not unique}
}]
+verify_ex_errcode 2.3b SQLITE_CONSTRAINT_UNIQUE
#-------------------------------------------------------------------------
# Test SQLITE_SCHEMA errors. And, for _v2(), test that if the schema
diff --git a/test/exclusive2.test b/test/exclusive2.test
index 2208da5..54203e3 100644
--- a/test/exclusive2.test
+++ b/test/exclusive2.test
@@ -25,6 +25,14 @@ ifcapable {!pager_pragmas} {
return
}
+# Tests in this file verify that locking_mode=exclusive causes SQLite to
+# use cached pages even if the database is changed on disk. This doesn't
+# work with mmap.
+if {[permutation]=="mmap"} {
+ finish_test
+ return
+}
+
# This module does not work right if the cache spills at unexpected
# moments. So disable the soft-heap-limit.
#
diff --git a/test/filectrl.test b/test/filectrl.test
index 1e4ec59..1d878bf 100644
--- a/test/filectrl.test
+++ b/test/filectrl.test
@@ -36,6 +36,12 @@ do_test filectrl-1.5 {
sqlite3 db test_control_lockproxy.db
file_control_lockproxy_test db [get_pwd]
} {}
+do_test filectrl-1.6 {
+ sqlite3 db test.db
+ set fn [file_control_tempfilename db]
+ puts -nonewline \[$fn\]
+ set fn
+} {/etilqs_/}
db close
forcedelete .test_control_lockproxy.db-conch test.proxy
finish_test
diff --git a/test/filefmt.test b/test/filefmt.test
index 1165cd6..bc1af18 100644
--- a/test/filefmt.test
+++ b/test/filefmt.test
@@ -213,4 +213,39 @@ do_execsql_test filefmt-3.3 {
PRAGMA integrity_check;
} {ok}
+reset_db
+do_execsql_test filefmt-4.1 {
+ PRAGMA auto_vacuum = 1;
+ CREATE TABLE t1(x, y);
+ CREATE TABLE t2(x, y);
+
+ INSERT INTO t1 VALUES(randomblob(100), randomblob(100));
+ INSERT INTO t1 VALUES(randomblob(100), randomblob(100));
+ INSERT INTO t1 VALUES(randomblob(100), randomblob(100));
+ INSERT INTO t1 VALUES(randomblob(100), randomblob(100));
+ INSERT INTO t1 VALUES(randomblob(100), randomblob(100));
+ INSERT INTO t1 VALUES(randomblob(100), randomblob(100));
+
+ INSERT INTO t2 SELECT randomblob(100), randomblob(100) FROM t1;
+ INSERT INTO t2 SELECT randomblob(100), randomblob(100) FROM t1;
+ INSERT INTO t2 SELECT randomblob(100), randomblob(100) FROM t1;
+ INSERT INTO t2 SELECT randomblob(100), randomblob(100) FROM t1;
+}
+
+do_test filefmt-4.2 {
+ sql36231 { INSERT INTO t2 SELECT * FROM t1 }
+} {}
+
+do_test filefmt-4.3 {
+ forcedelete bak.db
+ db backup bak.db
+} {}
+
+do_test filefmt-4.4 {
+ sqlite3 db2 bak.db
+ db2 eval { PRAGMA integrity_check }
+} {ok}
+db2 close
+
finish_test
+
diff --git a/test/fkey2.test b/test/fkey2.test
index f0cc4d2..3e5b27c 100644
--- a/test/fkey2.test
+++ b/test/fkey2.test
@@ -139,14 +139,21 @@ set FkeySimpleTests {
4.17 "UPDATE t7 SET a = 10" {0 {}}
5.1 "INSERT INTO t9 VALUES(1, 3)" {1 {no such table: main.nosuchtable}}
- 5.2 "INSERT INTO t10 VALUES(1, 3)" {1 {foreign key mismatch}}
+ 5.2 "INSERT INTO t10 VALUES(1, 3)"
+ {1 {foreign key mismatch - "t10" referencing "t9"}}
}
do_test fkey2-1.1.0 {
execsql [string map {/D/ {}} $FkeySimpleSchema]
} {}
foreach {tn zSql res} $FkeySimpleTests {
- do_test fkey2-1.1.$tn { catchsql $zSql } $res
+ do_test fkey2-1.1.$tn.1 { catchsql $zSql } $res
+ do_test fkey2-1.1.$tn.2 { execsql {PRAGMA foreign_key_check(t1)} } {}
+ do_test fkey2-1.1.$tn.3 { execsql {PRAGMA foreign_key_check(t2)} } {}
+ do_test fkey2-1.1.$tn.4 { execsql {PRAGMA foreign_key_check(t3)} } {}
+ do_test fkey2-1.1.$tn.5 { execsql {PRAGMA foreign_key_check(t4)} } {}
+ do_test fkey2-1.1.$tn.6 { execsql {PRAGMA foreign_key_check(t7)} } {}
+ do_test fkey2-1.1.$tn.7 { execsql {PRAGMA foreign_key_check(t8)} } {}
}
drop_all_tables
@@ -155,6 +162,12 @@ do_test fkey2-1.2.0 {
} {}
foreach {tn zSql res} $FkeySimpleTests {
do_test fkey2-1.2.$tn { catchsql $zSql } $res
+ do_test fkey2-1.2.$tn.2 { execsql {PRAGMA foreign_key_check(t1)} } {}
+ do_test fkey2-1.2.$tn.3 { execsql {PRAGMA foreign_key_check(t2)} } {}
+ do_test fkey2-1.2.$tn.4 { execsql {PRAGMA foreign_key_check(t3)} } {}
+ do_test fkey2-1.2.$tn.5 { execsql {PRAGMA foreign_key_check(t4)} } {}
+ do_test fkey2-1.2.$tn.6 { execsql {PRAGMA foreign_key_check(t7)} } {}
+ do_test fkey2-1.2.$tn.7 { execsql {PRAGMA foreign_key_check(t8)} } {}
}
drop_all_tables
@@ -165,6 +178,12 @@ do_test fkey2-1.3.0 {
foreach {tn zSql res} $FkeySimpleTests {
if {$res == "0 {}"} { set res {0 1} }
do_test fkey2-1.3.$tn { catchsql $zSql } $res
+ do_test fkey2-1.3.$tn.2 { execsql {PRAGMA foreign_key_check(t1)} } {}
+ do_test fkey2-1.3.$tn.3 { execsql {PRAGMA foreign_key_check(t2)} } {}
+ do_test fkey2-1.3.$tn.4 { execsql {PRAGMA foreign_key_check(t3)} } {}
+ do_test fkey2-1.3.$tn.5 { execsql {PRAGMA foreign_key_check(t4)} } {}
+ do_test fkey2-1.3.$tn.6 { execsql {PRAGMA foreign_key_check(t7)} } {}
+ do_test fkey2-1.3.$tn.7 { execsql {PRAGMA foreign_key_check(t8)} } {}
}
execsql { PRAGMA count_changes = 0 }
drop_all_tables
@@ -681,7 +700,7 @@ foreach zSql [list {
do_test fkey2-10.1.[incr tn] {
execsql $zSql
catchsql { INSERT INTO c DEFAULT VALUES }
- } {1 {foreign key mismatch}}
+ } {/1 {foreign key mismatch - "c" referencing "."}/}
}
# "rowid" cannot be used as part of a child or parent key definition
@@ -709,7 +728,7 @@ do_test fkey2-10.2.1 {
INSERT INTO t1(rowid, a, b) VALUES(1, 1, 1);
INSERT INTO t2 VALUES(1, 1);
}
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "t2" referencing "t1"}}
do_test fkey2-10.2.2 {
drop_all_tables
catchsql {
@@ -1223,7 +1242,7 @@ do_test fkey-2.14.3.8 {
CREATE TABLE cc(a, b, FOREIGN KEY(a, b) REFERENCES pp(x, z));
}
catchsql { INSERT INTO cc VALUES(1, 2) }
-} {1 {foreign key mismatch}}
+} {1 {foreign key mismatch - "cc" referencing "pp"}}
do_test fkey-2.14.3.9 {
execsql { DROP TABLE cc }
} {}
@@ -1414,10 +1433,12 @@ do_test fkey2-17.1.2 {
set STMT [sqlite3_prepare_v2 db "INSERT INTO two VALUES(4, 5, 6)" -1 dummy]
sqlite3_step $STMT
} {SQLITE_CONSTRAINT}
+verify_ex_errcode fkey2-17.1.2b SQLITE_CONSTRAINT_FOREIGNKEY
ifcapable autoreset {
do_test fkey2-17.1.3 {
sqlite3_step $STMT
} {SQLITE_CONSTRAINT}
+ verify_ex_errcode fkey2-17.1.3b SQLITE_CONSTRAINT_FOREIGNKEY
} else {
do_test fkey2-17.1.3 {
sqlite3_step $STMT
@@ -1426,6 +1447,7 @@ ifcapable autoreset {
do_test fkey2-17.1.4 {
sqlite3_finalize $STMT
} {SQLITE_CONSTRAINT}
+verify_ex_errcode fkey2-17.1.4b SQLITE_CONSTRAINT_FOREIGNKEY
do_test fkey2-17.1.5 {
execsql {
INSERT INTO one VALUES(2, 3, 4);
@@ -1469,9 +1491,11 @@ do_test fkey2-17.1.12 {
do_test fkey2-17.1.13 {
sqlite3_step $STMT
} {SQLITE_CONSTRAINT}
+verify_ex_errcode fkey2-17.1.13b SQLITE_CONSTRAINT_FOREIGNKEY
do_test fkey2-17.1.14 {
sqlite3_finalize $STMT
} {SQLITE_CONSTRAINT}
+verify_ex_errcode fkey2-17.1.14b SQLITE_CONSTRAINT_FOREIGNKEY
drop_all_tables
do_test fkey2-17.2.1 {
@@ -1625,9 +1649,11 @@ do_test fkey2-19.2 {
sqlite3_bind_int $S 1 2
sqlite3_step $S
} {SQLITE_CONSTRAINT}
+verify_ex_errcode fkey2-19.2b SQLITE_CONSTRAINT_FOREIGNKEY
do_test fkey2-19.3 {
sqlite3_reset $S
} {SQLITE_CONSTRAINT}
+verify_ex_errcode fkey2-19.3b SQLITE_CONSTRAINT_FOREIGNKEY
do_test fkey2-19.4 {
sqlite3_bind_int $S 1 1
sqlite3_step $S
diff --git a/test/fkey4.test b/test/fkey4.test
index d6dd2fc..79cf663 100644
--- a/test/fkey4.test
+++ b/test/fkey4.test
@@ -42,10 +42,12 @@ do_test fkey4-1.2 {
set ::STMT1 [sqlite3_prepare_v2 $::DB $::SQL -1 TAIL]
sqlite3_step $::STMT1
} {SQLITE_CONSTRAINT}
+verify_ex_errcode fkey4-1.2b SQLITE_CONSTRAINT_FOREIGNKEY
do_test fkey4-1.3 {
set ::STMT2 [sqlite3_prepare_v2 $::DB $::SQL -1 TAIL]
sqlite3_step $::STMT2
} {SQLITE_CONSTRAINT}
+verify_ex_errcode fkey4-1.3b SQLITE_CONSTRAINT_FOREIGNKEY
do_test fkey4-1.4 {
db eval {SELECT * FROM t2}
} {1 3}
diff --git a/test/fkey5.test b/test/fkey5.test
new file mode 100644
index 0000000..40a1a5e
--- /dev/null
+++ b/test/fkey5.test
@@ -0,0 +1,310 @@
+# 2012 December 17
+#
+# 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 file implements regression tests for SQLite library.
+#
+# This file tests the PRAGMA foreign_key_check command.
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+
+ifcapable {!foreignkey} {
+ finish_test
+ return
+}
+
+do_test fkey5-1.1 {
+ db eval {
+ CREATE TABLE p1(a INTEGER PRIMARY KEY); INSERT INTO p1 VALUES(88),(89);
+ CREATE TABLE p2(a INT PRIMARY KEY); INSERT INTO p2 VALUES(77),(78);
+ CREATE TABLE p3(a TEXT PRIMARY KEY);
+ INSERT INTO p3 VALUES(66),(67),('alpha'),('BRAVO');
+ CREATE TABLE p4(a TEXT PRIMARY KEY COLLATE nocase);
+ INSERT INTO p4 VALUES('alpha'),('BRAVO'),('55'),('Delta'),('ECHO');
+ CREATE TABLE p5(a INTEGER PRIMARY KEY, b, c, UNIQUE(b,c));
+ INSERT INTO p5 VALUES(1,'Alpha','abc'),(2,'beta','def');
+ CREATE TABLE p6(a INTEGER PRIMARY KEY, b TEXT COLLATE nocase,
+ c TEXT COLLATE rtrim, UNIQUE(b,c));
+ INSERT INTO p6 VALUES(1,'Alpha','abc '),(2,'bETA','def ');
+
+ CREATE TABLE c1(x INTEGER PRIMARY KEY references p1);
+ CREATE TABLE c2(x INTEGER PRIMARY KEY references p2);
+ CREATE TABLE c3(x INTEGER PRIMARY KEY references p3);
+ CREATE TABLE c4(x INTEGER PRIMARY KEY references p4);
+ CREATE TABLE c5(x INT references p1);
+ CREATE TABLE c6(x INT references p2);
+ CREATE TABLE c7(x INT references p3);
+ CREATE TABLE c8(x INT references p4);
+ CREATE TABLE c9(x TEXT UNIQUE references p1);
+ CREATE TABLE c10(x TEXT UNIQUE references p2);
+ CREATE TABLE c11(x TEXT UNIQUE references p3);
+ CREATE TABLE c12(x TEXT UNIQUE references p4);
+ CREATE TABLE c13(x TEXT COLLATE nocase references p3);
+ CREATE TABLE c14(x TEXT COLLATE nocase references p4);
+ CREATE TABLE c15(x, y, FOREIGN KEY(x,y) REFERENCES p5(b,c));
+ CREATE TABLE c16(x, y, FOREIGN KEY(x,y) REFERENCES p5(c,b));
+ CREATE TABLE c17(x, y, FOREIGN KEY(x,y) REFERENCES p6(b,c));
+ CREATE TABLE c18(x, y, FOREIGN KEY(x,y) REFERENCES p6(c,b));
+ CREATE TABLE c19(x TEXT COLLATE nocase, y TEXT COLLATE rtrim,
+ FOREIGN KEY(x,y) REFERENCES p5(b,c));
+ CREATE TABLE c20(x TEXT COLLATE nocase, y TEXT COLLATE rtrim,
+ FOREIGN KEY(x,y) REFERENCES p5(c,b));
+ CREATE TABLE c21(x TEXT COLLATE nocase, y TEXT COLLATE rtrim,
+ FOREIGN KEY(x,y) REFERENCES p6(b,c));
+ CREATE TABLE c22(x TEXT COLLATE nocase, y TEXT COLLATE rtrim,
+ FOREIGN KEY(x,y) REFERENCES p6(c,b));
+
+ PRAGMA foreign_key_check;
+ }
+} {}
+do_test fkey5-1.2 {
+ db eval {
+ INSERT INTO c1 VALUES(90),(87),(88);
+ PRAGMA foreign_key_check;
+ }
+} {c1 87 p1 0 c1 90 p1 0}
+do_test fkey5-1.3 {
+ db eval {
+ PRAGMA foreign_key_check(c1);
+ }
+} {c1 87 p1 0 c1 90 p1 0}
+do_test fkey5-1.4 {
+ db eval {
+ PRAGMA foreign_key_check(c2);
+ }
+} {}
+
+do_test fkey5-2.0 {
+ db eval {
+ INSERT INTO c5 SELECT x FROM c1;
+ DELETE FROM c1;
+ PRAGMA foreign_key_check;
+ }
+} {c5 1 p1 0 c5 3 p1 0}
+do_test fkey5-2.1 {
+ db eval {
+ PRAGMA foreign_key_check(c5);
+ }
+} {c5 1 p1 0 c5 3 p1 0}
+do_test fkey5-2.2 {
+ db eval {
+ PRAGMA foreign_key_check(c1);
+ }
+} {}
+
+do_test fkey5-3.0 {
+ db eval {
+ INSERT INTO c9 SELECT x FROM c5;
+ DELETE FROM c5;
+ PRAGMA foreign_key_check;
+ }
+} {c9 1 p1 0 c9 3 p1 0}
+do_test fkey5-3.1 {
+ db eval {
+ PRAGMA foreign_key_check(c9);
+ }
+} {c9 1 p1 0 c9 3 p1 0}
+do_test fkey5-3.2 {
+ db eval {
+ PRAGMA foreign_key_check(c5);
+ }
+} {}
+
+do_test fkey5-4.0 {
+ db eval {
+ DELETE FROM c9;
+ INSERT INTO c2 VALUES(79),(77),(76);
+ PRAGMA foreign_key_check;
+ }
+} {c2 76 p2 0 c2 79 p2 0}
+do_test fkey5-4.1 {
+ db eval {
+ PRAGMA foreign_key_check(c2);
+ }
+} {c2 76 p2 0 c2 79 p2 0}
+do_test fkey5-4.2 {
+ db eval {
+ INSERT INTO c6 SELECT x FROM c2;
+ DELETE FROM c2;
+ PRAGMA foreign_key_check;
+ }
+} {c6 1 p2 0 c6 3 p2 0}
+do_test fkey5-4.3 {
+ db eval {
+ PRAGMA foreign_key_check(c6);
+ }
+} {c6 1 p2 0 c6 3 p2 0}
+do_test fkey5-4.4 {
+ db eval {
+ INSERT INTO c10 SELECT x FROM c6;
+ DELETE FROM c6;
+ PRAGMA foreign_key_check;
+ }
+} {c10 1 p2 0 c10 3 p2 0}
+do_test fkey5-4.5 {
+ db eval {
+ PRAGMA foreign_key_check(c10);
+ }
+} {c10 1 p2 0 c10 3 p2 0}
+
+do_test fkey5-5.0 {
+ db eval {
+ DELETE FROM c10;
+ INSERT INTO c3 VALUES(68),(67),(65);
+ PRAGMA foreign_key_check;
+ }
+} {c3 65 p3 0 c3 68 p3 0}
+do_test fkey5-5.1 {
+ db eval {
+ PRAGMA foreign_key_check(c3);
+ }
+} {c3 65 p3 0 c3 68 p3 0}
+do_test fkey5-5.2 {
+ db eval {
+ INSERT INTO c7 SELECT x FROM c3;
+ INSERT INTO c7 VALUES('Alpha'),('alpha'),('foxtrot');
+ DELETE FROM c3;
+ PRAGMA foreign_key_check;
+ }
+} {c7 1 p3 0 c7 3 p3 0 c7 4 p3 0 c7 6 p3 0}
+do_test fkey5-5.3 {
+ db eval {
+ PRAGMA foreign_key_check(c7);
+ }
+} {c7 1 p3 0 c7 3 p3 0 c7 4 p3 0 c7 6 p3 0}
+do_test fkey5-5.4 {
+ db eval {
+ INSERT INTO c11 SELECT x FROM c7;
+ DELETE FROM c7;
+ PRAGMA foreign_key_check;
+ }
+} {c11 1 p3 0 c11 3 p3 0 c11 4 p3 0 c11 6 p3 0}
+do_test fkey5-5.5 {
+ db eval {
+ PRAGMA foreign_key_check(c11);
+ }
+} {c11 1 p3 0 c11 3 p3 0 c11 4 p3 0 c11 6 p3 0}
+
+do_test fkey5-6.0 {
+ db eval {
+ DELETE FROM c11;
+ INSERT INTO c4 VALUES(54),(55),(56);
+ PRAGMA foreign_key_check;
+ }
+} {c4 54 p4 0 c4 56 p4 0}
+do_test fkey5-6.1 {
+ db eval {
+ PRAGMA foreign_key_check(c4);
+ }
+} {c4 54 p4 0 c4 56 p4 0}
+do_test fkey5-6.2 {
+ db eval {
+ INSERT INTO c8 SELECT x FROM c4;
+ INSERT INTO c8 VALUES('Alpha'),('ALPHA'),('foxtrot');
+ DELETE FROM c4;
+ PRAGMA foreign_key_check;
+ }
+} {c8 1 p4 0 c8 3 p4 0 c8 6 p4 0}
+do_test fkey5-6.3 {
+ db eval {
+ PRAGMA foreign_key_check(c8);
+ }
+} {c8 1 p4 0 c8 3 p4 0 c8 6 p4 0}
+do_test fkey5-6.4 {
+ db eval {
+ INSERT INTO c12 SELECT x FROM c8;
+ DELETE FROM c8;
+ PRAGMA foreign_key_check;
+ }
+} {c12 1 p4 0 c12 3 p4 0 c12 6 p4 0}
+do_test fkey5-6.5 {
+ db eval {
+ PRAGMA foreign_key_check(c12);
+ }
+} {c12 1 p4 0 c12 3 p4 0 c12 6 p4 0}
+
+do_test fkey5-7.1 {
+ db eval {
+ INSERT OR IGNORE INTO c13 SELECT * FROM c12;
+ INSERT OR IGNORE INTO C14 SELECT * FROM c12;
+ DELETE FROM c12;
+ PRAGMA foreign_key_check;
+ }
+} {c14 1 p4 0 c14 3 p4 0 c14 6 p4 0 c13 1 p3 0 c13 2 p3 0 c13 3 p3 0 c13 4 p3 0 c13 5 p3 0 c13 6 p3 0}
+do_test fkey5-7.2 {
+ db eval {
+ PRAGMA foreign_key_check(c14);
+ }
+} {c14 1 p4 0 c14 3 p4 0 c14 6 p4 0}
+do_test fkey5-7.3 {
+ db eval {
+ PRAGMA foreign_key_check(c13);
+ }
+} {c13 1 p3 0 c13 2 p3 0 c13 3 p3 0 c13 4 p3 0 c13 5 p3 0 c13 6 p3 0}
+
+do_test fkey5-8.0 {
+ db eval {
+ DELETE FROM c13;
+ DELETE FROM c14;
+ INSERT INTO c19 VALUES('alpha','abc');
+ PRAGMA foreign_key_check(c19);
+ }
+} {c19 1 p5 0}
+do_test fkey5-8.1 {
+ db eval {
+ DELETE FROM c19;
+ INSERT INTO c19 VALUES('Alpha','abc');
+ PRAGMA foreign_key_check(c19);
+ }
+} {}
+do_test fkey5-8.2 {
+ db eval {
+ INSERT INTO c20 VALUES('Alpha','abc');
+ PRAGMA foreign_key_check(c20);
+ }
+} {c20 1 p5 0}
+do_test fkey5-8.3 {
+ db eval {
+ DELETE FROM c20;
+ INSERT INTO c20 VALUES('abc','Alpha');
+ PRAGMA foreign_key_check(c20);
+ }
+} {}
+do_test fkey5-8.4 {
+ db eval {
+ INSERT INTO c21 VALUES('alpha','abc ');
+ PRAGMA foreign_key_check(c21);
+ }
+} {}
+do_test fkey5-8.5 {
+ db eval {
+ DELETE FROM c21;
+ INSERT INTO c19 VALUES('Alpha','abc');
+ PRAGMA foreign_key_check(c21);
+ }
+} {}
+do_test fkey5-8.6 {
+ db eval {
+ INSERT INTO c22 VALUES('Alpha','abc');
+ PRAGMA foreign_key_check(c22);
+ }
+} {c22 1 p6 0}
+do_test fkey5-8.7 {
+ db eval {
+ DELETE FROM c22;
+ INSERT INTO c22 VALUES('abc ','ALPHA');
+ PRAGMA foreign_key_check(c22);
+ }
+} {}
+
+
+
+finish_test
diff --git a/test/fkey_malloc.test b/test/fkey_malloc.test
index 4a36f5f..b4b5b4e 100644
--- a/test/fkey_malloc.test
+++ b/test/fkey_malloc.test
@@ -29,6 +29,7 @@ do_malloc_test fkey_malloc-1 -sqlprep {
INSERT INTO t2 VALUES('aaa');
UPDATE t1 SET a = 'bbb';
DELETE FROM t1;
+ PRAGMA foreign_key_check;
}
do_malloc_test fkey_malloc-2 -sqlprep {
@@ -128,5 +129,3 @@ do_malloc_test fkey_malloc-7 -sqlprep {
}
finish_test
-
-
diff --git a/test/fts3ai.test b/test/fts3ai.test
index 144b4c3..b17b5bd 100644
--- a/test/fts3ai.test
+++ b/test/fts3ai.test
@@ -19,6 +19,11 @@ ifcapable !fts3 {
return
}
+ifcapable !utf16 {
+ finish_test
+ return
+}
+
# Return the UTF-16 representation of the supplied UTF-8 string $str.
# If $nt is true, append two 0x00 bytes as a nul terminator.
# NOTE(shess) Copied from capi3.test.
diff --git a/test/fts3aux1.test b/test/fts3aux1.test
index adda586..ef17949 100644
--- a/test/fts3aux1.test
+++ b/test/fts3aux1.test
@@ -354,10 +354,10 @@ do_execsql_test 3.1.1 {
do_catchsql_test 3.1.2 {
CREATE VIRTUAL TABLE terms2 USING fts4aux;
-} {1 {wrong number of arguments to fts4aux constructor}}
+} {1 {invalid arguments to fts4aux constructor}}
do_catchsql_test 3.1.3 {
CREATE VIRTUAL TABLE terms2 USING fts4aux(t2, t2);
-} {1 {wrong number of arguments to fts4aux constructor}}
+} {1 {invalid arguments to fts4aux constructor}}
do_execsql_test 3.2.1 {
CREATE VIRTUAL TABLE terms3 USING fts4aux(does_not_exist)
@@ -444,7 +444,6 @@ do_plansql_test 4.5 {
# odd name (one that requires quoting for use in SQL statements). And that
# the argument to the fts4aux constructor is properly dequoted before use.
#
-#
do_execsql_test 5.1 {
CREATE VIRTUAL TABLE "abc '!' def" USING fts4(x, y);
INSERT INTO "abc '!' def" VALUES('XX', 'YY');
@@ -458,5 +457,66 @@ do_execsql_test 5.2 {
SELECT * FROM "%%^^%%";
} {xx * 1 1 xx 0 1 1 yy * 1 1 yy 1 1 1}
+#-------------------------------------------------------------------------
+# Test that we can create an fts4aux table in the temp database.
+#
+forcedelete test.db2
+do_execsql_test 6.1 {
+ CREATE VIRTUAL TABLE ft1 USING fts4(x, y);
+ INSERT INTO ft1 VALUES('a b', 'c d');
+ INSERT INTO ft1 VALUES('e e', 'c d');
+ INSERT INTO ft1 VALUES('a a', 'b b');
+ CREATE VIRTUAL TABLE temp.aux1 USING fts4aux(main, ft1);
+ SELECT * FROM aux1;
+} {
+ a * 2 3 a 0 2 3
+ b * 2 3 b 0 1 1 b 1 1 2
+ c * 2 2 c 1 2 2
+ d * 2 2 d 1 2 2
+ e * 1 2 e 0 1 2
+}
+
+do_execsql_test 6.2 {
+ ATTACH 'test.db2' AS att;
+ CREATE VIRTUAL TABLE att.ft1 USING fts4(x, y);
+ INSERT INTO att.ft1 VALUES('v w', 'x y');
+ INSERT INTO att.ft1 VALUES('z z', 'x y');
+ INSERT INTO att.ft1 VALUES('v v', 'w w');
+ CREATE VIRTUAL TABLE temp.aux2 USING fts4aux(att, ft1);
+ SELECT * FROM aux2;
+} {
+ v * 2 3 v 0 2 3
+ w * 2 3 w 0 1 1 w 1 1 2
+ x * 2 2 x 1 2 2
+ y * 2 2 y 1 2 2
+ z * 1 2 z 0 1 2
+}
+
+foreach {tn q res1 res2} {
+ 1 { SELECT * FROM %%% WHERE term = 'a' } {a * 2 3 a 0 2 3} {}
+ 2 { SELECT * FROM %%% WHERE term = 'x' } {} {x * 2 2 x 1 2 2}
+
+ 3 { SELECT * FROM %%% WHERE term >= 'y' }
+ {} {y * 2 2 y 1 2 2 z * 1 2 z 0 1 2}
+
+ 4 { SELECT * FROM %%% WHERE term <= 'c' }
+ {a * 2 3 a 0 2 3 b * 2 3 b 0 1 1 b 1 1 2 c * 2 2 c 1 2 2} {}
+} {
+ set sql1 [string map {%%% aux1} $q]
+ set sql2 [string map {%%% aux2} $q]
+
+ do_execsql_test 7.$tn.1 $sql1 $res1
+ do_execsql_test 7.$tn.2 $sql2 $res2
+}
+
+do_test 8.1 {
+ catchsql { CREATE VIRTUAL TABLE att.aux3 USING fts4aux(main, ft1) }
+} {1 {invalid arguments to fts4aux constructor}}
+
+do_test 8.2 {
+ execsql {DETACH att}
+ catchsql { SELECT * FROM aux2 }
+} {1 {SQL logic error or missing database}}
finish_test
+
diff --git a/test/fts3conf.test b/test/fts3conf.test
index ce41027..e91efbe 100644
--- a/test/fts3conf.test
+++ b/test/fts3conf.test
@@ -136,4 +136,46 @@ do_execsql_test 2.2.2 { COMMIT }
do_execsql_test 2.2.3 { SELECT * FROM t1 } {{a b c} {a b c}}
fts3_integrity 2.2.4 db t1
+do_execsql_test 3.1 {
+ CREATE VIRTUAL TABLE t3 USING fts4;
+ REPLACE INTO t3(docid, content) VALUES (1, 'one two');
+ SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'one'
+} {X'0100000002000000'}
+
+do_execsql_test 3.2 {
+ REPLACE INTO t3(docid, content) VALUES (2, 'one two three four');
+ SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'four'
+} {X'0200000003000000'}
+
+do_execsql_test 3.3 {
+ REPLACE INTO t3(docid, content) VALUES (1, 'one two three four five six');
+ SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'six'
+} {X'0200000005000000'}
+
+do_execsql_test 3.4 {
+ UPDATE OR REPLACE t3 SET docid = 2 WHERE docid=1;
+ SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'six'
+} {X'0100000006000000'}
+
+do_execsql_test 3.5 {
+ UPDATE OR REPLACE t3 SET docid = 3 WHERE docid=2;
+ SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'six'
+} {X'0100000006000000'}
+
+do_execsql_test 3.6 {
+ REPLACE INTO t3(docid, content) VALUES (3, 'one two');
+ SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'one'
+} {X'0100000002000000'}
+
+do_execsql_test 3.7 {
+ REPLACE INTO t3(docid, content) VALUES (NULL, 'one two three four');
+ REPLACE INTO t3(docid, content) VALUES (NULL, 'one two three four five six');
+ SELECT docid FROM t3;
+} {3 4 5}
+
+do_execsql_test 3.8 {
+ UPDATE OR REPLACE t3 SET docid = 5, content='three four' WHERE docid = 4;
+ SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'one'
+} {X'0200000002000000'}
+
finish_test
diff --git a/test/fts3expr3.test b/test/fts3expr3.test
new file mode 100644
index 0000000..e3cc261
--- /dev/null
+++ b/test/fts3expr3.test
@@ -0,0 +1,210 @@
+# 2009 January 1
+#
+# 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 file implements regression tests for SQLite library. The
+# focus of this script is testing the part of the FTS3 expression
+# parser that rebalances large expressions.
+#
+# $Id: fts3expr2.test,v 1.2 2009/06/05 17:09:12 drh Exp $
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+source $testdir/malloc_common.tcl
+set ::testprefix fts3expr3
+
+# If SQLITE_ENABLE_FTS3 is defined, omit this file.
+ifcapable !fts3 {
+ finish_test
+ return
+}
+
+set sqlite_fts3_enable_parentheses 1
+
+proc strip_phrase_data {L} {
+ if {[lindex $L 0] eq "PHRASE"} {
+ return [list P [lrange $L 3 end]]
+ }
+ return [list \
+ [lindex $L 0] \
+ [strip_phrase_data [lindex $L 1]] \
+ [strip_phrase_data [lindex $L 2]] \
+ ]
+}
+proc test_fts3expr2 {expr} {
+ strip_phrase_data [
+ db one {SELECT fts3_exprtest_rebalance('simple', $expr, 'a', 'b', 'c')}
+ ]
+}
+
+proc balanced_exprtree_structure {nEntry} {
+ set L [list]
+ for {set i 1} {$i <= $nEntry} {incr i} {
+ lappend L xxx
+ }
+ while {[llength $L] > 1} {
+ set N [list]
+ if {[llength $L] % 2} {
+ foreach {a b} [lrange $L 0 end-1] { lappend N [list AND $a $b] }
+ lappend N [lindex $L end]
+ } else {
+ foreach {a b} $L { lappend N [list AND $a $b] }
+ }
+ set L $N
+ }
+ return [lindex $L 0]
+}
+
+proc balanced_and_tree {nEntry} {
+ set query [balanced_exprtree_structure $nEntry]
+ if {$query == "xxx"} {
+ return "P 1"
+ }
+ for {set i 1} {$i <= $nEntry} {incr i} {
+ regsub xxx $query "{P $i}" query
+ }
+ return $query
+}
+
+proc random_tree_structure {nEntry bParen op} {
+ set query xxx
+ for {set i 1} {$i < $nEntry} {incr i} {
+ set x1 [expr int(rand()*4.0)]
+ set x2 [expr int(rand()*2.0)]
+ if {$x1==0 && $bParen} {
+ set query "($query)"
+ }
+ if {$x2} {
+ set query "xxx $op $query"
+ } else {
+ set query "$query $op xxx"
+ }
+ }
+ return $query
+}
+
+proc random_and_query {nEntry {bParen 0}} {
+ set query [random_tree_structure $nEntry $bParen AND]
+ for {set i 1} {$i <= $nEntry} {incr i} {
+ regsub xxx $query $i query
+ }
+ return $query
+}
+
+proc random_or_query {nEntry} {
+ set query [random_tree_structure $nEntry 1 OR]
+ for {set i 1} {$i <= $nEntry} {incr i} {
+ regsub xxx $query $i query
+ }
+ return $query
+}
+
+proc random_andor_query {nEntry} {
+ set query [random_tree_structure $nEntry 1 AND]
+ for {set i 1} {$i <= $nEntry} {incr i} {
+ regsub xxx $query "([random_or_query $nEntry])" query
+ }
+ return $query
+}
+
+proc balanced_andor_tree {nEntry} {
+ set tree [balanced_exprtree_structure $nEntry]
+ set node "{[balanced_and_tree $nEntry]}"
+ regsub -all AND $node OR node
+ regsub -all xxx $tree $node tree
+ return $tree
+}
+
+# Test that queries like "1 AND 2 AND 3 AND 4..." are transformed to
+# balanced trees by FTS.
+#
+for {set i 1} {$i < 100} {incr i} {
+ do_test 1.$i {
+ test_fts3expr2 [random_and_query $i]
+ } [balanced_and_tree $i]
+}
+
+# Same again, except with parenthesis inserted at arbitrary points.
+#
+for {set i 1} {$i < 100} {incr i} {
+ do_test 2.$i {
+ test_fts3expr2 [random_and_query $i 1]
+ } [balanced_and_tree $i]
+}
+
+# Now attempt to balance two AND trees joined by an OR.
+#
+for {set i 1} {$i < 100} {incr i} {
+ do_test 3.$i {
+ test_fts3expr2 "[random_and_query $i 1] OR [random_and_query $i 1]"
+ } [list OR [balanced_and_tree $i] [balanced_and_tree $i]]
+}
+
+# Try trees of AND nodes with leaves that are themselves trees of OR nodes.
+#
+for {set i 2} {$i < 64} {incr i 4} {
+ do_test 3.$i {
+ test_fts3expr2 [random_andor_query $i]
+ } [balanced_andor_tree $i]
+}
+
+# These exceed the depth limit.
+#
+for {set i 65} {$i < 70} {incr i} {
+ do_test 3.$i {
+ list [catch {test_fts3expr2 [random_andor_query $i]} msg] $msg
+ } {1 {Error parsing expression}}
+}
+
+# This also exceeds the depth limit.
+#
+
+do_test 4.1.1 {
+ set q "1"
+ for {set i 2} {$i < 5000} {incr i} {
+ append q " AND $i"
+ }
+ list [catch {test_fts3expr2 $q} msg] $msg
+} {1 {Error parsing expression}}
+do_test 4.1.2 {
+ set q "1"
+ for {set i 2} {$i < 4000} {incr i} {
+ append q " AND $i"
+ }
+ catch {test_fts3expr2 $q}
+} {0}
+
+proc create_toggle_tree {nDepth} {
+ if {$nDepth == 0} { return xxx }
+ set nNew [expr $nDepth-1]
+ if {$nDepth % 2} {
+ return "([create_toggle_tree $nNew]) OR ([create_toggle_tree $nNew])"
+ }
+ return "([create_toggle_tree $nNew]) AND ([create_toggle_tree $nNew])"
+}
+
+do_test 4.2 {
+ list [catch {test_fts3expr2 [create_toggle_tree 17]} msg] $msg
+} {1 {Error parsing expression}}
+
+set query [random_andor_query 12]
+set result [balanced_andor_tree 12]
+do_faultsim_test fts3expr3-fault-1 -faults oom-* -body {
+ test_fts3expr2 $::query
+} -test {
+ faultsim_test_result [list 0 $::result]
+}
+
+set sqlite_fts3_enable_parentheses 0
+finish_test
+
+
+
+
diff --git a/test/fts3matchinfo.test b/test/fts3matchinfo.test
index 924db9c..3998c9a 100644
--- a/test/fts3matchinfo.test
+++ b/test/fts3matchinfo.test
@@ -407,5 +407,24 @@ do_catchsql_test 8.5.3.2 {
SELECT mit(matchinfo(t11, 'nxa')) FROM t11 WHERE t11 MATCH 'a*'
} {1 {database disk image is malformed}}
+#-------------------------------------------------------------------------
+do_execsql_test 8.1 {
+ CREATE VIRTUAL TABLE t12 USING fts4;
+ INSERT INTO t12 VALUES('a b c d');
+ SELECT mit(matchinfo(t12, 'x')) FROM t12 WHERE t12 MATCH 'a NEAR/1 d OR a';
+} {{0 0 0 0 0 0 1 1 1}}
+do_execsql_test 8.2 {
+ INSERT INTO t12 VALUES('a d c d');
+ SELECT mit(matchinfo(t12, 'x')) FROM t12 WHERE t12 MATCH 'a NEAR/1 d OR a';
+} {
+ {0 1 1 0 1 1 1 2 2} {1 1 1 1 1 1 1 2 2}
+}
+do_execsql_test 8.3 {
+ INSERT INTO t12 VALUES('a d d a');
+ SELECT mit(matchinfo(t12, 'x')) FROM t12 WHERE t12 MATCH 'a NEAR/1 d OR a';
+} {
+ {0 3 2 0 3 2 1 4 3} {1 3 2 1 3 2 1 4 3} {2 3 2 2 3 2 2 4 3}
+}
+
finish_test
diff --git a/test/fts3near.test b/test/fts3near.test
index 9c4409e..9276fa3 100644
--- a/test/fts3near.test
+++ b/test/fts3near.test
@@ -580,5 +580,19 @@ do_test fts3near-6.5 {
}
} {3}
+# Ticket 38b1ae018f.
+#
+do_execsql_test fts3near-7.1 {
+ CREATE VIRTUAL TABLE x USING fts4(y,z);
+ INSERT INTO x VALUES('aaa bbb ccc ddd', 'bbb ddd aaa ccc');
+ SELECT * FROM x where y MATCH 'bbb NEAR/6 aaa';
+} {{aaa bbb ccc ddd} {bbb ddd aaa ccc}}
+
+do_execsql_test fts3near-7.2 {
+ CREATE VIRTUAL TABLE t2 USING fts4(a, b);
+ INSERT INTO t2 VALUES('A B C', 'A D E');
+ SELECT * FROM t2 where t2 MATCH 'a:A NEAR E'
+} {}
+
finish_test
diff --git a/test/fts3tok1.test b/test/fts3tok1.test
new file mode 100644
index 0000000..98e55a0
--- /dev/null
+++ b/test/fts3tok1.test
@@ -0,0 +1,117 @@
+# 2013 April 22
+#
+# 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 file implements regression tests for SQLite library. The
+# focus of this script is testing the "fts3tokenize" virtual table
+# that is part of the FTS3 module.
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+ifcapable !fts3 { finish_test ; return }
+set ::testprefix fts3tok1
+
+#-------------------------------------------------------------------------
+# Simple test cases. Using the default (simple) tokenizer.
+#
+do_execsql_test 1.0 {
+ CREATE VIRTUAL TABLE t1 USING fts3tokenize(simple);
+ CREATE VIRTUAL TABLE t2 USING fts3tokenize();
+ CREATE VIRTUAL TABLE t3 USING fts3tokenize(simple, '', 'xyz ');
+}
+
+foreach {tn tbl} {1 t1 2 t2 3 t3} {
+ do_execsql_test 1.$tn.1 "SELECT * FROM $tbl WHERE input = 'one two three'" {
+ {one two three} one 0 3 0
+ {one two three} two 4 7 1
+ {one two three} three 8 13 2
+ }
+
+ do_execsql_test 1.$tn.2 "
+ SELECT token FROM $tbl WHERE input = 'OnE tWo tHrEe'
+ " {
+ one two three
+ }
+}
+
+do_execsql_test 1.4 {
+ SELECT token FROM t3 WHERE input = '1x2x3x'
+} {1 2 3}
+
+do_execsql_test 1.5 {
+ SELECT token FROM t1 WHERE input = '1x2x3x'
+} {1x2x3x}
+
+do_execsql_test 1.6 {
+ SELECT token FROM t3 WHERE input = '1''2x3x'
+} {1'2 3}
+
+do_execsql_test 1.7 {
+ SELECT token FROM t3 WHERE input = ''
+} {}
+
+do_execsql_test 1.8 {
+ SELECT token FROM t3 WHERE input = NULL
+} {}
+
+do_execsql_test 1.9 {
+ SELECT * FROM t3 WHERE input = 123
+} {123 123 0 3 0}
+
+do_execsql_test 1.10 {
+ SELECT * FROM t1 WHERE input = 'a b c' AND token = 'b';
+} {
+ {a b c} b 2 3 1
+}
+
+do_execsql_test 1.11 {
+ SELECT * FROM t1 WHERE token = 'b' AND input = 'a b c';
+} {
+ {a b c} b 2 3 1
+}
+
+do_execsql_test 1.12 {
+ SELECT * FROM t1 WHERE input < 'b' AND input = 'a b c';
+} {
+ {a b c} a 0 1 0
+ {a b c} b 2 3 1
+ {a b c} c 4 5 2
+}
+
+do_execsql_test 1.13.1 {
+ CREATE TABLE c1(x);
+ INSERT INTO c1(x) VALUES('a b c');
+ INSERT INTO c1(x) VALUES('d e f');
+}
+breakpoint
+do_execsql_test 1.13.2 {
+ SELECT * FROM c1, t1 WHERE input = x AND c1.rowid=t1.rowid;
+} {
+ {a b c} {a b c} a 0 1 0
+ {d e f} {d e f} e 2 3 1
+}
+
+
+#-------------------------------------------------------------------------
+# Error cases.
+#
+do_catchsql_test 2.0 {
+ CREATE VIRTUAL TABLE tX USING fts3tokenize(nosuchtokenizer);
+} {1 {unknown tokenizer: nosuchtokenizer}}
+
+do_catchsql_test 2.1 {
+ CREATE VIRTUAL TABLE t4 USING fts3tokenize;
+ SELECT * FROM t4;
+} {1 {SQL logic error or missing database}}
+
+
+finish_test
+
+
diff --git a/test/fts3tok_err.test b/test/fts3tok_err.test
new file mode 100644
index 0000000..aaa7272
--- /dev/null
+++ b/test/fts3tok_err.test
@@ -0,0 +1,49 @@
+# 2013 April 22
+#
+# 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 file implements regression tests for SQLite library. The
+# focus of this script is testing the "fts3tokenize" virtual table
+# that is part of the FTS3 module.
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+source $testdir/malloc_common.tcl
+ifcapable !fts3 { finish_test ; return }
+set ::testprefix fts3tok_err
+
+
+faultsim_save_and_close
+do_faultsim_test fts3tok_err-1 -faults oom* -prep {
+ faultsim_restore_and_reopen
+} -body {
+ execsql { CREATE VIRTUAL TABLE t1 USING fts3tokenize("simple"); }
+} -test {
+ faultsim_test_result {0 {}}
+}
+
+do_test fts3tok_err-2.prep {
+ faultsim_delete_and_reopen
+ execsql { CREATE VIRTUAL TABLE t1 USING fts3tokenize("simple"); }
+ faultsim_save_and_close
+} {}
+
+do_faultsim_test fts3tok_err-2 -faults oom* -prep {
+ faultsim_restore_and_reopen
+} -body {
+ execsql { SELECT token FROM t1 WHERE input = 'A galaxy far, far away' }
+} -test {
+ faultsim_test_result {0 {a galaxy far far away}}
+}
+
+
+finish_test
+
+
diff --git a/test/fts4content.test b/test/fts4content.test
index 59c4199..302f14e 100644
--- a/test/fts4content.test
+++ b/test/fts4content.test
@@ -46,6 +46,8 @@ ifcapable !fts3 {
# 8.* - Test that if the content=xxx and prefix options are used together,
# the 'rebuild' command still works.
#
+# 9.* - Test using content=xxx where xxx is a virtual table.
+#
do_execsql_test 1.1.1 {
CREATE TABLE t1(a, b, c);
@@ -522,4 +524,103 @@ do_execsql_test 8.4 { SELECT rowid FROM ft10 WHERE a MATCH 'ab*'; } {1 2 3}
do_execsql_test 8.5 { SELECT rowid FROM ft10 WHERE b MATCH 'abav*'; } {3}
do_execsql_test 8.6 { SELECT rowid FROM ft10 WHERE ft10 MATCH 'abas*'; } {1}
+#-------------------------------------------------------------------------
+# Test cases 9.*
+#
+reset_db
+register_echo_module [sqlite3_connection_pointer db]
+
+do_execsql_test 9.1 {
+ CREATE TABLE tbl1(a, b);
+ INSERT INTO tbl1 VALUES('a b', 'c d');
+ INSERT INTO tbl1 VALUES('e f', 'a b');
+ CREATE VIRTUAL TABLE e1 USING echo(tbl1);
+ CREATE VIRTUAL TABLE ft1 USING fts4(content=e1);
+ INSERT INTO ft1(ft1) VALUES('rebuild');
+}
+
+do_execsql_test 9.2 {
+ SELECT rowid, * FROM ft1 WHERE ft1 MATCH 'e'
+} {2 {e f} {a b}}
+
+do_execsql_test 9.3 {
+ SELECT rowid, * FROM ft1 WHERE ft1 MATCH 'a'
+} {1 {a b} {c d} 2 {e f} {a b}}
+
+do_execsql_test 9.4 {
+ DELETE FROM ft1 WHERE docid=1;
+}
+
+do_execsql_test 9.5 {
+ SELECT rowid, * FROM ft1 WHERE ft1 MATCH 'a'
+} {2 {e f} {a b}}
+
+do_execsql_test 9.6 {
+ INSERT INTO ft1(ft1) VALUES('rebuild');
+ SELECT rowid, * FROM ft1 WHERE ft1 MATCH 'a'
+} {1 {a b} {c d} 2 {e f} {a b}}
+
+
+#-------------------------------------------------------------------------
+# Test cases 10.*
+#
+reset_db
+register_fs_module [sqlite3_connection_pointer db]
+
+proc write_file {path text} {
+ set fd [open $path w]
+ puts -nonewline $fd $text
+ close $fd
+}
+
+write_file t1.txt {a b c d e f g h i j k l m n o p q r s t u v w x y z}
+write_file t2.txt {a b c d e f g h i j k l m a b c d e f g h i j k l m}
+write_file t3.txt {n o p q r s t u v w x y z n o p q r s t u v w x y z}
+
+do_execsql_test 10.1 {
+ CREATE TABLE idx(id INTEGER PRIMARY KEY, path TEXT);
+ INSERT INTO idx VALUES (1, 't1.txt');
+ INSERT INTO idx VALUES (2, 't2.txt');
+ INSERT INTO idx VALUES (3, 't3.txt');
+
+ CREATE VIRTUAL TABLE vt USING fs(idx);
+ SELECT * FROM vt;
+} {
+ 1 {a b c d e f g h i j k l m n o p q r s t u v w x y z}
+ 2 {a b c d e f g h i j k l m a b c d e f g h i j k l m}
+ 3 {n o p q r s t u v w x y z n o p q r s t u v w x y z}
+}
+
+do_execsql_test 10.2 {
+ SELECT * FROM vt WHERE rowid = 2;
+} {
+ 2 {a b c d e f g h i j k l m a b c d e f g h i j k l m}
+}
+
+do_execsql_test 10.3 {
+ CREATE VIRTUAL TABLE ft USING fts4(content=vt);
+ INSERT INTO ft(ft) VALUES('rebuild');
+}
+
+do_execsql_test 10.4 {
+ SELECT snippet(ft, '[', ']', '...', -1, 5) FROM ft WHERE ft MATCH 'e'
+} {
+ {...c d [e] f g...} {...c d [e] f g...}
+}
+
+do_execsql_test 10.5 {
+ SELECT snippet(ft, '[', ']', '...', -1, 5) FROM ft WHERE ft MATCH 't'
+} {
+ {...r s [t] u v...} {...r s [t] u v...}
+}
+
+do_execsql_test 10.6 { DELETE FROM ft WHERE docid=2 }
+
+do_execsql_test 10.7 {
+ SELECT snippet(ft, '[', ']', '...', -1, 5) FROM ft WHERE ft MATCH 'e'
+} {
+ {...c d [e] f g...}
+}
+
finish_test
+
diff --git a/test/fts4unicode.test b/test/fts4unicode.test
index 0ac60a6..8bd83f6 100644
--- a/test/fts4unicode.test
+++ b/test/fts4unicode.test
@@ -44,12 +44,12 @@ proc do_unicode_token_test3 {tn args} {
}
do_unicode_token_test 1.0 {a B c D} {0 a a 1 b B 2 c c 3 d D}
-do_unicode_token_test 1.1 {Ä Ö Ü} {0 ä Ä 1 ö Ö 2 ü Ü}
-do_unicode_token_test 1.2 {xÄx xÖx xÜx} {0 xäx xÄx 1 xöx xÖx 2 xüx xÜx}
+do_unicode_token_test 1.1 {Ä Ö Ü} {0 ä Ä 1 ö Ö 2 ü Ü}
+do_unicode_token_test 1.2 {xÄx xÖx xÜx} {0 xäx xÄx 1 xöx xÖx 2 xüx xÜx}
# 0x00DF is a small "sharp s". 0x1E9E is a capital sharp s.
do_unicode_token_test 1.3 "\uDF" "0 \uDF \uDF"
-do_unicode_token_test 1.4 "\u1E9E" "0 ß \u1E9E"
+do_unicode_token_test 1.4 "\u1E9E" "0 ß \u1E9E"
do_unicode_token_test 1.5 "\u1E9E" "0 \uDF \u1E9E"
do_unicode_token_test 1.6 "The quick brown fox" {
@@ -60,12 +60,15 @@ do_unicode_token_test 1.7 "The\u00bfquick\u224ebrown\u2263fox" {
}
do_unicode_token_test2 1.8 {a B c D} {0 a a 1 b B 2 c c 3 d D}
-do_unicode_token_test2 1.9 {Ä Ö Ü} {0 a Ä 1 o Ö 2 u Ü}
-do_unicode_token_test2 1.10 {xÄx xÖx xÜx} {0 xax xÄx 1 xox xÖx 2 xux xÜx}
+do_unicode_token_test2 1.9 {Ä Ö Ü} {0 a Ä 1 o Ö 2 u Ü}
+do_unicode_token_test2 1.10 {xÄx xÖx xÜx} {0 xax xÄx 1 xox xÖx 2 xux xÜx}
# Check that diacritics are removed if remove_diacritics=1 is specified.
# And that they do not break tokens.
-do_unicode_token_test2 1.10 "xx\u0301xx" "0 xxxx xx\u301xx"
+do_unicode_token_test2 1.11 "xx\u0301xx" "0 xxxx xx\u301xx"
+
+# Title-case mappings work
+do_unicode_token_test 1.12 "\u01c5" "0 \u01c6 \u01c5"
#-------------------------------------------------------------------------
#
@@ -383,5 +386,3 @@ foreach T $tokenizers {
finish_test
-
-
diff --git a/test/full.test b/test/full.test
new file mode 100644
index 0000000..a8fe371
--- /dev/null
+++ b/test/full.test
@@ -0,0 +1,20 @@
+# 2012 September 12
+#
+# 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 file runs the "full" test suite. It is a peer of the quick.test
+# and all.test scripts.
+#
+
+set testdir [file dirname $argv0]
+source $testdir/permutations.test
+
+run_test_suite full
+
+finish_test
diff --git a/test/func.test b/test/func.test
index e44c44b..4ab7688 100644
--- a/test/func.test
+++ b/test/func.test
@@ -1273,11 +1273,13 @@ do_test func-29.3 {
sqlite3_db_status db CACHE_MISS 1
db eval {SELECT typeof(+x) FROM t29 ORDER BY id}
} {integer null real blob text}
-do_test func-29.4 {
- set x [lindex [sqlite3_db_status db CACHE_MISS 1] 1]
- if {$x>100} {set x many}
- set x
-} {many}
+if {[permutation] != "mmap"} {
+ do_test func-29.4 {
+ set x [lindex [sqlite3_db_status db CACHE_MISS 1] 1]
+ if {$x>100} {set x many}
+ set x
+ } {many}
+}
do_test func-29.5 {
db close
sqlite3 db test.db
@@ -1289,6 +1291,21 @@ do_test func-29.6 {
if {$x<5} {set x 1}
set x
} {1}
-
+
+do_execsql_test func-30.1 {SELECT unicode('$');} 36
+do_execsql_test func-30.2 [subst {SELECT unicode('\u00A2');}] 162
+do_execsql_test func-30.3 [subst {SELECT unicode('\u20AC');}] 8364
+do_execsql_test func-30.4 {SELECT char(36,162,8364);} [subst {$\u00A2\u20AC}]
+
+for {set i 1} {$i<0xd800} {incr i 13} {
+ do_execsql_test func-30.5.$i {SELECT unicode(char($i))} $i
+}
+for {set i 57344} {$i<=0xfffd} {incr i 17} {
+ if {$i==0xfeff} continue
+ do_execsql_test func-30.5.$i {SELECT unicode(char($i))} $i
+}
+for {set i 65536} {$i<=0x10ffff} {incr i 139} {
+ do_execsql_test func-30.5.$i {SELECT unicode(char($i))} $i
+}
finish_test
diff --git a/test/fuzzer1.test b/test/fuzzer1.test
index dc8b445..473d0e1 100644
--- a/test/fuzzer1.test
+++ b/test/fuzzer1.test
@@ -24,12 +24,7 @@ ifcapable !vtab {
set ::testprefix fuzzer1
-# Test of test code. Only here to make the coverage metric better.
-do_test 0.1 {
- list [catch { register_fuzzer_module a b c } msg] $msg
-} {1 {wrong # args: should be "register_fuzzer_module DB"}}
-
-register_fuzzer_module db
+load_static_extension db fuzzer
# Check configuration errors.
#
@@ -1864,5 +1859,3 @@ do_execsql_test 10.3 {
} {1 21 41 61 81}
finish_test
-
-
diff --git a/test/fuzzerfault.test b/test/fuzzerfault.test
index 067da7f..6449612 100644
--- a/test/fuzzerfault.test
+++ b/test/fuzzerfault.test
@@ -17,7 +17,7 @@ source $testdir/tester.tcl
ifcapable !vtab { finish_test ; return }
set ::testprefix fuzzerfault
-register_fuzzer_module db
+load_static_extension db fuzzer
do_test 1-pre1 {
execsql {
@@ -30,7 +30,7 @@ do_test 1-pre1 {
} {}
do_faultsim_test 1 -prep {
faultsim_restore_and_reopen
- register_fuzzer_module db
+ load_static_extension db fuzzer
} -body {
execsql {
CREATE VIRTUAL TABLE x1 USING fuzzer(x1_rules);
@@ -43,7 +43,7 @@ do_faultsim_test 1 -prep {
do_test 2-pre1 {
faultsim_delete_and_reopen
- register_fuzzer_module db
+ load_static_extension db fuzzer
execsql {
CREATE TABLE x2_rules(ruleset, cFrom, cTo, cost);
INSERT INTO x2_rules VALUES(0, 'a', 'x', 1);
@@ -56,7 +56,7 @@ do_test 2-pre1 {
do_faultsim_test 2 -prep {
faultsim_restore_and_reopen
- register_fuzzer_module db
+ load_static_extension db fuzzer
} -body {
execsql {
SELECT count(*) FROM x2 WHERE word MATCH 'abc';
@@ -78,7 +78,7 @@ do_test 3-pre1 {
do_faultsim_test 3 -prep {
faultsim_restore_and_reopen
- register_fuzzer_module db
+ load_static_extension db fuzzer
} -body {
execsql {
CREATE VIRTUAL TABLE x1 USING fuzzer(x1_rules);
diff --git a/test/hook.test b/test/hook.test
index a195275..6346cc7 100644
--- a/test/hook.test
+++ b/test/hook.test
@@ -74,6 +74,7 @@ do_test hook-3.6 {
INSERT INTO t2 VALUES(6,7);
}
} {1 {constraint failed}}
+verify_ex_errcode hook-3.6b SQLITE_CONSTRAINT_COMMITHOOK
do_test hook-3.7 {
set ::commit_cnt
} {1 2 2 3 3 4 4 5 5 6 6 7}
diff --git a/test/in5.test b/test/in5.test
new file mode 100644
index 0000000..8a43b8d
--- /dev/null
+++ b/test/in5.test
@@ -0,0 +1,138 @@
+# 2012 September 18
+#
+# 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.
+#
+#***********************************************************************
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+
+do_test in5-1.1 {
+ execsql {
+ CREATE TABLE t1x(x INTEGER PRIMARY KEY);
+ INSERT INTO t1x VALUES(1),(3),(5),(7),(9);
+ CREATE TABLE t1y(y INTEGER UNIQUE);
+ INSERT INTO t1y VALUES(2),(4),(6),(8);
+ CREATE TABLE t1z(z TEXT UNIQUE);
+ INSERT INTO t1z VALUES('a'),('c'),('e'),('g');
+ CREATE TABLE t2(a INTEGER, b INTEGER, c TEXT, d TEXT);
+ INSERT INTO t2 VALUES(1,2,'a','12a'),(1,2,'b','12b'),
+ (2,3,'g','23g'),(3,5,'c','35c'),
+ (4,6,'h','46h'),(5,6,'e','56e');
+ CREATE TABLE t3x AS SELECT x FROM t1x;
+ CREATE TABLE t3y AS SELECT y FROM t1y;
+ CREATE TABLE t3z AS SELECT z FROM t1z;
+ SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z ORDER BY c;
+ }
+} {12a 56e}
+do_test in5-1.2 {
+ execsql {
+ SELECT d FROM t2 WHERE a IN t1y AND b IN t1x AND c IN t1z ORDER BY d;
+ }
+} {23g}
+do_test in5-1.3 {
+ execsql {
+ SELECT d FROM t2 WHERE a IN t3x AND b IN t3y AND c IN t3z ORDER BY d;
+ }
+} {12a 56e}
+
+
+do_test in5-2.1 {
+ execsql {
+ CREATE INDEX t2abc ON t2(a,b,c);
+ SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z ORDER BY d;
+ }
+} {12a 56e}
+do_test in5-2.2 {
+ execsql {
+ SELECT d FROM t2 WHERE a IN t1y AND b IN t1x AND c IN t1z ORDER BY d;
+ }
+} {23g}
+do_test in5-2.3 {
+ regexp {OpenEphemeral} [db eval {
+ EXPLAIN SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z
+ }]
+} {0}
+do_test in5-2.4 {
+ execsql {
+ SELECT d FROM t2 WHERE a IN t3x AND b IN t3y AND c IN t3z ORDER BY d;
+ }
+} {12a 56e}
+do_test in5-2.5.1 {
+ regexp {OpenEphemeral} [db eval {
+ EXPLAIN SELECT d FROM t2 WHERE a IN t3x AND b IN t1y AND c IN t1z
+ }]
+} {1}
+do_test in5-2.5.2 {
+ regexp {OpenEphemeral} [db eval {
+ EXPLAIN SELECT d FROM t2 WHERE a IN t1x AND b IN t3y AND c IN t1z
+ }]
+} {1}
+do_test in5-2.5.3 {
+ regexp {OpenEphemeral} [db eval {
+ EXPLAIN SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t3z
+ }]
+} {1}
+
+do_test in5-3.1 {
+ execsql {
+ DROP INDEX t2abc;
+ CREATE INDEX t2ab ON t2(a,b);
+ SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z ORDER BY d;
+ }
+} {12a 56e}
+do_test in5-3.2 {
+ execsql {
+ SELECT d FROM t2 WHERE a IN t1y AND b IN t1x AND c IN t1z ORDER BY d;
+ }
+} {23g}
+do_test in5-3.3 {
+ regexp {OpenEphemeral} [db eval {
+ EXPLAIN SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z
+ }]
+} {0}
+
+do_test in5-4.1 {
+ execsql {
+ DROP INDEX t2ab;
+ CREATE INDEX t2abcd ON t2(a,b,c,d);
+ SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z ORDER BY d;
+ }
+} {12a 56e}
+do_test in5-4.2 {
+ execsql {
+ SELECT d FROM t2 WHERE a IN t1y AND b IN t1x AND c IN t1z ORDER BY d;
+ }
+} {23g}
+do_test in5-4.3 {
+ regexp {OpenEphemeral} [db eval {
+ EXPLAIN SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z
+ }]
+} {0}
+
+
+do_test in5-5.1 {
+ execsql {
+ DROP INDEX t2abcd;
+ CREATE INDEX t2cbad ON t2(c,b,a,d);
+ SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z ORDER BY d;
+ }
+} {12a 56e}
+do_test in5-5.2 {
+ execsql {
+ SELECT d FROM t2 WHERE a IN t1y AND b IN t1x AND c IN t1z ORDER BY d;
+ }
+} {23g}
+do_test in5-5.3 {
+ regexp {OpenEphemeral} [db eval {
+ EXPLAIN SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z
+ }]
+} {0}
+
+finish_test
diff --git a/test/incrblob.test b/test/incrblob.test
index 1880128..4277e5c 100644
--- a/test/incrblob.test
+++ b/test/incrblob.test
@@ -123,6 +123,7 @@ foreach AutoVacuumMode [list 0 1] {
forcedelete test.db test.db-journal
sqlite3 db test.db
+ execsql "PRAGMA mmap_size = 0"
execsql "PRAGMA auto_vacuum = $AutoVacuumMode"
do_test incrblob-2.$AutoVacuumMode.1 {
@@ -149,6 +150,7 @@ foreach AutoVacuumMode [list 0 1] {
# Open and close the db to make sure the page cache is empty.
db close
sqlite3 db test.db
+ execsql "PRAGMA mmap_size = 0"
# Read the last 20 bytes of the blob via a blob handle.
set ::blob [db incrblob blobs v 1]
@@ -171,6 +173,7 @@ foreach AutoVacuumMode [list 0 1] {
# Open and close the db to make sure the page cache is empty.
db close
sqlite3 db test.db
+ execsql "PRAGMA mmap_size = 0"
# Write the second-to-last 20 bytes of the blob via a blob handle.
#
@@ -200,6 +203,7 @@ foreach AutoVacuumMode [list 0 1] {
# Open and close the db to make sure the page cache is empty.
db close
sqlite3 db test.db
+ execsql { PRAGMA mmap_size = 0 }
execsql { SELECT i FROM blobs }
} {45}
@@ -437,7 +441,7 @@ if {[permutation] != "memsubsys1"} {
} {}
do_test incrblob-6.2 {
execsql {
- SELECT rowid FROM blobs
+ SELECT rowid FROM blobs ORDER BY rowid
}
} {1 2 3}
do_test incrblob-6.3 {
@@ -505,7 +509,7 @@ if {[permutation] != "memsubsys1"} {
sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
#-----------------------------------------------------------------------
-# The following tests verify the behaviour of the incremental IO
+# The following tests verify the behavior of the incremental IO
# APIs in the following cases:
#
# 7.1 A row that containing an open blob is modified.
@@ -516,7 +520,7 @@ sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
# 7.3 An INCREMENTAL VACUUM moves an overflow page that is part
# of an open blob.
#
-# In the first case above, correct behaviour is for all subsequent
+# In the first case above, correct behavior is for all subsequent
# read/write operations on the blob-handle to return SQLITE_ABORT.
# More accurately, blob-handles are invalidated whenever the table
# they belong to is written to.
diff --git a/test/incrvacuum3.test b/test/incrvacuum3.test
new file mode 100644
index 0000000..f01dc10
--- /dev/null
+++ b/test/incrvacuum3.test
@@ -0,0 +1,154 @@
+# 2013 Feb 25
+#
+# 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 file implements regression tests for the SQLite library, focusing
+# on the incremental vacuum feature.
+#
+# The tests in this file were added at the same time as optimizations
+# were made to:
+#
+# * Truncate the database after a rollback mode commit, and
+#
+# * Avoid moving pages to locations from which they may need to be moved
+# a second time if an incremental-vacuum proccess is allowed to vacuum
+# the entire database.
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set testprefix incrvacuum3
+
+# If this build of the library does not support auto-vacuum, omit this
+# whole file.
+ifcapable {!autovacuum || !pragma} {
+ finish_test
+ return
+}
+
+proc check_on_disk {} {
+
+ # Copy the wal and journal files for database "test.db" to "test2.db".
+ forcedelete test2.db test2.db-journal test2.db-wal
+ if {[file exists test.db-journal]} {
+ forcecopy test.db-journal test2.db-journal
+ }
+ if {[file exists test.db-wal]} {
+ forcecopy test.db-wal test2.db-wal
+ }
+
+ # Now copy the database file itself. Do this using open/read/puts
+ # instead of the [file copy] command in order to avoid attempting
+ # to read the 512 bytes begining at offset $sqlite_pending_byte.
+ #
+ set sz [file size test.db]
+ set fd [open test.db]
+ set fd2 [open test2.db w]
+ fconfigure $fd -encoding binary -translation binary
+ fconfigure $fd2 -encoding binary -translation binary
+ if {$sz>$::sqlite_pending_byte} {
+ puts -nonewline $fd2 [read $fd $::sqlite_pending_byte]
+ seek $fd [expr $::sqlite_pending_byte+512]
+ seek $fd2 [expr $::sqlite_pending_byte+512]
+ }
+ puts -nonewline $fd2 [read $fd]
+ close $fd2
+ close $fd
+
+ # Open "test2.db" and check it is Ok.
+ sqlite3 dbcheck test2.db
+ set ret [dbcheck eval { PRAGMA integrity_check }]
+ dbcheck close
+ set ret
+}
+
+# Run these tests once in rollback journal mode, and once in wal mode.
+#
+foreach {T jrnl_mode} {
+ 1 delete
+ 2 wal
+} {
+ catch { db close }
+ forcedelete test.db test.db-journal test.db-wal
+ sqlite3 db test.db
+ db eval {
+ PRAGMA cache_size = 5;
+ PRAGMA page_size = 1024;
+ PRAGMA auto_vacuum = 2;
+ }
+ db eval "PRAGMA journal_mode = $jrnl_mode"
+
+ foreach {tn sql} {
+ 1 {
+ CREATE TABLE t1(x UNIQUE);
+ INSERT INTO t1 VALUES(randomblob(400));
+ INSERT INTO t1 VALUES(randomblob(400));
+ INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 4
+ INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 8
+ INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 16
+ INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 32
+ INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 64
+ INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128
+ INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 256
+ }
+
+ 2 {
+ DELETE FROM t1 WHERE rowid%8;
+ }
+
+ 3 {
+ BEGIN;
+ PRAGMA incremental_vacuum = 100;
+ INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 64
+ INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128
+ INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 256
+ ROLLBACK;
+ }
+
+ 4 {
+ BEGIN;
+ SAVEPOINT one;
+ PRAGMA incremental_vacuum = 100;
+ SAVEPOINT two;
+ INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 64
+ INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128
+ INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 256
+ }
+
+ 5 { ROLLBACK to two }
+
+ 6 { ROLLBACK to one }
+
+ 7 {
+ INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 64
+ PRAGMA incremental_vacuum = 1000;
+ INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128
+ INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 256
+ ROLLBACK;
+ }
+
+ 8 {
+ BEGIN;
+ INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 64
+ PRAGMA incremental_vacuum = 1000;
+ INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128
+ COMMIT;
+ }
+ } {
+ do_execsql_test $T.1.$tn.1 $sql
+ do_execsql_test $T.1.$tn.2 {PRAGMA integrity_check} ok
+ do_test $T.1.$tn.3 { check_on_disk } ok
+ }
+
+ do_execsql_test $T.1.x.1 { PRAGMA freelist_count } 0
+ do_execsql_test $T.1.x.2 { SELECT count(*) FROM t1 } 128
+}
+
+finish_test
+
diff --git a/test/incrvacuum_ioerr.test b/test/incrvacuum_ioerr.test
index 946925d..50f7fa2 100644
--- a/test/incrvacuum_ioerr.test
+++ b/test/incrvacuum_ioerr.test
@@ -139,8 +139,9 @@ ifcapable shared_cache {
# Figure out how big the database is and how many free pages it
# has before running incremental-vacuum.
#
- set nPage [expr {[file size test.db]/1024}]
set nFree [execsql {pragma freelist_count} db1]
+ set nPage [execsql {pragma page_count} db1]
+ puts "nFree=$nFree nPage=$nPage"
# Now run incremental-vacuum to vacuum 5 pages from the db file.
# The iTest'th I/O call is set to fail.
@@ -158,11 +159,11 @@ ifcapable shared_cache {
set ::sqlite_io_error_hardhit 0
set nFree2 [execsql {pragma freelist_count} db1]
- set nPage2 [expr {[file size test.db]/1024}]
+ set nPage2 [execsql {pragma page_count} db1]
do_test incrvacuum-ioerr-4.$iTest.2 {
set shrink [expr {$nPage-$nPage2}]
- expr {$shrink==0 || $shrink==5}
+ expr {$shrink==0 || $shrink==5 || ($nFree<5 && $shrink==$nFree)}
} {1}
do_test incrvacuum-ioerr-4.$iTest.3 {
diff --git a/test/index5.test b/test/index5.test
index c8e94b3..7895391 100644
--- a/test/index5.test
+++ b/test/index5.test
@@ -36,11 +36,10 @@ db close
testvfs tvfs
tvfs filter xWrite
tvfs script write_cb
-proc write_cb {xCall file handle iOfst} {
+proc write_cb {xCall file handle iOfst args} {
if {[file tail $file]=="test.db"} {
lappend ::write_list [expr $iOfst/1024]
}
- puts "$xCall $file $args"
}
do_test 1.2 {
@@ -65,11 +64,12 @@ do_test 1.3 {
}
set iPrev $iNext
}
+ puts -nonewline \
+ " (forward=$nForward, back=$nBackward, noncontiguous=$nNoncont)"
- expr {$nForward > $nBackward}
+ expr {$nForward > 2*($nBackward + $nNoncont)}
} {1}
db close
tvfs delete
finish_test
-
diff --git a/test/instr.test b/test/instr.test
new file mode 100644
index 0000000..b328cd1
--- /dev/null
+++ b/test/instr.test
@@ -0,0 +1,210 @@
+# 2012 October 24
+#
+# 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 file implements regression tests for SQLite library. The
+# focus of this file is testing the built-in INSTR() functions.
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+
+# Create a table to work with.
+#
+do_test instr-1.1 {
+ db eval {SELECT instr('abcdefg','a');}
+} {1}
+do_test instr-1.2 {
+ db eval {SELECT instr('abcdefg','b');}
+} {2}
+do_test instr-1.3 {
+ db eval {SELECT instr('abcdefg','c');}
+} {3}
+do_test instr-1.4 {
+ db eval {SELECT instr('abcdefg','d');}
+} {4}
+do_test instr-1.5 {
+ db eval {SELECT instr('abcdefg','e');}
+} {5}
+do_test instr-1.6 {
+ db eval {SELECT instr('abcdefg','f');}
+} {6}
+do_test instr-1.7 {
+ db eval {SELECT instr('abcdefg','g');}
+} {7}
+do_test instr-1.8 {
+ db eval {SELECT instr('abcdefg','h');}
+} {0}
+do_test instr-1.9 {
+ db eval {SELECT instr('abcdefg','abcdefg');}
+} {1}
+do_test instr-1.10 {
+ db eval {SELECT instr('abcdefg','abcdefgh');}
+} {0}
+do_test instr-1.11 {
+ db eval {SELECT instr('abcdefg','bcdefg');}
+} {2}
+do_test instr-1.12 {
+ db eval {SELECT instr('abcdefg','bcdefgh');}
+} {0}
+do_test instr-1.13 {
+ db eval {SELECT instr('abcdefg','cdefg');}
+} {3}
+do_test instr-1.14 {
+ db eval {SELECT instr('abcdefg','cdefgh');}
+} {0}
+do_test instr-1.15 {
+ db eval {SELECT instr('abcdefg','defg');}
+} {4}
+do_test instr-1.16 {
+ db eval {SELECT instr('abcdefg','defgh');}
+} {0}
+do_test instr-1.17 {
+ db eval {SELECT instr('abcdefg','efg');}
+} {5}
+do_test instr-1.18 {
+ db eval {SELECT instr('abcdefg','efgh');}
+} {0}
+do_test instr-1.19 {
+ db eval {SELECT instr('abcdefg','fg');}
+} {6}
+do_test instr-1.20 {
+ db eval {SELECT instr('abcdefg','fgh');}
+} {0}
+do_test instr-1.21 {
+ db eval {SELECT coalesce(instr('abcdefg',NULL),'nil');}
+} {nil}
+do_test instr-1.22 {
+ db eval {SELECT coalesce(instr(NULL,'x'),'nil');}
+} {nil}
+do_test instr-1.23 {
+ db eval {SELECT instr(12345,34);}
+} {3}
+do_test instr-1.24 {
+ db eval {SELECT instr(123456.78,34);}
+} {3}
+do_test instr-1.25 {
+ db eval {SELECT instr(123456.78,x'3334');}
+} {3}
+do_test instr-1.26 {
+ db eval {SELECT instr('äbcdefg','efg');}
+} {5}
+do_test instr-1.27 {
+ db eval {SELECT instr('€xyzzy','xyz');}
+} {2}
+do_test instr-1.28 {
+ db eval {SELECT instr('abc€xyzzy','xyz');}
+} {5}
+do_test instr-1.29 {
+ db eval {SELECT instr('abc€xyzzy','€xyz');}
+} {4}
+do_test instr-1.30 {
+ db eval {SELECT instr('abc€xyzzy','c€xyz');}
+} {3}
+do_test instr-1.31 {
+ db eval {SELECT instr(x'0102030405',x'01');}
+} {1}
+do_test instr-1.32 {
+ db eval {SELECT instr(x'0102030405',x'02');}
+} {2}
+do_test instr-1.33 {
+ db eval {SELECT instr(x'0102030405',x'03');}
+} {3}
+do_test instr-1.34 {
+ db eval {SELECT instr(x'0102030405',x'04');}
+} {4}
+do_test instr-1.35 {
+ db eval {SELECT instr(x'0102030405',x'05');}
+} {5}
+do_test instr-1.36 {
+ db eval {SELECT instr(x'0102030405',x'06');}
+} {0}
+do_test instr-1.37 {
+ db eval {SELECT instr(x'0102030405',x'0102030405');}
+} {1}
+do_test instr-1.38 {
+ db eval {SELECT instr(x'0102030405',x'02030405');}
+} {2}
+do_test instr-1.39 {
+ db eval {SELECT instr(x'0102030405',x'030405');}
+} {3}
+do_test instr-1.40 {
+ db eval {SELECT instr(x'0102030405',x'0405');}
+} {4}
+do_test instr-1.41 {
+ db eval {SELECT instr(x'0102030405',x'0506');}
+} {0}
+do_test instr-1.42 {
+ db eval {SELECT instr(x'0102030405',x'');}
+} {1}
+do_test instr-1.43 {
+ db eval {SELECT instr(x'',x'');}
+} {1}
+do_test instr-1.44 {
+ db eval {SELECT instr('','');}
+} {1}
+do_test instr-1.45 {
+ db eval {SELECT instr('abcdefg','');}
+} {1}
+unset -nocomplain longstr
+set longstr abcdefghijklmonpqrstuvwxyz
+append longstr $longstr
+append longstr $longstr
+append longstr $longstr
+append longstr $longstr
+append longstr $longstr
+append longstr $longstr
+append longstr $longstr
+append longstr $longstr
+append longstr $longstr
+append longstr $longstr
+append longstr $longstr
+append longstr $longstr
+# puts [string length $longstr]
+append longstr Xabcde
+do_test instr-1.46 {
+ db eval {SELECT instr($longstr,'X');}
+} {106497}
+do_test instr-1.47 {
+ db eval {SELECT instr($longstr,'Y');}
+} {0}
+do_test instr-1.48 {
+ db eval {SELECT instr($longstr,'Xa');}
+} {106497}
+do_test instr-1.49 {
+ db eval {SELECT instr($longstr,'zXa');}
+} {106496}
+set longstr [string map {a ä} $longstr]
+do_test instr-1.50 {
+ db eval {SELECT instr($longstr,'X');}
+} {106497}
+do_test instr-1.51 {
+ db eval {SELECT instr($longstr,'Y');}
+} {0}
+do_test instr-1.52 {
+ db eval {SELECT instr($longstr,'Xä');}
+} {106497}
+do_test instr-1.53 {
+ db eval {SELECT instr($longstr,'zXä');}
+} {106496}
+do_test instr-1.54 {
+ db eval {SELECT instr(x'78c3a4e282ac79','x');}
+} {1}
+do_test instr-1.55 {
+ db eval {SELECT instr(x'78c3a4e282ac79','y');}
+} {4}
+do_test instr-1.56 {
+ db eval {SELECT instr(x'78c3a4e282ac79',x'79');}
+} {7}
+do_test instr-1.57 {
+ db eval {SELECT instr('xä€y',x'79');}
+} {4}
+
+
+finish_test
diff --git a/test/interrupt.test b/test/interrupt.test
index b311cbb..92ab4c3 100644
--- a/test/interrupt.test
+++ b/test/interrupt.test
@@ -166,6 +166,8 @@ for {set i 1} {$i<$max_count-5} {incr i 1} {
} {1 interrupted}
}
+if {0} { # This doesn't work anymore since the collation factor is
+ # no longer called during schema parsing.
# Interrupt during parsing
#
do_test interrupt-5.1 {
@@ -179,5 +181,5 @@ do_test interrupt-5.1 {
CREATE INDEX fake ON fake1(a COLLATE fake_collation, b, c DESC);
}
} {1 interrupt}
-
+}
finish_test
diff --git a/test/intpkey.test b/test/intpkey.test
index 05b6cdf..db39421 100644
--- a/test/intpkey.test
+++ b/test/intpkey.test
@@ -376,7 +376,7 @@ do_test intpkey-5.1 {
} {0 zero entry 0}
do_test intpkey-5.2 {
execsql {
- SELECT rowid, a FROM t1
+ SELECT rowid, a FROM t1 ORDER BY rowid
}
} {-4 -4 0 0 5 5 6 6 11 11}
diff --git a/test/io.test b/test/io.test
index 9363b0c..11f9cc8 100644
--- a/test/io.test
+++ b/test/io.test
@@ -16,6 +16,7 @@
set testdir [file dirname $argv0]
source $testdir/tester.tcl
+set ::testprefix io
db close
sqlite3_simulate_device
@@ -38,6 +39,10 @@ sqlite3 db test.db -vfs devsym
#
# io-5.* - Test that the default page size is selected and used
# correctly.
+#
+# io-6.* - Test that the pager-cache is not being flushed unnecessarily
+# after a transaction that uses the special atomic-write path
+# is committed.
#
set ::nWrite 0
@@ -207,7 +212,7 @@ do_test io-2.5.3 {
# Changed 2010-03-27: The size of the database is now stored in
# bytes 28..31 and so when a page is added to the database, page 1
# is immediately modified and the journal file immediately comes into
-# existance. To fix this test, the BEGIN is changed into a a
+# existence. To fix this test, the BEGIN is changed into a a
# BEGIN IMMEDIATE and the INSERT is omitted.
#
do_test io-2.6.1 {
@@ -565,5 +570,75 @@ foreach {char sectorsize pgsize} {
} $pgsize
}
+#----------------------------------------------------------------------
+#
+do_test io-6.1 {
+ db close
+ sqlite3_simulate_device -char atomic
+ forcedelete test.db
+ sqlite3 db test.db -vfs devsym
+ execsql {
+ PRAGMA mmap_size = 0;
+ PRAGMA page_size = 1024;
+ PRAGMA cache_size = 2000;
+ CREATE TABLE t1(x);
+ CREATE TABLE t2(x);
+ CREATE TABLE t3(x);
+ CREATE INDEX i3 ON t3(x);
+ INSERT INTO t3 VALUES(randomblob(100));
+ INSERT INTO t3 SELECT randomblob(100) FROM t3;
+ INSERT INTO t3 SELECT randomblob(100) FROM t3;
+ INSERT INTO t3 SELECT randomblob(100) FROM t3;
+ INSERT INTO t3 SELECT randomblob(100) FROM t3;
+ INSERT INTO t3 SELECT randomblob(100) FROM t3;
+ INSERT INTO t3 SELECT randomblob(100) FROM t3;
+ INSERT INTO t3 SELECT randomblob(100) FROM t3;
+ INSERT INTO t3 SELECT randomblob(100) FROM t3;
+ INSERT INTO t3 SELECT randomblob(100) FROM t3;
+ INSERT INTO t3 SELECT randomblob(100) FROM t3;
+ INSERT INTO t3 SELECT randomblob(100) FROM t3;
+ }
+
+ db_save_and_close
+} {}
+
+foreach {tn sql} {
+ 1 { BEGIN;
+ INSERT INTO t1 VALUES('123');
+ INSERT INTO t2 VALUES('456');
+ COMMIT;
+ }
+ 2 { BEGIN;
+ INSERT INTO t1 VALUES('123');
+ COMMIT;
+ }
+} {
+
+ # These tests don't work with memsubsys1, as it causes the effective page
+ # cache size to become too small to hold the entire db in memory.
+ if {[permutation] == "memsubsys1"} continue
+
+ db_restore
+ sqlite3 db test.db -vfs devsym
+ execsql {
+ PRAGMA cache_size = 2000;
+ PRAGMA mmap_size = 0;
+ SELECT x FROM t3 ORDER BY rowid;
+ SELECT x FROM t3 ORDER BY x;
+ }
+ do_execsql_test 6.2.$tn.1 { PRAGMA integrity_check } {ok}
+ do_execsql_test 6.2.$tn.2 $sql
+
+ # Corrupt the database file on disk. This should not matter for the
+ # purposes of the following "PRAGMA integrity_check", as the entire
+ # database should be cached in the pager-cache. If corruption is
+ # reported, it indicates that executing $sql caused the pager cache
+ # to be flushed. Which is a bug.
+ hexio_write test.db [expr 1024 * 5] [string repeat 00 2048]
+ do_execsql_test 6.2.$tn.3 { PRAGMA integrity_check } {ok}
+ db close
+}
+
sqlite3_simulate_device -char {} -sectorsize 0
finish_test
+
diff --git a/test/ioerr6.test b/test/ioerr6.test
new file mode 100644
index 0000000..66c48ad
--- /dev/null
+++ b/test/ioerr6.test
@@ -0,0 +1,92 @@
+# 2012 December 18
+#
+# 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.
+#
+#***********************************************************************
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+source $testdir/malloc_common.tcl
+set ::testprefix ioerr6
+
+ifcapable !atomicwrite {
+ puts "skipping tests - not compiled with SQLITE_ENABLE_ATOMIC_WRITE..."
+ finish_test
+ return
+}
+
+if {[permutation]=="inmemory_journal"} {
+ # These tests will not work with in-memory journals (as persistent VFS
+ # errors commencing after a transaction has started to write to the db
+ # cannot be recovered from).
+ finish_test
+ return
+}
+
+faultsim_save_and_close
+
+do_test 1.1 {
+ testvfs shmfault -default true
+ shmfault devchar atomic
+ sqlite3 db test.db
+ execsql {
+ CREATE TABLE t1(a, b);
+ CREATE INDEX i1 ON t1(a, b);
+ INSERT INTO t1 VALUES(1, 2);
+ INSERT INTO t1 VALUES(2, 4);
+ INSERT INTO t1 VALUES(3, 6);
+ INSERT INTO t1 VALUES(4, 8);
+ }
+
+ # Cause the first call to xWrite() to fail with SQLITE_FULL.
+ shmfault full 2 1
+ catchsql { INSERT INTO t1 VALUES(5, 10) }
+} {1 {database or disk is full}}
+
+do_test 1.2 {
+ execsql { PRAGMA integrity_check }
+} {ok}
+
+db close
+shmfault delete
+
+do_faultsim_test 2 -faults full* -prep {
+ shmfault devchar atomic
+ faultsim_restore
+ sqlite3 db test.db
+} -body {
+ db eval {
+ CREATE TABLE t1(x PRIMARY KEY);
+ INSERT INTO t1 VALUES('abc');
+ }
+} -test {
+ set res [db one { PRAGMA integrity_check }]
+ if {$res != "ok"} {
+ error "integrity check: $res"
+ }
+}
+
+do_faultsim_test 3 -faults full* -prep {
+ shmfault devchar atomic
+ faultsim_restore
+ sqlite3 db test.db
+} -body {
+ db eval {
+ CREATE TABLE t1(x);
+ CREATE TABLE t2(x);
+ }
+} -test {
+ db eval { CREATE TABLE t3(x) }
+ if {[db one { PRAGMA integrity_check }] != "ok"} {
+ error "integrity check failed"
+ }
+}
+
+finish_test
+
diff --git a/test/like.test b/test/like.test
index 767efd5..80ba418 100644
--- a/test/like.test
+++ b/test/like.test
@@ -406,7 +406,7 @@ do_test like-5.2 {
do_test like-5.3 {
execsql {
CREATE TABLE t2(x TEXT COLLATE NOCASE);
- INSERT INTO t2 SELECT * FROM t1;
+ INSERT INTO t2 SELECT * FROM t1 ORDER BY rowid;
CREATE INDEX i2 ON t2(x COLLATE NOCASE);
}
set sqlite_like_count 0
@@ -662,8 +662,8 @@ ifcapable like_opt&&!icu {
set res [sqlite3_exec_hex db {
EXPLAIN QUERY PLAN SELECT x FROM t2 WHERE x LIKE '%ff%25'
}]
- regexp {INDEX i2} $res
- } {0}
+ regexp {SCAN TABLE t2} $res
+ } {1}
}
do_test like-9.5.1 {
set res [sqlite3_exec_hex db {
diff --git a/test/limit.test b/test/limit.test
index e5aac70..c5b75c2 100644
--- a/test/limit.test
+++ b/test/limit.test
@@ -468,5 +468,152 @@ do_test limit-12.4 {
}
} {1 {no such column: x}}
+# Ticket [db4d96798da8b]
+# LIMIT does not work with nested views containing UNION ALL
+#
+do_test limit-13.1 {
+ db eval {
+ CREATE TABLE t13(x);
+ INSERT INTO t13 VALUES(1),(2);
+ CREATE VIEW v13a AS SELECT x AS y FROM t13;
+ CREATE VIEW v13b AS SELECT y AS z FROM v13a UNION ALL SELECT y+10 FROM v13a;
+ CREATE VIEW v13c AS SELECT z FROM v13b UNION ALL SELECT z+20 FROM v13b;
+ }
+} {}
+do_test limit-13.2 {
+ db eval {SELECT z FROM v13c LIMIT 1}
+} {1}
+do_test limit-13.3 {
+ db eval {SELECT z FROM v13c LIMIT 2}
+} {1 2}
+do_test limit-13.4 {
+ db eval {SELECT z FROM v13c LIMIT 3}
+} {1 2 11}
+do_test limit-13.5 {
+ db eval {SELECT z FROM v13c LIMIT 4}
+} {1 2 11 12}
+do_test limit-13.6 {
+ db eval {SELECT z FROM v13c LIMIT 5}
+} {1 2 11 12 21}
+do_test limit-13.7 {
+ db eval {SELECT z FROM v13c LIMIT 6}
+} {1 2 11 12 21 22}
+do_test limit-13.8 {
+ db eval {SELECT z FROM v13c LIMIT 7}
+} {1 2 11 12 21 22 31}
+do_test limit-13.9 {
+ db eval {SELECT z FROM v13c LIMIT 8}
+} {1 2 11 12 21 22 31 32}
+do_test limit-13.10 {
+ db eval {SELECT z FROM v13c LIMIT 9}
+} {1 2 11 12 21 22 31 32}
+do_test limit-13.11 {
+ db eval {SELECT z FROM v13c LIMIT 1 OFFSET 1}
+} {2}
+do_test limit-13.12 {
+ db eval {SELECT z FROM v13c LIMIT 2 OFFSET 1}
+} {2 11}
+do_test limit-13.13 {
+ db eval {SELECT z FROM v13c LIMIT 3 OFFSET 1}
+} {2 11 12}
+do_test limit-13.14 {
+ db eval {SELECT z FROM v13c LIMIT 4 OFFSET 1}
+} {2 11 12 21}
+do_test limit-13.15 {
+ db eval {SELECT z FROM v13c LIMIT 5 OFFSET 1}
+} {2 11 12 21 22}
+do_test limit-13.16 {
+ db eval {SELECT z FROM v13c LIMIT 6 OFFSET 1}
+} {2 11 12 21 22 31}
+do_test limit-13.17 {
+ db eval {SELECT z FROM v13c LIMIT 7 OFFSET 1}
+} {2 11 12 21 22 31 32}
+do_test limit-13.18 {
+ db eval {SELECT z FROM v13c LIMIT 8 OFFSET 1}
+} {2 11 12 21 22 31 32}
+do_test limit-13.21 {
+ db eval {SELECT z FROM v13c LIMIT 1 OFFSET 2}
+} {11}
+do_test limit-13.22 {
+ db eval {SELECT z FROM v13c LIMIT 2 OFFSET 2}
+} {11 12}
+do_test limit-13.23 {
+ db eval {SELECT z FROM v13c LIMIT 3 OFFSET 2}
+} {11 12 21}
+do_test limit-13.24 {
+ db eval {SELECT z FROM v13c LIMIT 4 OFFSET 2}
+} {11 12 21 22}
+do_test limit-13.25 {
+ db eval {SELECT z FROM v13c LIMIT 5 OFFSET 2}
+} {11 12 21 22 31}
+do_test limit-13.26 {
+ db eval {SELECT z FROM v13c LIMIT 6 OFFSET 2}
+} {11 12 21 22 31 32}
+do_test limit-13.27 {
+ db eval {SELECT z FROM v13c LIMIT 7 OFFSET 2}
+} {11 12 21 22 31 32}
+do_test limit-13.31 {
+ db eval {SELECT z FROM v13c LIMIT 1 OFFSET 3}
+} {12}
+do_test limit-13.32 {
+ db eval {SELECT z FROM v13c LIMIT 2 OFFSET 3}
+} {12 21}
+do_test limit-13.33 {
+ db eval {SELECT z FROM v13c LIMIT 3 OFFSET 3}
+} {12 21 22}
+do_test limit-13.34 {
+ db eval {SELECT z FROM v13c LIMIT 4 OFFSET 3}
+} {12 21 22 31}
+do_test limit-13.35 {
+ db eval {SELECT z FROM v13c LIMIT 5 OFFSET 3}
+} {12 21 22 31 32}
+do_test limit-13.36 {
+ db eval {SELECT z FROM v13c LIMIT 6 OFFSET 3}
+} {12 21 22 31 32}
+do_test limit-13.41 {
+ db eval {SELECT z FROM v13c LIMIT 1 OFFSET 4}
+} {21}
+do_test limit-13.42 {
+ db eval {SELECT z FROM v13c LIMIT 2 OFFSET 4}
+} {21 22}
+do_test limit-13.43 {
+ db eval {SELECT z FROM v13c LIMIT 3 OFFSET 4}
+} {21 22 31}
+do_test limit-13.44 {
+ db eval {SELECT z FROM v13c LIMIT 4 OFFSET 4}
+} {21 22 31 32}
+do_test limit-13.45 {
+ db eval {SELECT z FROM v13c LIMIT 5 OFFSET 4}
+} {21 22 31 32}
+do_test limit-13.51 {
+ db eval {SELECT z FROM v13c LIMIT 1 OFFSET 5}
+} {22}
+do_test limit-13.52 {
+ db eval {SELECT z FROM v13c LIMIT 2 OFFSET 5}
+} {22 31}
+do_test limit-13.53 {
+ db eval {SELECT z FROM v13c LIMIT 3 OFFSET 5}
+} {22 31 32}
+do_test limit-13.54 {
+ db eval {SELECT z FROM v13c LIMIT 4 OFFSET 5}
+} {22 31 32}
+do_test limit-13.61 {
+ db eval {SELECT z FROM v13c LIMIT 1 OFFSET 6}
+} {31}
+do_test limit-13.62 {
+ db eval {SELECT z FROM v13c LIMIT 2 OFFSET 6}
+} {31 32}
+do_test limit-13.63 {
+ db eval {SELECT z FROM v13c LIMIT 3 OFFSET 6}
+} {31 32}
+do_test limit-13.71 {
+ db eval {SELECT z FROM v13c LIMIT 1 OFFSET 7}
+} {32}
+do_test limit-13.72 {
+ db eval {SELECT z FROM v13c LIMIT 2 OFFSET 7}
+} {32}
+do_test limit-13.81 {
+ db eval {SELECT z FROM v13c LIMIT 1 OFFSET 8}
+} {}
finish_test
diff --git a/test/loadext.test b/test/loadext.test
index 72eff12..0d5b841 100644
--- a/test/loadext.test
+++ b/test/loadext.test
@@ -139,20 +139,22 @@ do_test loadext-2.1 {
sqlite3_load_extension db "${testextension}xx"
} msg]
list $rc $msg
-} [list 1 [format $dlerror_nosuchfile ${testextension}xx]]
+} /[list 1 [format $dlerror_nosuchfile ${testextension}xx.*]]/
# Try to load an extension for which the file is not a shared object
#
do_test loadext-2.2 {
- set fd [open "${testextension}xx" w]
+ set fd [open "./notasharedlib.so" w]
+ puts $fd blah
+ close $fd
+ set fd [open "./notasharedlib.dll" w]
puts $fd blah
close $fd
set rc [catch {
- sqlite3_load_extension db "${testextension}xx"
+ sqlite3_load_extension db "./notasharedlib"
} msg]
- set expected_error_pattern [format $dlerror_notadll ${testextension}xx]
- list $rc [string match $expected_error_pattern $msg]
-} [list 1 1]
+ list $rc $msg
+} /[list 1 [format $dlerror_notadll ./notasharedlib.*]]/
# Try to load an extension for which the file is present but the
# entry point is not.
@@ -196,7 +198,7 @@ do_test loadext-3.2 {
regsub {0x[1234567890abcdefABCDEF]*} $res XXX res
}
set res
-} [list 1 [format $dlerror_nosymbol $testextension sqlite3_extension_init]]
+} /[list 1 [format $dlerror_nosymbol $testextension sqlite3_.*_init]]/
do_test loadext-3.3 {
catchsql {
SELECT load_extension($::testextension,'testloadext_init')
diff --git a/test/lock.test b/test/lock.test
index 22f359c..6ec85ee 100644
--- a/test/lock.test
+++ b/test/lock.test
@@ -247,11 +247,34 @@ do_test lock-2.8 {
execsql {UPDATE t1 SET a = 0 WHERE 0}
catchsql {BEGIN EXCLUSIVE;} db2
} {1 {database is locked}}
+do_test lock-2.8b {
+ db2 eval {PRAGMA busy_timeout}
+} {400}
do_test lock-2.9 {
db2 timeout 0
execsql COMMIT
} {}
+do_test lock-2.9b {
+ db2 eval {PRAGMA busy_timeout}
+} {0}
integrity_check lock-2.10
+do_test lock-2.11 {
+ db2 eval {PRAGMA busy_timeout(400)}
+ execsql BEGIN
+ execsql {UPDATE t1 SET a = 0 WHERE 0}
+ catchsql {BEGIN EXCLUSIVE;} db2
+} {1 {database is locked}}
+do_test lock-2.11b {
+ db2 eval {PRAGMA busy_timeout}
+} {400}
+do_test lock-2.12 {
+ db2 eval {PRAGMA busy_timeout(0)}
+ execsql COMMIT
+} {}
+do_test lock-2.12b {
+ db2 eval {PRAGMA busy_timeout}
+} {0}
+integrity_check lock-2.13
# Try to start two transactions in a row
#
diff --git a/test/malloc.test b/test/malloc.test
index 0d213d7..5d03aa8 100644
--- a/test/malloc.test
+++ b/test/malloc.test
@@ -842,7 +842,7 @@ do_malloc_test 36 -sqlprep {
SELECT test_agg_errmsg16(), group_concat(a) FROM t1
}
-# At one point, if an OOM occured immediately after obtaining a shared lock
+# At one point, if an OOM occurred immediately after obtaining a shared lock
# on the database file, the file remained locked. This test case ensures
# that bug has been fixed.i
if {[db eval {PRAGMA locking_mode}]!="exclusive"} {
diff --git a/test/malloc3.test b/test/malloc3.test
index 2dfde46..f4a6c3b 100644
--- a/test/malloc3.test
+++ b/test/malloc3.test
@@ -27,6 +27,24 @@ if {!$MEMDEBUG} {
return
}
+
+# Do not run these tests with an in-memory journal.
+#
+# In the pager layer, if an IO or OOM error occurs during a ROLLBACK, or
+# when flushing a page to disk due to cache-stress, the pager enters an
+# "error state". The only way out of the error state is to unlock the
+# database file and end the transaction, leaving whatever journal and
+# database files happen to be on disk in place. The next time the current
+# (or any other) connection opens a read transaction, hot-journal rollback
+# is performed if necessary.
+#
+# Of course, this doesn't work with an in-memory journal.
+#
+if {[permutation]=="inmemory_journal"} {
+ finish_test
+ return
+}
+
#--------------------------------------------------------------------------
# NOTES ON RECOVERING FROM A MALLOC FAILURE
#
@@ -147,6 +165,7 @@ if {!$MEMDEBUG} {
# ::run_test_script. At the end of this file, the proc [run_test] is used
# to execute the program (and all test cases contained therein).
#
+set ::run_test_sql_id 0
set ::run_test_script [list]
proc TEST {id t} {lappend ::run_test_script -test [list $id $t]}
proc PREP {p} {lappend ::run_test_script -prep [string trim $p]}
@@ -162,13 +181,14 @@ proc DEBUG {s} {lappend ::run_test_script -debug $s}
# transaction only.
#
proc SQL {a1 {a2 ""}} {
- # An SQL primitive parameter is a list of two elements, a boolean value
- # indicating if the statement may cause transaction rollback when malloc()
- # fails, and the sql statement itself.
+ # An SQL primitive parameter is a list of three elements, an id, a boolean
+ # value indicating if the statement may cause transaction rollback when
+ # malloc() fails, and the sql statement itself.
+ set id [incr ::run_test_sql_id]
if {$a2 == ""} {
- lappend ::run_test_script -sql [list true [string trim $a1]]
+ lappend ::run_test_script -sql [list $id true [string trim $a1]]
} else {
- lappend ::run_test_script -sql [list false [string trim $a2]]
+ lappend ::run_test_script -sql [list $id false [string trim $a2]]
}
}
@@ -258,7 +278,7 @@ TEST 5 {
set sql {
BEGIN;DELETE FROM abc;
}
-for {set i 1} {$i < 15} {incr i} {
+for {set i 1} {$i < 100} {incr i} {
set a $i
set b "String value $i"
set c [string repeat X $i]
@@ -529,12 +549,13 @@ proc run_test {arglist iRepeat {pcstart 0} {iFailStart 1}} {
}
for {set i 0} {$i < $pcstart} {incr i} {
- set k2 [lindex $arglist [expr 2 * $i]]
- set v2 [lindex $arglist [expr 2 * $i + 1]]
+ set k2 [lindex $arglist [expr {2 * $i}]]
+ set v2 [lindex $arglist [expr {2 * $i + 1}]]
set ac [sqlite3_get_autocommit $::DB] ;# Auto-Commit
switch -- $k2 {
- -sql {db eval [lindex $v2 1]}
+ -sql {db eval [lindex $v2 2]}
-prep {db eval $v2}
+ -debug {eval $v2}
}
set nac [sqlite3_get_autocommit $::DB] ;# New Auto-Commit
if {$ac && !$nac} {set begin_pc $i}
@@ -545,33 +566,39 @@ proc run_test {arglist iRepeat {pcstart 0} {iFailStart 1}} {
set iFail $iFailStart
set pc $pcstart
while {$pc*2 < [llength $arglist]} {
+ # Fetch the current instruction type and payload.
+ set k [lindex $arglist [expr {2 * $pc}]]
+ set v [lindex $arglist [expr {2 * $pc + 1}]]
# Id of this iteration:
- set k [lindex $arglist [expr 2 * $pc]]
set iterid "pc=$pc.iFail=$iFail$k"
- set v [lindex $arglist [expr 2 * $pc + 1]]
switch -- $k {
-test {
foreach {id script} $v {}
+ set testid "malloc3-(test $id).$iterid"
+ eval $script
incr pc
}
-sql {
set ::rollback_hook_count 0
+ set id [lindex $v 0]
+ set testid "malloc3-(integrity $id).$iterid"
+
set ac [sqlite3_get_autocommit $::DB] ;# Auto-Commit
sqlite3_memdebug_fail $iFail -repeat 0
- set rc [catch {db eval [lindex $v 1]} msg] ;# True error occurs
+ set rc [catch {db eval [lindex $v 2]} msg] ;# True error occurs
set nac [sqlite3_get_autocommit $::DB] ;# New Auto-Commit
if {$rc != 0 && $nac && !$ac} {
# Before [db eval] the auto-commit flag was clear. Now it
- # is set. Since an error occured we assume this was not a
- # commit - therefore a rollback occured. Check that the
+ # is set. Since an error occurred we assume this was not a
+ # commit - therefore a rollback occurred. Check that the
# rollback-hook was invoked.
- do_test malloc3-rollback_hook.$iterid {
+ do_test malloc3-rollback_hook_count.$iterid {
set ::rollback_hook_count
} {1}
}
@@ -582,8 +609,9 @@ proc run_test {arglist iRepeat {pcstart 0} {iFailStart 1}} {
# calls should be equal to the number of benign failures.
# Otherwise a malloc() failed and the error was not reported.
#
- if {$nFail!=$nBenign} {
- error "Unreported malloc() failure"
+ set expr {$nFail!=$nBenign}
+ if {[expr $expr]} {
+ error "Unreported malloc() failure, test \"$testid\", $expr"
}
if {$ac && !$nac} {
@@ -595,24 +623,23 @@ proc run_test {arglist iRepeat {pcstart 0} {iFailStart 1}} {
incr pc
set iFail 1
- integrity_check "malloc3-(integrity).$iterid"
+ integrity_check $testid
} elseif {[regexp {.*out of memory} $msg] || [db errorcode] == 3082} {
# Out of memory error, as expected.
#
- integrity_check "malloc3-(integrity).$iterid"
+ integrity_check $testid
incr iFail
if {$nac && !$ac} {
-
- if {![lindex $v 0] && [db errorcode] != 3082} {
- # error "Statement \"[lindex $v 1]\" caused a rollback"
+ if {![lindex $v 1] && [db errorcode] != 3082} {
+ # error "Statement \"[lindex $v 2]\" caused a rollback"
}
for {set i $begin_pc} {$i < $pc} {incr i} {
- set k2 [lindex $arglist [expr 2 * $i]]
- set v2 [lindex $arglist [expr 2 * $i + 1]]
+ set k2 [lindex $arglist [expr {2 * $i}]]
+ set v2 [lindex $arglist [expr {2 * $i + 1}]]
set catchupsql ""
switch -- $k2 {
- -sql {set catchupsql [lindex $v2 1]}
+ -sql {set catchupsql [lindex $v2 2]}
-prep {set catchupsql $v2}
}
db eval $catchupsql
@@ -622,7 +649,8 @@ proc run_test {arglist iRepeat {pcstart 0} {iFailStart 1}} {
error $msg
}
- while {[lindex $arglist [expr 2 * ($pc -1)]] == "-test"} {
+ # back up to the previous "-test" block.
+ while {[lindex $arglist [expr {2 * ($pc - 1)}]] == "-test"} {
incr pc -1
}
}
@@ -642,7 +670,7 @@ proc run_test {arglist iRepeat {pcstart 0} {iFailStart 1}} {
}
}
-# Turn of the Tcl interface's prepared statement caching facility. Then
+# Turn off the Tcl interface's prepared statement caching facility. Then
# run the tests with "persistent" malloc failures.
sqlite3_extended_result_codes db 1
db cache size 0
diff --git a/test/mallocG.test b/test/mallocG.test
index eab533b..4f2395d 100644
--- a/test/mallocG.test
+++ b/test/mallocG.test
@@ -53,6 +53,11 @@ do_malloc_test mallocG-3 -sqlprep {
AND x BETWEEN 'i' AND 'm'
}
+ifcapable !utf16 {
+ finish_test
+ return
+}
+
proc utf16 {utf8} {
set utf16 [encoding convertto unicode $utf8]
append utf16 "\x00\x00"
diff --git a/test/malloc_common.tcl b/test/malloc_common.tcl
index 5937b95..2ac619b 100644
--- a/test/malloc_common.tcl
+++ b/test/malloc_common.tcl
@@ -264,7 +264,7 @@ proc faultsim_test_result_int {args} {
set t [list $testrc $testresult]
set r $args
if { ($testnfail==0 && $t != [lindex $r 0]) || [lsearch $r $t]<0 } {
- error "nfail=$testnfail rc=$testrc result=$testresult"
+ error "nfail=$testnfail rc=$testrc result=$testresult list=$r"
}
}
diff --git a/test/memdb.test b/test/memdb.test
index 1da3d7c..802fb1a 100644
--- a/test/memdb.test
+++ b/test/memdb.test
@@ -365,7 +365,7 @@ do_test memdb-6.15 {
ifcapable subquery&&vtab {
do_test memdb-7.1 {
- register_wholenumber_module db
+ load_static_extension db wholenumber
execsql {
CREATE TABLE t6(x);
CREATE VIRTUAL TABLE nums USING wholenumber;
diff --git a/test/minmax.test b/test/minmax.test
index 5990245..fb9bbb3 100644
--- a/test/minmax.test
+++ b/test/minmax.test
@@ -17,6 +17,7 @@
set testdir [file dirname $argv0]
source $testdir/tester.tcl
+set ::testprefix minmax
do_test minmax-1.0 {
execsql {
@@ -299,7 +300,7 @@ ifcapable {compound && subquery} {
SELECT max(rowid) FROM t4 UNION SELECT max(rowid) FROM t5
)
}
- } {1}
+ } {{}}
do_test minmax-9.2 {
execsql {
SELECT max(rowid) FROM (
@@ -536,7 +537,96 @@ do_test minmax-12.17 {
}
} {5}
+#-------------------------------------------------------------------------
+reset_db
+
+proc do_test_13 {op name sql1 sql2 res} {
+ set ::sqlite_search_count 0
+ uplevel [list do_execsql_test $name.1 $sql1 $res]
+ set a $::sqlite_search_count
+
+ set ::sqlite_search_count 0
+ uplevel [list do_execsql_test $name.2 $sql2 $res]
+ set b $::sqlite_search_count
+
+ uplevel [list do_test $name.3 [list expr "$a $op $b"] 1]
+}
+
+# Run a test named $name. Check that SQL statements $sql1 and $sql2 both
+# return the same result, but that $sql2 increments the $sqlite_search_count
+# variable more often (indicating that it is visiting more rows to determine
+# the result).
+#
+proc do_test_13_opt {name sql1 sql2 res} {
+ uplevel [list do_test_13 < $name $sql1 $sql2 $res]
+}
+
+# Like [do_test_13_noopt], except this time check that the $sqlite_search_count
+# variable is incremented the same number of times by both SQL statements.
+#
+proc do_test_13_noopt {name sql1 sql2 res} {
+ uplevel [list do_test_13 == $name $sql1 $sql2 $res]
+}
+
+do_execsql_test 13.1 {
+ CREATE TABLE t1(a, b, c);
+ INSERT INTO t1 VALUES('a', 1, 1);
+ INSERT INTO t1 VALUES('b', 6, 6);
+ INSERT INTO t1 VALUES('c', 5, 5);
+ INSERT INTO t1 VALUES('a', 4, 4);
+ INSERT INTO t1 VALUES('a', 5, 5);
+ INSERT INTO t1 VALUES('c', 6, 6);
+ INSERT INTO t1 VALUES('b', 4, 4);
+ INSERT INTO t1 VALUES('c', 7, 7);
+ INSERT INTO t1 VALUES('b', 2, 2);
+ INSERT INTO t1 VALUES('b', 3, 3);
+ INSERT INTO t1 VALUES('a', 3, 3);
+ INSERT INTO t1 VALUES('b', 5, 5);
+ INSERT INTO t1 VALUES('c', 4, 4);
+ INSERT INTO t1 VALUES('c', 3, 3);
+ INSERT INTO t1 VALUES('a', 2, 2);
+ SELECT * FROM t1 ORDER BY a, b, c;
+} {a 1 1 a 2 2 a 3 3 a 4 4 a 5 5
+ b 2 2 b 3 3 b 4 4 b 5 5 b 6 6
+ c 3 3 c 4 4 c 5 5 c 6 6 c 7 7
+}
+do_execsql_test 13.2 { CREATE INDEX i1 ON t1(a, b, c) }
+
+do_test_13_opt 13.3 {
+ SELECT min(b) FROM t1 WHERE a='b'
+} {
+ SELECT min(c) FROM t1 WHERE a='b'
+} {2}
+
+do_test_13_opt 13.4 {
+ SELECT a, min(b) FROM t1 WHERE a='b'
+} {
+ SELECT a, min(c) FROM t1 WHERE a='b'
+} {b 2}
+
+do_test_13_opt 13.4 {
+ SELECT a||c, max(b)+4 FROM t1 WHERE a='c'
+} {
+ SELECT a||c, max(c)+4 FROM t1 WHERE a='c'
+} {c7 11}
+
+do_test_13_noopt 13.5 {
+ SELECT a||c, max(b+1) FROM t1 WHERE a='c'
+} {
+ SELECT a||c, max(c+1) FROM t1 WHERE a='c'
+} {c7 8}
+
+do_test_13_noopt 13.6 {
+ SELECT count(b) FROM t1 WHERE a='c'
+} {
+ SELECT count(c) FROM t1 WHERE a='c'
+} {5}
+do_test_13_noopt 13.7 {
+ SELECT min(b), count(b) FROM t1 WHERE a='a';
+} {
+ SELECT min(c), count(c) FROM t1 WHERE a='a';
+} {1 5}
finish_test
diff --git a/test/minmax2.test b/test/minmax2.test
index 2f504d4..da8fec3 100644
--- a/test/minmax2.test
+++ b/test/minmax2.test
@@ -289,7 +289,7 @@ ifcapable {compound && subquery} {
SELECT max(rowid) FROM t4 UNION SELECT max(rowid) FROM t5
)
}
- } {1}
+ } {{}}
do_test minmax2-9.2 {
execsql {
SELECT max(rowid) FROM (
diff --git a/test/misc7.test b/test/misc7.test
index 4868c12..72c1cd6 100644
--- a/test/misc7.test
+++ b/test/misc7.test
@@ -488,6 +488,34 @@ do_test misc7-21.1 {
list $rc $msg
} {1 {unable to open database file}}
+# Try to do hot-journal rollback with a read-only connection. The
+# error code should be SQLITE_READONLY_ROLLBACK.
+#
+do_test misc7-22.1 {
+ db close
+ forcedelete test.db copy.db-journal
+ sqlite3 db test.db
+ execsql {
+ CREATE TABLE t1(a, b);
+ INSERT INTO t1 VALUES(1, 2);
+ INSERT INTO t1 VALUES(3, 4);
+ }
+ db close
+ sqlite3 db test.db -readonly 1
+ catchsql {
+ INSERT INTO t1 VALUES(5, 6);
+ }
+} {1 {attempt to write a readonly database}}
+do_test misc7-22.2 { execsql { SELECT * FROM t1 } } {1 2 3 4}
+do_test misc7-22.3 {
+ set fd [open test.db-journal w]
+ puts $fd [string repeat abc 1000]
+ close $fd
+ catchsql { SELECT * FROM t1 }
+} {1 {attempt to write a readonly database}}
+do_test misc7-22.4 {
+ sqlite3_extended_errcode db
+} SQLITE_READONLY_ROLLBACK
db close
forcedelete test.db
diff --git a/test/mmap1.test b/test/mmap1.test
new file mode 100644
index 0000000..ece3e02
--- /dev/null
+++ b/test/mmap1.test
@@ -0,0 +1,331 @@
+# 2013 March 20
+#
+# 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.
+#
+#***********************************************************************
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+ifcapable !mmap {
+ finish_test
+ return
+}
+source $testdir/lock_common.tcl
+set testprefix mmap1
+
+proc nRead {db} {
+ set bt [btree_from_db $db]
+ db_enter $db
+ array set stats [btree_pager_stats $bt]
+ db_leave $db
+ # puts [array get stats]
+ return $stats(read)
+}
+
+proc register_rblob_code {dbname seed} {
+ return [subst -nocommands {
+ set ::rcnt $seed
+ proc rblob {n} {
+ set ::rcnt [expr (([set ::rcnt] << 3) + [set ::rcnt] + 456) & 0xFFFFFFFF]
+ set str [format %.8x [expr [set ::rcnt] ^ 0xbdf20da3]]
+ string range [string repeat [set str] [expr [set n]/4]] 1 [set n]
+ }
+ $dbname func rblob rblob
+ }]
+}
+
+# For cases 1.1 and 1.4, the number of pages read using xRead() is 4 on
+# unix and 9 on windows. The difference is that windows only ever maps
+# an integer number of OS pages (i.e. creates mappings that are a multiple
+# of 4KB in size). Whereas on unix any sized mapping may be created.
+#
+foreach {t mmap_size nRead c2init} {
+ 1.1 { PRAGMA mmap_size = 67108864 } /[49]/ {PRAGMA mmap_size = 0}
+ 1.2 { PRAGMA mmap_size = 53248 } 150 {PRAGMA mmap_size = 0}
+ 1.3 { PRAGMA mmap_size = 0 } 344 {PRAGMA mmap_size = 0}
+ 1.4 { PRAGMA mmap_size = 67108864 } /[49]/ {PRAGMA mmap_size = 67108864 }
+ 1.5 { PRAGMA mmap_size = 53248 } 150 {PRAGMA mmap_size = 67108864 }
+ 1.6 { PRAGMA mmap_size = 0 } 344 {PRAGMA mmap_size = 67108864 }
+} {
+
+ do_multiclient_test tn {
+ sql1 {PRAGMA cache_size=2000}
+ sql2 {PRAGMA cache_size=2000}
+
+ sql1 {PRAGMA page_size=1024}
+ sql1 $mmap_size
+ sql2 $c2init
+
+ code2 [register_rblob_code db2 0]
+
+ sql2 {
+ PRAGMA page_size=1024;
+ PRAGMA auto_vacuum = 1;
+ CREATE TABLE t1(a, b, UNIQUE(a, b));
+ INSERT INTO t1 VALUES(rblob(500), rblob(500));
+ INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1; -- 2
+ INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1; -- 4
+ INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1; -- 8
+ INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1; -- 16
+ INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1; -- 32
+ }
+ do_test $t.$tn.1 {
+ sql1 "SELECT count(*) FROM t1; PRAGMA integrity_check ; PRAGMA page_count"
+ } {32 ok 77}
+
+ # Have connection 2 shrink the file. Check connection 1 can still read it.
+ sql2 { DELETE FROM t1 WHERE rowid%2; }
+ do_test $t.$tn.2 {
+ sql1 "SELECT count(*) FROM t1; PRAGMA integrity_check ; PRAGMA page_count"
+ } {16 ok 42}
+
+ # Have connection 2 grow the file. Check connection 1 can still read it.
+ sql2 { INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1 }
+ do_test $t.$tn.3 {
+ sql1 "SELECT count(*) FROM t1; PRAGMA integrity_check ; PRAGMA page_count"
+ } {32 ok 79}
+
+ # Have connection 2 grow the file again. Check connection 1 is still ok.
+ sql2 { INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1 }
+ do_test $t.$tn.4 {
+ sql1 "SELECT count(*) FROM t1; PRAGMA integrity_check ; PRAGMA page_count"
+ } {64 ok 149}
+
+ # Check that the number of pages read by connection 1 indicates that the
+ # "PRAGMA mmap_size" command worked.
+ do_test $t.$tn.5 { nRead db } $nRead
+ }
+}
+
+set ::rcnt 0
+proc rblob {n} {
+ set ::rcnt [expr (($::rcnt << 3) + $::rcnt + 456) & 0xFFFFFFFF]
+ set str [format %.8x [expr $::rcnt ^ 0xbdf20da3]]
+ string range [string repeat $str [expr $n/4]] 1 $n
+}
+
+reset_db
+db func rblob rblob
+
+do_execsql_test 2.1 {
+ PRAGMA auto_vacuum = 1;
+ PRAGMA mmap_size = 67108864;
+ PRAGMA journal_mode = wal;
+ CREATE TABLE t1(a, b, UNIQUE(a, b));
+ INSERT INTO t1 VALUES(rblob(500), rblob(500));
+ INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1; -- 2
+ INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1; -- 4
+ INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1; -- 8
+ INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1; -- 16
+ INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1; -- 32
+ PRAGMA wal_checkpoint;
+} {67108864 wal 0 103 103}
+
+do_execsql_test 2.2 {
+ PRAGMA auto_vacuum;
+ SELECT count(*) FROM t1;
+} {1 32}
+
+if {[permutation] != "inmemory_journal"} {
+ do_test 2.3 {
+ sqlite3 db2 test.db
+ db2 func rblob rblob
+ db2 eval {
+ DELETE FROM t1 WHERE (rowid%4);
+ PRAGMA wal_checkpoint;
+ }
+ db2 eval {
+ INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1; -- 16
+ SELECT count(*) FROM t1;
+ }
+ } {16}
+
+ do_execsql_test 2.4 {
+ PRAGMA wal_checkpoint;
+ } {0 24 24}
+ db2 close
+}
+
+reset_db
+execsql { PRAGMA mmap_size = 67108864; }
+db func rblob rblob
+do_execsql_test 3.1 {
+ PRAGMA auto_vacuum = 1;
+
+ CREATE TABLE t1(a, b, UNIQUE(a, b));
+ INSERT INTO t1 VALUES(rblob(500), rblob(500));
+ INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1; -- 2
+ INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1; -- 4
+ INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1; -- 8
+
+ CREATE TABLE t2(a, b, UNIQUE(a, b));
+ INSERT INTO t2 SELECT * FROM t1;
+} {}
+
+do_test 3.2 {
+ set nRow 0
+ db eval {SELECT * FROM t2 ORDER BY a, b} {
+ if {$nRow==4} { db eval { DELETE FROM t1 } }
+ incr nRow
+ }
+ set nRow
+} {8}
+
+#-------------------------------------------------------------------------
+# Ensure that existing cursors using xFetch() pages see changes made
+# to rows using the incrblob API.
+#
+reset_db
+execsql { PRAGMA mmap_size = 67108864; }
+set aaa [string repeat a 400]
+set bbb [string repeat b 400]
+set ccc [string repeat c 400]
+set ddd [string repeat d 400]
+set eee [string repeat e 400]
+
+do_execsql_test 4.1 {
+ PRAGMA page_size = 1024;
+ CREATE TABLE t1(x);
+ INSERT INTO t1 VALUES($aaa);
+ INSERT INTO t1 VALUES($bbb);
+ INSERT INTO t1 VALUES($ccc);
+ INSERT INTO t1 VALUES($ddd);
+ SELECT * FROM t1;
+ BEGIN;
+} [list $aaa $bbb $ccc $ddd]
+
+do_test 4.2 {
+ set ::STMT [sqlite3_prepare db "SELECT * FROM t1 ORDER BY rowid" -1 dummy]
+ sqlite3_step $::STMT
+ sqlite3_column_text $::STMT 0
+} $aaa
+
+do_test 4.3 {
+ foreach r {2 3 4} {
+ set fd [db incrblob t1 x $r]
+ puts -nonewline $fd $eee
+ close $fd
+ }
+
+ set res [list]
+ while {"SQLITE_ROW" == [sqlite3_step $::STMT]} {
+ lappend res [sqlite3_column_text $::STMT 0]
+ }
+ set res
+} [list $eee $eee $eee]
+
+do_test 4.4 {
+ sqlite3_finalize $::STMT
+} SQLITE_OK
+
+do_execsql_test 4.5 { COMMIT }
+
+#-------------------------------------------------------------------------
+# Ensure that existing cursors holding xFetch() references are not
+# confused if those pages are moved to make way for the root page of a
+# new table or index.
+#
+reset_db
+execsql { PRAGMA mmap_size = 67108864; }
+do_execsql_test 5.1 {
+ PRAGMA auto_vacuum = 2;
+ PRAGMA page_size = 1024;
+ CREATE TABLE t1(x);
+ INSERT INTO t1 VALUES($aaa);
+ INSERT INTO t1 VALUES($bbb);
+ INSERT INTO t1 VALUES($ccc);
+ INSERT INTO t1 VALUES($ddd);
+
+ PRAGMA auto_vacuum;
+ SELECT * FROM t1;
+} [list 2 $aaa $bbb $ccc $ddd]
+
+do_test 5.2 {
+ set ::STMT [sqlite3_prepare db "SELECT * FROM t1 ORDER BY rowid" -1 dummy]
+ sqlite3_step $::STMT
+ sqlite3_column_text $::STMT 0
+} $aaa
+
+do_execsql_test 5.3 {
+ CREATE TABLE t2(x);
+ INSERT INTO t2 VALUES('tricked you!');
+ INSERT INTO t2 VALUES('tricked you!');
+}
+
+do_test 5.4 {
+ sqlite3_step $::STMT
+ sqlite3_column_text $::STMT 0
+} $bbb
+
+do_test 5.5 {
+ sqlite3_finalize $::STMT
+} SQLITE_OK
+
+#-------------------------------------------------------------------------
+# Test various mmap_size settings.
+#
+foreach {tn1 mmap1 mmap2} {
+ 1 6144 167773
+ 2 18432 140399
+ 3 43008 401302
+ 4 92160 253899
+ 5 190464 2
+ 6 387072 752431
+ 7 780288 291143
+ 8 1566720 594306
+ 9 3139584 829137
+ 10 6285312 793963
+ 11 12576768 1015590
+} {
+ do_multiclient_test tn {
+ sql1 {
+ CREATE TABLE t1(a PRIMARY KEY);
+ CREATE TABLE t2(x);
+ INSERT INTO t2 VALUES('');
+ }
+
+ code1 [register_rblob_code db 0]
+ code2 [register_rblob_code db2 444]
+
+ sql1 "PRAGMA mmap_size = $mmap1"
+ sql2 "PRAGMA mmap_size = $mmap2"
+
+ do_test $tn1.$tn {
+ for {set i 1} {$i <= 100} {incr i} {
+ if {$i % 2} {
+ set c1 sql1
+ set c2 sql2
+ } else {
+ set c1 sql2
+ set c2 sql1
+ }
+
+ $c1 {
+ INSERT INTO t1 VALUES( rblob(5000) );
+ UPDATE t2 SET x = (SELECT md5sum(a) FROM t1);
+ }
+
+ set res [$c2 {
+ SELECT count(*) FROM t1;
+ SELECT x == (SELECT md5sum(a) FROM t1) FROM t2;
+ PRAGMA integrity_check;
+ }]
+ if {$res != [list $i 1 ok]} {
+ do_test $tn1.$tn.$i {
+ set ::res
+ } [list $i 1 ok]
+ }
+ }
+ set res 1
+ } {1}
+ }
+}
+
+
+finish_test
diff --git a/test/mmap2.test b/test/mmap2.test
new file mode 100644
index 0000000..1f8346b
--- /dev/null
+++ b/test/mmap2.test
@@ -0,0 +1,87 @@
+# 2013 March 20
+#
+# 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 file tests the effect of the mmap() or mremap() system calls
+# returning an error on the library.
+#
+# If either mmap() or mremap() fails, SQLite should log an error
+# message, then continue accessing the database using read() and
+# write() exclusively.
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set testprefix mmap2
+
+if {$::tcl_platform(platform)!="unix" || [test_syscall defaultvfs] != "unix"} {
+ finish_test
+ return
+}
+ifcapable !mmap {
+ finish_test
+ return
+}
+
+db close
+sqlite3_shutdown
+test_sqlite3_log xLog
+proc xLog {error_code msg} {
+ if {[string match os_unix.c* $msg]} {
+ lappend ::log $msg
+ }
+}
+
+foreach syscall {mmap mremap} {
+ test_syscall uninstall
+ if {[catch {test_syscall install $syscall}]} continue
+
+ for {set i 1} {$i < 20} {incr i} {
+ reset_db
+ execsql { PRAGMA mmap_size = 8000000 }
+
+ test_syscall fault $i 1
+ test_syscall errno $syscall ENOMEM
+ set ::log ""
+
+ do_execsql_test 1.$syscall.$i.1 {
+ CREATE TABLE t1(a, b, UNIQUE(a, b));
+ INSERT INTO t1 VALUES(randomblob(1000), randomblob(1000));
+ INSERT INTO t1 SELECT randomblob(1000), randomblob(1000) FROM t1;
+ INSERT INTO t1 SELECT randomblob(1000), randomblob(1000) FROM t1;
+ INSERT INTO t1 SELECT randomblob(1000), randomblob(1000) FROM t1;
+ INSERT INTO t1 SELECT randomblob(1000), randomblob(1000) FROM t1;
+ INSERT INTO t1 SELECT randomblob(1000), randomblob(1000) FROM t1;
+ INSERT INTO t1 SELECT randomblob(1000), randomblob(1000) FROM t1;
+ }
+
+ set nFail [test_syscall fault 0 0]
+
+ do_execsql_test 1.$syscall.$i.2 {
+ SELECT count(*) FROM t1;
+ PRAGMA integrity_check;
+ } {64 ok}
+
+ do_test 1.$syscall.$i.3 {
+ expr {$nFail==0 || $nFail==1}
+ } {1}
+
+ do_test 1.$syscall.$i.4.nFail=$nFail {
+ regexp ".*${syscall}.*" $::log
+ } [expr $nFail>0]
+ }
+}
+
+db close
+test_syscall uninstall
+sqlite3_shutdown
+test_sqlite3_log
+sqlite3_initialize
+finish_test
diff --git a/test/notify2.test b/test/notify2.test
index 4016b6d..9e40ed6 100644
--- a/test/notify2.test
+++ b/test/notify2.test
@@ -150,9 +150,9 @@ set sql $zSql
# Hit some other kind of error. This is a malfunction.
error $msg
} else {
- # No error occured. Check that any SELECT statements in the transaction
+ # No error occurred. Check that any SELECT statements in the transaction
# returned "1". Otherwise, the invariant was false, indicating that
- # some malfunction has occured.
+ # some malfunction has occurred.
foreach r $msg { if {$r != 1} { puts "Invariant check failed: $msg" } }
}
}
diff --git a/test/notnull.test b/test/notnull.test
index 240aaba..01738a4 100644
--- a/test/notnull.test
+++ b/test/notnull.test
@@ -48,6 +48,7 @@ do_test notnull-1.2 {
SELECT * FROM t1 order by a;
}
} {1 {t1.a may not be NULL}}
+verify_ex_errcode notnull-1.2b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-1.3 {
catchsql {
DELETE FROM t1;
@@ -62,6 +63,7 @@ do_test notnull-1.4 {
SELECT * FROM t1 order by a;
}
} {1 {t1.a may not be NULL}}
+verify_ex_errcode notnull-1.4b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-1.5 {
catchsql {
DELETE FROM t1;
@@ -69,6 +71,7 @@ do_test notnull-1.5 {
SELECT * FROM t1 order by a;
}
} {1 {t1.a may not be NULL}}
+verify_ex_errcode notnull-1.5b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-1.6 {
catchsql {
DELETE FROM t1;
@@ -104,6 +107,7 @@ do_test notnull-1.10 {
SELECT * FROM t1 order by a;
}
} {1 {t1.b may not be NULL}}
+verify_ex_errcode notnull-1.10b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-1.11 {
catchsql {
DELETE FROM t1;
@@ -146,6 +150,7 @@ do_test notnull-1.16 {
SELECT * FROM t1 order by a;
}
} {1 {t1.c may not be NULL}}
+verify_ex_errcode notnull-1.16b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-1.17 {
catchsql {
DELETE FROM t1;
@@ -153,6 +158,7 @@ do_test notnull-1.17 {
SELECT * FROM t1 order by a;
}
} {1 {t1.d may not be NULL}}
+verify_ex_errcode notnull-1.17b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-1.18 {
catchsql {
DELETE FROM t1;
@@ -174,6 +180,7 @@ do_test notnull-1.20 {
SELECT * FROM t1 order by a;
}
} {1 {t1.e may not be NULL}}
+verify_ex_errcode notnull-1.20b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-1.21 {
catchsql {
DELETE FROM t1;
@@ -190,6 +197,7 @@ do_test notnull-2.1 {
SELECT * FROM t1 ORDER BY a;
}
} {1 {t1.a may not be NULL}}
+verify_ex_errcode notnull-2.1b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-2.2 {
catchsql {
DELETE FROM t1;
@@ -198,6 +206,7 @@ do_test notnull-2.2 {
SELECT * FROM t1 ORDER BY a;
}
} {1 {t1.a may not be NULL}}
+verify_ex_errcode notnull-2.2b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-2.3 {
catchsql {
DELETE FROM t1;
@@ -214,6 +223,7 @@ do_test notnull-2.4 {
SELECT * FROM t1 ORDER BY a;
}
} {1 {t1.a may not be NULL}}
+verify_ex_errcode notnull-2.4b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-2.5 {
catchsql {
DELETE FROM t1;
@@ -222,6 +232,7 @@ do_test notnull-2.5 {
SELECT * FROM t1 ORDER BY a;
}
} {1 {t1.b may not be NULL}}
+verify_ex_errcode notnull-2.6b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-2.6 {
catchsql {
DELETE FROM t1;
@@ -262,6 +273,7 @@ do_test notnull-2.10 {
SELECT * FROM t1 ORDER BY a;
}
} {1 {t1.e may not be NULL}}
+verify_ex_errcode notnull-2.10b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-3.0 {
execsql {
@@ -287,6 +299,7 @@ do_test notnull-3.2 {
SELECT * FROM t1 order by a;
}
} {1 {t1.a may not be NULL}}
+verify_ex_errcode notnull-3.2b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-3.3 {
catchsql {
DELETE FROM t1;
@@ -301,6 +314,7 @@ do_test notnull-3.4 {
SELECT * FROM t1 order by a;
}
} {1 {t1.a may not be NULL}}
+verify_ex_errcode notnull-3.4b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-3.5 {
catchsql {
DELETE FROM t1;
@@ -308,6 +322,7 @@ do_test notnull-3.5 {
SELECT * FROM t1 order by a;
}
} {1 {t1.a may not be NULL}}
+verify_ex_errcode notnull-3.5b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-3.6 {
catchsql {
DELETE FROM t1;
@@ -343,6 +358,7 @@ do_test notnull-3.10 {
SELECT * FROM t1 order by a;
}
} {1 {t1.b may not be NULL}}
+verify_ex_errcode notnull-3.10b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-3.11 {
catchsql {
DELETE FROM t1;
@@ -385,6 +401,7 @@ do_test notnull-3.16 {
SELECT * FROM t1 order by a;
}
} {1 {t1.c may not be NULL}}
+verify_ex_errcode notnull-3.16b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-3.17 {
catchsql {
DELETE FROM t1;
@@ -392,6 +409,7 @@ do_test notnull-3.17 {
SELECT * FROM t1 order by a;
}
} {1 {t1.d may not be NULL}}
+verify_ex_errcode notnull-3.17b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-3.18 {
catchsql {
DELETE FROM t1;
@@ -413,6 +431,7 @@ do_test notnull-3.20 {
SELECT * FROM t1 order by a;
}
} {1 {t1.e may not be NULL}}
+verify_ex_errcode notnull-3.20b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-3.21 {
catchsql {
DELETE FROM t1;
@@ -429,6 +448,7 @@ do_test notnull-4.1 {
SELECT * FROM t1 ORDER BY a;
}
} {1 {t1.a may not be NULL}}
+verify_ex_errcode notnull-4.1b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-4.2 {
catchsql {
DELETE FROM t1;
@@ -437,6 +457,7 @@ do_test notnull-4.2 {
SELECT * FROM t1 ORDER BY a;
}
} {1 {t1.a may not be NULL}}
+verify_ex_errcode notnull-4.2b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-4.3 {
catchsql {
DELETE FROM t1;
@@ -453,6 +474,7 @@ do_test notnull-4.4 {
SELECT * FROM t1 ORDER BY a;
}
} {1 {t1.a may not be NULL}}
+verify_ex_errcode notnull-4.4b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-4.5 {
catchsql {
DELETE FROM t1;
@@ -461,6 +483,7 @@ do_test notnull-4.5 {
SELECT * FROM t1 ORDER BY a;
}
} {1 {t1.b may not be NULL}}
+verify_ex_errcode notnull-4.5b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-4.6 {
catchsql {
DELETE FROM t1;
@@ -501,6 +524,7 @@ do_test notnull-4.10 {
SELECT * FROM t1 ORDER BY a;
}
} {1 {t1.e may not be NULL}}
+verify_ex_errcode notnull-4.10b SQLITE_CONSTRAINT_NOTNULL
# Test that bug 29ab7be99f is fixed.
#
@@ -519,6 +543,7 @@ do_test notnull-5.2 {
INSERT INTO t1 SELECT * FROM t2;
}
} {1 {t1.b may not be NULL}}
+verify_ex_errcode notnull-5.2b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-5.3 {
execsql { SELECT * FROM t1 }
} {1 2}
@@ -531,9 +556,9 @@ do_test notnull-5.4 {
COMMIT;
}
} {1 {t1.b may not be NULL}}
+verify_ex_errcode notnull-5.4b SQLITE_CONSTRAINT_NOTNULL
do_test notnull-5.5 {
execsql { SELECT * FROM t1 }
} {1 2}
finish_test
-
diff --git a/test/numcast.test b/test/numcast.test
new file mode 100644
index 0000000..926bbe4
--- /dev/null
+++ b/test/numcast.test
@@ -0,0 +1,46 @@
+# 2013 March 20
+#
+# 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 file implements regression tests for SQLite library.
+# This particular file does testing of casting strings into numeric
+# values.
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+
+foreach enc {utf8 utf16le utf16be} {
+ do_test numcast-$enc.0 {
+ db close
+ sqlite3 db :memory:
+ db eval "PRAGMA encoding='$enc'"
+ set x [db eval {PRAGMA encoding}]
+ string map {- {}} [string tolower $x]
+ } $enc
+ foreach {idx str rval ival} {
+ 1 12345.0 12345.0 12345
+ 2 12345.0e0 12345.0 12345
+ 3 -12345.0e0 -12345.0 -12345
+ 4 -12345.25 -12345.25 -12345
+ 5 { -12345.0} -12345.0 -12345
+ 6 { 876xyz} 876.0 876
+ 7 { 456Ä·89} 456.0 456
+ 8 { Ä  321.5} 0.0 0
+ } {
+ do_test numcast-$enc.$idx.1 {
+ db eval {SELECT CAST($str AS real)}
+ } $rval
+ do_test numcast-$enc.$idx.2 {
+ db eval {SELECT CAST($str AS integer)}
+ } $ival
+ }
+}
+
+finish_test
diff --git a/test/orderby1.test b/test/orderby1.test
new file mode 100644
index 0000000..f459fc8
--- /dev/null
+++ b/test/orderby1.test
@@ -0,0 +1,438 @@
+# 2012 Sept 27
+#
+# 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 file implements regression tests for SQLite library. The
+# focus of this file is testing that the optimizations that disable
+# ORDER BY clauses when the natural order of a query is correct.
+#
+
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set ::testprefix orderby1
+
+# Generate test data for a join. Verify that the join gets the
+# correct answer.
+#
+do_test 1.0 {
+ db eval {
+ BEGIN;
+ CREATE TABLE album(
+ aid INTEGER PRIMARY KEY,
+ title TEXT UNIQUE NOT NULL
+ );
+ CREATE TABLE track(
+ tid INTEGER PRIMARY KEY,
+ aid INTEGER NOT NULL REFERENCES album,
+ tn INTEGER NOT NULL,
+ name TEXT,
+ UNIQUE(aid, tn)
+ );
+ INSERT INTO album VALUES(1, '1-one'), (2, '2-two'), (3, '3-three');
+ INSERT INTO track VALUES
+ (NULL, 1, 1, 'one-a'),
+ (NULL, 2, 2, 'two-b'),
+ (NULL, 3, 3, 'three-c'),
+ (NULL, 1, 3, 'one-c'),
+ (NULL, 2, 1, 'two-a'),
+ (NULL, 3, 1, 'three-a');
+ COMMIT;
+ }
+} {}
+do_test 1.1a {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn
+ }
+} {one-a one-c two-a two-b three-a three-c}
+
+# Verify that the ORDER BY clause is optimized out
+#
+do_test 1.1b {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn
+ }
+} {~/ORDER BY/} ;# ORDER BY optimized out
+
+# The same query with ORDER BY clause optimization disabled via + operators
+# should give exactly the same answer.
+#
+do_test 1.2a {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title, +tn
+ }
+} {one-a one-c two-a two-b three-a three-c}
+
+# The output is sorted manually in this case.
+#
+do_test 1.2b {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title, +tn
+ }
+} {/ORDER BY/} ;# separate sorting pass due to "+" on ORDER BY terms
+
+# The same query with ORDER BY optimizations turned off via built-in test.
+#
+do_test 1.3a {
+ optimization_control db order-by-idx-join 0
+ db cache flush
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn
+ }
+} {one-a one-c two-a two-b three-a three-c}
+do_test 1.3b {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn
+ }
+} {/ORDER BY/} ;# separate sorting pass due to disabled optimization
+optimization_control db all 1
+db cache flush
+
+# Reverse order sorts
+#
+do_test 1.4a {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn
+ }
+} {three-a three-c two-a two-b one-a one-c}
+do_test 1.4b {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title DESC, +tn
+ }
+} {three-a three-c two-a two-b one-a one-c} ;# verify same order after sorting
+do_test 1.4c {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn
+ }
+} {~/ORDER BY/} ;# optimized out
+
+
+do_test 1.5a {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn DESC
+ }
+} {one-c one-a two-b two-a three-c three-a}
+do_test 1.5b {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title, +tn DESC
+ }
+} {one-c one-a two-b two-a three-c three-a} ;# verify same order after sorting
+do_test 1.5c {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn DESC
+ }
+} {~/ORDER BY/} ;# optimized out
+
+do_test 1.6a {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn DESC
+ }
+} {three-c three-a two-b two-a one-c one-a}
+do_test 1.6b {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title DESC, +tn DESC
+ }
+} {three-c three-a two-b two-a one-c one-a} ;# verify same order after sorting
+do_test 1.6c {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn DESC
+ }
+} {~/ORDER BY/} ;# ORDER BY optimized-out
+
+
+# Reconstruct the test data to use indices rather than integer primary keys.
+#
+do_test 2.0 {
+ db eval {
+ BEGIN;
+ DROP TABLE album;
+ DROP TABLE track;
+ CREATE TABLE album(
+ aid INT PRIMARY KEY,
+ title TEXT NOT NULL
+ );
+ CREATE INDEX album_i1 ON album(title, aid);
+ CREATE TABLE track(
+ aid INTEGER NOT NULL REFERENCES album,
+ tn INTEGER NOT NULL,
+ name TEXT,
+ UNIQUE(aid, tn)
+ );
+ INSERT INTO album VALUES(1, '1-one'), (20, '2-two'), (3, '3-three');
+ INSERT INTO track VALUES
+ (1, 1, 'one-a'),
+ (20, 2, 'two-b'),
+ (3, 3, 'three-c'),
+ (1, 3, 'one-c'),
+ (20, 1, 'two-a'),
+ (3, 1, 'three-a');
+ COMMIT;
+ }
+} {}
+do_test 2.1a {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn
+ }
+} {one-a one-c two-a two-b three-a three-c}
+
+# Verify that the ORDER BY clause is optimized out
+#
+do_test 2.1b {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn
+ }
+} {~/ORDER BY/} ;# ORDER BY optimized out
+
+do_test 2.1c {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, aid, tn
+ }
+} {one-a one-c two-a two-b three-a three-c}
+do_test 2.1d {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, aid, tn
+ }
+} {~/ORDER BY/} ;# ORDER BY optimized out
+
+# The same query with ORDER BY clause optimization disabled via + operators
+# should give exactly the same answer.
+#
+do_test 2.2a {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title, +tn
+ }
+} {one-a one-c two-a two-b three-a three-c}
+
+# The output is sorted manually in this case.
+#
+do_test 2.2b {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title, +tn
+ }
+} {/ORDER BY/} ;# separate sorting pass due to "+" on ORDER BY terms
+
+# The same query with ORDER BY optimizations turned off via built-in test.
+#
+do_test 2.3a {
+ optimization_control db order-by-idx-join 0
+ db cache flush
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn
+ }
+} {one-a one-c two-a two-b three-a three-c}
+do_test 2.3b {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn
+ }
+} {/ORDER BY/} ;# separate sorting pass due to disabled optimization
+optimization_control db all 1
+db cache flush
+
+# Reverse order sorts
+#
+do_test 2.4a {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn
+ }
+} {three-a three-c two-a two-b one-a one-c}
+do_test 2.4b {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title DESC, +tn
+ }
+} {three-a three-c two-a two-b one-a one-c} ;# verify same order after sorting
+do_test 2.4c {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn
+ }
+} {~/ORDER BY/} ;# optimized out
+
+
+do_test 2.5a {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn DESC
+ }
+} {one-c one-a two-b two-a three-c three-a}
+do_test 2.5b {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title, +tn DESC
+ }
+} {one-c one-a two-b two-a three-c three-a} ;# verify same order after sorting
+do_test 2.5c {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn DESC
+ }
+} {~/ORDER BY/} ;# optimized out
+
+do_test 2.6a {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn DESC
+ }
+} {three-c three-a two-b two-a one-c one-a}
+do_test 2.6b {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title DESC, +tn DESC
+ }
+} {three-c three-a two-b two-a one-c one-a} ;# verify same order after sorting
+do_test 2.6c {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn DESC
+ }
+} {~/ORDER BY/} ;# ORDER BY optimized out
+
+
+# Generate another test dataset, but this time using mixed ASC/DESC indices.
+#
+do_test 3.0 {
+ db eval {
+ BEGIN;
+ DROP TABLE album;
+ DROP TABLE track;
+ CREATE TABLE album(
+ aid INTEGER PRIMARY KEY,
+ title TEXT UNIQUE NOT NULL
+ );
+ CREATE TABLE track(
+ tid INTEGER PRIMARY KEY,
+ aid INTEGER NOT NULL REFERENCES album,
+ tn INTEGER NOT NULL,
+ name TEXT,
+ UNIQUE(aid ASC, tn DESC)
+ );
+ INSERT INTO album VALUES(1, '1-one'), (2, '2-two'), (3, '3-three');
+ INSERT INTO track VALUES
+ (NULL, 1, 1, 'one-a'),
+ (NULL, 2, 2, 'two-b'),
+ (NULL, 3, 3, 'three-c'),
+ (NULL, 1, 3, 'one-c'),
+ (NULL, 2, 1, 'two-a'),
+ (NULL, 3, 1, 'three-a');
+ COMMIT;
+ }
+} {}
+do_test 3.1a {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn DESC
+ }
+} {one-c one-a two-b two-a three-c three-a}
+
+# Verify that the ORDER BY clause is optimized out
+#
+do_test 3.1b {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn DESC
+ }
+} {~/ORDER BY/} ;# ORDER BY optimized out
+
+# The same query with ORDER BY clause optimization disabled via + operators
+# should give exactly the same answer.
+#
+do_test 3.2a {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title, +tn DESC
+ }
+} {one-c one-a two-b two-a three-c three-a}
+
+# The output is sorted manually in this case.
+#
+do_test 3.2b {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title, +tn DESC
+ }
+} {/ORDER BY/} ;# separate sorting pass due to "+" on ORDER BY terms
+
+# The same query with ORDER BY optimizations turned off via built-in test.
+#
+do_test 3.3a {
+ optimization_control db order-by-idx-join 0
+ db cache flush
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn DESC
+ }
+} {one-c one-a two-b two-a three-c three-a}
+do_test 3.3b {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn DESC
+ }
+} {/ORDER BY/} ;# separate sorting pass due to disabled optimization
+optimization_control db all 1
+db cache flush
+
+# Without the mixed ASC/DESC on ORDER BY
+#
+do_test 3.4a {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn
+ }
+} {one-a one-c two-a two-b three-a three-c}
+do_test 3.4b {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title, +tn
+ }
+} {one-a one-c two-a two-b three-a three-c} ;# verify same order after sorting
+do_test 3.4c {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn
+ }
+} {~/ORDER BY/} ;# optimized out
+
+
+do_test 3.5a {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn DESC
+ }
+} {three-c three-a two-b two-a one-c one-a}
+do_test 3.5b {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title DESC, +tn DESC
+ }
+} {three-c three-a two-b two-a one-c one-a} ;# verify same order after sorting
+do_test 3.5c {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn DESC
+ }
+} {~/ORDER BY/} ;# optimzed out
+
+
+do_test 3.6a {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn
+ }
+} {three-a three-c two-a two-b one-a one-c}
+do_test 3.6b {
+ db eval {
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title DESC, +tn
+ }
+} {three-a three-c two-a two-b one-a one-c} ;# verify same order after sorting
+do_test 3.6c {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn
+ }
+} {~/ORDER BY/} ;# inverted ASC/DESC is optimized out
+
+
+finish_test
diff --git a/test/orderby2.test b/test/orderby2.test
new file mode 100644
index 0000000..cfd6477
--- /dev/null
+++ b/test/orderby2.test
@@ -0,0 +1,117 @@
+# 2012 Sept 27
+#
+# 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 file implements regression tests for SQLite library. The
+# focus of this file is testing that the optimizations that disable
+# ORDER BY clauses when the natural order of a query is correct.
+#
+
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set ::testprefix orderby2
+
+# Generate test data for a join. Verify that the join gets the
+# correct answer.
+#
+do_test 1.0 {
+ db eval {
+ CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
+ INSERT INTO t1 VALUES(1,11), (2,22);
+ CREATE TABLE t2(d, e, UNIQUE(d,e));
+ INSERT INTO t2 VALUES(10, 'ten'), (11,'eleven'), (12,'twelve'),
+ (11, 'oneteen');
+ }
+} {}
+
+do_test 1.1a {
+ db eval {
+ SELECT e FROM t1, t2 WHERE a=1 AND d=b ORDER BY d, e;
+ }
+} {eleven oneteen}
+do_test 1.1b {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT e FROM t1, t2 WHERE a=1 AND d=b ORDER BY d, e;
+ }
+} {~/ORDER BY/}
+
+do_test 1.2a {
+ db eval {
+ SELECT e FROM t1, t2 WHERE a=1 AND d=b ORDER BY e;
+ }
+} {eleven oneteen}
+do_test 1.2b {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT e FROM t1, t2 WHERE a=1 AND d=b ORDER BY e;
+ }
+} {~/ORDER BY/}
+
+do_test 1.3a {
+ db eval {
+ SELECT e, b FROM t1, t2 WHERE a=1 ORDER BY d, e;
+ }
+} {ten 11 eleven 11 oneteen 11 twelve 11}
+do_test 1.3b {
+ db eval {
+ EXPLAIN QUERY PLAN
+ SELECT e, b FROM t1, t2 WHERE a=1 ORDER BY d, e;
+ }
+} {~/ORDER BY/}
+
+# The following tests derived from TH3 test module cov1/where34.test
+#
+do_test 2.0 {
+ db eval {
+ CREATE TABLE t31(a,b); CREATE INDEX t31ab ON t31(a,b);
+ CREATE TABLE t32(c,d); CREATE INDEX t32cd ON t32(c,d);
+ CREATE TABLE t33(e,f); CREATE INDEX t33ef ON t33(e,f);
+ CREATE TABLE t34(g,h); CREATE INDEX t34gh ON t34(g,h);
+
+ INSERT INTO t31 VALUES(1,4), (2,3), (1,3);
+ INSERT INTO t32 VALUES(4,5), (3,6), (3,7), (4,8);
+ INSERT INTO t33 VALUES(5,9), (7,10), (6,11), (8,12), (8,13), (7,14);
+ INSERT INTO t34 VALUES(11,20), (10,21), (12,22), (9,23), (13,24),
+ (14,25), (12,26);
+ SELECT a||','||c||','||e||','||g FROM t31, t32, t33, t34
+ WHERE c=b AND e=d AND g=f
+ ORDER BY a ASC, c ASC, e DESC, g ASC;
+ }
+} {1,3,7,10 1,3,7,14 1,3,6,11 1,4,8,12 1,4,8,12 1,4,8,13 1,4,5,9 2,3,7,10 2,3,7,14 2,3,6,11}
+do_test 2.1 {
+ db eval {
+ SELECT a||','||c||','||e||','||g FROM t31, t32, t33, t34
+ WHERE c=b AND e=d AND g=f
+ ORDER BY +a ASC, +c ASC, +e DESC, +g ASC;
+ }
+} {1,3,7,10 1,3,7,14 1,3,6,11 1,4,8,12 1,4,8,12 1,4,8,13 1,4,5,9 2,3,7,10 2,3,7,14 2,3,6,11}
+do_test 2.2 {
+ db eval {
+ SELECT a||','||c||','||e||','||g FROM t31, t32, t33, t34
+ WHERE c=b AND e=d AND g=f
+ ORDER BY a ASC, c ASC, e ASC, g ASC;
+ }
+} {1,3,6,11 1,3,7,10 1,3,7,14 1,4,5,9 1,4,8,12 1,4,8,12 1,4,8,13 2,3,6,11 2,3,7,10 2,3,7,14}
+do_test 2.3 {
+ optimization_control db cover-idx-scan off
+ db cache flush
+ db eval {
+ SELECT a||','||c||','||e||','||g FROM t31, t32, t33, t34
+ WHERE c=b AND e=d AND g=f
+ ORDER BY a ASC, c ASC, e ASC, g ASC;
+ }
+} {1,3,6,11 1,3,7,10 1,3,7,14 1,4,5,9 1,4,8,12 1,4,8,12 1,4,8,13 2,3,6,11 2,3,7,10 2,3,7,14}
+optimization_control db all on
+db cache flush
+
+
+
+finish_test
diff --git a/test/orderby3.test b/test/orderby3.test
new file mode 100644
index 0000000..f005f0d
--- /dev/null
+++ b/test/orderby3.test
@@ -0,0 +1,123 @@
+# 2013 January 09
+#
+# 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 file implements regression tests for SQLite library. The
+# focus of this file is testing that the optimizations that disable
+# ORDER BY clauses work correctly on a 3-way join. See ticket
+# http://www.sqlite.org/src/956e4d7f89
+#
+
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set ::testprefix orderby3
+
+# Generate test data for a join. Verify that the join gets the
+# correct answer.
+#
+do_execsql_test 1.0 {
+ CREATE TABLE t1(a INTEGER PRIMARY KEY);
+ CREATE TABLE t2(b INTEGER PRIMARY KEY, c INTEGER);
+ CREATE TABLE t3(d INTEGER);
+
+ INSERT INTO t1 VALUES(1),(2),(3);
+
+ INSERT INTO t2 VALUES(3, 1);
+ INSERT INTO t2 VALUES(4, 2);
+ INSERT INTO t2 VALUES(5, 3);
+
+ INSERT INTO t3 VALUES(4),(3),(5);
+} {}
+do_execsql_test 1.1.asc {
+ SELECT t1.a
+ FROM t1, t2, t3
+ WHERE t1.a=t2.c AND t2.b=t3.d
+ ORDER BY t1.a;
+} {1 2 3}
+do_execsql_test 1.1.desc {
+ SELECT t1.a
+ FROM t1, t2, t3
+ WHERE t1.a=t2.c AND t2.b=t3.d
+ ORDER BY t1.a DESC;
+} {3 2 1}
+do_execsql_test 1.123.asc {
+ SELECT t1.a
+ FROM t1 CROSS JOIN t2 CROSS JOIN t3
+ WHERE t1.a=t2.c AND t2.b=t3.d
+ ORDER BY t1.a;
+} {1 2 3}
+do_execsql_test 1.123.desc {
+ SELECT t1.a
+ FROM t1 CROSS JOIN t2 CROSS JOIN t3
+ WHERE t1.a=t2.c AND t2.b=t3.d
+ ORDER BY t1.a DESC;
+} {3 2 1}
+do_execsql_test 1.132.asc {
+ SELECT t1.a
+ FROM t1 CROSS JOIN t3 CROSS JOIN t2
+ WHERE t1.a=t2.c AND t2.b=t3.d
+ ORDER BY t1.a;
+} {1 2 3}
+do_execsql_test 1.132.desc {
+ SELECT t1.a
+ FROM t1 CROSS JOIN t3 CROSS JOIN t2
+ WHERE t1.a=t2.c AND t2.b=t3.d
+ ORDER BY t1.a DESC;
+} {3 2 1}
+do_execsql_test 1.213.asc {
+ SELECT t1.a
+ FROM t2 CROSS JOIN t1 CROSS JOIN t3
+ WHERE t1.a=t2.c AND t2.b=t3.d
+ ORDER BY t1.a;
+} {1 2 3}
+do_execsql_test 1.213.desc {
+ SELECT t1.a
+ FROM t2 CROSS JOIN t1 CROSS JOIN t3
+ WHERE t1.a=t2.c AND t2.b=t3.d
+ ORDER BY t1.a DESC;
+} {3 2 1}
+do_execsql_test 1.231.asc {
+ SELECT t1.a
+ FROM t2 CROSS JOIN t3 CROSS JOIN t1
+ WHERE t1.a=t2.c AND t2.b=t3.d
+ ORDER BY t1.a;
+} {1 2 3}
+do_execsql_test 1.231.desc {
+ SELECT t1.a
+ FROM t2 CROSS JOIN t3 CROSS JOIN t1
+ WHERE t1.a=t2.c AND t2.b=t3.d
+ ORDER BY t1.a DESC;
+} {3 2 1}
+do_execsql_test 1.312.asc {
+ SELECT t1.a
+ FROM t3 CROSS JOIN t1 CROSS JOIN t2
+ WHERE t1.a=t2.c AND t2.b=t3.d
+ ORDER BY t1.a;
+} {1 2 3}
+do_execsql_test 1.312.desc {
+ SELECT t1.a
+ FROM t3 CROSS JOIN t1 CROSS JOIN t2
+ WHERE t1.a=t2.c AND t2.b=t3.d
+ ORDER BY t1.a DESC;
+} {3 2 1}
+do_execsql_test 1.321.asc {
+ SELECT t1.a
+ FROM t3 CROSS JOIN t2 CROSS JOIN t1
+ WHERE t1.a=t2.c AND t2.b=t3.d
+ ORDER BY t1.a;
+} {1 2 3}
+do_execsql_test 1.321.desc {
+ SELECT t1.a
+ FROM t3 CROSS JOIN t2 CROSS JOIN t1
+ WHERE t1.a=t2.c AND t2.b=t3.d
+ ORDER BY t1.a DESC;
+} {3 2 1}
+
+finish_test
diff --git a/test/orderby4.test b/test/orderby4.test
new file mode 100644
index 0000000..ec6eb04
--- /dev/null
+++ b/test/orderby4.test
@@ -0,0 +1,56 @@
+# 2013 March 26
+#
+# 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 file implements regression tests for SQLite library. The
+# focus of this file is testing that the optimizations that disable
+# ORDER BY clauses work correctly on multi-value primary keys and
+# unique indices when only some prefix of the terms in the key are
+# used. See ticket http://www.sqlite.org/src/info/a179fe74659
+#
+
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set ::testprefix orderby4
+
+# Generate test data for a join. Verify that the join gets the
+# correct answer.
+#
+do_execsql_test 1.1 {
+ CREATE TABLE t1(a, b, PRIMARY KEY(a,b));
+ INSERT INTO t1 VALUES(1,1),(1,2);
+ CREATE TABLE t2(x, y, PRIMARY KEY(x,y));
+ INSERT INTO t2 VALUES(3,3),(4,4);
+ SELECT a, x FROM t1, t2 ORDER BY 1, 2;
+} {1 3 1 3 1 4 1 4}
+do_execsql_test 1.2 {
+ SELECT a, x FROM t1 CROSS JOIN t2 ORDER BY 1, 2;
+} {1 3 1 3 1 4 1 4}
+do_execsql_test 1.3 {
+ SELECT a, x FROM t2 CROSS JOIN t1 ORDER BY 1, 2;
+} {1 3 1 3 1 4 1 4}
+
+do_execsql_test 2.1 {
+ CREATE TABLE t3(a);
+ INSERT INTO t3 VALUES(1),(1);
+ CREATE INDEX t3a ON t3(a);
+ CREATE TABLE t4(x);
+ INSERT INTO t4 VALUES(3),(4);
+ CREATE INDEX t4x ON t4(x);
+ SELECT a, x FROM t3, t4 ORDER BY 1, 2;
+} {1 3 1 3 1 4 1 4}
+do_execsql_test 2.2 {
+ SELECT a, x FROM t3 CROSS JOIN t4 ORDER BY 1, 2;
+} {1 3 1 3 1 4 1 4}
+do_execsql_test 2.3 {
+ SELECT a, x FROM t4 CROSS JOIN t3 ORDER BY 1, 2;
+} {1 3 1 3 1 4 1 4}
+
+finish_test
diff --git a/test/pager1.test b/test/pager1.test
index 61a0c0c..72e4780 100644
--- a/test/pager1.test
+++ b/test/pager1.test
@@ -15,6 +15,7 @@ source $testdir/tester.tcl
source $testdir/lock_common.tcl
source $testdir/malloc_common.tcl
source $testdir/wal_common.tcl
+set testprefix pager1
# 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).
@@ -752,7 +753,7 @@ sqlite3 db test.db -readonly 1
do_catchsql_test pager1.4.5.6 {
SELECT * FROM t1;
SELECT * FROM t2;
-} {1 {disk I/O error}}
+} {1 {attempt to write a readonly database}}
db close
# Snapshot the file-system just before multi-file commit. Save the name
@@ -883,12 +884,20 @@ do_execsql_test pager1.4.7.1 {
tv filter {}
db close
tv delete
+catch {
+ test_syscall install fchmod
+ test_syscall fault 1 1
+}
do_test pager1.4.7.2 {
faultsim_restore_and_reopen
catch {file attributes test.db-journal -permissions r--------}
catch {file attributes test.db-journal -readonly 1}
catchsql { SELECT * FROM t1 }
} {1 {unable to open database file}}
+catch {
+ test_syscall reset
+ test_syscall fault 0 0
+}
do_test pager1.4.7.3 {
db close
catch {file attributes test.db-journal -permissions rw-rw-rw-}
@@ -1365,7 +1374,7 @@ do_test pager1-9.4.1 {
} {SQLITE_DONE SQLITE_OK}
do_test pager1-9.4.2 {
list [file size test.db2] [file size test.db]
-} {0 0}
+} {1024 0}
db2 close
#-------------------------------------------------------------------------
@@ -1374,6 +1383,7 @@ db2 close
#
testvfs tv -default 1
foreach sectorsize {
+ 16
32 64 128 256 512 1024 2048
4096 8192 16384 32768 65536 131072 262144
} {
@@ -1396,7 +1406,7 @@ foreach sectorsize {
COMMIT;
}
file size test.db-journal
- } [expr $sectorsize > 65536 ? 65536 : $sectorsize]
+ } [expr $sectorsize > 65536 ? 65536 : ($sectorsize<32 ? 512 : $sectorsize)]
do_test pager1-10.$sectorsize.2 {
execsql {
@@ -2235,7 +2245,6 @@ do_test pager1-25-1 {
}
db close
} {}
-breakpoint
do_test pager1-25-2 {
faultsim_delete_and_reopen
execsql {
@@ -2487,4 +2496,323 @@ do_test pager1-32.1 {
# Cleanup 20MB file left by the previous test.
forcedelete test.db
+#-------------------------------------------------------------------------
+# Test that if a transaction is committed in journal_mode=DELETE mode,
+# and the call to unlink() returns an ENOENT error, the COMMIT does not
+# succeed.
+#
+if {$::tcl_platform(platform)=="unix"} {
+ do_test pager1-33.1 {
+ sqlite3 db test.db
+ execsql {
+ CREATE TABLE t1(x);
+ INSERT INTO t1 VALUES('one');
+ INSERT INTO t1 VALUES('two');
+ BEGIN;
+ INSERT INTO t1 VALUES('three');
+ INSERT INTO t1 VALUES('four');
+ }
+ forcedelete bak-journal
+ file rename test.db-journal bak-journal
+
+ catchsql COMMIT
+ } {1 {disk I/O error}}
+
+ do_test pager1-33.2 {
+ file rename bak-journal test.db-journal
+ execsql { SELECT * FROM t1 }
+ } {one two}
+}
+
+#-------------------------------------------------------------------------
+# Test that appending pages to the database file then moving those pages
+# to the free-list before the transaction is committed does not cause
+# an error.
+#
+foreach {tn pragma strsize} {
+ 1 { PRAGMA mmap_size = 0 } 2400
+ 2 { } 2400
+ 3 { PRAGMA mmap_size = 0 } 4400
+ 4 { } 4400
+} {
+ reset_db
+ db func a_string a_string
+ db eval $pragma
+ do_execsql_test 34.$tn.1 {
+ CREATE TABLE t1(a, b);
+ INSERT INTO t1 VALUES(1, 2);
+ }
+ do_execsql_test 34.$tn.2 {
+ BEGIN;
+ INSERT INTO t1 VALUES(2, a_string($strsize));
+ DELETE FROM t1 WHERE oid=2;
+ COMMIT;
+ PRAGMA integrity_check;
+ } {ok}
+}
+
+#-------------------------------------------------------------------------
+#
+reset_db
+do_test 35 {
+ sqlite3 db test.db
+
+ execsql {
+ CREATE TABLE t1(x, y);
+ PRAGMA journal_mode = WAL;
+ INSERT INTO t1 VALUES(1, 2);
+ }
+
+ execsql {
+ BEGIN;
+ CREATE TABLE t2(a, b);
+ }
+
+ hexio_write test.db-shm [expr 16*1024] [string repeat 0055 8192]
+ catchsql ROLLBACK
+} {0 {}}
+
+do_multiclient_test tn {
+ sql1 {
+ PRAGMA auto_vacuum = 0;
+ CREATE TABLE t1(x, y);
+ INSERT INTO t1 VALUES(1, 2);
+ }
+
+ do_test 36.$tn.1 {
+ sql2 { PRAGMA max_page_count = 2 }
+ list [catch { sql2 { CREATE TABLE t2(x) } } msg] $msg
+ } {1 {database or disk is full}}
+
+ sql1 { PRAGMA checkpoint_fullfsync = 1 }
+ sql1 { CREATE TABLE t2(x) }
+
+ do_test 36.$tn.2 {
+ sql2 { INSERT INTO t2 VALUES('xyz') }
+ list [catch { sql2 { CREATE TABLE t3(x) } } msg] $msg
+ } {1 {database or disk is full}}
+}
+
+forcedelete test1 test2
+foreach {tn uri} {
+ 1 {file:?mode=memory&cache=shared}
+ 2 {file:one?mode=memory&cache=shared}
+ 3 {file:test1?cache=shared}
+ 4 {file:test2?another=parameter&yet=anotherone}
+} {
+ do_test 37.$tn {
+ catch { db close }
+ sqlite3_shutdown
+ sqlite3_config_uri 1
+ sqlite3 db $uri
+
+ db eval {
+ CREATE TABLE t1(x);
+ INSERT INTO t1 VALUES(1);
+ SELECT * FROM t1;
+ }
+ } {1}
+
+ do_execsql_test 37.$tn.2 {
+ VACUUM;
+ SELECT * FROM t1;
+ } {1}
+
+ db close
+ sqlite3_shutdown
+ sqlite3_config_uri 0
+}
+
+do_test 38.1 {
+ catch { db close }
+ forcedelete test.db
+ set fd [open test.db w]
+ puts $fd "hello world"
+ close $fd
+ sqlite3 db test.db
+ catchsql { CREATE TABLE t1(x) }
+} {1 {file is encrypted or is not a database}}
+do_test 38.2 {
+ catch { db close }
+ forcedelete test.db
+} {}
+
+do_test 39.1 {
+ sqlite3 db test.db
+ execsql {
+ PRAGMA auto_vacuum = 1;
+ CREATE TABLE t1(x);
+ INSERT INTO t1 VALUES('xxx');
+ INSERT INTO t1 VALUES('two');
+ INSERT INTO t1 VALUES(randomblob(400));
+ INSERT INTO t1 VALUES(randomblob(400));
+ INSERT INTO t1 VALUES(randomblob(400));
+ INSERT INTO t1 VALUES(randomblob(400));
+ BEGIN;
+ UPDATE t1 SET x = 'one' WHERE rowid=1;
+ }
+ set ::stmt [sqlite3_prepare db "SELECT * FROM t1 ORDER BY rowid" -1 dummy]
+ sqlite3_step $::stmt
+ sqlite3_column_text $::stmt 0
+} {one}
+do_test 39.2 {
+ execsql { CREATE TABLE t2(x) }
+ sqlite3_step $::stmt
+ sqlite3_column_text $::stmt 0
+} {two}
+do_test 39.3 {
+ sqlite3_finalize $::stmt
+ execsql COMMIT
+} {}
+
+do_execsql_test 39.4 {
+ PRAGMA auto_vacuum = 2;
+ CREATE TABLE t3(x);
+ CREATE TABLE t4(x);
+
+ DROP TABLE t2;
+ DROP TABLE t3;
+ DROP TABLE t4;
+}
+do_test 39.5 {
+ db close
+ sqlite3 db test.db
+ execsql {
+ PRAGMA cache_size = 1;
+ PRAGMA incremental_vacuum;
+ PRAGMA integrity_check;
+ }
+} {ok}
+
+do_test 40.1 {
+ reset_db
+ execsql {
+ PRAGMA auto_vacuum = 1;
+ CREATE TABLE t1(x PRIMARY KEY);
+ INSERT INTO t1 VALUES(randomblob(1200));
+ PRAGMA page_count;
+ }
+} {6}
+do_test 40.2 {
+ execsql {
+ INSERT INTO t1 VALUES(randomblob(1200));
+ INSERT INTO t1 VALUES(randomblob(1200));
+ INSERT INTO t1 VALUES(randomblob(1200));
+ }
+} {}
+do_test 40.3 {
+ db close
+ sqlite3 db test.db
+ execsql {
+ PRAGMA cache_size = 1;
+ CREATE TABLE t2(x);
+ PRAGMA integrity_check;
+ }
+} {ok}
+
+do_test 41.1 {
+ reset_db
+ execsql {
+ CREATE TABLE t1(x PRIMARY KEY);
+ INSERT INTO t1 VALUES(randomblob(200));
+ INSERT INTO t1 SELECT randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200) FROM t1;
+ }
+} {}
+do_test 41.2 {
+ testvfs tv -default 1
+ tv sectorsize 16384;
+ tv devchar [list]
+ db close
+ sqlite3 db test.db
+ execsql {
+ PRAGMA cache_size = 1;
+ DELETE FROM t1 WHERE rowid%4;
+ PRAGMA integrity_check;
+ }
+} {ok}
+db close
+tv delete
+
+set pending_prev [sqlite3_test_control_pending_byte 0x1000000]
+do_test 42.1 {
+ reset_db
+ execsql {
+ CREATE TABLE t1(x, y);
+ INSERT INTO t1 VALUES(randomblob(200), randomblob(200));
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ }
+ db close
+ sqlite3_test_control_pending_byte 0x0010000
+ sqlite3 db test.db
+ db eval { PRAGMA mmap_size = 0 }
+ catchsql { SELECT sum(length(y)) FROM t1 }
+} {1 {database disk image is malformed}}
+do_test 42.2 {
+ reset_db
+ execsql {
+ CREATE TABLE t1(x, y);
+ INSERT INTO t1 VALUES(randomblob(200), randomblob(200));
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ }
+ db close
+
+ testvfs tv -default 1
+ tv sectorsize 16384;
+ tv devchar [list]
+ sqlite3 db test.db -vfs tv
+ execsql { UPDATE t1 SET x = randomblob(200) }
+} {}
+db close
+tv delete
+sqlite3_test_control_pending_byte $pending_prev
+
+do_test 43.1 {
+ reset_db
+ execsql {
+ CREATE TABLE t1(x, y);
+ INSERT INTO t1 VALUES(1, 2);
+ CREATE TABLE t2(x, y);
+ INSERT INTO t2 VALUES(1, 2);
+ CREATE TABLE t3(x, y);
+ INSERT INTO t3 VALUES(1, 2);
+ }
+ db close
+ sqlite3 db test.db
+
+ db eval { PRAGMA mmap_size = 0 }
+ db eval { SELECT * FROM t1 }
+ sqlite3_db_status db CACHE_MISS 0
+} {0 2 0}
+
+do_test 43.2 {
+ db eval { SELECT * FROM t2 }
+ sqlite3_db_status db CACHE_MISS 1
+} {0 3 0}
+
+do_test 43.3 {
+ db eval { SELECT * FROM t3 }
+ sqlite3_db_status db CACHE_MISS 0
+} {0 1 0}
+
finish_test
+
diff --git a/test/pager2.test b/test/pager2.test
index fa5f7b9..0e2b33b 100644
--- a/test/pager2.test
+++ b/test/pager2.test
@@ -118,7 +118,6 @@ tv delete
#-------------------------------------------------------------------------
-#
# pager2-2.1: Test a ROLLBACK with journal_mode=off.
# pager2-2.2: Test shrinking the database (auto-vacuum) with
# journal_mode=off
@@ -148,4 +147,22 @@ do_test pager2-2.2 {
file size test.db
} {3072}
+#-------------------------------------------------------------------------
+# Test that shared in-memory databases seem to work.
+#
+db close
+do_test pager2-3.1 {
+ forcedelete test.db
+ sqlite3_shutdown
+ sqlite3_config_uri 1
+
+ sqlite3 db1 {file:test.db?mode=memory&cache=shared}
+ sqlite3 db2 {file:test.db?mode=memory&cache=shared}
+ sqlite3 db3 test.db
+
+ db1 eval { CREATE TABLE t1(a, b) }
+ db2 eval { INSERT INTO t1 VALUES(1, 2) }
+ list [catch { db3 eval { INSERT INTO t1 VALUES(3, 4) } } msg] $msg
+} {1 {no such table: t1}}
+
finish_test
diff --git a/test/pagerfault.test b/test/pagerfault.test
index e04e97e..23754fa 100644
--- a/test/pagerfault.test
+++ b/test/pagerfault.test
@@ -1246,5 +1246,304 @@ do_faultsim_test pagerfault-27 -faults ioerr-persistent -prep {
faultsim_integrity_check
}
+
+#-------------------------------------------------------------------------
+#
+do_test pagerfault-28-pre {
+ faultsim_delete_and_reopen
+ db func a_string a_string
+ execsql {
+ PRAGMA page_size = 512;
+
+ PRAGMA journal_mode = wal;
+ PRAGMA wal_autocheckpoint = 0;
+ PRAGMA cache_size = 100000;
+
+ BEGIN;
+ CREATE TABLE t2(a UNIQUE, b UNIQUE);
+ INSERT INTO t2 VALUES( a_string(800), a_string(800) );
+ INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
+ INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
+ INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
+ INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
+ INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
+ INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
+ INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
+ INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
+ INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
+ INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
+ INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
+ COMMIT;
+ CREATE TABLE t1(a PRIMARY KEY, b);
+ }
+ expr {[file size test.db-shm] >= 96*1024}
+} {1}
+faultsim_save_and_close
+
+do_faultsim_test pagerfault-28a -faults oom* -prep {
+ faultsim_restore_and_reopen
+ execsql { PRAGMA mmap_size=0 }
+
+ sqlite3 db2 test.db
+ db2 eval { SELECT count(*) FROM t2 }
+
+ db func a_string a_string
+ execsql {
+ BEGIN;
+ INSERT INTO t1 VALUES(a_string(2000), a_string(2000));
+ INSERT INTO t1 VALUES(a_string(2000), a_string(2000));
+ }
+ set ::STMT [sqlite3_prepare db "SELECT * FROM t1 ORDER BY a" -1 DUMMY]
+ sqlite3_step $::STMT
+} -body {
+ execsql { ROLLBACK }
+} -test {
+ db2 close
+ sqlite3_finalize $::STMT
+ catchsql { ROLLBACK }
+ faultsim_integrity_check
+}
+
+faultsim_restore_and_reopen
+sqlite3 db2 test.db
+db2 eval {SELECT count(*) FROM t2}
+db close
+
+do_faultsim_test pagerfault-28b -faults oom* -prep {
+ sqlite3 db test.db
+} -body {
+ execsql { SELECT count(*) FROM t2 }
+} -test {
+ faultsim_test_result {0 2048}
+ db close
+}
+
+db2 close
+
+#-------------------------------------------------------------------------
+# Try this:
+#
+# 1) Put the pager in ERROR state (error during rollback)
+#
+# 2) Next time the connection is used inject errors into all xWrite() and
+# xUnlock() calls. This causes the hot-journal rollback to fail and
+# the pager to declare its locking state UNKNOWN.
+#
+# 3) Same again.
+#
+# 4a) Stop injecting errors. Allow the rollback to succeed. Check that
+# the database is Ok. Or,
+#
+# 4b) Close and reopen the db. Check that the db is Ok.
+#
+proc custom_injectinstall {} {
+ testvfs custom -default true
+ custom filter {xWrite xUnlock}
+}
+proc custom_injectuninstall {} {
+ catch {db close}
+ catch {db2 close}
+ custom delete
+}
+proc custom_injectstart {iFail} {
+ custom ioerr $iFail 1
+}
+proc custom_injectstop {} {
+ custom ioerr
+}
+set ::FAULTSIM(custom) [list \
+ -injectinstall custom_injectinstall \
+ -injectstart custom_injectstart \
+ -injectstop custom_injectstop \
+ -injecterrlist {{1 {disk I/O error}}} \
+ -injectuninstall custom_injectuninstall \
+]
+
+do_test pagerfault-29-pre {
+ faultsim_delete_and_reopen
+ db func a_string a_string
+ execsql {
+ PRAGMA page_size = 1024;
+ PRAGMA cache_size = 5;
+
+ BEGIN;
+ CREATE TABLE t2(a UNIQUE, b UNIQUE);
+ INSERT INTO t2 VALUES( a_string(800), a_string(800) );
+ INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
+ INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
+ INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
+ INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
+ COMMIT;
+ }
+ expr {[file size test.db] >= 50*1024}
+} {1}
+faultsim_save_and_close
+foreach {tn tt} {
+ 29 { catchsql ROLLBACK }
+ 30 { db close ; sqlite3 db test.db }
+} {
+ do_faultsim_test pagerfault-$tn -faults custom -prep {
+ faultsim_restore_and_reopen
+ db func a_string a_string
+ execsql {
+ PRAGMA cache_size = 5;
+ BEGIN;
+ UPDATE t2 SET a = a_string(799);
+ }
+ } -body {
+ catchsql ROLLBACK
+ catchsql ROLLBACK
+ catchsql ROLLBACK
+ } -test {
+ eval $::tt
+ if {"ok" != [db one {PRAGMA integrity_check}]} {
+ error "integrity check failed"
+ }
+ }
+}
+
+do_test pagerfault-31-pre {
+ sqlite3_shutdown
+ sqlite3_config_uri 1
+} {SQLITE_OK}
+do_faultsim_test pagerfault-31 -faults oom* -body {
+ sqlite3 db {file:one?mode=memory&cache=shared}
+ db eval {
+ CREATE TABLE t1(x);
+ INSERT INTO t1 VALUES(1);
+ SELECT * FROM t1;
+ }
+} -test {
+ faultsim_test_result {0 1} {1 {}}
+ catch { db close }
+}
+sqlite3_shutdown
+sqlite3_config_uri 0
+
+do_test pagerfault-32-pre {
+ reset_db
+ execsql {
+ CREATE TABLE t1(x);
+ INSERT INTO t1 VALUES('one');
+ }
+} {}
+faultsim_save_and_close
+
+do_faultsim_test pagerfault-32 -prep {
+ faultsim_restore_and_reopen
+ db eval { SELECT * FROM t1; }
+} -body {
+ execsql { SELECT * FROM t1; }
+} -test {
+ faultsim_test_result {0 one}
+}
+sqlite3_shutdown
+sqlite3_config_uri 0
+
+do_faultsim_test pagerfault-33a -prep {
+ sqlite3 db :memory:
+ execsql {
+ CREATE TABLE t1(a, b);
+ INSERT INTO t1 VALUES(1, 2);
+ }
+} -body {
+ execsql { VACUUM }
+} -test {
+ faultsim_test_result {0 {}}
+}
+do_faultsim_test pagerfault-33b -prep {
+ sqlite3 db ""
+ execsql {
+ CREATE TABLE t1(a, b);
+ INSERT INTO t1 VALUES(1, 2);
+ }
+} -body {
+ execsql { VACUUM }
+} -test {
+ faultsim_test_result {0 {}}
+}
+
+do_test pagerfault-34-pre {
+ reset_db
+ execsql {
+ CREATE TABLE t1(x PRIMARY KEY);
+ }
+} {}
+faultsim_save_and_close
+do_faultsim_test pagerfault-34 -prep {
+ faultsim_restore_and_reopen
+ execsql {
+ BEGIN;
+ INSERT INTO t1 VALUES( randomblob(4000) );
+ DELETE FROM t1;
+ }
+} -body {
+ execsql COMMIT
+} -test {
+ faultsim_test_result {0 {}}
+}
+
+do_test pagerfault-35-pre {
+ faultsim_delete_and_reopen
+ execsql {
+ CREATE TABLE t1(x PRIMARY KEY, y);
+ INSERT INTO t1 VALUES(randomblob(200), randomblob(200));
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ }
+ faultsim_save_and_close
+} {}
+testvfs tv -default 1
+tv sectorsize 8192;
+tv devchar [list]
+do_faultsim_test pagerfault-35 -prep {
+ faultsim_restore_and_reopen
+} -body {
+ execsql { UPDATE t1 SET x=randomblob(200) }
+} -test {
+ faultsim_test_result {0 {}}
+}
+catch {db close}
+tv delete
+
+sqlite3_shutdown
+sqlite3_config_uri 1
+do_test pagerfault-36-pre {
+ faultsim_delete_and_reopen
+ execsql {
+ CREATE TABLE t1(x PRIMARY KEY, y);
+ INSERT INTO t1 VALUES(randomblob(200), randomblob(200));
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
+ }
+ faultsim_save_and_close
+} {}
+do_faultsim_test pagerfault-36 -prep {
+ faultsim_restore
+ sqlite3 db file:test.db?cache=shared
+ sqlite3 db2 file:test.db?cache=shared
+ db2 eval {
+ BEGIN;
+ SELECT count(*) FROM sqlite_master;
+ }
+ db eval {
+ PRAGMA cache_size = 1;
+ BEGIN;
+ UPDATE t1 SET x = randomblob(200);
+ }
+} -body {
+ execsql ROLLBACK db
+} -test {
+ catch { db eval {UPDATE t1 SET x = randomblob(200)} }
+ faultsim_test_result {0 {}}
+ catch { db close }
+ catch { db2 close }
+}
+
+sqlite3_shutdown
+sqlite3_config_uri 0
+
finish_test
diff --git a/test/pageropt.test b/test/pageropt.test
index 8231196..7191661 100644
--- a/test/pageropt.test
+++ b/test/pageropt.test
@@ -87,12 +87,17 @@ do_test pageropt-1.4 {
# But if the other thread modifies the database, then the cache
# must refill.
#
+ifcapable mmap {
+ set x [expr {[permutation]=="mmap" ? 1 : 6}]
+} else {
+ set x 6
+}
do_test pageropt-1.5 {
db2 eval {CREATE TABLE t2(y)}
pagercount_sql {
SELECT hex(x) FROM t1
}
-} [list 6 0 0 $blobcontent]
+} [list $x 0 0 $blobcontent]
do_test pageropt-1.6 {
pagercount_sql {
SELECT hex(x) FROM t1
diff --git a/test/permutations.test b/test/permutations.test
index 2ff77f9..bc3ceb8 100644
--- a/test/permutations.test
+++ b/test/permutations.test
@@ -96,7 +96,7 @@ if {$::tcl_platform(platform)!="unix"} {
set alltests [test_set $alltests -exclude {
all.test async.test quick.test veryquick.test
memleak.test permutations.test soak.test fts3.test
- mallocAll.test rtree.test
+ mallocAll.test rtree.test full.test
}]
set allquicktests [test_set $alltests -exclude {
@@ -138,6 +138,14 @@ test_suite "veryquick" -prefix "" -description {
test_set $allquicktests -exclude *malloc* *ioerr* *fault*
]
+test_suite "mmap" -prefix "mm-" -description {
+ Similar to veryquick. Except with memory mapping disabled.
+} -presql {
+ pragma mmap_size = 268435456;
+} -files [
+ test_set $allquicktests -exclude *malloc* *ioerr* *fault* -include malloc.test
+]
+
test_suite "valgrind" -prefix "" -description {
Run the "veryquick" test suite with a couple of multi-process tests (that
fail under valgrind) omitted.
@@ -178,6 +186,7 @@ test_suite "fts3" -prefix "" -description {
fts3ak.test fts3al.test fts3am.test fts3an.test fts3ao.test
fts3atoken.test fts3b.test fts3c.test fts3cov.test fts3d.test
fts3defer.test fts3defer2.test fts3e.test fts3expr.test fts3expr2.test
+ fts3expr3.test
fts3near.test fts3query.test fts3shared.test fts3snippet.test
fts3sort.test
fts3fault.test fts3malloc.test fts3matchinfo.test
@@ -234,10 +243,14 @@ lappend ::testsuitelist xxx
# Run some tests using pre-allocated page and scratch blocks.
#
+# mmap1.test is excluded because a good number of its tests depend on
+# the page-cache being larger than the database. But this permutation
+# causes the effective limit on the page-cache to be just 24 pages.
+#
test_suite "memsubsys1" -description {
Tests using pre-allocated page and scratch blocks
} -files [
- test_set $::allquicktests -exclude ioerr5.test malloc5.test
+ test_set $::allquicktests -exclude ioerr5.test malloc5.test mmap1.test
] -initialize {
catch {db close}
sqlite3_shutdown
diff --git a/test/pragma.test b/test/pragma.test
index 3c8d23a..a6d198e 100644
--- a/test/pragma.test
+++ b/test/pragma.test
@@ -534,12 +534,20 @@ do_test pragma-6.2.2 {
b DEFAULT (5+3),
c TEXT,
d INTEGER DEFAULT NULL,
- e TEXT DEFAULT ''
+ e TEXT DEFAULT '',
+ UNIQUE(b,c,d),
+ PRIMARY KEY(e,b,c)
);
PRAGMA table_info(t5);
}
-} {0 a TEXT 0 CURRENT_TIMESTAMP 0 1 b {} 0 5+3 0 2 c TEXT 0 <<NULL>> 0 3 d INTEGER 0 NULL 0 4 e TEXT 0 '' 0}
+} {0 a TEXT 0 CURRENT_TIMESTAMP 0 1 b {} 0 5+3 2 2 c TEXT 0 <<NULL>> 3 3 d INTEGER 0 NULL 0 4 e TEXT 0 '' 1}
db nullvalue {}
+do_test pragma-6.2.3 {
+ execsql {
+ CREATE TABLE t2_3(a,b INTEGER PRIMARY KEY,c);
+ pragma table_info(t2_3)
+ }
+} {0 a {} 0 {} 0 1 b INTEGER 0 {} 1 2 c {} 0 {} 0}
ifcapable {foreignkey} {
do_test pragma-6.3.1 {
execsql {
@@ -928,6 +936,16 @@ proc check_temp_store {} {
return "unknown"
}
+# Application_ID
+#
+do_test pragma-8.3.1 {
+ execsql {
+ PRAGMA application_id;
+ }
+} {0}
+do_test pragma-8.3.2 {
+ execsql {PRAGMA Application_ID(12345); PRAGMA application_id;}
+} {12345}
# Test temp_store and temp_store_directory pragmas
#
@@ -1618,6 +1636,48 @@ do_test 22.4.3 {
execsql { PRAGMA aux.integrity_check; }
} {ok}
-finish_test
-
+db close
+forcedelete test.db test.db-wal test.db-journal
+sqlite3 db test.db
+sqlite3 db2 test.db
+do_test 23.1 {
+ db eval {
+ CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c,d);
+ CREATE INDEX i1 ON t1(b,c);
+ CREATE INDEX i2 ON t1(c,d);
+ CREATE TABLE t2(x INTEGER REFERENCES t1);
+ }
+ db2 eval {SELECT name FROM sqlite_master}
+} {t1 i1 i2 t2}
+do_test 23.2 {
+ db eval {
+ DROP INDEX i2;
+ CREATE INDEX i2 ON t1(c,d,b);
+ }
+ db2 eval {PRAGMA index_info(i2)}
+} {0 2 c 1 3 d 2 1 b}
+do_test 23.3 {
+ db eval {
+ CREATE INDEX i3 ON t1(d,b,c);
+ }
+ db2 eval {PRAGMA index_list(t1)}
+} {0 i3 0 1 i2 0 2 i1 0}
+do_test 23.4 {
+ db eval {
+ ALTER TABLE t1 ADD COLUMN e;
+ }
+ db2 eval {
+ PRAGMA table_info(t1);
+ }
+} {/4 e {} 0 {} 0/}
+do_test 23.5 {
+ db eval {
+ DROP TABLE t2;
+ CREATE TABLE t2(x, y INTEGER REFERENCES t1);
+ }
+ db2 eval {
+ PRAGMA foreign_key_list(t2);
+ }
+} {0 0 t1 y {} {NO ACTION} {NO ACTION} NONE}
+finish_test
diff --git a/test/regexp1.test b/test/regexp1.test
new file mode 100644
index 0000000..0e63cd9
--- /dev/null
+++ b/test/regexp1.test
@@ -0,0 +1,211 @@
+# 2012 December 31
+#
+# 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 file implements test for the REGEXP operator in test_regexp.c.
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+
+do_test regexp1-1.1 {
+ load_static_extension db regexp
+ db eval {
+ CREATE TABLE t1(x INTEGER PRIMARY KEY, y TEXT);
+ INSERT INTO t1 VALUES(1, 'For since by man came death,');
+ INSERT INTO t1 VALUES(2, 'by man came also the resurrection of the dead.');
+ INSERT INTO t1 VALUES(3, 'For as in Adam all die,');
+ INSERT INTO t1 VALUES(4, 'even so in Christ shall all be made alive.');
+
+ SELECT x FROM t1 WHERE y REGEXP '^For ' ORDER BY x;
+ }
+} {1 3}
+
+do_execsql_test regexp1-1.2 {
+ SELECT x FROM t1 WHERE y REGEXP 'by|in' ORDER BY x;
+} {1 2 3 4}
+do_execsql_test regexp1-1.3 {
+ SELECT x FROM t1 WHERE y REGEXP 'by|Christ' ORDER BY x;
+} {1 2 4}
+do_execsql_test regexp1-1.4 {
+ SELECT x FROM t1 WHERE y REGEXP 'shal+ al+' ORDER BY x;
+} {4}
+do_execsql_test regexp1-1.5 {
+ SELECT x FROM t1 WHERE y REGEXP 'shall x*y*z*all' ORDER BY x;
+} {4}
+do_execsql_test regexp1-1.6 {
+ SELECT x FROM t1 WHERE y REGEXP 'shallx?y? ?z?all' ORDER BY x;
+} {4}
+do_execsql_test regexp1-1.7 {
+ SELECT x FROM t1 WHERE y REGEXP 'r{2}' ORDER BY x;
+} {2}
+do_execsql_test regexp1-1.8 {
+ SELECT x FROM t1 WHERE y REGEXP 'r{3}' ORDER BY x;
+} {}
+do_execsql_test regexp1-1.9 {
+ SELECT x FROM t1 WHERE y REGEXP 'r{1}' ORDER BY x;
+} {1 2 3 4}
+do_execsql_test regexp1-1.10 {
+ SELECT x FROM t1 WHERE y REGEXP 'ur{2,10}e' ORDER BY x;
+} {2}
+do_execsql_test regexp1-1.11 {
+ SELECT x FROM t1 WHERE y REGEXP '[Aa]dam' ORDER BY x;
+} {3}
+do_execsql_test regexp1-1.12 {
+ SELECT x FROM t1 WHERE y REGEXP '[^Aa]dam' ORDER BY x;
+} {}
+do_execsql_test regexp1-1.13 {
+ SELECT x FROM t1 WHERE y REGEXP '[^b-zB-Z]dam' ORDER BY x;
+} {3}
+do_execsql_test regexp1-1.14 {
+ SELECT x FROM t1 WHERE y REGEXP 'alive' ORDER BY x;
+} {4}
+do_execsql_test regexp1-1.15 {
+ SELECT x FROM t1 WHERE y REGEXP '^alive' ORDER BY x;
+} {}
+do_execsql_test regexp1-1.16 {
+ SELECT x FROM t1 WHERE y REGEXP 'alive$' ORDER BY x;
+} {}
+do_execsql_test regexp1-1.17 {
+ SELECT x FROM t1 WHERE y REGEXP 'alive.$' ORDER BY x;
+} {4}
+do_execsql_test regexp1-1.18 {
+ SELECT x FROM t1 WHERE y REGEXP 'alive\.$' ORDER BY x;
+} {4}
+do_execsql_test regexp1-1.19 {
+ SELECT x FROM t1 WHERE y REGEXP 'ma[nd]' ORDER BY x;
+} {1 2 4}
+do_execsql_test regexp1-1.20 {
+ SELECT x FROM t1 WHERE y REGEXP '\bma[nd]' ORDER BY x;
+} {1 2 4}
+do_execsql_test regexp1-1.21 {
+ SELECT x FROM t1 WHERE y REGEXP 'ma[nd]\b' ORDER BY x;
+} {1 2}
+do_execsql_test regexp1-1.22 {
+ SELECT x FROM t1 WHERE y REGEXP 'ma\w' ORDER BY x;
+} {1 2 4}
+do_execsql_test regexp1-1.23 {
+ SELECT x FROM t1 WHERE y REGEXP 'ma\W' ORDER BY x;
+} {}
+do_execsql_test regexp1-1.24 {
+ SELECT x FROM t1 WHERE y REGEXP '\sma\w' ORDER BY x;
+} {1 2 4}
+do_execsql_test regexp1-1.25 {
+ SELECT x FROM t1 WHERE y REGEXP '\Sma\w' ORDER BY x;
+} {}
+do_execsql_test regexp1-1.26 {
+ SELECT x FROM t1 WHERE y REGEXP 'alive\S$' ORDER BY x;
+} {4}
+do_execsql_test regexp1-1.27 {
+ SELECT x FROM t1 WHERE y REGEXP
+ '\b(unto|us|son|given|his|name|called|' ||
+ 'wonderful|councelor|mighty|god|everlasting|father|' ||
+ 'prince|peace|alive)\b';
+} {4}
+
+do_execsql_test regexp1-2.1 {
+ SELECT 'aaaabbbbcccc' REGEXP 'ab*c',
+ 'aaaacccc' REGEXP 'ab*c';
+} {1 1}
+do_execsql_test regexp1-2.2 {
+ SELECT 'aaaabbbbcccc' REGEXP 'ab+c',
+ 'aaaacccc' REGEXP 'ab+c';
+} {1 0}
+do_execsql_test regexp1-2.3 {
+ SELECT 'aaaabbbbcccc' REGEXP 'ab?c',
+ 'aaaacccc' REGEXP 'ab?c';
+} {0 1}
+do_execsql_test regexp1-2.4 {
+ SELECT 'aaaabbbbbbcccc' REGEXP 'ab{3,5}c',
+ 'aaaabbbbbcccc' REGEXP 'ab{3,5}c',
+ 'aaaabbbbcccc' REGEXP 'ab{3,5}c',
+ 'aaaabbbcccc' REGEXP 'ab{3,5}c',
+ 'aaaabbcccc' REGEXP 'ab{3,5}c',
+ 'aaaabcccc' REGEXP 'ab{3,5}c'
+} {0 1 1 1 0 0}
+do_execsql_test regexp1-2.5 {
+ SELECT 'aaaabbbbcccc' REGEXP 'a(a|b|c)+c',
+ 'aaaabbbbcccc' REGEXP '^a(a|b|c){11}c$',
+ 'aaaabbbbcccc' REGEXP '^a(a|b|c){10}c$',
+ 'aaaabbbbcccc' REGEXP '^a(a|b|c){9}c$'
+} {1 0 1 0}
+do_execsql_test regexp1-2.6 {
+ SELECT 'aaaabbbbcccc' REGEXP '^a(a|bb|c)+c$',
+ 'aaaabbbbcccc' REGEXP '^a(a|bbb|c)+c$',
+ 'aaaabbbbcccc' REGEXP '^a(a|bbbb|c)+c$'
+} {1 0 1}
+do_execsql_test regexp1-2.7 {
+ SELECT 'aaaabbbbcccc' REGEXP '^a([ac]+|bb){3}c$',
+ 'aaaabbbbcccc' REGEXP '^a([ac]+|bb){4}c$',
+ 'aaaabbbbcccc' REGEXP '^a([ac]+|bb){5}c$'
+} {0 1 1}
+
+do_execsql_test regexp1-2.8 {
+ SELECT 'abc*def+ghi.jkl[mno]pqr' REGEXP 'c.d',
+ 'abc*def+ghi.jkl[mno]pqr' REGEXP 'c\*d',
+ 'abc*def+ghi.jkl[mno]pqr' REGEXP 'f\+g',
+ 'abc*def+ghi.jkl[mno]pqr' REGEXP 'i\.j',
+ 'abc*def+ghi.jkl[mno]pqr' REGEXP 'l\[mno\]p'
+} {1 1 1 1 1}
+
+do_test regexp1-2.9 {
+ set v1 "abc\ndef"
+ db eval {SELECT $v1 REGEXP '^abc\ndef$'}
+} {1}
+do_test regexp1-2.10 {
+ set v1 "abc\adef"
+ db eval {SELECT $v1 REGEXP '^abc\adef$'}
+} {1}
+do_test regexp1-2.11 {
+ set v1 "abc\tdef"
+ db eval {SELECT $v1 REGEXP '^abc\tdef$'}
+} {1}
+do_test regexp1-2.12 {
+ set v1 "abc\rdef"
+ db eval {SELECT $v1 REGEXP '^abc\rdef$'}
+} {1}
+do_test regexp1-2.13 {
+ set v1 "abc\fdef"
+ db eval {SELECT $v1 REGEXP '^abc\fdef$'}
+} {1}
+do_test regexp1-2.14 {
+ set v1 "abc\vdef"
+ db eval {SELECT $v1 REGEXP '^abc\vdef$'}
+} {1}
+do_execsql_test regexp1-2.15 {
+ SELECT 'abc\def' REGEXP '^abc\\def',
+ 'abc(def' REGEXP '^abc\(def',
+ 'abc)def' REGEXP '^abc\)def',
+ 'abc*def' REGEXP '^abc\*def',
+ 'abc.def' REGEXP '^abc\.def',
+ 'abc+def' REGEXP '^abc\+def',
+ 'abc?def' REGEXP '^abc\?def',
+ 'abc[def' REGEXP '^abc\[def',
+ 'abc$def' REGEXP '^abc\$',
+ '^def' REGEXP '\^def',
+ 'abc{4}x' REGEXP '^abc\{4\}x$',
+ 'abc|def' REGEXP '^abc\|def$'
+} {1 1 1 1 1 1 1 1 1 1 1 1}
+
+do_execsql_test regexp1-2.20 {
+ SELECT 'abc$¢€xyz' REGEXP '^abc\u0024\u00a2\u20acxyz$',
+ 'abc$¢€xyz' REGEXP '^abc\u0024\u00A2\u20ACxyz$',
+ 'abc$¢€xyz' REGEXP '^abc\x24\xa2\u20acxyz$'
+} {1 1 1}
+do_execsql_test regexp1-2.21 {
+ SELECT 'abc$¢€xyz' REGEXP '^abc[\u0024][\u00a2][\u20ac]xyz$',
+ 'abc$¢€xyz' REGEXP '^abc[\u0024\u00A2\u20AC]{3}xyz$',
+ 'abc$¢€xyz' REGEXP '^abc[\x24][\xa2\u20ac]+xyz$'
+} {1 1 1}
+do_execsql_test regexp1-2.22 {
+ SELECT 'abc$¢€xyz' REGEXP '^abc[^\u0025-X][^ -\u007f][^\u20ab]xyz$'
+} {1}
+
+finish_test
diff --git a/test/releasetest.tcl b/test/releasetest.tcl
index 3b4662c..b635061 100644
--- a/test/releasetest.tcl
+++ b/test/releasetest.tcl
@@ -81,6 +81,22 @@ array set ::Configs {
-DSQLITE_DEFAULT_FILE_FORMAT=4
-DSQLITE_ENABLE_UPDATE_DELETE_LIMIT=1
}
+ "Check-Symbols" {
+ -DSQLITE_MEMDEBUG=1
+ -DSQLITE_ENABLE_FTS3_PARENTHESIS=1
+ -DSQLITE_ENABLE_FTS3=1
+ -DSQLITE_ENABLE_RTREE=1
+ -DSQLITE_ENABLE_MEMSYS5=1
+ -DSQLITE_ENABLE_MEMSYS3=1
+ -DSQLITE_ENABLE_COLUMN_METADATA=1
+ -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT=1
+ -DSQLITE_SECURE_DELETE=1
+ -DSQLITE_SOUNDEX=1
+ -DSQLITE_ENABLE_ATOMIC_WRITE=1
+ -DSQLITE_ENABLE_IOTRACE=1
+ -DSQLITE_ENABLE_MEMORY_MANAGEMENT=1
+ -DSQLITE_ENABLE_OVERSIZE_CELL_CHECK=1
+ }
"Debug-One" {
-O2
-DSQLITE_DEBUG=1
@@ -164,7 +180,8 @@ array set ::Configs {
array set ::Platforms {
Linux-x86_64 {
- "Debug-One" "checksymbols test"
+ "Check-Symbols" checksymbols
+ "Debug-One" test
"Secure-Delete" test
"Unlock-Notify" "QUICKTEST_INCLUDE=notify2.test test"
"Update-Delete-Limit" test
@@ -330,15 +347,17 @@ proc main {argv} {
# If the configuration included the SQLITE_DEBUG option, then remove
# it and run veryquick.test. If it did not include the SQLITE_DEBUG option
# add it and run veryquick.test.
- set debug_idx [lsearch -glob $config_options -DSQLITE_DEBUG*]
- if {$debug_idx < 0} {
- run_test_suite "${zConfig}_debug" test [
- concat $config_options -DSQLITE_DEBUG=1
- ]
- } else {
- run_test_suite "${zConfig}_ndebug" test [
- lreplace $config_options $debug_idx $debug_idx
- ]
+ if {$target!="checksymbols"} {
+ set debug_idx [lsearch -glob $config_options -DSQLITE_DEBUG*]
+ if {$debug_idx < 0} {
+ run_test_suite "${zConfig}_debug" test [
+ concat $config_options -DSQLITE_DEBUG=1
+ ]
+ } else {
+ run_test_suite "${zConfig}_ndebug" test [
+ lreplace $config_options $debug_idx $debug_idx
+ ]
+ }
}
}
diff --git a/test/resolver01.test b/test/resolver01.test
new file mode 100644
index 0000000..3ca6ace
--- /dev/null
+++ b/test/resolver01.test
@@ -0,0 +1,39 @@
+# 2013-04-13
+#
+# 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 file tests features of the name resolver (the component that
+# figures out what identifiers in the SQL statement refer to) that
+# were fixed by ticket [2500cdb9be]
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+
+do_test resolver01-1.1 {
+ catchsql {
+ CREATE TABLE t1(x, y); INSERT INTO t1 VALUES(11,22);
+ CREATE TABLE t2(y, z); INSERT INTO t2 VALUES(33,44);
+ SELECT 1 AS y FROM t1, t2 ORDER BY y;
+ }
+} {0 1}
+do_test resolver01-1.2 {
+ catchsql {
+ SELECT 2 AS y FROM t1, t2 ORDER BY y COLLATE nocase;
+ }
+} {0 2}
+do_test resolver01-1.3 {
+ catchsql {
+ SELECT 3 AS y FROM t1, t2 ORDER BY +y;
+ }
+} {0 3}
+
+
+finish_test
diff --git a/test/selectA.test b/test/selectA.test
index 5b86377..5fd2288 100644
--- a/test/selectA.test
+++ b/test/selectA.test
@@ -281,13 +281,13 @@ do_test selectA-2.34 {
do_test selectA-2.35 {
execsql {
SELECT x,y,z FROM t2 UNION SELECT a,b,c FROM t1
- ORDER BY b COLLATE NOCASE,a,c
+ ORDER BY y COLLATE NOCASE,x,z
}
} {1 a a 9.9 b B {} C c hello d D abc e e hare m M {} U u 5200000.0 X x -23 Y y mad Z z}
do_test selectA-2.36 {
execsql {
SELECT x,y,z FROM t2 UNION SELECT a,b,c FROM t1
- ORDER BY b COLLATE NOCASE DESC,a,c
+ ORDER BY y COLLATE NOCASE DESC,x,z
}
} {mad Z z -23 Y y 5200000.0 X x {} U u hare m M abc e e hello d D {} C c 9.9 b B 1 a a}
do_test selectA-2.37 {
@@ -311,7 +311,7 @@ do_test selectA-2.39 {
do_test selectA-2.40 {
execsql {
SELECT x,y,z FROM t2 UNION SELECT a,b,c FROM t1
- ORDER BY c COLLATE BINARY DESC,a,b
+ ORDER BY z COLLATE BINARY DESC,x,y
}
} {mad Z z -23 Y y 5200000.0 X x {} U u abc e e {} C c 1 a a hare m M hello d D 9.9 b B}
do_test selectA-2.41 {
@@ -602,7 +602,7 @@ do_test selectA-2.85 {
do_test selectA-2.86 {
execsql {
SELECT x,y,z FROM t2 UNION SELECT a,b,c FROM t3
- ORDER BY b COLLATE NOCASE,a,c
+ ORDER BY y COLLATE NOCASE,x,z
}
} {1 a a 9.9 b B {} C c hello d D abc e e hare m M {} U u 5200000.0 X x -23 Y y mad Z z}
do_test selectA-2.87 {
@@ -632,7 +632,7 @@ do_test selectA-2.90 {
do_test selectA-2.91 {
execsql {
SELECT x,y,z FROM t2 UNION SELECT a,b,c FROM t3
- ORDER BY c COLLATE BINARY DESC,a,b
+ ORDER BY z COLLATE BINARY DESC,x,y
}
} {mad Z z -23 Y y 5200000.0 X x {} U u abc e e {} C c 1 a a hare m M hello d D 9.9 b B}
do_test selectA-2.92 {
@@ -893,13 +893,13 @@ do_test selectA-3.34 {
do_test selectA-3.35 {
execsql {
SELECT x,y,z FROM t2 UNION SELECT a,b,c FROM t1
- ORDER BY b COLLATE NOCASE,a,c
+ ORDER BY y COLLATE NOCASE,x,z
}
} {1 a a 9.9 b B {} C c hello d D abc e e hare m M {} U u 5200000.0 X x -23 Y y mad Z z}
do_test selectA-3.36 {
execsql {
SELECT x,y,z FROM t2 UNION SELECT a,b,c FROM t1
- ORDER BY b COLLATE NOCASE DESC,a,c
+ ORDER BY y COLLATE NOCASE DESC,x,z
}
} {mad Z z -23 Y y 5200000.0 X x {} U u hare m M abc e e hello d D {} C c 9.9 b B 1 a a}
do_test selectA-3.37 {
@@ -923,7 +923,7 @@ do_test selectA-3.39 {
do_test selectA-3.40 {
execsql {
SELECT x,y,z FROM t2 UNION SELECT a,b,c FROM t1
- ORDER BY c COLLATE BINARY DESC,a,b
+ ORDER BY z COLLATE BINARY DESC,x,y
}
} {mad Z z -23 Y y 5200000.0 X x {} U u abc e e {} C c 1 a a hare m M hello d D 9.9 b B}
do_test selectA-3.41 {
@@ -1214,7 +1214,7 @@ do_test selectA-3.85 {
do_test selectA-3.86 {
execsql {
SELECT x,y,z FROM t2 UNION SELECT a,b,c FROM t3
- ORDER BY b COLLATE NOCASE,a,c
+ ORDER BY y COLLATE NOCASE,x,z
}
} {1 a a 9.9 b B {} C c hello d D abc e e hare m M {} U u 5200000.0 X x -23 Y y mad Z z}
do_test selectA-3.87 {
@@ -1244,7 +1244,7 @@ do_test selectA-3.90 {
do_test selectA-3.91 {
execsql {
SELECT x,y,z FROM t2 UNION SELECT a,b,c FROM t3
- ORDER BY c COLLATE BINARY DESC,a,b
+ ORDER BY z COLLATE BINARY DESC,x,y
}
} {mad Z z -23 Y y 5200000.0 X x {} U u abc e e {} C c 1 a a hare m M hello d D 9.9 b B}
do_test selectA-3.92 {
diff --git a/test/selectD.test b/test/selectD.test
new file mode 100644
index 0000000..89f999e
--- /dev/null
+++ b/test/selectD.test
@@ -0,0 +1,174 @@
+# 2012 December 19
+#
+# 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 file implements regression tests for name resolution in SELECT
+# statements that have parenthesized FROM clauses.
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+
+
+for {set i 1} {$i<=2} {incr i} {
+ db close
+ forcedelete test$i.db
+ sqlite3 db test$i.db
+ if {$i==2} {
+ optimization_control db query-flattener off
+ }
+ do_test selectD-$i.0 {
+ db eval {
+ ATTACH ':memory:' AS aux1;
+ CREATE TABLE t1(a,b); INSERT INTO t1 VALUES(111,'x1');
+ CREATE TABLE t2(a,b); INSERT INTO t2 VALUES(222,'x2');
+ CREATE TEMP TABLE t3(a,b); INSERT INTO t3 VALUES(333,'x3');
+ CREATE TABLE main.t4(a,b); INSERT INTO main.t4 VALUES(444,'x4');
+ CREATE TABLE aux1.t4(a,b); INSERT INTO aux1.t4 VALUES(555,'x5');
+ }
+ } {}
+ do_test selectD-$i.1 {
+ db eval {
+ SELECT *
+ FROM (t1), (t2), (t3), (t4)
+ WHERE t4.a=t3.a+111
+ AND t3.a=t2.a+111
+ AND t2.a=t1.a+111;
+ }
+ } {111 x1 222 x2 333 x3 444 x4}
+ do_test selectD-$i.2.1 {
+ db eval {
+ SELECT *
+ FROM t1 JOIN (t2 JOIN (t3 JOIN t4 ON t4.a=t3.a+111)
+ ON t3.a=t2.a+111)
+ ON t2.a=t1.a+111;
+ }
+ } {111 x1 222 x2 333 x3 444 x4}
+ do_test selectD-$i.2.2 {
+ db eval {
+ SELECT t3.a
+ FROM t1 JOIN (t2 JOIN (t3 JOIN t4 ON t4.a=t3.a+111)
+ ON t3.a=t2.a+111)
+ ON t2.a=t1.a+111;
+ }
+ } {333}
+ do_test selectD-$i.2.3 {
+ db eval {
+ SELECT t3.*
+ FROM t1 JOIN (t2 JOIN (t3 JOIN t4 ON t4.a=t3.a+111)
+ ON t3.a=t2.a+111)
+ ON t2.a=t1.a+111;
+ }
+ } {333 x3}
+ do_test selectD-$i.2.3 {
+ db eval {
+ SELECT t3.*, t2.*
+ FROM t1 JOIN (t2 JOIN (t3 JOIN t4 ON t4.a=t3.a+111)
+ ON t3.a=t2.a+111)
+ ON t2.a=t1.a+111;
+ }
+ } {333 x3 222 x2}
+ do_test selectD-$i.2.4 {
+ db eval {
+ SELECT *
+ FROM t1 JOIN (t2 JOIN (main.t4 JOIN aux1.t4 ON aux1.t4.a=main.t4.a+111)
+ ON main.t4.a=t2.a+222)
+ ON t2.a=t1.a+111;
+ }
+ } {111 x1 222 x2 444 x4 555 x5}
+ do_test selectD-$i.2.5 {
+ db eval {
+ SELECT *
+ FROM t1 JOIN (t2 JOIN (main.t4 AS x JOIN aux1.t4 ON aux1.t4.a=x.a+111)
+ ON x.a=t2.a+222)
+ ON t2.a=t1.a+111;
+ }
+ } {111 x1 222 x2 444 x4 555 x5}
+ do_test selectD-$i.2.6 {
+ catchsql {
+ SELECT *
+ FROM t1 JOIN (t2 JOIN (main.t4 JOIN aux.t4 ON aux.t4.a=main.t4.a+111)
+ ON main.t4.a=t2.a+222)
+ ON t2.a=t1.a+111;
+ }
+ } {1 {no such table: aux.t4}}
+ do_test selectD-$i.2.7 {
+ db eval {
+ SELECT x.a, y.b
+ FROM t1 JOIN (t2 JOIN (main.t4 x JOIN aux1.t4 y ON y.a=x.a+111)
+ ON x.a=t2.a+222)
+ ON t2.a=t1.a+111;
+ }
+ } {444 x5}
+ do_test selectD-$i.3 {
+ db eval {
+ UPDATE t2 SET a=111;
+ UPDATE t3 SET a=111;
+ UPDATE t4 SET a=111;
+ SELECT *
+ FROM t1 JOIN (t2 JOIN (t3 JOIN t4 USING(a)) USING (a)) USING (a);
+ }
+ } {111 x1 x2 x3 x4}
+ do_test selectD-$i.4 {
+ db eval {
+ UPDATE t2 SET a=111;
+ UPDATE t3 SET a=111;
+ UPDATE t4 SET a=111;
+ SELECT *
+ FROM t1 LEFT JOIN (t2 LEFT JOIN (t3 LEFT JOIN t4 USING(a))
+ USING (a))
+ USING (a);
+ }
+ } {111 x1 x2 x3 x4}
+ do_test selectD-$i.5 {
+ db eval {
+ UPDATE t3 SET a=222;
+ UPDATE t4 SET a=222;
+ SELECT *
+ FROM (t1 LEFT JOIN t2 USING(a)) JOIN (t3 LEFT JOIN t4 USING(a))
+ ON t1.a=t3.a-111;
+ }
+ } {111 x1 x2 222 x3 x4}
+ do_test selectD-$i.6 {
+ db eval {
+ UPDATE t4 SET a=333;
+ SELECT *
+ FROM (t1 LEFT JOIN t2 USING(a)) JOIN (t3 LEFT JOIN t4 USING(a))
+ ON t1.a=t3.a-111;
+ }
+ } {111 x1 x2 222 x3 {}}
+ do_test selectD-$i.7 {
+ db eval {
+ SELECT t1.*, t2.*, t3.*, t4.b
+ FROM (t1 LEFT JOIN t2 USING(a)) JOIN (t3 LEFT JOIN t4 USING(a))
+ ON t1.a=t3.a-111;
+ }
+ } {111 x1 111 x2 222 x3 {}}
+}
+
+# The following test was added on 2013-04-24 in order to verify that
+# the datatypes and affinities of sub-sub-queries are set prior to computing
+# the datatypes and affinities of the parent sub-queries because the
+# latter computation depends on the former.
+#
+do_execsql_test selectD-4.1 {
+ CREATE TABLE t41(a INTEGER PRIMARY KEY, b INTEGER);
+ CREATE TABLE t42(d INTEGER PRIMARY KEY, e INTEGER);
+ CREATE TABLE t43(f INTEGER PRIMARY KEY, g INTEGER);
+ EXPLAIN QUERY PLAN
+ SELECT *
+ FROM t41
+ LEFT JOIN (SELECT count(*) AS cnt, x1.d
+ FROM (t42 INNER JOIN t43 ON d=g) AS x1
+ WHERE x1.d>5
+ GROUP BY x1.d) AS x2
+ ON t41.b=x2.d;
+} {/.*SEARCH SUBQUERY 1 AS x2 USING AUTOMATIC.*/}
+
+finish_test
diff --git a/test/selectE.test b/test/selectE.test
new file mode 100644
index 0000000..d7592bb
--- /dev/null
+++ b/test/selectE.test
@@ -0,0 +1,95 @@
+# 2013-05-07
+#
+# 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 file implements regression tests for compound SELECT statements
+# that have ORDER BY clauses with collating sequences that differ
+# from the collating sequence used for comparison in the compound.
+#
+# Ticket 6709574d2a8d8b9be3a9cb1afbf4ff2de48ea4e7:
+# drh added on 2013-05-06 15:21:16:
+#
+# In the code shown below (which is intended to be run from the
+# sqlite3.exe command-line tool) the three SELECT statements should all
+# generate the same answer. But the third one does not. It is as if the
+# COLLATE clause on the ORDER BY somehow got pulled into the EXCEPT
+# operator. Note that the ".print" commands are instructions to the
+# sqlite3.exe shell program to output delimiter lines so that you can more
+# easily tell where the output of one query ends and the next query
+# begins.
+#
+# CREATE TABLE t1(a);
+# INSERT INTO t1 VALUES('abc'),('def');
+# CREATE TABLE t2(a);
+# INSERT INTO t2 VALUES('DEF');
+#
+# SELECT a FROM t1 EXCEPT SELECT a FROM t2 ORDER BY a;
+# .print -----
+# SELECT a FROM (SELECT a FROM t1 EXCEPT SELECT a FROM t2)
+# ORDER BY a COLLATE nocase;
+# .print -----
+# SELECT a FROM t1 EXCEPT SELECT a FROM t2 ORDER BY a COLLATE nocase;
+#
+# Bisecting shows that this problem was introduced in SQLite version 3.6.0
+# by check-in [8bbfa97837a74ef] on 2008-06-15.
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+
+do_test selectE-1.0 {
+ db eval {
+ CREATE TABLE t1(a);
+ INSERT INTO t1 VALUES('abc'),('def'),('ghi');
+ CREATE TABLE t2(a);
+ INSERT INTO t2 VALUES('DEF'),('abc');
+ CREATE TABLE t3(a);
+ INSERT INTO t3 VALUES('def'),('jkl');
+
+ SELECT a FROM t1 EXCEPT SELECT a FROM t2
+ ORDER BY a COLLATE nocase;
+ }
+} {def ghi}
+do_test selectE-1.1 {
+ db eval {
+ SELECT a FROM t2 EXCEPT SELECT a FROM t3
+ ORDER BY a COLLATE nocase;
+ }
+} {abc DEF}
+do_test selectE-1.2 {
+ db eval {
+ SELECT a FROM t2 EXCEPT SELECT a FROM t3
+ ORDER BY a COLLATE binary;
+ }
+} {DEF abc}
+do_test selectE-1.3 {
+ db eval {
+ SELECT a FROM t2 EXCEPT SELECT a FROM t3
+ ORDER BY a;
+ }
+} {DEF abc}
+
+do_test selectE-2.1 {
+ db eval {
+ DELETE FROM t2;
+ DELETE FROM t3;
+ INSERT INTO t2 VALUES('ABC'),('def'),('GHI'),('jkl');
+ INSERT INTO t3 SELECT lower(a) FROM t2;
+ SELECT a COLLATE nocase FROM t2 EXCEPT SELECT a FROM t3
+ ORDER BY 1
+ }
+} {}
+do_test selectE-2.2 {
+ db eval {
+ SELECT a COLLATE nocase FROM t2 EXCEPT SELECT a FROM t3
+ ORDER BY 1 COLLATE binary
+ }
+} {}
+
+finish_test
diff --git a/test/shared9.test b/test/shared9.test
new file mode 100644
index 0000000..1982a59
--- /dev/null
+++ b/test/shared9.test
@@ -0,0 +1,230 @@
+# 2012 October 5
+#
+# 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.
+#
+#***********************************************************************
+#
+# The tests in this file are intended to show if two connections attach
+# to the same shared cache using different database names, views and
+# virtual tables may still be accessed.
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+source $testdir/lock_common.tcl
+set testprefix shared9
+
+ifcapable !view||!trigger {
+ finish_test
+ return
+}
+
+db close
+set enable_shared_cache [sqlite3_enable_shared_cache 1]
+
+sqlite3 db1 test.db
+sqlite3 db2 test.db
+forcedelete test.db2
+
+do_test 1.1 {
+ db1 eval {
+ ATTACH 'test.db2' AS 'fred';
+ CREATE TABLE fred.t1(a, b, c);
+ CREATE VIEW fred.v1 AS SELECT * FROM t1;
+
+ CREATE TABLE fred.t2(a, b);
+ CREATE TABLE fred.t3(a, b);
+ CREATE TRIGGER fred.trig AFTER INSERT ON t2 BEGIN
+ DELETE FROM t3;
+ INSERT INTO t3 SELECT * FROM t2;
+ END;
+ INSERT INTO t2 VALUES(1, 2);
+ SELECT * FROM t3;
+ }
+} {1 2}
+
+do_test 1.2 { db2 eval "ATTACH 'test.db2' AS 'jones'" } {}
+do_test 1.3 { db2 eval "SELECT * FROM v1" } {}
+do_test 1.4 { db2 eval "INSERT INTO t2 VALUES(3, 4)" } {}
+
+ifcapable fts3 {
+ do_test 1.5 {
+ db1 eval {
+ CREATE VIRTUAL TABLE fred.t4 USING fts4;
+ INSERT INTO t4 VALUES('hello world');
+ }
+ } {}
+
+ do_test 1.6 {
+ db2 eval {
+ INSERT INTO t4 VALUES('shared cache');
+ SELECT * FROM t4 WHERE t4 MATCH 'hello';
+ }
+ } {{hello world}}
+
+ do_test 1.7 {
+ db1 eval {
+ SELECT * FROM t4 WHERE t4 MATCH 'c*';
+ }
+ } {{shared cache}}
+}
+
+db1 close
+db2 close
+
+#-------------------------------------------------------------------------
+# The following tests attempt to find a similar problem with collation
+# sequence names - pointers to database handle specific allocations leaking
+# into schema objects and being used after the original handle has been
+# closed.
+#
+forcedelete test.db test.db2
+sqlite3 db1 test.db
+sqlite3 db2 test.db
+foreach x {collate1 collate2 collate3} {
+ proc $x {a b} { string compare $a $b }
+ db1 collate $x $x
+ db2 collate $x $x
+}
+do_test 2.1 {
+ db1 eval {
+ CREATE TABLE t1(a, b, c COLLATE collate1);
+ CREATE INDEX i1 ON t1(a COLLATE collate2, c, b);
+ }
+} {}
+do_test 2.2 {
+ db1 close
+ db2 eval "INSERT INTO t1 VALUES('abc', 'def', 'ghi')"
+} {}
+db2 close
+
+#-------------------------------------------------------------------------
+# At one point, the following would cause a collation sequence belonging
+# to connection [db1] to be invoked by a call to [db2 eval]. Which is a
+# problem if [db1] has already been closed.
+#
+forcedelete test.db test.db2
+sqlite3 db1 test.db
+sqlite3 db2 test.db
+
+proc mycollate_db1 {a b} {set ::invoked_mycollate_db1 1 ; string compare $a $b}
+proc mycollate_db2 {a b} {string compare $a $b}
+
+db1 collate mycollate mycollate_db1
+db2 collate mycollate mycollate_db2
+
+do_test 2.3 {
+ set ::invoked_mycollate_db1 0
+ db1 eval {
+ CREATE TABLE t1(a COLLATE mycollate, CHECK (a IN ('one', 'two', 'three')));
+ INSERT INTO t1 VALUES('one');
+ }
+ db1 close
+ set ::invoked_mycollate_db1
+} {1}
+do_test 2.4 {
+ set ::invoked_mycollate_db1 0
+ db2 eval {
+ INSERT INTO t1 VALUES('two');
+ }
+ db2 close
+ set ::invoked_mycollate_db1
+} {0}
+
+forcedelete test.db test.db2
+sqlite3 db1 test.db
+sqlite3 db2 test.db
+db1 collate mycollate mycollate_db1
+db2 collate mycollate mycollate_db2
+
+do_test 2.13 {
+ set ::invoked_mycollate_db1 0
+ db1 eval {
+ CREATE TABLE t1(a, CHECK (a COLLATE mycollate IN ('one', 'two', 'three')));
+ INSERT INTO t1 VALUES('one');
+ }
+ db1 close
+ set ::invoked_mycollate_db1
+} {1}
+do_test 2.14 {
+ set ::invoked_mycollate_db1 0
+ db2 eval {
+ INSERT INTO t1 VALUES('two');
+ }
+ db2 close
+ set ::invoked_mycollate_db1
+} {0}
+
+#-------------------------------------------------------------------------
+# This test verifies that a bug causing a busy-handler belonging to one
+# shared-cache connection to be executed as a result of an sqlite3_step()
+# on another has been fixed.
+#
+forcedelete test.db test.db2
+sqlite3 db1 test.db
+sqlite3 db2 test.db
+
+proc busyhandler {handle args} {
+ set ::busyhandler_invoked_for $handle
+ return 1
+}
+db1 busy [list busyhandler db1]
+db2 busy [list busyhandler db2]
+
+do_test 3.1 {
+ db1 eval {
+ BEGIN;
+ CREATE TABLE t1(a, b);
+ CREATE TABLE t2(a, b);
+ INSERT INTO t1 VALUES(1, 2);
+ INSERT INTO t2 VALUES(1, 2);
+ }
+ # Keep this next COMMIT as a separate statement. This ensures that COMMIT
+ # has already been compiled and loaded into the tcl interface statement
+ # cache when it is attempted below.
+ db1 eval COMMIT
+ db1 eval {
+ BEGIN;
+ INSERT INTO t1 VALUES(3, 4);
+ }
+} {}
+
+do_test 3.2 {
+ set ::tf [launch_testfixture]
+ testfixture $::tf {
+ sqlite3 db test.db
+ db eval {
+ BEGIN;
+ SELECT * FROM t1;
+ }
+ }
+} {1 2}
+
+do_test 3.3 {
+ db2 eval { SELECT * FROM t2 }
+} {1 2}
+
+do_test 3.4 {
+ list [catch { db1 eval COMMIT } msg] $msg
+} {1 {database is locked}}
+
+# At one point the following would fail, showing that the busy-handler
+# belonging to [db2] was invoked instead.
+do_test 3.5 {
+ set ::busyhandler_invoked_for
+} {db1}
+do_test 3.6 {
+ close $::tf
+ db1 eval COMMIT
+} {}
+
+db1 close
+db2 close
+
+sqlite3_enable_shared_cache $::enable_shared_cache
+finish_test
diff --git a/test/sharedA.test b/test/sharedA.test
new file mode 100644
index 0000000..146fb26
--- /dev/null
+++ b/test/sharedA.test
@@ -0,0 +1,188 @@
+# 2013 May 14
+#
+# 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.
+#
+#***********************************************************************
+#
+# Test some specific circumstances to do with shared cache mode.
+#
+
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+if {[run_thread_tests]==0} { finish_test ; return }
+db close
+set ::testprefix sharedA
+
+set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
+
+#-------------------------------------------------------------------------
+#
+do_test 0.1 {
+ sqlite3 db1 test.db
+ sqlite3 db2 test.db
+
+ db1 eval {
+ CREATE TABLE t1(x);
+ INSERT INTO t1 VALUES(randomblob(100));
+ INSERT INTO t1 SELECT randomblob(100) FROM t1;
+ INSERT INTO t1 SELECT randomblob(100) FROM t1;
+ INSERT INTO t1 SELECT randomblob(100) FROM t1;
+ INSERT INTO t1 SELECT randomblob(100) FROM t1;
+ INSERT INTO t1 SELECT randomblob(100) FROM t1;
+ INSERT INTO t1 SELECT randomblob(100) FROM t1;
+ CREATE INDEX i1 ON t1(x);
+ }
+
+ db1 eval {
+ BEGIN;
+ DROP INDEX i1;
+ }
+
+ db2 close
+
+ db1 eval {
+ INSERT INTO t1 SELECT randomblob(100) FROM t1;
+ ROLLBACK;
+ PRAGMA integrity_check;
+ }
+} {ok}
+
+db1 close
+forcedelete test.db
+
+
+#-------------------------------------------------------------------------
+#
+do_test 1.1 {
+ sqlite3 db1 test.db
+ sqlite3 db2 test.db
+ db2 eval {
+ CREATE TABLE t1(x);
+ INSERT INTO t1 VALUES(123);
+ }
+ db1 eval {
+ SELECT * FROM t1;
+ CREATE INDEX i1 ON t1(x);
+ }
+} {123}
+
+do_test 1.2 {
+ db2 eval { SELECT * FROM t1 ORDER BY x; }
+
+ db1 eval {
+ BEGIN; DROP INDEX i1;
+ }
+ db1 close
+
+ db2 eval { SELECT * FROM t1 ORDER BY x; }
+} {123}
+
+do_test 1.3 {
+ db2 close
+} {}
+
+#-------------------------------------------------------------------------
+#
+# sqlite3RollbackAll() loops through all attached b-trees and rolls
+# back each one separately. Then if the SQLITE_InternChanges flag is
+# set, it resets the schema. Both of the above steps must be done
+# while holding a mutex, otherwise another thread might slip in and
+# try to use the new schema with the old data.
+#
+# The following sequence of tests attempt to verify that the actions
+# taken by sqlite3RollbackAll() are thread-atomic (that they cannot be
+# interrupted by a separate thread.)
+#
+# Note that a TCL interpreter can only be used within the thread in which
+# it was originally created (because it uses thread-local-storage).
+# The tvfs callbacks must therefore only run on the main thread.
+# There is some trickery in the read_callback procedure to ensure that
+# this is the case.
+#
+testvfs tvfs
+
+# Set up two databases and two database connections.
+#
+# db1: main(test.db), two(test2.db)
+# db2: main(test.db)
+#
+# The cache for test.db is shared between db1 and db2.
+#
+do_test 2.1 {
+ forcedelete test.db test.db2
+ sqlite3 db1 test.db -vfs tvfs
+ db1 eval { ATTACH 'test.db2' AS two }
+
+ db1 eval {
+ CREATE TABLE t1(x);
+ INSERT INTO t1 VALUES(1);
+ INSERT INTO t1 VALUES(2);
+ INSERT INTO t1 VALUES(3);
+ CREATE TABLE two.t2(x);
+ INSERT INTO t2 SELECT * FROM t1;
+ }
+
+ sqlite3 db2 test.db -vfs tvfs
+ db2 eval { SELECT * FROM t1 }
+} {1 2 3}
+
+# Create a prepared statement on db2 that will attempt a schema change
+# in test.db. Meanwhile, start a transaction on db1 that changes
+# the schema of test.db and that creates a rollback journal on test2.db
+#
+do_test 2.2 {
+ set ::STMT [sqlite3_prepare db2 "CREATE INDEX i1 ON t1(x)" -1 tail]
+ db1 eval {
+ BEGIN;
+ CREATE INDEX i1 ON t1(x);
+ INSERT INTO t2 VALUES('value!');
+ }
+} {}
+
+# Set up a callback that will cause db2 to try to execute its
+# schema change when db1 accesses the journal file of test2.db.
+#
+# This callback will be invoked after the content of test.db has
+# be rolled back but before the schema has been reset. If the
+# sqlite3RollbackAll() operation is not thread-atomic, then the
+# db2 statement in the callback will see old content with the newer
+# schema, which is wrong.
+#
+tvfs filter xRead
+tvfs script read_callback
+unset -nocomplain ::some_time_laster
+unset -nocomplain ::thread_result
+proc read_callback {call file args} {
+ if {[string match *test.db2-journal $file]} {
+ tvfs filter {} ;# Ensure that tvfs callbacks to do run on the
+ # child thread
+ sqlthread spawn ::thread_result [subst -nocommands {
+ sqlite3_step $::STMT
+ set rc [sqlite3_finalize $::STMT]
+ }]
+ after 1000 { set ::some_time_later 1 }
+ vwait ::some_time_later
+ }
+}
+do_test 2.3 { db1 eval ROLLBACK } {}
+
+# Verify that the db2 statement invoked by the callback detected the
+# schema change.
+#
+if {[info exists ::thread_result]==0} { vwait ::thread_result }
+do_test 2.4 {
+ list $::thread_result [sqlite3_errmsg db2]
+} {SQLITE_SCHEMA {database schema has changed}}
+
+db1 close
+db2 close
+tvfs delete
+
+sqlite3_enable_shared_cache $::enable_shared_cache
+finish_test
diff --git a/test/shared_err.test b/test/shared_err.test
index f501fc7..17add94 100644
--- a/test/shared_err.test
+++ b/test/shared_err.test
@@ -401,6 +401,8 @@ do_malloc_test shared_err-8 -tclprep {
execsql {INSERT INTO t1 VALUES($a, $b)} db2
}
execsql {COMMIT} db2
+ execsql BEGIN
+ execsql ROLLBACK
set ::DB2 [sqlite3_connection_pointer db2]
set ::STMT [sqlite3_prepare $::DB2 "SELECT a FROM t1 ORDER BY a" -1 DUMMY]
sqlite3_step $::STMT ;# Cursor points at 0000000000
@@ -409,8 +411,7 @@ do_malloc_test shared_err-8 -tclprep {
execsql {
BEGIN;
INSERT INTO t1 VALUES(6, NULL);
- ROLLBACK;
- }
+ ROLLBACK}
} -cleanup {
# UPDATE: As of [5668], if the rollback fails SQLITE_CORRUPT is returned.
# So these tests have been updated to expect SQLITE_CORRUPT and its
diff --git a/test/shell1.test b/test/shell1.test
index 47f9e41..c60f3af 100644
--- a/test/shell1.test
+++ b/test/shell1.test
@@ -149,7 +149,7 @@ do_test shell1-1.14.3 {
set res [catchcmd "-separator" ""]
set rc [lindex $res 0]
list $rc \
- [regexp {Error: missing argument for option: -separator} $res]
+ [regexp {Error: missing argument to -separator} $res]
} {1 1}
# -stats print memory stats before each finalize
@@ -168,7 +168,7 @@ do_test shell1-1.15.3 {
set res [catchcmd "-nullvalue" ""]
set rc [lindex $res 0]
list $rc \
- [regexp {Error: missing argument for option: -nullvalue} $res]
+ [regexp {Error: missing argument to -nullvalue} $res]
} {1 1}
# -version show SQLite version
@@ -220,7 +220,7 @@ do_test shell1-2.3.2 {
} {0 {}}
do_test shell1-2.3.3 {
catchcmd "test.db" ".explain \"1 2 3\""
-} {0 {}}
+} {1 {ERROR: Not a boolean value: "1 2 3". Assuming "no".}}
do_test shell1-2.3.4 {
catchcmd "test.db" ".explain \"OFF\""
} {0 {}}
@@ -253,7 +253,7 @@ do_test shell1-2.4.2 {
# .backup ?DB? FILE Backup DB (default "main") to FILE
do_test shell1-3.1.1 {
catchcmd "test.db" ".backup"
-} {1 {Error: unknown command or invalid arguments: "backup". Enter ".help" for help}}
+} {1 {missing FILENAME argument on .backup}}
do_test shell1-3.1.2 {
catchcmd "test.db" ".backup FOO"
} {0 {}}
@@ -263,7 +263,7 @@ do_test shell1-3.1.3 {
do_test shell1-3.1.4 {
# too many arguments
catchcmd "test.db" ".backup FOO BAR BAD"
-} {1 {Error: unknown command or invalid arguments: "backup". Enter ".help" for help}}
+} {1 {too many arguments to .backup}}
# .bail ON|OFF Stop after hitting an error. Default OFF
do_test shell1-3.2.1 {
@@ -326,10 +326,6 @@ do_test shell1-3.5.4 {
do_test shell1-3.6.1 {
catchcmd "test.db" ".exit"
} {0 {}}
-do_test shell1-3.6.2 {
- # too many arguments
- catchcmd "test.db" ".exit BAD"
-} {1 {Error: unknown command or invalid arguments: "exit". Enter ".help" for help}}
# .explain ON|OFF Turn output mode suitable for EXPLAIN on or off.
do_test shell1-3.7.1 {
@@ -680,6 +676,15 @@ do_test shell1-3.26.4 {
catchcmd "test.db" ".width 1 1"
# this should be treated the same as a '1' width for col 1 and 2
} {0 {}}
+do_test shell1-3.26.5 {
+ catchcmd "test.db" ".mode column\n.width 10 -10\nSELECT 'abcdefg', 123456;"
+ # this should be treated the same as a '1' width for col 1 and 2
+} {0 {abcdefg 123456}}
+do_test shell1-3.26.6 {
+ catchcmd "test.db" ".mode column\n.width -10 10\nSELECT 'abcdefg', 123456;"
+ # this should be treated the same as a '1' width for col 1 and 2
+} {0 { abcdefg 123456 }}
+
# .timer ON|OFF Turn the CPU timer measurement on or off
do_test shell1-3.27.1 {
@@ -701,18 +706,23 @@ do_test shell1-3-28.1 {
".log stdout\nSELECT coalesce(sqlite_log(123,'hello'),'456');"
} "0 {(123) hello\n456}"
+do_test shell1-3-29.1 {
+ catchcmd "test.db" ".print this is a test"
+} {0 {this is a test}}
+
# Test the output of the ".dump" command
#
do_test shell1-4.1 {
db eval {
CREATE TABLE t1(x);
- INSERT INTO t1 VALUES(null), (1), (2.25), ('hello'), (x'807f');
+ INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
}
catchcmd test.db {.dump}
} {0 {PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE t1(x);
INSERT INTO "t1" VALUES(NULL);
+INSERT INTO "t1" VALUES('');
INSERT INTO "t1" VALUES(1);
INSERT INTO "t1" VALUES(2.25);
INSERT INTO "t1" VALUES('hello');
@@ -724,10 +734,58 @@ COMMIT;}}
do_test shell1-4.2 {
catchcmd test.db ".mode insert t1\nselect * from t1;"
} {0 {INSERT INTO t1 VALUES(NULL);
+INSERT INTO t1 VALUES('');
INSERT INTO t1 VALUES(1);
INSERT INTO t1 VALUES(2.25);
INSERT INTO t1 VALUES('hello');
INSERT INTO t1 VALUES(X'807f');}}
+# Test the output of ".mode tcl"
+#
+do_test shell1-4.3 {
+ catchcmd test.db ".mode tcl\nselect * from t1;"
+} {0 {""
+""
+"1"
+"2.25"
+"hello"
+"\200\177"}}
+
+# Test the output of ".mode tcl" with multiple columns
+#
+do_test shell1-4.4 {
+ db eval {
+ CREATE TABLE t2(x,y);
+ INSERT INTO t2 VALUES(null, ''), (1, 2.25), ('hello', x'807f');
+ }
+ catchcmd test.db ".mode tcl\nselect * from t2;"
+} {0 {"" ""
+"1" "2.25"
+"hello" "\200\177"}}
+
+# Test the output of ".mode tcl" with ".nullvalue"
+#
+do_test shell1-4.5 {
+ catchcmd test.db ".mode tcl\n.nullvalue NULL\nselect * from t2;"
+} {0 {"NULL" ""
+"1" "2.25"
+"hello" "\200\177"}}
+
+# Test the output of ".mode tcl" with Tcl reserved characters
+#
+do_test shell1-4.6 {
+ db eval {
+ CREATE TABLE tcl1(x);
+ INSERT INTO tcl1 VALUES('"'), ('['), (']'), ('\{'), ('\}'), (';'), ('$');
+ }
+ foreach {x y} [catchcmd test.db ".mode tcl\nselect * from tcl1;"] break
+ list $x $y [llength $y]
+} {0 {"\""
+"["
+"]"
+"\\{"
+"\\}"
+";"
+"$"} 7}
finish_test
diff --git a/test/speed1p.test b/test/speed1p.test
index 915f165..6bf7b10 100644
--- a/test/speed1p.test
+++ b/test/speed1p.test
@@ -24,6 +24,8 @@ set testdir [file dirname $argv0]
source $testdir/tester.tcl
speed_trial_init speed1
+sqlite3_memdebug_vfs_oom_test 0
+
# Set a uniform random seed
expr srand(0)
@@ -78,7 +80,6 @@ do_test speed1p-1.0 {
}
} {i2a i2b t1 t2}
-
# 50000 INSERTs on an unindexed table
#
set list {}
diff --git a/test/spellfix.test b/test/spellfix.test
index afef981..dfa487a 100644
--- a/test/spellfix.test
+++ b/test/spellfix.test
@@ -16,7 +16,7 @@ set testprefix spellfix
ifcapable !vtab { finish_test ; return }
-register_spellfix_module db
+load_static_extension db spellfix nextchar
set vocab {
rabbi rabbit rabbits rabble rabid rabies raccoon raccoons race raced racer
@@ -84,6 +84,26 @@ foreach {tn word res} {
} $res
}
+# Tests of the next_char function.
+#
+do_test 1.10 {
+ db eval {
+ CREATE TABLE vocab(w TEXT PRIMARY KEY);
+ INSERT INTO vocab SELECT word FROM t1;
+ }
+} {}
+do_execsql_test 1.11 {
+ SELECT next_char('re','vocab','w');
+} {a}
+do_execsql_test 1.12 {
+ SELECT next_char('r','vocab','w');
+} {ae}
+do_execsql_test 1.13 {
+ SELECT next_char('','vocab','w');
+} {r}
+do_test 1.14 {
+ catchsql {SELECT next_char('','xyzzy','a')}
+} {1 {no such table: xyzzy}}
do_execsql_test 2.1 {
CREATE VIRTUAL TABLE t2 USING spellfix1;
@@ -136,16 +156,52 @@ do_test 3.2 {
}
} {}
-breakpoint
foreach {tn word res} {
1 kos* {kosher 3 kiosk 4 kudo 2 kiss 3 kissed 3}
2 kellj* {killjoy 5 kill 4 killed 4 killer 4 killers 4}
3 kellj {kill 4 kills 5 killjoy 7 keel 4 killed 6}
} {
- do_execsql_test 1.2.$tn {
+ do_execsql_test 3.2.$tn {
SELECT word, matchlen FROM t3 WHERE word MATCH $word
ORDER BY score, word LIMIT 5
} $res
-}
+}
+
+do_execsql_test 4.0 {
+ INSERT INTO t3(command) VALUES('edit_cost_table=NULL');
+}
+foreach {tn word res} {
+ 1 kosher {kosher 0 kisser 51 kissers 76 kissed 126 kisses 126}
+ 2 kellj {keels 60 killjoy 68 kills 80 keel 120 kill 125}
+ 3 kashar {kosher 80 kisser 91 kissers 116 kissed 166 kisses 166}
+} {
+ do_execsql_test 4.1.$tn {
+ SELECT word, distance FROM t3 WHERE word MATCH $word
+ ORDER BY score, word LIMIT 5
+ } $res
+}
+do_execsql_test 5.0 {
+ CREATE TABLE costs2(iLang, cFrom, cTo, iCost);
+ INSERT INTO costs2 VALUES(0, 'a', 'o', 1);
+ INSERT INTO costs2 VALUES(0, 'e', 'o', 4);
+ INSERT INTO costs2 VALUES(0, 'i', 'o', 8);
+ INSERT INTO costs2 VALUES(0, 'u', 'o', 16);
+ INSERT INTO t3(command) VALUES('edit_cost_table="costs2"');
+}
+
+foreach {tn word res} {
+ 1 kasher {kosher 1}
+ 2 kesher {kosher 4}
+ 3 kisher {kosher 8}
+ 4 kosher {kosher 0}
+ 5 kusher {kosher 16}
+} {
+ do_execsql_test 5.1.$tn {
+ SELECT word, distance FROM t3 WHERE word MATCH $word
+ ORDER BY score, word LIMIT 1
+ } $res
+}
+
+
finish_test
diff --git a/test/stat.test b/test/stat.test
index 926d9b7..ac88f7a 100644
--- a/test/stat.test
+++ b/test/stat.test
@@ -76,11 +76,16 @@ do_test stat-1.4 {
do_execsql_test stat-2.1 {
CREATE TABLE t3(a PRIMARY KEY, b);
INSERT INTO t3(rowid, a, b) VALUES(2, a_string(111), a_string(222));
- INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3;
- INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3;
- INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3;
- INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3;
- INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3;
+ INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3
+ ORDER BY rowid;
+ INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3
+ ORDER BY rowid;
+ INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3
+ ORDER BY rowid;
+ INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3
+ ORDER BY rowid;
+ INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3
+ ORDER BY rowid;
SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
FROM stat WHERE name != 'sqlite_master';
} [list \
diff --git a/test/subquery.test b/test/subquery.test
index d9d2952..f601d3f 100644
--- a/test/subquery.test
+++ b/test/subquery.test
@@ -421,7 +421,7 @@ do_test subquery-3.5.7 {
# and expose bugs to do with re-using statements that have been
# passed to sqlite3_reset().
#
-# One problem was that VDBE memory cells were not being initialised
+# One problem was that VDBE memory cells were not being initialized
# to NULL on the second and subsequent executions.
#
do_test subquery-4.1.1 {
diff --git a/test/subquery2.test b/test/subquery2.test
index 0420004..4406efc 100644
--- a/test/subquery2.test
+++ b/test/subquery2.test
@@ -15,6 +15,7 @@
set testdir [file dirname $argv0]
source $testdir/tester.tcl
+set ::testprefix subquery2
ifcapable !subquery {
finish_test
@@ -82,5 +83,25 @@ do_test subquery2-1.22 {
}
} {1 3 5 7}
+#-------------------------------------------------------------------------
+# Test that ticket d6b36be38a has been fixed.
+do_execsql_test 2.1 {
+ CREATE TABLE t4(a, b);
+ CREATE TABLE t5(a, b);
+ INSERT INTO t5 VALUES(3, 5);
+
+ INSERT INTO t4 VALUES(1, 1);
+ INSERT INTO t4 VALUES(2, 3);
+ INSERT INTO t4 VALUES(3, 6);
+ INSERT INTO t4 VALUES(4, 10);
+ INSERT INTO t4 VALUES(5, 15);
+}
+
+do_execsql_test 2.2 {
+ SELECT *
+ FROM (SELECT * FROM t4 ORDER BY a LIMIT -1 OFFSET 1)
+ LIMIT (SELECT a FROM t5)
+} {2 3 3 6 4 10}
+
finish_test
diff --git a/test/syscall.test b/test/syscall.test
index d841a9a..5bf1225 100644
--- a/test/syscall.test
+++ b/test/syscall.test
@@ -60,7 +60,7 @@ foreach s {
open close access getcwd stat fstat ftruncate
fcntl read pread write pwrite fchmod fallocate
pread64 pwrite64 unlink openDirectory mkdir rmdir
- statvfs fchown umask
+ statvfs fchown umask mmap munmap mremap
} {
if {[test_syscall exists $s]} {lappend syscall_list $s}
}
diff --git a/test/sysfault.test b/test/sysfault.test
index 07d525c..92fb534 100644
--- a/test/sysfault.test
+++ b/test/sysfault.test
@@ -243,5 +243,35 @@ do_faultsim_test 3 -faults vfsfault-* -prep {
faultsim_test_result {0 20000}
}
-finish_test
+#-------------------------------------------------------------------------
+# Test errors in mmap().
+#
+proc vfsfault_install {} {
+ test_syscall reset
+ test_syscall install {mmap}
+}
+
+faultsim_delete_and_reopen
+execsql {
+ CREATE TABLE t1(a, b);
+ INSERT INTO t1 VALUES(1, 2);
+}
+faultsim_save_and_close
+
+do_faultsim_test 4 -faults vfsfault-* -prep {
+ faultsim_restore_and_reopen
+ file_control_chunksize_test db main 8192
+ execsql {
+ PRAGMA mmap_size = 1000000;
+ }
+} -body {
+ test_syscall errno mmap EACCES
+
+ execsql {
+ SELECT * FROM t1;
+ }
+} -test {
+ faultsim_test_result {0 {1 2}} {1 {disk I/O error}}
+}
+finish_test
diff --git a/test/tclsqlite.test b/test/tclsqlite.test
index c954c71..3d9cd46 100644
--- a/test/tclsqlite.test
+++ b/test/tclsqlite.test
@@ -143,11 +143,11 @@ do_test tcl-1.21 {
set v [catch {db total_changes xyz} msg]
lappend v $msg
} {1 {wrong # args: should be "db total_changes "}}
-do_test tcl-1.20 {
+do_test tcl-1.22 {
set v [catch {db copy} msg]
lappend v $msg
} {1 {wrong # args: should be "db copy CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"}}
-do_test tcl-1.21 {
+do_test tcl-1.23 {
set v [catch {sqlite3 db2 test.db -vfs nosuchvfs} msg]
lappend v $msg
} {1 {no such vfs: nosuchvfs}}
diff --git a/test/temptable.test b/test/temptable.test
index 5eeb0f5..6a1e2b9 100644
--- a/test/temptable.test
+++ b/test/temptable.test
@@ -150,7 +150,7 @@ do_test temptable-3.4 {
# Check for correct name collision processing. A name collision can
# occur when process A creates a temporary table T then process B
# creates a permanent table also named T. The temp table in process A
-# hides the existance of the permanent table.
+# hides the existence of the permanent table.
#
do_test temptable-4.1 {
execsql {
diff --git a/test/tester.tcl b/test/tester.tcl
index 68b2c8d..761a36e 100644
--- a/test/tester.tcl
+++ b/test/tester.tcl
@@ -31,6 +31,7 @@
# Test the capability of the SQLite version built into the interpreter to
# determine if a specific test can be run:
#
+# capable EXPR
# ifcapable EXPR
#
# Calulate checksums based on database contents:
@@ -53,6 +54,7 @@
# do_ioerr_test TESTNAME ARGS...
# crashsql ARGS...
# integrity_check TESTNAME ?DB?
+# verify_ex_errcode TESTNAME EXPECTED ?DB?
# do_test TESTNAME SCRIPT EXPECTED
# do_execsql_test TESTNAME SQL EXPECTED
# do_catchsql_test TESTNAME SQL EXPECTED
@@ -121,7 +123,7 @@ if {[info command sqlite_orig]==""} {
set res
} else {
# This command is not opening a new database connection. Pass the
- # arguments through to the C implemenation as the are.
+ # arguments through to the C implementation as the are.
#
uplevel 1 sqlite_orig $args
}
@@ -134,7 +136,7 @@ proc getFileRetries {} {
# NOTE: Return the default number of retries for [file] operations. A
# value of zero or less here means "disabled".
#
- return [expr {$::tcl_platform(platform) eq "windows" ? 10 : 0}]
+ return [expr {$::tcl_platform(platform) eq "windows" ? 50 : 0}]
}
return $::G(file-retries)
}
@@ -460,6 +462,7 @@ if {0==[info exists ::SLAVE]} {
set TC(count) 0
set TC(fail_list) [list]
set TC(omit_list) [list]
+ set TC(warn_list) [list]
proc set_test_counter {counter args} {
if {[llength $args]} {
@@ -494,6 +497,18 @@ proc fail_test {name} {
}
}
+# Remember a warning message to be displayed at the conclusion of all testing
+#
+proc warning {msg {append 1}} {
+ puts "Warning: $msg"
+ set warnList [set_test_counter warn_list]
+ if {$append} {
+ lappend warnList $msg
+ }
+ set_test_counter warn_list $warnList
+}
+
+
# Increment the number of tests run
#
proc incr_ntest {} {
@@ -537,16 +552,19 @@ proc do_test {name cmd expected} {
} else {
if {[regexp {^~?/.*/$} $expected]} {
if {[string index $expected 0]=="~"} {
- set re [string range $expected 2 end-1]
+ set re [string map {# {[-0-9.]+}} [string range $expected 2 end-1]]
set ok [expr {![regexp $re $result]}]
} else {
- set re [string range $expected 1 end-1]
+ set re [string map {# {[-0-9.]+}} [string range $expected 1 end-1]]
set ok [regexp $re $result]
}
} else {
set ok [expr {[string compare $result $expected]==0}]
}
if {!$ok} {
+ # if {![info exists ::testprefix] || $::testprefix eq ""} {
+ # error "no test prefix"
+ # }
puts "\nExpected: \[$expected\]\n Got: \[$result\]"
fail_test $name
} else {
@@ -779,6 +797,9 @@ proc finalize_testing {} {
if {$nErr>0} {
puts "Failures on these tests: [set_test_counter fail_list]"
}
+ foreach warning [set_test_counter warn_list] {
+ puts "Warning: $warning"
+ }
run_thread_tests 1
if {[llength $omitList]>0} {
puts "Omitted test cases:"
@@ -964,6 +985,12 @@ proc integrity_check {name {db db}} {
}
}
+# Check the extended error code
+#
+proc verify_ex_errcode {name expected {db db}} {
+ do_test $name [list sqlite3_extended_errcode $db] $expected
+}
+
# Return true if the SQL statement passed as the second argument uses a
# statement transaction.
@@ -994,6 +1021,12 @@ proc fix_ifcapable_expr {expr} {
return $ret
}
+# Returns non-zero if the capabilities are present; zero otherwise.
+#
+proc capable {expr} {
+ set e [fix_ifcapable_expr $expr]; return [expr ($e)]
+}
+
# Evaluate a boolean expression of capabilities. If true, execute the
# code. Omit the code if false.
#
@@ -1020,7 +1053,7 @@ proc ifcapable {expr code {else ""} {elsecode ""}} {
# boolean, indicating whether or not the process actually crashed or
# reported some other error. The second element in the returned list is the
# error message. This is "child process exited abnormally" if the crash
-# occured.
+# occurred.
#
# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
#
@@ -1101,6 +1134,25 @@ proc crashsql {args} {
lappend r $msg
}
+proc run_ioerr_prep {} {
+ set ::sqlite_io_error_pending 0
+ catch {db close}
+ catch {db2 close}
+ catch {forcedelete test.db}
+ catch {forcedelete test.db-journal}
+ catch {forcedelete test2.db}
+ catch {forcedelete test2.db-journal}
+ set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
+ sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
+ if {[info exists ::ioerropts(-tclprep)]} {
+ eval $::ioerropts(-tclprep)
+ }
+ if {[info exists ::ioerropts(-sqlprep)]} {
+ execsql $::ioerropts(-sqlprep)
+ }
+ expr 0
+}
+
# Usage: do_ioerr_test <test number> <options...>
#
# This proc is used to implement test cases that check that IO errors
@@ -1133,10 +1185,26 @@ proc do_ioerr_test {testname args} {
# TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
# a couple of obscure IO errors that do not return them.
set ::ioerropts(-erc) 0
+
+ # Create a single TCL script from the TCL and SQL specified
+ # as the body of the test.
+ set ::ioerrorbody {}
+ if {[info exists ::ioerropts(-tclbody)]} {
+ append ::ioerrorbody "$::ioerropts(-tclbody)\n"
+ }
+ if {[info exists ::ioerropts(-sqlbody)]} {
+ append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
+ }
+
+ save_prng_state
+ if {$::ioerropts(-cksum)} {
+ run_ioerr_prep
+ eval $::ioerrorbody
+ set ::goodcksum [cksum]
+ }
set ::go 1
#reset_prng_state
- save_prng_state
for {set n $::ioerropts(-start)} {$::go} {incr n} {
set ::TN $n
incr ::ioerropts(-count) -1
@@ -1153,27 +1221,12 @@ proc do_ioerr_test {testname args} {
# Delete the files test.db and test2.db, then execute the TCL and
# SQL (in that order) to prepare for the test case.
do_test $testname.$n.1 {
- set ::sqlite_io_error_pending 0
- catch {db close}
- catch {db2 close}
- catch {forcedelete test.db}
- catch {forcedelete test.db-journal}
- catch {forcedelete test2.db}
- catch {forcedelete test2.db-journal}
- set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
- sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
- if {[info exists ::ioerropts(-tclprep)]} {
- eval $::ioerropts(-tclprep)
- }
- if {[info exists ::ioerropts(-sqlprep)]} {
- execsql $::ioerropts(-sqlprep)
- }
- expr 0
+ run_ioerr_prep
} {0}
# Read the 'checksum' of the database.
if {$::ioerropts(-cksum)} {
- set checksum [cksum]
+ set ::checksum [cksum]
}
# Set the Nth IO error to fail.
@@ -1181,20 +1234,10 @@ proc do_ioerr_test {testname args} {
set ::sqlite_io_error_persist $::ioerropts(-persist)
set ::sqlite_io_error_pending $n
}] $n
-
- # Create a single TCL script from the TCL and SQL specified
- # as the body of the test.
- set ::ioerrorbody {}
- if {[info exists ::ioerropts(-tclbody)]} {
- append ::ioerrorbody "$::ioerropts(-tclbody)\n"
- }
- if {[info exists ::ioerropts(-sqlbody)]} {
- append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
- }
- # Execute the TCL Script created in the above block. If
- # there are at least N IO operations performed by SQLite as
- # a result of the script, the Nth will fail.
+ # Execute the TCL script created for the body of this test. If
+ # at least N IO operations performed by SQLite as a result of
+ # the script, the Nth will fail.
do_test $testname.$n.3 {
set ::sqlite_io_error_hit 0
set ::sqlite_io_error_hardhit 0
@@ -1290,7 +1333,7 @@ proc do_ioerr_test {testname args} {
}
}
- # If an IO error occured, then the checksum of the database should
+ # If an IO error occurred, then the checksum of the database should
# be the same as before the script that caused the IO error was run.
#
if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
@@ -1298,8 +1341,15 @@ proc do_ioerr_test {testname args} {
catch {db close}
catch {db2 close}
set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
- cksum
- } $checksum
+ set nowcksum [cksum]
+ set res [expr {$nowcksum==$::checksum || $nowcksum==$::goodcksum}]
+ if {$res==0} {
+ puts "now=$nowcksum"
+ puts "the=$::checksum"
+ puts "fwd=$::goodcksum"
+ }
+ set res
+ } 1
}
set ::sqlite_io_error_hardhit 0
diff --git a/test/thread001.test b/test/thread001.test
index 7e0893f..a796c57 100644
--- a/test/thread001.test
+++ b/test/thread001.test
@@ -87,7 +87,7 @@ foreach {tn same_db shared_cache} [list \
do_test t1 {
execsql {
SELECT
- (SELECT md5sum(a, b) FROM ab WHERE a < (SELECT max(a) FROM ab)) ==
+ (SELECT md5sum(a, b) FROM ab WHERE +a < (SELECT max(a) FROM ab)) ==
(SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab))
}
} {1}
@@ -131,7 +131,7 @@ foreach {tn same_db shared_cache} [list \
do_test thread001.$tn.6 {
execsql {
SELECT
- (SELECT md5sum(a, b) FROM ab WHERE a < (SELECT max(a) FROM ab)) ==
+ (SELECT md5sum(a, b) FROM ab WHERE +a < (SELECT max(a) FROM ab)) ==
(SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab))
}
} {1}
diff --git a/test/tkt-2d1a5c67d.test b/test/tkt-2d1a5c67d.test
index bf9595f..3fef187 100644
--- a/test/tkt-2d1a5c67d.test
+++ b/test/tkt-2d1a5c67d.test
@@ -46,7 +46,7 @@ for {set ii 1} {$ii<=10} {incr ii} {
db close
forcedelete test.db test.db-wal
sqlite3 db test.db
-register_wholenumber_module db
+load_static_extension db wholenumber
db eval {
PRAGMA journal_mode=WAL;
CREATE TABLE t1(a,b);
diff --git a/test/tkt-31338dca7e.test b/test/tkt-31338dca7e.test
index 9423c68..41dd9d3 100644
--- a/test/tkt-31338dca7e.test
+++ b/test/tkt-31338dca7e.test
@@ -91,7 +91,7 @@ do_test tkt-31338-3.1 {
INSERT INTO t3 VALUES(4);
CREATE TABLE t4(h);
INSERT INTO t4 VALUES(5);
-
+
SELECT * FROM t3 LEFT JOIN t1 ON d=g LEFT JOIN t4 ON c=h
WHERE (a=1 AND h=4)
OR (b IN (
diff --git a/test/tkt-385a5b56b9.test b/test/tkt-385a5b56b9.test
index 614e82d..8184864 100644
--- a/test/tkt-385a5b56b9.test
+++ b/test/tkt-385a5b56b9.test
@@ -39,7 +39,7 @@ do_eqp_test 2.1 { SELECT DISTINCT x FROM t2 } {
}
do_eqp_test 2.2 { SELECT DISTINCT y FROM t2 } {
- 0 0 0 {SCAN TABLE t2 (~1000000 rows)}
+ 0 0 0 {SCAN TABLE t2 USING COVERING INDEX t2y (~1000000 rows)}
}
do_eqp_test 2.3 { SELECT DISTINCT x, y FROM t2 WHERE y=10 } {
@@ -51,4 +51,3 @@ do_eqp_test 2.4 { SELECT DISTINCT x, y FROM t2 WHERE x=10 } {
}
finish_test
-
diff --git a/test/tkt-4dd95f6943.test b/test/tkt-4dd95f6943.test
new file mode 100644
index 0000000..353031e
--- /dev/null
+++ b/test/tkt-4dd95f6943.test
@@ -0,0 +1,151 @@
+# 2013 March 13
+#
+# 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 file implements regression tests for SQLite library.
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set ::testprefix tkt-4dd95f6943
+
+do_execsql_test 1.0 {
+ CREATE TABLE t1(x);
+ INSERT INTO t1 VALUES (3), (4), (2), (1), (5), (6);
+}
+
+foreach {tn1 idx} {
+ 1 { CREATE INDEX i1 ON t1(x ASC) }
+ 2 { CREATE INDEX i1 ON t1(x DESC) }
+} {
+ do_execsql_test 1.$tn1.1 { DROP INDEX IF EXISTS i1; }
+ do_execsql_test 1.$tn1.2 $idx
+
+ do_execsql_test 1.$tn1.3 {
+ SELECT x FROM t1 WHERE x IN(2, 4, 5) ORDER BY x ASC;
+ } {2 4 5}
+
+ do_execsql_test 1.$tn1.4 {
+ SELECT x FROM t1 WHERE x IN(2, 4, 5) ORDER BY x DESC;
+ } {5 4 2}
+}
+
+
+do_execsql_test 2.0 {
+ CREATE TABLE t2(x, y);
+ INSERT INTO t2 VALUES (5, 3), (5, 4), (5, 2), (5, 1), (5, 5), (5, 6);
+ INSERT INTO t2 VALUES (1, 3), (1, 4), (1, 2), (1, 1), (1, 5), (1, 6);
+ INSERT INTO t2 VALUES (3, 3), (3, 4), (3, 2), (3, 1), (3, 5), (3, 6);
+ INSERT INTO t2 VALUES (2, 3), (2, 4), (2, 2), (2, 1), (2, 5), (2, 6);
+ INSERT INTO t2 VALUES (4, 3), (4, 4), (4, 2), (4, 1), (4, 5), (4, 6);
+ INSERT INTO t2 VALUES (6, 3), (6, 4), (6, 2), (6, 1), (6, 5), (6, 6);
+
+ CREATE TABLE t3(a, b);
+ INSERT INTO t3 VALUES (2, 2), (4, 4), (5, 5);
+ CREATE UNIQUE INDEX t3i1 ON t3(a ASC);
+ CREATE UNIQUE INDEX t3i2 ON t3(b DESC);
+}
+
+foreach {tn1 idx} {
+ 1 { CREATE INDEX i1 ON t2(x ASC, y ASC) }
+ 2 { CREATE INDEX i1 ON t2(x ASC, y DESC) }
+ 3 { CREATE INDEX i1 ON t2(x DESC, y ASC) }
+ 4 { CREATE INDEX i1 ON t2(x DESC, y DESC) }
+
+ 5 { CREATE INDEX i1 ON t2(y ASC, x ASC) }
+ 6 { CREATE INDEX i1 ON t2(y ASC, x DESC) }
+ 7 { CREATE INDEX i1 ON t2(y DESC, x ASC) }
+ 8 { CREATE INDEX i1 ON t2(y DESC, x DESC) }
+} {
+ do_execsql_test 2.$tn1.1 { DROP INDEX IF EXISTS i1; }
+ do_execsql_test 2.$tn1.2 $idx
+
+ foreach {tn2 inexpr} {
+ 3 "(2, 4, 5)"
+ 4 "(SELECT a FROM t3)"
+ 5 "(SELECT b FROM t3)"
+ } {
+ do_execsql_test 2.$tn1.$tn2.1 "
+ SELECT x, y FROM t2 WHERE x = 1 AND y IN $inexpr ORDER BY x ASC, y ASC;
+ " {1 2 1 4 1 5}
+
+ do_execsql_test 2.$tn1.$tn2.2 "
+ SELECT x, y FROM t2 WHERE x = 2 AND y IN $inexpr ORDER BY x ASC, y DESC;
+ " {2 5 2 4 2 2}
+
+ do_execsql_test 2.$tn1.$tn2.3 "
+ SELECT x, y FROM t2 WHERE x = 3 AND y IN $inexpr ORDER BY x DESC, y ASC;
+ " {3 2 3 4 3 5}
+
+ do_execsql_test 2.$tn1.$tn2.4 "
+ SELECT x, y FROM t2 WHERE x = 4 AND y IN $inexpr ORDER BY x DESC, y DESC;
+ " {4 5 4 4 4 2}
+
+ do_execsql_test 2.$tn1.$tn2.5 "
+ SELECT a, x, y FROM t2, t3 WHERE a = 4 AND x = 1 AND y IN $inexpr
+ ORDER BY a, x ASC, y ASC;
+ " {4 1 2 4 1 4 4 1 5}
+ do_execsql_test 2.$tn1.$tn2.6 "
+ SELECT a, x, y FROM t2, t3 WHERE a = 2 AND x = 1 AND y IN $inexpr
+ ORDER BY x ASC, y ASC;
+ " {2 1 2 2 1 4 2 1 5}
+
+ do_execsql_test 2.$tn1.$tn2.7 "
+ SELECT a, x, y FROM t2, t3 WHERE a = 4 AND x = 1 AND y IN $inexpr
+ ORDER BY a, x ASC, y DESC;
+ " {4 1 5 4 1 4 4 1 2}
+ do_execsql_test 2.$tn1.8 "
+ SELECT a, x, y FROM t2, t3 WHERE a = 2 AND x = 1 AND y IN $inexpr
+ ORDER BY x ASC, y DESC;
+ " {2 1 5 2 1 4 2 1 2}
+
+ do_execsql_test 2.$tn1.$tn2.9 "
+ SELECT a, x, y FROM t2, t3 WHERE a = 4 AND x = 1 AND y IN $inexpr
+ ORDER BY a, x DESC, y ASC;
+ " {4 1 2 4 1 4 4 1 5}
+ do_execsql_test 2.$tn1.10 "
+ SELECT a, x, y FROM t2, t3 WHERE a = 2 AND x = 1 AND y IN $inexpr
+ ORDER BY x DESC, y ASC;
+ " {2 1 2 2 1 4 2 1 5}
+
+ do_execsql_test 2.$tn1.$tn2.11 "
+ SELECT a, x, y FROM t2, t3 WHERE a = 4 AND x = 1 AND y IN $inexpr
+ ORDER BY a, x DESC, y DESC;
+ " {4 1 5 4 1 4 4 1 2}
+ do_execsql_test 2.$tn1.$tn2.12 "
+ SELECT a, x, y FROM t2, t3 WHERE a = 2 AND x = 1 AND y IN $inexpr
+ ORDER BY x DESC, y DESC;
+ " {2 1 5 2 1 4 2 1 2}
+ }
+}
+
+do_execsql_test 3.0 {
+ CREATE TABLE t7(x);
+ INSERT INTO t7 VALUES (1), (2), (3);
+ CREATE INDEX i7 ON t7(x);
+
+ CREATE TABLE t8(y);
+ INSERT INTO t8 VALUES (1), (2), (3);
+}
+
+foreach {tn idxdir sortdir sortdata} {
+ 1 ASC ASC {1 2 3}
+ 2 ASC DESC {3 2 1}
+ 3 DESC ASC {1 2 3}
+ 4 ASC DESC {3 2 1}
+} {
+
+ do_execsql_test 3.$tn "
+ DROP INDEX IF EXISTS i8;
+ CREATE UNIQUE INDEX i8 ON t8(y $idxdir);
+ SELECT x FROM t7 WHERE x IN (SELECT y FROM t8) ORDER BY x $sortdir;
+ " $sortdata
+}
+
+finish_test
diff --git a/test/tkt-5d863f876e.test b/test/tkt-5d863f876e.test
index 0a9017d..86024e3 100644
--- a/test/tkt-5d863f876e.test
+++ b/test/tkt-5d863f876e.test
@@ -17,6 +17,8 @@
set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/lock_common.tcl
+set ::testprefix tkt-5d863f876e
+ifcapable !wal {finish_test ; return }
do_multiclient_test tn {
do_test $tn.1 {
diff --git a/test/tkt-6bfb98dfc0.test b/test/tkt-6bfb98dfc0.test
new file mode 100644
index 0000000..675a3fc
--- /dev/null
+++ b/test/tkt-6bfb98dfc0.test
@@ -0,0 +1,61 @@
+# 2013 March 27
+#
+# 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 file implements regression tests for SQLite library. Specifically,
+# it tests that ticket [6bfb98dfc0]
+#
+# The final INSERT in the script below reports that the database is
+# corrupt (SQLITE_CORRUPT) and aborts even though the database is not
+# corrupt.
+#
+# PRAGMA page_size=512;
+# CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
+# INSERT INTO t1 VALUES(1,randomblob(400));
+# INSERT INTO t1 VALUES(2,randomblob(400));
+# INSERT INTO t1 SELECT x+2, randomblob(400) FROM t1;
+# INSERT INTO t1 SELECT x+4, randomblob(400) FROM t1;
+# INSERT INTO t1 SELECT x+8, randomblob(400) FROM t1;
+# INSERT INTO t1 SELECT x+16, randomblob(400) FROM t1;
+# INSERT INTO t1 SELECT x+32, randomblob(400) FROM t1;
+# INSERT INTO t1 SELECT x+64, randomblob(400) FROM t1 WHERE x<10;
+# CREATE TRIGGER r1 AFTER INSERT ON t1 WHEN new.x=74 BEGIN
+# DELETE FROM t1;
+# INSERT INTO t1 VALUES(75, randomblob(400));
+# INSERT INTO t1 VALUES(76, randomblob(400));
+# END;
+# INSERT INTO t1 VALUES(74, randomblob(400));
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+
+do_test tkt-6bfb98dfc0.100 {
+ db eval {
+ PRAGMA page_size=512;
+ CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
+ INSERT INTO t1 VALUES(1,randomblob(400));
+ INSERT INTO t1 VALUES(2,randomblob(400));
+ INSERT INTO t1 SELECT x+2, randomblob(400) FROM t1;
+ INSERT INTO t1 SELECT x+4, randomblob(400) FROM t1;
+ INSERT INTO t1 SELECT x+8, randomblob(400) FROM t1;
+ INSERT INTO t1 SELECT x+16, randomblob(400) FROM t1;
+ INSERT INTO t1 SELECT x+32, randomblob(400) FROM t1;
+ INSERT INTO t1 SELECT x+64, randomblob(400) FROM t1 WHERE x<10;
+ CREATE TRIGGER r1 AFTER INSERT ON t1 WHEN new.x=74 BEGIN
+ DELETE FROM t1;
+ INSERT INTO t1 VALUES(75, randomblob(400));
+ INSERT INTO t1 VALUES(76, randomblob(400));
+ END;
+ INSERT INTO t1 VALUES(74, randomblob(400));
+ SELECT x, length(y) FROM t1 ORDER BY x;
+ }
+} {75 400 76 400}
+
+finish_test
diff --git a/test/tkt-78e04e52ea.test b/test/tkt-78e04e52ea.test
index 9524d84..a664ceb 100644
--- a/test/tkt-78e04e52ea.test
+++ b/test/tkt-78e04e52ea.test
@@ -44,7 +44,7 @@ do_test tkt-78e04-1.4 {
execsql {
EXPLAIN QUERY PLAN SELECT * FROM "" WHERE "" LIKE 'abc%';
}
-} {0 0 0 {SCAN TABLE (~500000 rows)}}
+} {0 0 0 {SCAN TABLE USING COVERING INDEX i1 (~500000 rows)}}
do_test tkt-78e04-1.5 {
execsql {
DROP TABLE "";
diff --git a/test/tkt-7a31705a7e6.test b/test/tkt-7a31705a7e6.test
new file mode 100644
index 0000000..6470122
--- /dev/null
+++ b/test/tkt-7a31705a7e6.test
@@ -0,0 +1,26 @@
+# 2013 February 26
+#
+# 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 file implements regression tests for SQLite library. Specifically,
+# it tests that ticket [7a31705a7e6c95d514e6f20a6900f436bbc9fed8] in the
+# name resolver has been fixed.
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+
+do_execsql_test tkt-7a31705a7e6-1.1 {
+ CREATE TABLE t1 (a INTEGER PRIMARY KEY);
+ CREATE TABLE t2 (a INTEGER PRIMARY KEY, b INTEGER);
+ CREATE TABLE t2x (b INTEGER PRIMARY KEY);
+ SELECT t1.a FROM ((t1 JOIN t2 ON t1.a=t2.a) AS x JOIN t2x ON x.b=t2x.b) as y;
+} {}
+
diff --git a/test/tkt-80ba201079.test b/test/tkt-80ba201079.test
index 0122e95..ea0799b 100644
--- a/test/tkt-80ba201079.test
+++ b/test/tkt-80ba201079.test
@@ -17,7 +17,7 @@
set testdir [file dirname $argv0]
source $testdir/tester.tcl
-set ::testprefix tkt-80ba2
+set ::testprefix tkt-80ba201079
do_test tkt-80ba2-100 {
db eval {
diff --git a/test/tkt-a7b7803e.test b/test/tkt-a7b7803e.test
new file mode 100644
index 0000000..b617cf6
--- /dev/null
+++ b/test/tkt-a7b7803e.test
@@ -0,0 +1,84 @@
+# 2012 December 19
+#
+# 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 file implements regression tests for SQLite library. Specifically,
+# it tests that ticket [a7b7803e8d1e8699cd8a460a38133b98892d2e17] has
+# been fixed.
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+source $testdir/lock_common.tcl
+source $testdir/malloc_common.tcl
+
+do_test tkt-a7b7803e.1 {
+ db eval {
+ CREATE TABLE t1(a,b);
+ INSERT INTO t1 VALUES(0,'first'),(99,'fuzzy');
+ SELECT (t1.a==0) AS x, b
+ FROM t1
+ WHERE a=0 OR x;
+ }
+} {1 first}
+do_test tkt-a7b7803e.2 {
+ db eval {
+ SELECT a, (t1.b='fuzzy') AS x
+ FROM t1
+ WHERE x
+ }
+} {99 1}
+do_test tkt-a7b7803e.3 {
+ db eval {
+ SELECT (a=99) AS x, (t1.b='fuzzy') AS y, *
+ FROM t1
+ WHERE x AND y
+ }
+} {1 1 99 fuzzy}
+do_test tkt-a7b7803e.4 {
+ db eval {
+ SELECT (a=99) AS x, (t1.b='first') AS y, *
+ FROM t1
+ WHERE x OR y
+ ORDER BY a
+ }
+} {0 1 0 first 1 0 99 fuzzy}
+do_test tkt-a7b7803e.5 {
+ db eval {
+ SELECT (M.a=99) AS x, M.b, (N.b='first') AS y, N.b
+ FROM t1 M, t1 N
+ WHERE x OR y
+ ORDER BY M.a, N.a
+ }
+} {0 first 1 first 1 fuzzy 1 first 1 fuzzy 0 fuzzy}
+do_test tkt-a7b7803e.6 {
+ db eval {
+ SELECT (M.a=99) AS x, M.b, (N.b='first') AS y, N.b
+ FROM t1 M, t1 N
+ WHERE x AND y
+ ORDER BY M.a, N.a
+ }
+} {1 fuzzy 1 first}
+do_test tkt-a7b7803e.7 {
+ db eval {
+ SELECT (M.a=99) AS x, M.b, (N.b='first') AS y, N.b
+ FROM t1 M JOIN t1 N ON x AND y
+ ORDER BY M.a, N.a
+ }
+} {1 fuzzy 1 first}
+do_test tkt-a7b7803e.8 {
+ db eval {
+ SELECT (M.a=99) AS x, M.b, (N.b='first') AS y, N.b
+ FROM t1 M JOIN t1 N ON x
+ ORDER BY M.a, N.a
+ }
+} {1 fuzzy 1 first 1 fuzzy 0 fuzzy}
+
+
+finish_test
diff --git a/test/tkt-cbd054fa6b.test b/test/tkt-cbd054fa6b.test
index 180acf5..51e0199 100644
--- a/test/tkt-cbd054fa6b.test
+++ b/test/tkt-cbd054fa6b.test
@@ -50,7 +50,7 @@ do_test tkt-cbd05-1.3 {
WHERE idx = 't1_x'
GROUP BY tbl,idx
}
-} {t1 t1_x { A B C D E F G H I}}
+} {/t1 t1_x .[ ABCDEFGHI]{10}./}
do_test tkt-cbd05-2.1 {
db eval {
@@ -82,6 +82,6 @@ do_test tkt-cbd05-2.3 {
WHERE idx = 't1_x'
GROUP BY tbl,idx
}
-} {t1 t1_x { A B C D E F G H I}}
+} {/t1 t1_x .[ ABCDEFGHI]{10}./}
finish_test
diff --git a/test/tkt-fc7bd6358f.test b/test/tkt-fc7bd6358f.test
new file mode 100644
index 0000000..a1b13c4
--- /dev/null
+++ b/test/tkt-fc7bd6358f.test
@@ -0,0 +1,78 @@
+# 2013 March 05
+#
+# 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 file implements regression tests for SQLite library. Specifically,
+# it tests that ticket [fc7bd6358f]:
+#
+# The following SQL yields an incorrect result (zero rows) in all
+# versions of SQLite between 3.6.14 and 3.7.15.2:
+#
+# CREATE TABLE t(textid TEXT);
+# INSERT INTO t VALUES('12');
+# INSERT INTO t VALUES('34');
+# CREATE TABLE i(intid INTEGER PRIMARY KEY);
+# INSERT INTO i VALUES(12);
+# INSERT INTO i VALUES(34);
+#
+# SELECT t1.textid AS a, i.intid AS b, t2.textid AS c
+# FROM t t1, i, t t2
+# WHERE t1.textid = i.intid
+# AND t1.textid = t2.textid;
+#
+# The correct result should be two rows, one with 12|12|12 and the other
+# with 34|34|34. With this bug, no rows are returned. Bisecting shows that
+# this bug was introduced with check-in [dd4d67a67454] on 2009-04-23.
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+
+do_test tkt-fc7bd6358f.100 {
+ db eval {
+ CREATE TABLE t(textid TEXT);
+ INSERT INTO t VALUES('12');
+ INSERT INTO t VALUES('34');
+ CREATE TABLE i(intid INTEGER PRIMARY KEY);
+ INSERT INTO i VALUES(12);
+ INSERT INTO i VALUES(34);
+ }
+} {}
+unset -nocomplain from
+unset -nocomplain where
+unset -nocomplain a
+unset -nocomplain b
+foreach {a from} {
+ 1 {FROM t t1, i, t t2}
+ 2 {FROM i, t t1, t t2}
+ 3 {FROM t t1, t t2, i}
+} {
+ foreach {b where} {
+ 1 {WHERE t1.textid=i.intid AND t1.textid=t2.textid}
+ 2 {WHERE i.intid=t1.textid AND t1.textid=t2.textid}
+ 3 {WHERE t1.textid=i.intid AND i.intid=t2.textid}
+ 4 {WHERE t1.textid=i.intid AND t2.textid=i.intid}
+ 5 {WHERE i.intid=t1.textid AND i.intid=t2.textid}
+ 6 {WHERE i.intid=t1.textid AND t2.textid=i.intid}
+ 7 {WHERE t1.textid=t2.textid AND i.intid=t2.textid}
+ 8 {WHERE t1.textid=t2.textid AND t2.textid=i.intid}
+ } {
+ do_test tkt-fc7bd6358f.110.$a.$b.1 {
+ db eval {PRAGMA automatic_index=ON}
+ db eval "SELECT t1.textid, i.intid, t2.textid $from $where"
+ } {12 12 12 34 34 34}
+ do_test tkt-fc7bd6358f.110.$a.$b.2 {
+ db eval {PRAGMA automatic_index=OFF}
+ db eval "SELECT t1.textid, i.intid, t2.textid $from $where"
+ } {12 12 12 34 34 34}
+ }
+}
+
+
+finish_test
diff --git a/test/tkt2409.test b/test/tkt2409.test
index 9ac6542..936f57c 100644
--- a/test/tkt2409.test
+++ b/test/tkt2409.test
@@ -107,7 +107,7 @@ do_test tkt2409-1.2 {
integrity_check tkt2409-1.3
# Check that the transaction was rolled back. Because the INSERT
-# statement in which the "I/O error" occured did not open a statement
+# statement in which the "I/O error" occurred did not open a statement
# transaction, SQLite had no choice but to roll back the transaction.
#
do_test tkt2409-1.4 {
@@ -175,7 +175,7 @@ do_test tkt2409-3.2 {
integrity_check tkt2409-3.3
# Check that the transaction was rolled back. Because the INSERT
-# statement in which the "I/O error" occured did not open a statement
+# statement in which the "I/O error" occurred did not open a statement
# transaction, SQLite had no choice but to roll back the transaction.
#
do_test tkt2409-3.4 {
diff --git a/test/tkt2822.test b/test/tkt2822.test
index 281e5dc..d3512d3 100644
--- a/test/tkt2822.test
+++ b/test/tkt2822.test
@@ -208,12 +208,15 @@ do_test tkt2822-5.4 {
# In "ORDER BY +b" the term is now an expression rather than
# a label. It therefore matches by rule (3) instead of rule (2).
+#
+# 2013-04-13: This is busted. Changed to conform to PostgreSQL and
+# MySQL and Oracle behavior.
#
do_test tkt2822-5.5 {
execsql {
SELECT a AS b FROM t3 ORDER BY +b;
}
-} {9 1}
+} {1 9}
# Tests for rule 2 in compound queries
#
@@ -273,11 +276,21 @@ do_test tkt2822-7.1 {
SELECT * FROM t7 ORDER BY 0;
}
} {1 {1st ORDER BY term out of range - should be between 1 and 25}}
-do_test tkt2822-7.2 {
+do_test tkt2822-7.2.1 {
catchsql {
SELECT * FROM t7 ORDER BY 1, 0;
}
} {1 {2nd ORDER BY term out of range - should be between 1 and 25}}
+do_test tkt2822-7.2.2 {
+ catchsql {
+ SELECT * FROM t7 ORDER BY 1, 26;
+ }
+} {1 {2nd ORDER BY term out of range - should be between 1 and 25}}
+do_test tkt2822-7.2.3 {
+ catchsql {
+ SELECT * FROM t7 ORDER BY 1, 65536;
+ }
+} {1 {2nd ORDER BY term out of range - should be between 1 and 25}}
do_test tkt2822-7.3 {
catchsql {
SELECT * FROM t7 ORDER BY 1, 2, 0;
diff --git a/test/tkt3457.test b/test/tkt3457.test
index 7b9a1b3..0475741 100644
--- a/test/tkt3457.test
+++ b/test/tkt3457.test
@@ -62,6 +62,14 @@ do_test tkt3457-1.1 {
execsql COMMIT
} {}
+# Disable fchmod to make sure SQLite itself does not try to change the
+# permission bits on us
+#
+catch {
+ test_syscall install fchmod
+ test_syscall fault 1 1
+}
+
do_test tkt3457-1.2 {
forcecopy bak.db-journal test.db-journal
file attributes test.db-journal -permissions ---------
@@ -84,4 +92,10 @@ do_test tkt3457-1.5 {
catchsql { SELECT * FROM t1 }
} {0 {1 2 3 4 5 6}}
+# Reenable fchmod
+catch {
+ test_syscall uninstall
+ test_syscall fault 0 0
+}
+
finish_test
diff --git a/test/tkt3527.test b/test/tkt3527.test
index d9b1dad..da3d05a 100644
--- a/test/tkt3527.test
+++ b/test/tkt3527.test
@@ -52,8 +52,8 @@ do_test tkt3527-1.1 {
INSERT INTO Element VALUES(5,'Elem5');
INSERT INTO ElemOr Values(3,4);
INSERT INTO ElemOr Values(3,5);
- INSERT INTO ElemAnd VALUES(1,3,1,1,1);
- INSERT INTO ElemAnd VALUES(1,2,1,1,1);
+ INSERT INTO ElemAnd VALUES(1,3,'a','b','c');
+ INSERT INTO ElemAnd VALUES(1,2,'x','y','z');
CREATE VIEW ElemView1 AS
SELECT
@@ -112,12 +112,12 @@ do_test tkt3527-1.1 {
SELECT * FROM ElemView1;
}
-} {1 1 Elem1 2 1 1 1 0 0 1 1 Elem1 3 1 1 1 0 0 3 3 Elem3 4 {} {} {} 0 1 3 3 Elem3 5 {} {} {} 0 1}
+} {1 1 Elem1 2 x y z 0 0 1 1 Elem1 3 a b c 0 0 3 3 Elem3 4 {} {} {} 0 1 3 3 Elem3 5 {} {} {} 0 1}
do_test tkt3527-1.2 {
db eval {
SELECT * FROM ElemView2;
}
-} {1 1 Elem1 2 1 1 1 0 0 1 1 Elem1 3 1 1 1 0 0 1.3 3 Elem3 4 {} {} {} 1 1 1.3 3 Elem3 5 {} {} {} 1 1 3 3 Elem3 4 {} {} {} 0 1 3 3 Elem3 5 {} {} {} 0 1}
+} {1 1 Elem1 2 x y z 0 0 1 1 Elem1 3 a b c 0 0 1.3 3 Elem3 4 {} {} {} 1 1 1.3 3 Elem3 5 {} {} {} 1 1 3 3 Elem3 4 {} {} {} 0 1 3 3 Elem3 5 {} {} {} 0 1}
finish_test
diff --git a/test/tkt3762.test b/test/tkt3762.test
index c24041c..424b1e6 100644
--- a/test/tkt3762.test
+++ b/test/tkt3762.test
@@ -10,8 +10,8 @@
#***********************************************************************
#
# Ticket #3762: Make sure that an incremental vacuum that reduces the
-# size of the database file such that a pointer-map page is elemented
-# can be correctly rolled back.
+# size of the database file such that if a pointer-map page is eliminated
+# it can be correctly rolled back.
#
# That ticket #3762 has been fixed has already been verified by the
# savepoint6.test test script. But this script is simplier and a
diff --git a/test/transitive1.test b/test/transitive1.test
new file mode 100644
index 0000000..ff81af6
--- /dev/null
+++ b/test/transitive1.test
@@ -0,0 +1,50 @@
+# 2013 April 17
+#
+# 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 file implements regression tests for SQLite library. The
+# focus of this script is testing of transitive WHERE clause constraints
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+
+do_execsql_test transitive1-100 {
+ CREATE TABLE t1(a TEXT, b TEXT, c TEXT COLLATE NOCASE);
+ INSERT INTO t1 VALUES('abc','abc','Abc');
+ INSERT INTO t1 VALUES('def','def','def');
+ INSERT INTO t1 VALUES('ghi','ghi','GHI');
+ CREATE INDEX t1a1 ON t1(a);
+ CREATE INDEX t1a2 ON t1(a COLLATE nocase);
+
+ SELECT * FROM t1 WHERE a=b AND c=b AND c='DEF';
+} {def def def}
+do_execsql_test transitive1-110 {
+ SELECT * FROM t1 WHERE a=b AND c=b AND c>='DEF' ORDER BY +a;
+} {def def def ghi ghi GHI}
+do_execsql_test transitive1-120 {
+ SELECT * FROM t1 WHERE a=b AND c=b AND c<='DEF' ORDER BY +a;
+} {abc abc Abc def def def}
+
+do_execsql_test transitive1-200 {
+ CREATE TABLE t2(a INTEGER, b INTEGER, c TEXT);
+ INSERT INTO t2 VALUES(100,100,100);
+ INSERT INTO t2 VALUES(20,20,20);
+ INSERT INTO t2 VALUES(3,3,3);
+
+ SELECT * FROM t2 WHERE a=b AND c=b AND c=20;
+} {20 20 20}
+do_execsql_test transitive1-210 {
+ SELECT * FROM t2 WHERE a=b AND c=b AND c>=20 ORDER BY +a;
+} {3 3 3 20 20 20}
+do_execsql_test transitive1-220 {
+ SELECT * FROM t2 WHERE a=b AND c=b AND c<=20 ORDER BY +a;
+} {20 20 20 100 100 100}
+
+finish_test
diff --git a/test/trigger1.test b/test/trigger1.test
index 9d917bd..1ebe12c 100644
--- a/test/trigger1.test
+++ b/test/trigger1.test
@@ -11,20 +11,20 @@
# with the database COMMIT/ROLLBACK logic.
#
# 1. CREATE and DROP TRIGGER tests
-# trig-1.1: Error if table does not exist
-# trig-1.2: Error if trigger already exists
-# trig-1.3: Created triggers are deleted if the transaction is rolled back
-# trig-1.4: DROP TRIGGER removes trigger
-# trig-1.5: Dropped triggers are restored if the transaction is rolled back
-# trig-1.6: Error if dropped trigger doesn't exist
-# trig-1.7: Dropping the table automatically drops all triggers
-# trig-1.8: A trigger created on a TEMP table is not inserted into sqlite_master
-# trig-1.9: Ensure that we cannot create a trigger on sqlite_master
-# trig-1.10:
-# trig-1.11:
-# trig-1.12: Ensure that INSTEAD OF triggers cannot be created on tables
-# trig-1.13: Ensure that AFTER triggers cannot be created on views
-# trig-1.14: Ensure that BEFORE triggers cannot be created on views
+# trigger1-1.1: Error if table does not exist
+# trigger1-1.2: Error if trigger already exists
+# trigger1-1.3: Created triggers are deleted if the transaction is rolled back
+# trigger1-1.4: DROP TRIGGER removes trigger
+# trigger1-1.5: Dropped triggers are restored if the transaction is rolled back
+# trigger1-1.6: Error if dropped trigger doesn't exist
+# trigger1-1.7: Dropping the table automatically drops all triggers
+# trigger1-1.8: A trigger created on a TEMP table is not inserted into sqlite_master
+# trigger1-1.9: Ensure that we cannot create a trigger on sqlite_master
+# trigger1-1.10:
+# trigger1-1.11:
+# trigger1-1.12: Ensure that INSTEAD OF triggers cannot be created on tables
+# trigger1-1.13: Ensure that AFTER triggers cannot be created on views
+# trigger1-1.14: Ensure that BEFORE triggers cannot be created on views
#
set testdir [file dirname $argv0]
@@ -210,7 +210,7 @@ do_test trigger1-1.12 {
delete from t1 WHERE a=old.a+2;
end;
}
-} {1 {cannot create INSTEAD OF trigger on table: main.t1}}
+} {1 {cannot create INSTEAD OF trigger on table: t1}}
ifcapable view {
# Ensure that we cannot create BEFORE triggers on views
@@ -221,7 +221,7 @@ do_test trigger1-1.13 {
delete from t1 WHERE a=old.a+2;
end;
}
-} {1 {cannot create BEFORE trigger on view: main.v1}}
+} {1 {cannot create BEFORE trigger on view: v1}}
# Ensure that we cannot create AFTER triggers on views
do_test trigger1-1.14 {
catchsql {
@@ -231,7 +231,7 @@ do_test trigger1-1.14 {
delete from t1 WHERE a=old.a+2;
end;
}
-} {1 {cannot create AFTER trigger on view: main.v1}}
+} {1 {cannot create AFTER trigger on view: v1}}
} ;# ifcapable view
# Check for memory leaks in the trigger parser
@@ -265,32 +265,32 @@ ifcapable tempdb {
END;
}
} {0 {}}
- do_test trigger-3.2 {
+ do_test trigger1-3.2 {
catchsql {
INSERT INTO t1 VALUES(1,2);
SELECT * FROM t2;
}
} {1 {no such table: main.t2}}
- do_test trigger-3.3 {
+ do_test trigger1-3.3 {
db close
set rc [catch {sqlite3 db test.db} err]
if {$rc} {lappend rc $err}
set rc
} {0}
- do_test trigger-3.4 {
+ do_test trigger1-3.4 {
catchsql {
INSERT INTO t1 VALUES(1,2);
SELECT * FROM t2;
}
} {1 {no such table: main.t2}}
- do_test trigger-3.5 {
+ do_test trigger1-3.5 {
catchsql {
CREATE TEMP TABLE t2(x,y);
INSERT INTO t1 VALUES(1,2);
SELECT * FROM t2;
}
} {1 {no such table: main.t2}}
- do_test trigger-3.6.1 {
+ do_test trigger1-3.6.1 {
catchsql {
DROP TRIGGER r1;
CREATE TEMP TRIGGER r1 AFTER INSERT ON t1 BEGIN
@@ -300,7 +300,7 @@ ifcapable tempdb {
SELECT * FROM t2;
}
} {0 {1 2 200 100}}
- do_test trigger-3.6.2 {
+ do_test trigger1-3.6.2 {
catchsql {
DROP TRIGGER r1;
DELETE FROM t1;
@@ -312,7 +312,7 @@ ifcapable tempdb {
SELECT * FROM t2;
}
} {0 {1 2}}
- do_test trigger-3.7 {
+ do_test trigger1-3.7 {
execsql {
DROP TABLE t2;
CREATE TABLE t2(x,y);
@@ -320,7 +320,7 @@ ifcapable tempdb {
}
} {}
- # There are two versions of trigger-3.8 and trigger-3.9. One that uses
+ # There are two versions of trigger1-3.8 and trigger1-3.9. One that uses
# compound SELECT statements, and another that does not.
ifcapable compound {
do_test trigger1-3.8 {
@@ -423,6 +423,7 @@ do_test trigger1-6.2 {
do_test trigger1-6.3 {
catchsql {DELETE FROM t2}
} {1 {deletes are not permitted}}
+verify_ex_errcode trigger1-6.3b SQLITE_CONSTRAINT_TRIGGER
do_test trigger1-6.4 {
execsql {SELECT * FROM t2}
} {3 4 7 8}
@@ -446,7 +447,7 @@ do_test trigger1-6.8 {
execsql {SELECT * FROM t2}
} {3 4 7 8}
-integrity_check trigger-7.1
+integrity_check trigger1-7.1
# Check to make sure the name of a trigger can be quoted so that keywords
# can be used as trigger names. Ticket #468
@@ -491,7 +492,7 @@ do_test trigger1-8.6 {
ifcapable conflict {
# Make sure REPLACE works inside of triggers.
#
- # There are two versions of trigger-9.1 and trigger-9.2. One that uses
+ # There are two versions of trigger1-9.1 and trigger1-9.2. One that uses
# compound SELECT statements, and another that does not.
ifcapable compound {
do_test trigger1-9.1 {
@@ -612,7 +613,7 @@ ifcapable tempdb&&attach {
SELECT * FROM insert_log;
}
} {main 11 12 13 temp 14 15 16 aux 17 18 19}
- do_test trigger1-10.8 {
+ do_test trigger1-10.9 {
# Drop and re-create the insert_log table in a different database. Note
# that we can change the column names because the trigger programs don't
# use them explicitly.
diff --git a/test/trigger3.test b/test/trigger3.test
index 34d1970..fe91b9d 100644
--- a/test/trigger3.test
+++ b/test/trigger3.test
@@ -45,6 +45,7 @@ do_test trigger3-1.1 {
INSERT INTO tbl VALUES (1, 5, 6);
}
} {1 {Trigger abort}}
+verify_ex_errcode trigger3-1.1b SQLITE_CONSTRAINT_TRIGGER
do_test trigger3-1.2 {
execsql {
SELECT * FROM tbl;
@@ -63,6 +64,7 @@ do_test trigger3-2.1 {
INSERT INTO tbl VALUES (2, 5, 6);
}
} {1 {Trigger fail}}
+verify_ex_errcode trigger3-2.1b SQLITE_CONSTRAINT_TRIGGER
do_test trigger3-2.2 {
execsql {
SELECT * FROM tbl;
@@ -77,6 +79,7 @@ do_test trigger3-3.1 {
INSERT INTO tbl VALUES (3, 5, 6);
}
} {1 {Trigger rollback}}
+verify_ex_errcode trigger3-3.1b SQLITE_CONSTRAINT_TRIGGER
do_test trigger3-3.2 {
execsql {
SELECT * FROM tbl;
@@ -92,6 +95,7 @@ do_test trigger3-3.3 {
INSERT INTO tbl VALUES (3, 9, 10);
}
} {1 {Trigger rollback}}
+verify_ex_errcode trigger3-3.3b SQLITE_CONSTRAINT_TRIGGER
do_test trigger3-3.4 {
execsql {SELECT * FROM tbl}
} {}
@@ -172,6 +176,7 @@ do_test trigger3-7.1 {
INSERT INTO tbl_view VALUES(1, 2, 3);
}
} {1 {View rollback}}
+verify_ex_errcode trigger3-7.1b SQLITE_CONSTRAINT_TRIGGER
do_test trigger3-7.2 {
catchsql {
INSERT INTO tbl_view VALUES(2, 2, 3);
@@ -182,6 +187,7 @@ do_test trigger3-7.3 {
INSERT INTO tbl_view VALUES(3, 2, 3);
}
} {1 {View abort}}
+verify_ex_errcode trigger3-7.3b SQLITE_CONSTRAINT_TRIGGER
} ;# ifcapable view
diff --git a/test/triggerA.test b/test/triggerA.test
index 0bc017f..821e2d9 100644
--- a/test/triggerA.test
+++ b/test/triggerA.test
@@ -192,6 +192,13 @@ do_test triggerA-2.10 {
SELECT * FROM result4 ORDER BY a;
}
} {3 305 3 9900305 4 404 4 9900404 5 504 5 9900504}
+do_test triggerA-2.11 {
+ db eval {
+ DELETE FROM result4;
+ UPDATE v5 SET b = main.v5.b+9900000 WHERE main.v5.x BETWEEN 3 AND 5;
+ SELECT * FROM result4 ORDER BY a;
+ }
+} {3 305 3 9900305 4 404 4 9900404 5 504 5 9900504}
# Only run the reamining tests if memory debugging is turned on.
#
diff --git a/test/triggerC.test b/test/triggerC.test
index 12a5e4a..8d98487 100644
--- a/test/triggerC.test
+++ b/test/triggerC.test
@@ -222,7 +222,7 @@ foreach {n tdefn rc} {
execsql $tdefn
catchsql {
INSERT INTO t2 VALUES(10);
- SELECT * FROM t2;
+ SELECT * FROM t2 ORDER BY rowid;
}
} $rc
}
@@ -547,7 +547,7 @@ foreach {n insert log} {
eval concat [execsql "
DELETE FROM log;
$insert ;
- SELECT * FROM log;
+ SELECT * FROM log ORDER BY rowid;
"]
} [join $log " "]
}
@@ -584,8 +584,8 @@ foreach {n dml t5g t5} {
execsql "
BEGIN;
$dml ;
- SELECT * FROM t5g;
- SELECT * FROM t5;
+ SELECT * FROM t5g ORDER BY rowid;
+ SELECT * FROM t5 ORDER BY rowid;
ROLLBACK;
"
} [concat $t5g $t5]
@@ -611,8 +611,8 @@ foreach {n dml t5g t5} {
execsql "
BEGIN;
$dml ;
- SELECT * FROM t5g;
- SELECT * FROM t5;
+ SELECT * FROM t5g ORDER BY rowid;
+ SELECT * FROM t5 ORDER BY rowid;
ROLLBACK;
"
} [concat $t5g $t5]
@@ -633,8 +633,8 @@ foreach {n dml t5g t5} {
execsql "
BEGIN;
$dml ;
- SELECT * FROM t5g;
- SELECT * FROM t5;
+ SELECT * FROM t5g ORDER BY rowid;
+ SELECT * FROM t5 ORDER BY rowid;
ROLLBACK;
"
} [concat $t5g $t5]
@@ -949,6 +949,48 @@ do_catchsql_test triggerC-13.2 {
UPDATE t12 SET a=a+1, b=b+1;
} {1 {too many levels of trigger recursion}}
+#-------------------------------------------------------------------------
+# The following tests seek to verify that constant values (i.e. literals)
+# are not factored out of loops within trigger programs. SQLite does
+# not factor constants out of loops within trigger programs as it may only
+# do so in code generated before the first table or index is opened. And
+# by the time a trigger program is coded, at least one table or index has
+# always been opened.
+#
+# At one point, due to a bug allowing constant factoring within triggers,
+# the following SQL would produce the wrong result.
+#
+set SQL {
+ CREATE TABLE t1(a, b, c);
+ CREATE INDEX i1 ON t1(a, c);
+ CREATE INDEX i2 ON t1(b, c);
+ INSERT INTO t1 VALUES(1, 2, 3);
+
+ CREATE TABLE t2(e, f);
+ CREATE INDEX i3 ON t2(e);
+ INSERT INTO t2 VALUES(1234567, 3);
+
+ CREATE TABLE empty(x);
+ CREATE TABLE not_empty(x);
+ INSERT INTO not_empty VALUES(2);
+
+ CREATE TABLE t4(x);
+ CREATE TABLE t5(g, h, i);
+
+ CREATE TRIGGER trig BEFORE INSERT ON t4 BEGIN
+ INSERT INTO t5 SELECT * FROM t1 WHERE
+ (a IN (SELECT x FROM empty) OR b IN (SELECT x FROM not_empty))
+ AND c IN (SELECT f FROM t2 WHERE e=1234567);
+ END;
+
+ INSERT INTO t4 VALUES(0);
+ SELECT * FROM t5;
+}
+reset_db
+do_execsql_test triggerC-14.1 $SQL {1 2 3}
+reset_db
+optimization_control db factor-constants 0
+do_execsql_test triggerC-14.2 $SQL {1 2 3}
finish_test
diff --git a/test/unique.test b/test/unique.test
index 56c49ee..eac19b5 100644
--- a/test/unique.test
+++ b/test/unique.test
@@ -48,6 +48,7 @@ do_test unique-1.3 {
INSERT INTO t1(a,b,c) VALUES(1,3,4)
}
} {1 {column a is not unique}}
+verify_ex_errcode unique-1.3b SQLITE_CONSTRAINT_UNIQUE
do_test unique-1.4 {
execsql {
SELECT * FROM t1 ORDER BY a;
@@ -58,6 +59,7 @@ do_test unique-1.5 {
INSERT INTO t1(a,b,c) VALUES(3,2,4)
}
} {1 {column b is not unique}}
+verify_ex_errcode unique-1.5b SQLITE_CONSTRAINT_UNIQUE
do_test unique-1.6 {
execsql {
SELECT * FROM t1 ORDER BY a;
@@ -99,6 +101,7 @@ do_test unique-2.3 {
INSERT INTO t2 VALUES(1,5);
}
} {1 {column a is not unique}}
+verify_ex_errcode unique-2.3b SQLITE_CONSTRAINT_UNIQUE
do_test unique-2.4 {
catchsql {
SELECT * FROM t2 ORDER BY a
@@ -125,6 +128,7 @@ do_test unique-2.8 {
CREATE UNIQUE INDEX i2 ON t2(a);
}
} {1 {indexed columns are not unique}}
+verify_ex_errcode unique-2.8b SQLITE_CONSTRAINT_UNIQUE
do_test unique-2.9 {
catchsql {
CREATE INDEX i2 ON t2(a);
@@ -163,6 +167,7 @@ do_test unique-3.4 {
SELECT * FROM t3 ORDER BY a,b,c,d;
}
} {1 {columns a, c, d are not unique}}
+verify_ex_errcode unique-3.4b SQLITE_CONSTRAINT_UNIQUE
integrity_check unique-3.5
# Make sure NULLs are distinct as far as the UNIQUE tests are
@@ -217,6 +222,7 @@ do_test unique-4.9 {
do_test unique-4.10 {
catchsql {CREATE UNIQUE INDEX i4c ON t4(b)}
} {1 {indexed columns are not unique}}
+verify_ex_errcode unique-4.10b SQLITE_CONSTRAINT_UNIQUE
integrity_check unique-4.99
# Test the error message generation logic. In particular, make sure we
@@ -249,5 +255,7 @@ do_test unique-5.2 {
INSERT INTO t5 VALUES(1,2,3,4,5,6);
}
} {1 {columns first_column_with_long_name, second_column_with_long_name, third_column_with_long_name, fourth_column_with_long_name, fifth_column_with_long_name, sixth_column_with_long_name are not unique}}
+verify_ex_errcode unique-5.2b SQLITE_CONSTRAINT_UNIQUE
+
finish_test
diff --git a/test/unordered.test b/test/unordered.test
index 6c7c2bb..4aa8310 100644
--- a/test/unordered.test
+++ b/test/unordered.test
@@ -51,7 +51,7 @@ foreach idxmode {ordered unordered} {
0 0 0 {USE TEMP B-TREE FOR ORDER BY}}
4 "SELECT max(a) FROM t1"
{0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (~1 rows)}}
- {0 0 0 {SEARCH TABLE t1 (~1 rows)}}
+ {0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (~1 rows)}}
5 "SELECT group_concat(b) FROM t1 GROUP BY a"
{0 0 0 {SCAN TABLE t1 USING INDEX i1 (~128 rows)}}
{0 0 0 {SCAN TABLE t1 (~128 rows)} 0 0 0 {USE TEMP B-TREE FOR GROUP BY}}
diff --git a/test/view.test b/test/view.test
index b444090..779f77b 100644
--- a/test/view.test
+++ b/test/view.test
@@ -576,4 +576,39 @@ do_test view-20.1 {
}
} {}
+# Ticket [d58ccbb3f1b]: Prevent Table.nRef overflow.
+db close
+sqlite3 db :memory:
+do_test view-21.1 {
+ catchsql {
+ CREATE TABLE t1(x);
+ INSERT INTO t1 VALUES(5);
+ CREATE VIEW v1 AS SELECT x*2 FROM t1;
+ CREATE VIEW v2 AS SELECT * FROM v1 UNION SELECT * FROM v1;
+ CREATE VIEW v4 AS SELECT * FROM v2 UNION SELECT * FROM v2;
+ CREATE VIEW v8 AS SELECT * FROM v4 UNION SELECT * FROM v4;
+ CREATE VIEW v16 AS SELECT * FROM v8 UNION SELECT * FROM v8;
+ CREATE VIEW v32 AS SELECT * FROM v16 UNION SELECT * FROM v16;
+ CREATE VIEW v64 AS SELECT * FROM v32 UNION SELECT * FROM v32;
+ CREATE VIEW v128 AS SELECT * FROM v64 UNION SELECT * FROM v64;
+ CREATE VIEW v256 AS SELECT * FROM v128 UNION SELECT * FROM v128;
+ CREATE VIEW v512 AS SELECT * FROM v256 UNION SELECT * FROM v256;
+ CREATE VIEW v1024 AS SELECT * FROM v512 UNION SELECT * FROM v512;
+ CREATE VIEW v2048 AS SELECT * FROM v1024 UNION SELECT * FROM v1024;
+ CREATE VIEW v4096 AS SELECT * FROM v2048 UNION SELECT * FROM v2048;
+ CREATE VIEW v8192 AS SELECT * FROM v4096 UNION SELECT * FROM v4096;
+ CREATE VIEW v16384 AS SELECT * FROM v8192 UNION SELECT * FROM v8192;
+ CREATE VIEW v32768 AS SELECT * FROM v16384 UNION SELECT * FROM v16384;
+ CREATE VIEW vx AS SELECT * FROM v32768 UNION SELECT * FROM v32768;
+ }
+} {1 {too many references to "v1": max 65535}}
+ifcapable progress {
+ do_test view-21.2 {
+ db progress 1000 {expr 1}
+ catchsql {
+ SELECT * FROM v32768;
+ }
+ } {1 interrupted}
+}
+
finish_test
diff --git a/test/vtab1.test b/test/vtab1.test
index 3409943..1f17e53 100644
--- a/test/vtab1.test
+++ b/test/vtab1.test
@@ -1091,12 +1091,54 @@ do_test vtab1.13-3 {
} {15 {} 16}
+do_test vtab1-14.001 {
+ execsql {SELECT rowid, * FROM echo_c WHERE +rowid IN (1,2,3)}
+} {1 3 G H 2 {} 15 16 3 15 {} 16}
+do_test vtab1-14.002 {
+ execsql {SELECT rowid, * FROM echo_c WHERE rowid IN (1,2,3)}
+} {1 3 G H 2 {} 15 16 3 15 {} 16}
+do_test vtab1-14.003 {
+ execsql {SELECT rowid, * FROM echo_c WHERE +rowid IN (0,1,5,2,'a',3,NULL)}
+} {1 3 G H 2 {} 15 16 3 15 {} 16}
+do_test vtab1-14.004 {
+ execsql {SELECT rowid, * FROM echo_c WHERE rowid IN (0,1,5,'a',2,3,NULL)}
+} {1 3 G H 2 {} 15 16 3 15 {} 16}
+do_test vtab1-14.005 {
+ execsql {SELECT rowid, * FROM echo_c WHERE rowid NOT IN (0,1,5,'a',2,3)}
+} {}
+do_test vtab1-14.006 {
+ execsql {SELECT rowid, * FROM echo_c WHERE rowid NOT IN (0,5,'a',2,3)}
+} {1 3 G H}
+do_test vtab1-14.007 {
+ execsql {SELECT rowid, * FROM echo_c WHERE +rowid NOT IN (0,5,'a',2,3,NULL)}
+} {}
+do_test vtab1-14.008 {
+ execsql {SELECT rowid, * FROM echo_c WHERE rowid NOT IN (0,5,'a',2,3,NULL)}
+} {}
+do_test vtab1-14.011 {
+ execsql {SELECT * FROM echo_c WHERE +a IN (1,3,8,'x',NULL,15,24)}
+} {3 G H 15 {} 16}
+do_test vtab1-14.012 {
+ execsql {SELECT * FROM echo_c WHERE a IN (1,3,8,'x',NULL,15,24)}
+} {3 G H 15 {} 16}
+do_test vtab1-14.013 {
+ execsql {SELECT * FROM echo_c WHERE a NOT IN (1,8,'x',15,24)}
+} {3 G H}
+do_test vtab1-14.014 {
+ execsql {SELECT * FROM echo_c WHERE a NOT IN (1,8,'x',NULL,15,24)}
+} {}
+do_test vtab1-14.015 {
+ execsql {SELECT * FROM echo_c WHERE +a NOT IN (1,8,'x',NULL,15,24)}
+} {}
+
+
+
do_test vtab1-14.1 {
execsql { DELETE FROM c }
set echo_module ""
execsql { SELECT * FROM echo_c WHERE rowid IN (1, 2, 3) }
set echo_module
-} [list xBestIndex {SELECT rowid, * FROM 'c'} xFilter {SELECT rowid, * FROM 'c'}]
+} {/xBestIndex {SELECT rowid, . FROM 'c' WHERE rowid = .} xFilter {SELECT rowid, . FROM 'c' WHERE rowid = .} 1/}
do_test vtab1-14.2 {
set echo_module ""
@@ -1114,7 +1156,7 @@ do_test vtab1-14.4 {
set echo_module ""
execsql { SELECT * FROM echo_c WHERE a IN (1, 2) }
set echo_module
-} [list xBestIndex {SELECT rowid, * FROM 'c'} xFilter {SELECT rowid, * FROM 'c'}]
+} {/xBestIndex {SELECT rowid, . FROM 'c' WHERE a = .} xFilter {SELECT rowid, . FROM 'c' WHERE a = .} 1/}
do_test vtab1-15.1 {
execsql {
@@ -1293,4 +1335,44 @@ do_test 19.3 {
db2 close
} {}
+#-------------------------------------------------------------------------
+# Test that the bug fixed by [b0c1ba655d69] really is fixed.
+#
+do_execsql_test 20.1 {
+ CREATE TABLE t7 (a, b);
+ CREATE TABLE t8 (c, d);
+ CREATE INDEX i2 ON t7(a);
+ CREATE INDEX i3 ON t7(b);
+ CREATE INDEX i4 ON t8(c);
+ CREATE INDEX i5 ON t8(d);
+
+ CREATE VIRTUAL TABLE t7v USING echo(t7);
+ CREATE VIRTUAL TABLE t8v USING echo(t8);
+}
+
+do_test 20.2 {
+ for {set i 0} {$i < 1000} {incr i} {
+ db eval {INSERT INTO t7 VALUES($i, $i)}
+ db eval {INSERT INTO t8 VALUES($i, $i)}
+ }
+} {}
+
+do_execsql_test 20.3 {
+ SELECT a, b FROM (
+ SELECT a, b FROM t7 WHERE a=11 OR b=12
+ UNION ALL
+ SELECT c, d FROM t8 WHERE c=5 OR d=6
+ )
+ ORDER BY 1, 2;
+} {5 5 6 6 11 11 12 12}
+
+do_execsql_test 20.4 {
+ SELECT a, b FROM (
+ SELECT a, b FROM t7v WHERE a=11 OR b=12
+ UNION ALL
+ SELECT c, d FROM t8v WHERE c=5 OR d=6
+ )
+ ORDER BY 1, 2;
+} {5 5 6 6 11 11 12 12}
+
finish_test
diff --git a/test/wal.test b/test/wal.test
index 24ce5f8..c8078a1 100644
--- a/test/wal.test
+++ b/test/wal.test
@@ -727,6 +727,9 @@ do_test wal-11.9 {
list [expr [file size test.db]/1024] [log_deleted test.db-wal]
} {37 1}
sqlite3_wal db test.db
+set nWal 39
+if {[permutation]!="mmap"} {set nWal 37}
+ifcapable !mmap {set nWal 37}
do_test wal-11.10 {
execsql {
PRAGMA cache_size = 10;
@@ -735,7 +738,7 @@ do_test wal-11.10 {
SELECT count(*) FROM t1;
}
list [expr [file size test.db]/1024] [file size test.db-wal]
-} [list 37 [wal_file_size 37 1024]]
+} [list 37 [wal_file_size $nWal 1024]]
do_test wal-11.11 {
execsql {
SELECT count(*) FROM t1;
@@ -745,7 +748,7 @@ do_test wal-11.11 {
} {32 16}
do_test wal-11.12 {
list [expr [file size test.db]/1024] [file size test.db-wal]
-} [list 37 [wal_file_size 37 1024]]
+} [list 37 [wal_file_size $nWal 1024]]
do_test wal-11.13 {
execsql {
INSERT INTO t1 VALUES( blob(900) );
@@ -755,7 +758,7 @@ do_test wal-11.13 {
} {17 ok}
do_test wal-11.14 {
list [expr [file size test.db]/1024] [file size test.db-wal]
-} [list 37 [wal_file_size 37 1024]]
+} [list 37 [wal_file_size $nWal 1024]]
#-------------------------------------------------------------------------
@@ -1509,10 +1512,10 @@ do_test wal-23.3 {
faultsim_restore_and_reopen
execsql { SELECT * FROM t1 }
} {1 2 3 4}
-set nPage [expr 2+$AUTOVACUUM]
do_test wal-23.4 {
set ::log
-} [list SQLITE_OK "Recovered $nPage frames from WAL file $walfile"]
+} [list SQLITE_NOTICE_RECOVER_WAL \
+ "recovered 2 frames from WAL file $walfile"]
ifcapable autovacuum {
diff --git a/test/wal5.test b/test/wal5.test
index 6eceed5..68750f1 100644
--- a/test/wal5.test
+++ b/test/wal5.test
@@ -235,7 +235,16 @@ foreach {testprefix do_wal_checkpoint} {
do_test 2.3.$tn.5 { sql1 { INSERT INTO t2 VALUES(3, 4) } } {}
do_test 2.3.$tn.6 { file_page_counts } {1 4 1 4}
do_test 2.3.$tn.7 { code1 { do_wal_checkpoint db -mode full } } {1 4 3}
- do_test 2.3.$tn.8 { file_page_counts } {1 4 2 4}
+
+ # The checkpoint above only writes page 1 of the db file. The other
+ # page (page 2) is locked by the read-transaction opened by the
+ # [sql2] commmand above. So normally, the db is 1 page in size here.
+ # However, in mmap() mode, the db is pre-allocated to 2 pages at the
+ # start of the checkpoint, even though page 2 cannot be written.
+ set nDb 2
+ if {[permutation]!="mmap"} {set nDb 1}
+ ifcapable !mmap {set nDb 1}
+ do_test 2.3.$tn.8 { file_page_counts } [list $nDb 4 2 4]
}
# Check that checkpoints block on the correct locks. And respond correctly
@@ -343,4 +352,3 @@ foreach {testprefix do_wal_checkpoint} {
finish_test
-
diff --git a/test/wal8.test b/test/wal8.test
index 4b97de7..3399538 100644
--- a/test/wal8.test
+++ b/test/wal8.test
@@ -26,6 +26,7 @@
set testdir [file dirname $argv0]
source $testdir/tester.tcl
set ::testprefix wal8
+ifcapable !wal {finish_test ; return }
db close
forcedelete test.db test.db-wal
diff --git a/test/wal9.test b/test/wal9.test
new file mode 100644
index 0000000..ae2a52b
--- /dev/null
+++ b/test/wal9.test
@@ -0,0 +1,94 @@
+# 2012 October 15
+#
+# 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 test case tests that a problem causing a failing assert() has
+# been fixed. The problem occurred if a writer process with a subset
+# of the *shm file mapped rolled back a transaction begun after the
+# entire WAL file was checkpointed into the db file (i.e. a transaction
+# that would have restarted the WAL file from the beginning).
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set testprefix wal9
+
+sqlite3 db2 test.db
+
+do_execsql_test 1.0 {
+ PRAGMA page_size = 1024;
+ PRAGMA journal_mode = WAL;
+ PRAGMA wal_autocheckpoint = 0;
+ CREATE TABLE t(x);
+} {wal 0}
+
+do_test 1.1 {
+ execsql "SELECT * FROM t" db2
+} {}
+
+do_execsql_test 1.2 {
+ BEGIN;
+ INSERT INTO t VALUES(randomblob(100));
+ INSERT INTO t SELECT randomblob(100) FROM t;
+ INSERT INTO t SELECT randomblob(100) FROM t;
+ INSERT INTO t SELECT randomblob(100) FROM t;
+ INSERT INTO t SELECT randomblob(100) FROM t;
+ INSERT INTO t SELECT randomblob(100) FROM t;
+ INSERT INTO t SELECT randomblob(100) FROM t;
+ INSERT INTO t SELECT randomblob(100) FROM t;
+
+ INSERT INTO t SELECT randomblob(100) FROM t;
+ INSERT INTO t SELECT randomblob(100) FROM t;
+ INSERT INTO t SELECT randomblob(100) FROM t;
+ INSERT INTO t SELECT randomblob(100) FROM t;
+ INSERT INTO t SELECT randomblob(100) FROM t;
+ INSERT INTO t SELECT randomblob(100) FROM t;
+ INSERT INTO t SELECT randomblob(100) FROM t;
+ INSERT INTO t SELECT randomblob(100) FROM t;
+
+ INSERT INTO t SELECT randomblob(100) FROM t;
+ INSERT INTO t SELECT randomblob(100) FROM t;
+ COMMIT;
+} {}
+
+# Check file sizes are as expected. The real requirement here is that
+# the *shm file is now more than one chunk (>32KiB).
+#
+# The sizes of various files are slightly different in normal and
+# auto-vacuum mode.
+do_test 1.3 { file size test.db } {1024}
+do_test 1.4 { expr {[file size test.db-wal]>(1500*1024)} } {1}
+do_test 1.5 { expr {[file size test.db-shm]>32768} } {1}
+do_test 1.6 {
+ foreach {a b c} [db eval {PRAGMA wal_checkpoint}] break
+ list [expr {$a==0}] [expr {$b>14500}] [expr {$c>14500}] [expr {$b==$c}]
+} {1 1 1 1}
+
+# At this point connection [db2] has mapped the first 32KB of the *shm file
+# only. Because the entire WAL file has been checkpointed, it is not
+# necessary to map any more of the *-shm file to read or write the database
+# (since all data will be read directly from the db file).
+#
+# However, at one point if a transaction that had not yet written to the
+# WAL file was rolled back an assert() attempting to verify that the entire
+# *-shm file was mapped would fail. If NDEBUG was defined (and the assert()
+# disabled) this bug caused SQLite to ignore the return code of a mmap()
+# call.
+#
+do_test 1.7 {
+ execsql {
+ BEGIN;
+ INSERT INTO t VALUES('hello');
+ ROLLBACK;
+ } db2
+} {}
+db2 close
+
+finish_test
diff --git a/test/walfault.test b/test/walfault.test
index 6f9aedd..4a9d98a 100644
--- a/test/walfault.test
+++ b/test/walfault.test
@@ -548,6 +548,44 @@ do_faultsim_test walfault-14 -prep {
set nRow [db eval {SELECT count(*) FROM abc}]
if {!(($nRow==2 && $testrc) || $nRow==3)} { error "Bad db content" }
}
-finish_test
+
+#-------------------------------------------------------------------------
+# Test fault-handling when switching out of exclusive-locking mode.
+#
+do_test walfault-14-pre {
+ faultsim_delete_and_reopen
+ execsql {
+ PRAGMA auto_vacuum = 0;
+ PRAGMA journal_mode = WAL;
+ BEGIN;
+ CREATE TABLE abc(a PRIMARY KEY);
+ INSERT INTO abc VALUES(randomblob(1500));
+ INSERT INTO abc VALUES(randomblob(1500));
+ COMMIT;
+ }
+ faultsim_save_and_close
+} {}
+do_faultsim_test walfault-14 -prep {
+ faultsim_restore_and_reopen
+ breakpoint
+ execsql {
+ SELECT count(*) FROM abc;
+ PRAGMA locking_mode = exclusive;
+ BEGIN;
+ INSERT INTO abc VALUES(randomblob(1500));
+ COMMIT;
+ }
+} -body {
+ db eval {
+ PRAGMA locking_mode = normal;
+ BEGIN;
+ INSERT INTO abc VALUES(randomblob(1500));
+ COMMIT;
+ }
+} -test {
+ faultsim_integrity_check
+ set nRow [db eval {SELECT count(*) FROM abc}]
+ if {$nRow!=3 && $nRow!=4} { error "Bad db content" }
+}
finish_test
diff --git a/test/where.test b/test/where.test
index 3826a5f..2dbc283 100644
--- a/test/where.test
+++ b/test/where.test
@@ -379,11 +379,26 @@ ifcapable subquery {
SELECT * FROM t1 WHERE rowid+0 IN (1,2,3,1234) order by 1;
}
} {1 0 4 2 1 9 3 1 16 102}
- do_test where-5.3 {
+ do_test where-5.3a {
count {
SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1;
}
- } {1 0 4 2 1 9 3 1 16 14}
+ } {1 0 4 2 1 9 3 1 16 13}
+ do_test where-5.3b {
+ count {
+ SELECT * FROM t1 WHERE w IN (3,-1,1,2) order by 1;
+ }
+ } {1 0 4 2 1 9 3 1 16 13}
+ do_test where-5.3c {
+ count {
+ SELECT * FROM t1 WHERE w IN (3,2,-1,1,2) order by 1;
+ }
+ } {1 0 4 2 1 9 3 1 16 13}
+ do_test where-5.3d {
+ count {
+ SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1 DESC;
+ }
+ } {3 1 16 2 1 9 1 0 4 12}
do_test where-5.4 {
count {
SELECT * FROM t1 WHERE w+0 IN (-1,1,2,3) order by 1;
@@ -452,6 +467,30 @@ ifcapable subquery {
SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,16) ORDER BY 1;
}
} {2 1 9 3 1 16 11}
+ do_test where-5.100 {
+ db eval {
+ SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969)
+ ORDER BY x, y
+ }
+ } {2 1 9 54 5 3025 62 5 3969}
+ do_test where-5.101 {
+ db eval {
+ SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969)
+ ORDER BY x DESC, y DESC
+ }
+ } {62 5 3969 54 5 3025 2 1 9}
+ do_test where-5.102 {
+ db eval {
+ SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969)
+ ORDER BY x DESC, y
+ }
+ } {54 5 3025 62 5 3969 2 1 9}
+ do_test where-5.103 {
+ db eval {
+ SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969)
+ ORDER BY x, y DESC
+ }
+ } {2 1 9 62 5 3969 54 5 3025}
}
# This procedure executes the SQL. Then it checks to see if the OP_Sort
@@ -511,11 +550,16 @@ do_test where-6.7 {
}
} {1 100 4 2 99 9 3 98 16 nosort}
ifcapable subquery {
- do_test where-6.8 {
+ do_test where-6.8a {
cksort {
SELECT * FROM t3 WHERE a IN (3,5,7,1,9,4,2) ORDER BY a LIMIT 3
}
- } {1 100 4 2 99 9 3 98 16 sort}
+ } {1 100 4 2 99 9 3 98 16 nosort}
+ do_test where-6.8b {
+ cksort {
+ SELECT * FROM t3 WHERE a IN (3,5,7,1,9,4,2) ORDER BY a DESC LIMIT 3
+ }
+ } {9 92 100 7 94 64 5 96 36 nosort}
}
do_test where-6.9.1 {
cksort {
@@ -1079,6 +1123,7 @@ do_test where-13.12 {
# When optimizing out ORDER BY clauses, make sure that trailing terms
# of the ORDER BY clause do not reference other tables in a join.
#
+if {[permutation] != "no_optimization"} {
do_test where-14.1 {
execsql {
CREATE TABLE t8(a INTEGER PRIMARY KEY, b TEXT UNIQUE);
@@ -1088,34 +1133,34 @@ do_test where-14.1 {
cksort {
SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, y.b
}
-} {1/4 1/1 4/4 4/1 sort}
+} {1/4 1/1 4/4 4/1 nosort}
do_test where-14.2 {
cksort {
SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, y.b DESC
}
-} {1/1 1/4 4/1 4/4 sort}
+} {1/1 1/4 4/1 4/4 nosort}
do_test where-14.3 {
cksort {
SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, x.b
}
-} {1/1 1/4 4/1 4/4 nosort}
+} {1/4 1/1 4/4 4/1 nosort}
do_test where-14.4 {
cksort {
SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, x.b DESC
}
-} {1/1 1/4 4/1 4/4 nosort}
+} {1/4 1/1 4/4 4/1 nosort}
do_test where-14.5 {
# This test case changed from "nosort" to "sort". See ticket 2a5629202f.
cksort {
SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||x.b
}
-} {4/1 4/4 1/1 1/4 sort}
+} {/4/[14] 4/[14] 1/[14] 1/[14] sort/}
do_test where-14.6 {
# This test case changed from "nosort" to "sort". See ticket 2a5629202f.
cksort {
SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||x.b DESC
}
-} {4/1 4/4 1/1 1/4 sort}
+} {/4/[14] 4/[14] 1/[14] 1/[14] sort/}
do_test where-14.7 {
cksort {
SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||y.b
@@ -1130,7 +1175,7 @@ do_test where-14.7.2 {
cksort {
SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a, x.a||x.b
}
-} {4/1 4/4 1/1 1/4 nosort}
+} {4/4 4/1 1/4 1/1 nosort}
do_test where-14.8 {
cksort {
SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||y.b DESC
@@ -1156,6 +1201,7 @@ do_test where-14.12 {
SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||x.b DESC
}
} {4/4 4/1 1/4 1/1 sort}
+} ;# {permutation != "no_optimization"}
# Ticket #2445.
#
diff --git a/test/where2.test b/test/where2.test
index d61c089..e8c2f36 100644
--- a/test/where2.test
+++ b/test/where2.test
@@ -167,24 +167,54 @@ ifcapable subquery {
}
} {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
}
- do_test where2-4.6 {
+ do_test where2-4.6a {
queryplan {
SELECT * FROM t1
WHERE x IN (1,2,3,4,5,6,7,8)
AND y IN (10000,10001,10002,10003,10004,10005)
- ORDER BY 2
+ ORDER BY x
+ }
+ } {99 6 10000 10006 nosort t1 i1xy}
+ do_test where2-4.6b {
+ queryplan {
+ SELECT * FROM t1
+ WHERE x IN (1,2,3,4,5,6,7,8)
+ AND y IN (10000,10001,10002,10003,10004,10005)
+ ORDER BY x DESC
+ }
+ } {99 6 10000 10006 nosort t1 i1xy}
+ do_test where2-4.6c {
+ queryplan {
+ SELECT * FROM t1
+ WHERE x IN (1,2,3,4,5,6,7,8)
+ AND y IN (10000,10001,10002,10003,10004,10005)
+ ORDER BY x, y
+ }
+ } {99 6 10000 10006 nosort t1 i1xy}
+ do_test where2-4.6d {
+ queryplan {
+ SELECT * FROM t1
+ WHERE x IN (1,2,3,4,5,6,7,8)
+ AND y IN (10000,10001,10002,10003,10004,10005)
+ ORDER BY x, y DESC
}
} {99 6 10000 10006 sort t1 i1xy}
# Duplicate entires on the RHS of an IN operator do not cause duplicate
# output rows.
#
- do_test where2-4.6 {
+ do_test where2-4.6x {
queryplan {
SELECT * FROM t1 WHERE z IN (10207,10006,10006,10207)
ORDER BY w
}
} {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
+ do_test where2-4.6y {
+ queryplan {
+ SELECT * FROM t1 WHERE z IN (10207,10006,10006,10207)
+ ORDER BY w DESC
+ }
+ } {100 6 10201 10207 99 6 10000 10006 sort t1 i1zyx}
ifcapable compound {
do_test where2-4.7 {
queryplan {
@@ -207,11 +237,16 @@ do_test where2-5.1 {
} {99 6 10000 10006 nosort t1 i1w}
ifcapable subquery {
- do_test where2-5.2 {
+ do_test where2-5.2a {
queryplan {
SELECT * FROM t1 WHERE w IN (99) ORDER BY w
}
- } {99 6 10000 10006 sort t1 i1w}
+ } {99 6 10000 10006 nosort t1 i1w}
+ do_test where2-5.2b {
+ queryplan {
+ SELECT * FROM t1 WHERE w IN (99) ORDER BY w DESC
+ }
+ } {99 6 10000 10006 nosort t1 i1w}
}
# Verify that OR clauses get translated into IN operators.
diff --git a/test/where8.test b/test/where8.test
index a7d5edb..9b6014e 100644
--- a/test/where8.test
+++ b/test/where8.test
@@ -290,6 +290,38 @@ do_test where8-3.15 {
}
} {I I I I I I I I I I II II II II II II II II II II III III III III III 9 1}
+
+do_test where8-3.21 {
+ execsql_status {
+ SELECT a, d FROM t1, (t2) WHERE (a=d OR b=e) AND a<5 ORDER BY a
+ }
+} {1 1 2 2 3 3 4 2 4 4 0 0}
+do_test where8-3.21.1 {
+ execsql_status {
+ SELECT a, d FROM t1, ((t2)) AS t3 WHERE (a=d OR b=e) AND a<5 ORDER BY a
+ }
+} {1 1 2 2 3 3 4 2 4 4 0 0}
+if {[permutation] != "no_optimization"} {
+do_test where8-3.21.2 {
+ execsql_status {
+ SELECT a, d FROM t1, ((SELECT * FROM t2)) AS t3 WHERE (a=d OR b=e) AND a<5 ORDER BY a
+ }
+} {1 1 2 2 3 3 4 2 4 4 0 0}
+}
+do_test where8-3.22 {
+ execsql_status {
+ SELECT a, d FROM ((((((t1))), (((t2))))))
+ WHERE (a=d OR b=e) AND a<5 ORDER BY a
+ }
+} {1 1 2 2 3 3 4 2 4 4 0 0}
+if {[permutation] != "no_optimization"} {
+do_test where8-3.23 {
+ execsql_status {
+ SELECT * FROM ((SELECT * FROM t2)) AS t3;
+ }
+} {1 {} I 2 four IV 3 {} IX 4 sixteen XVI 5 {} XXV 6 thirtysix XXXVI 7 fortynine XLIX 8 sixtyeight LXIV 9 eightyone LXXXIX 10 {} C 9 0}
+}
+
#-----------------------------------------------------------------------
# The following tests - where8-4.* - verify that adding or removing
# indexes does not change the results returned by various queries.
diff --git a/test/where9.test b/test/where9.test
index 23260a6..1e94fdf 100644
--- a/test/where9.test
+++ b/test/where9.test
@@ -232,7 +232,7 @@ do_test where9-1.3.3 {
} {90 91 92 97 scan 98 sort 0}
do_test where9-1.3.4 {
count_steps {
- SELECT a FROM t4
+ SELECT a FROM (t4)
WHERE (b IS NULL AND c NOT NULL AND d NOT NULL)
OR (b NOT NULL AND c NOT NULL AND d IS NULL)
OR (b NOT NULL AND c IS NULL AND d NOT NULL)
@@ -692,7 +692,7 @@ do_test where9-6.5.3 {
do_test where9-6.5.4 {
db eval {
SELECT count(*) FROM t1 UNION ALL
- SELECT a FROM t1 WHERE a%100 IN (5,31,57,82,83,84,85,86,87);
+ SELECT a FROM t1 WHERE a%100 IN (5,31,57,82,83,84,85,86,87) ORDER BY rowid;
ROLLBACK;
}
} {99 105 131 157 182 183 184 185 186 187}
@@ -876,5 +876,42 @@ do_test where9-8.1 {
ORDER BY +a;
}
} {2 3 4 5 {} {} 5 55 3 4 5 6 2 4 5 55}
+do_test where9-8.2 {
+ db eval {
+ SELECT *
+ FROM t81 LEFT JOIN (t82) ON y=b JOIN t83
+ WHERE c==p OR d==p
+ ORDER BY +a;
+ }
+} {2 3 4 5 {} {} 5 55 3 4 5 6 2 4 5 55}
+do_test where9-8.3 {
+ db eval {
+ SELECT *
+ FROM (t81) LEFT JOIN (main.t82) ON y=b JOIN t83
+ WHERE c==p OR d==p
+ ORDER BY +a;
+ }
+} {2 3 4 5 {} {} 5 55 3 4 5 6 2 4 5 55}
+
+# Fix for ticket [f2369304e47167e3e644e2f1fe9736063391d7b7]
+# Incorrect results when OR is used in the ON clause of a LEFT JOIN
+#
+do_test where9-9.1 {
+ db eval {
+ CREATE TABLE t91(x); INSERT INTO t91 VALUES(1);
+ CREATE TABLE t92(y INTEGER PRIMARY KEY,a,b);
+ INSERT INTO t92 VALUES(1,2,3);
+ SELECT 1 FROM t91 LEFT JOIN t92 ON a=2 OR b=3;
+ SELECT 2 FROM t91 LEFT JOIN t92 ON a=2 AND b=3;
+ SELECT 3 FROM t91 LEFT JOIN t92 ON (a=2 OR b=3) AND y IS NULL;
+ SELECT 4 FROM t91 LEFT JOIN t92 ON (a=2 AND b=3) AND y IS NULL;
+ CREATE TEMP TABLE x9 AS SELECT * FROM t91 LEFT JOIN t92 ON a=2 OR b=3;
+ SELECT 5 FROM x9 WHERE y IS NULL;
+ SELECT 6 FROM t91 LEFT JOIN t92 ON a=2 OR b=3 WHERE y IS NULL;
+ SELECT 7 FROM t91 LEFT JOIN t92 ON a=2 AND b=3 WHERE y IS NULL;
+ SELECT 8 FROM t91 LEFT JOIN t92 ON a=22 OR b=33 WHERE y IS NULL;
+ SELECT 9 FROM t91 LEFT JOIN t92 ON a=22 AND b=33 WHERE y IS NULL;
+ }
+} {1 2 3 4 8 9}
finish_test
diff --git a/test/whereD.test b/test/whereD.test
index 58fe934..9ac5a68 100644
--- a/test/whereD.test
+++ b/test/whereD.test
@@ -180,7 +180,7 @@ do_test 4.2 {
SELECT * FROM t41 AS x LEFT JOIN t42 AS y ON (y.d=x.c) OR (y.e=x.b);
}
} {1 2 3 3 6 9 4 5 6 {} {} {}}
-do_test 4.2 {
+do_test 4.3 {
db eval {
SELECT * FROM t41 AS x LEFT JOIN t42 AS y ON (y.d=x.c) OR (y.d=x.b);
}
diff --git a/test/whereE.test b/test/whereE.test
new file mode 100644
index 0000000..e686a46
--- /dev/null
+++ b/test/whereE.test
@@ -0,0 +1,62 @@
+# 2012 November 9
+#
+# 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 file implements regression tests for SQLite library. The
+# focus of this file is testing the query planner to make sure it
+# is making good planning decisions.
+#
+
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set ::testprefix whereE
+
+do_execsql_test 1.1 {
+ CREATE TABLE t1(a,b);
+ INSERT INTO t1 VALUES(1,10), (2,20), (3,30), (2,22), (3, 33);
+ INSERT INTO t1 SELECT * FROM t1;
+ INSERT INTO t1 SELECT * FROM t1;
+ INSERT INTO t1 SELECT * FROM t1;
+ INSERT INTO t1 SELECT * FROM t1;
+ INSERT INTO t1 SELECT * FROM t1;
+ INSERT INTO t1 SELECT * FROM t1;
+ INSERT INTO t1 SELECT * FROM t1;
+ INSERT INTO t1 SELECT * FROM t1;
+ INSERT INTO t1 SELECT * FROM t1;
+ INSERT INTO t1 SELECT * FROM t1;
+ ALTER TABLE t1 ADD COLUMN c;
+ UPDATE t1 SET c=a*rowid+10000;
+ CREATE INDEX t1ab ON t1(a,b);
+
+ CREATE TABLE t2(x,y);
+ INSERT INTO t2 VALUES(4,44),(5,55),(6,66),(7,77);
+ INSERT INTO t2 SELECT x+4, (x+4)*11 FROM t2;
+ INSERT INTO t2 SELECT x+8, (x+8)*11 FROM t2;
+ INSERT INTO t2 SELECT x+16, (x+16)*11 FROM t2;
+ INSERT INTO t2 SELECT x+32, (x+32)*11 FROM t2;
+ INSERT INTO t2 SELECT x+64, (x+32)*11 FROM t2;
+ ALTER TABLE t2 ADD COLUMN z;
+ UPDATE t2 SET z=2;
+ CREATE UNIQUE INDEX t2zx ON t2(z,x);
+
+ EXPLAIN QUERY PLAN SELECT x FROM t1, t2 WHERE a=z AND c=x;
+} {/.*SCAN TABLE t1 .*SEARCH TABLE t2 .*/}
+do_execsql_test 1.2 {
+ EXPLAIN QUERY PLAN SELECT x FROM t2, t1 WHERE a=z AND c=x;
+} {/.*SCAN TABLE t1 .*SEARCH TABLE t2 .*/}
+do_execsql_test 1.3 {
+ ANALYZE;
+ EXPLAIN QUERY PLAN SELECT x FROM t1, t2 WHERE a=z AND c=x;
+} {/.*SCAN TABLE t1 .*SEARCH TABLE t2 .*/}
+do_execsql_test 1.4 {
+ EXPLAIN QUERY PLAN SELECT x FROM t2, t1 WHERE a=z AND c=x;
+} {/.*SCAN TABLE t1 .*SEARCH TABLE t2 .*/}
+
+finish_test
diff --git a/test/whereF.test b/test/whereF.test
new file mode 100644
index 0000000..57bdbee
--- /dev/null
+++ b/test/whereF.test
@@ -0,0 +1,115 @@
+# 2012 November 9
+#
+# 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.
+#
+#***********************************************************************
+#
+# Test cases for query planning decisions.
+
+
+#
+# The tests in this file demonstrate the behaviour of the query planner
+# in determining the order in which joined tables are scanned.
+#
+# Assume there are two tables being joined - t1 and t2. Each has a cost
+# if it is the outer loop, and a cost if it is the inner loop. As follows:
+#
+# t1(outer) - cost of scanning t1 as the outer loop.
+# t1(inner) - cost of scanning t1 as the inner loop.
+# t2(outer) - cost of scanning t2 as the outer loop.
+# t2(inner) - cost of scanning t2 as the inner loop.
+#
+# Depending on the order in which the planner nests the scans, the total
+# cost of the join query is one of:
+#
+# t1(outer) * t2(inner)
+# t2(outer) * t1(inner)
+#
+# The tests in this file attempt to verify that the planner nests joins in
+# the correct order when the following are true:
+#
+# + (t1(outer) * t2(inner)) > (t1(inner) * t2(outer)
+# + t1(outer) < t2(outer)
+#
+# In other words, when the best overall query plan has t2 as the outer loop,
+# but when the outer loop is considered independent of the inner, t1 is the
+# most efficient choice.
+#
+# In order to make them more predictable, automatic indexes are turned off for
+# the tests in this file.
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set testprefix x
+
+do_execsql_test 1.0 {
+ PRAGMA automatic_index = 0;
+ CREATE TABLE t1(a, b, c);
+ CREATE TABLE t2(d, e, f);
+ CREATE UNIQUE INDEX i1 ON t1(a);
+ CREATE UNIQUE INDEX i2 ON t2(d);
+} {}
+
+foreach {tn sql} {
+ 1 "SELECT * FROM t1, t2 WHERE t1.a=t2.e AND t2.d<t1.b AND t1.c!=10"
+ 2 "SELECT * FROM t2, t1 WHERE t1.a=t2.e AND t2.d<t1.b AND t1.c!=10"
+ 3 "SELECT * FROM t2 CROSS JOIN t1 WHERE t1.a=t2.e AND t2.d<t1.b AND t1.c!=10"
+} {
+ do_test 1.$tn {
+ db eval "EXPLAIN QUERY PLAN $sql"
+ } {/.*SCAN TABLE t2 .*SEARCH TABLE t1 .*/}
+}
+
+do_execsql_test 2.0 {
+ DROP TABLE t1;
+ DROP TABLE t2;
+ CREATE TABLE t1(a, b, c);
+ CREATE TABLE t2(d, e, f);
+
+ CREATE UNIQUE INDEX i1 ON t1(a);
+ CREATE UNIQUE INDEX i2 ON t1(b);
+ CREATE UNIQUE INDEX i3 ON t2(d);
+} {}
+
+foreach {tn sql} {
+ 1 "SELECT * FROM t1, t2 WHERE t1.a>? AND t2.d>t1.c AND t1.b=t2.e"
+ 2 "SELECT * FROM t2, t1 WHERE t1.a>? AND t2.d>t1.c AND t1.b=t2.e"
+ 3 "SELECT * FROM t2 CROSS JOIN t1 WHERE t1.a>? AND t2.d>t1.c AND t1.b=t2.e"
+} {
+ do_test 2.$tn {
+ db eval "EXPLAIN QUERY PLAN $sql"
+ } {/.*SCAN TABLE t2 .*SEARCH TABLE t1 .*/}
+}
+
+do_execsql_test 3.0 {
+ DROP TABLE t1;
+ DROP TABLE t2;
+ CREATE TABLE t1(a, b, c);
+ CREATE TABLE t2(d, e, f);
+
+ CREATE UNIQUE INDEX i1 ON t1(a, b);
+ CREATE INDEX i2 ON t2(d);
+} {}
+
+foreach {tn sql} {
+ 1 {SELECT t1.a, t1.b, t2.d, t2.e FROM t1, t2
+ WHERE t2.d=t1.b AND t1.a=(t2.d+1) AND t1.b = (t2.e+1)}
+
+ 2 {SELECT t1.a, t1.b, t2.d, t2.e FROM t2, t1
+ WHERE t2.d=t1.b AND t1.a=(t2.d+1) AND t1.b = (t2.e+1)}
+
+ 3 {SELECT t1.a, t1.b, t2.d, t2.e FROM t2 CROSS JOIN t1
+ WHERE t2.d=t1.b AND t1.a=(t2.d+1) AND t1.b = (t2.e+1)}
+} {
+ do_test 3.$tn {
+ db eval "EXPLAIN QUERY PLAN $sql"
+ } {/.*SCAN TABLE t2 .*SEARCH TABLE t1 .*/}
+}
+
+finish_test
diff --git a/test/win32lock.test b/test/win32lock.test
index d014be4..7241720 100644
--- a/test/win32lock.test
+++ b/test/win32lock.test
@@ -27,6 +27,7 @@ proc xLog {error_code msg} {
lappend ::log $msg
}
sqlite3 db test.db
+db eval {PRAGMA mmap_size=0}
do_test win32lock-1.1 {
db eval {
diff --git a/test/zerodamage.test b/test/zerodamage.test
index 3d18c8d..de5088b 100644
--- a/test/zerodamage.test
+++ b/test/zerodamage.test
@@ -18,7 +18,7 @@
set testdir [file dirname $argv0]
source $testdir/tester.tcl
-set testprefix wal5
+set testprefix zerodamage
ifcapable !vtab {
finish_test
@@ -59,7 +59,7 @@ do_test zerodamage-2.0 {
}
tv filter xDelete
tv script xDeleteCallback
- register_wholenumber_module db
+ load_static_extension db wholenumber
db eval {
PRAGMA page_size=1024;
PRAGMA journal_mode=DELETE;
@@ -89,31 +89,33 @@ do_test zerodamage-2.1 {
concat [file_control_powersafe_overwrite db -1] [set ::max_journal_size]
} {0 0 24704}
-# Run a WAL-mode transaction with POWERSAFE_OVERWRITE on to verify that the
-# WAL file does not get too big.
-#
-do_test zerodamage-3.0 {
- db eval {
- PRAGMA journal_mode=WAL;
- }
- db close
- sqlite3 db file:test.db?psow=TRUE -uri 1
- db eval {
- UPDATE t1 SET y=randomblob(50) WHERE x=124;
- }
- file size test.db-wal
-} {1080}
+ifcapable wal {
+ # Run a WAL-mode transaction with POWERSAFE_OVERWRITE on to verify that the
+ # WAL file does not get too big.
+ #
+ do_test zerodamage-3.0 {
+ db eval {
+ PRAGMA journal_mode=WAL;
+ }
+ db close
+ sqlite3 db file:test.db?psow=TRUE -uri 1
+ db eval {
+ UPDATE t1 SET y=randomblob(50) WHERE x=124;
+ }
+ file size test.db-wal
+ } {1080}
-# Repeat the previous with POWERSAFE_OVERWRITE off. Verify that the WAL file
-# is padded.
-#
-do_test zerodamage-3.1 {
- db close
- sqlite3 db file:test.db?psow=FALSE -uri 1
- db eval {
- UPDATE t1 SET y=randomblob(50) WHERE x=124;
- }
- file size test.db-wal
-} {8416}
+ # Repeat the previous with POWERSAFE_OVERWRITE off. Verify that the WAL file
+ # is padded.
+ #
+ do_test zerodamage-3.1 {
+ db close
+ sqlite3 db file:test.db?psow=FALSE -uri 1
+ db eval {
+ UPDATE t1 SET y=randomblob(50) WHERE x=124;
+ }
+ file size test.db-wal
+ } {8416}
+}
finish_test
diff --git a/tool/build-all-msvc.bat b/tool/build-all-msvc.bat
index a2d7dae..758036f 100755
--- a/tool/build-all-msvc.bat
+++ b/tool/build-all-msvc.bat
@@ -6,10 +6,54 @@
:: Multi-Platform Build Tool for MSVC
::
+REM
+REM This batch script is used to build the SQLite DLL for multiple platforms
+REM and configurations using MSVC. The built SQLite DLLs, their associated
+REM import libraries, and optionally their symbols files, are placed within
+REM the directory specified on the command line, in sub-directories named for
+REM their respective platforms and configurations. This batch script must be
+REM run from inside a Visual Studio Command Prompt for the desired version of
+REM Visual Studio ^(the initial platform configured for the command prompt does
+REM not really matter^). Exactly one command line argument is required, the
+REM name of an existing directory to be used as the final destination directory
+REM for the generated output files, which will be placed in sub-directories
+REM created therein. Ideally, the directory specified should be empty.
+REM
+REM Example:
+REM
+REM CD /D C:\dev\sqlite\core
+REM tool\build-all-msvc.bat C:\Temp
+REM
+REM In the example above, "C:\dev\sqlite\core" represents the root of the
+REM source tree for SQLite and "C:\Temp" represents the final destination
+REM directory for the generated output files.
+REM
+REM There are several environment variables that may be set to modify the
+REM behavior of this batch script and its associated Makefile. The list of
+REM platforms to build may be overriden by using the PLATFORMS environment
+REM variable, which should contain a list of platforms ^(e.g. x86 x86_amd64
+REM x86_arm^). All platforms must be supported by the version of Visual Studio
+REM being used. The list of configurations to build may be overridden by
+REM setting the CONFIGURATIONS environment variable, which should contain a
+REM list of configurations to build ^(e.g. Debug Retail^). Neither of these
+REM variable values may contain any double quotes, surrounding or embedded.
+REM Finally, the NCRTLIBPATH and NSDKLIBPATH environment variables may be set
+REM to specify the location of the CRT and SDK, respectively, needed to compile
+REM executables native to the architecture of the build machine during any
+REM cross-compilation that may be necessary, depending on the platforms to be
+REM built. These values in these two variables should be surrounded by double
+REM quotes if they contain spaces.
+REM
+REM Please note that the SQLite build process performed by the Makefile
+REM associated with this batch script requires both Gawk ^(gawk.exe^) and Tcl
+REM 8.5 ^(tclsh85.exe^) to be present in a directory contained in the PATH
+REM environment variable unless a pre-existing amalgamation file is used.
+REM
SETLOCAL
REM SET __ECHO=ECHO
REM SET __ECHO2=ECHO
+REM SET __ECHO3=ECHO
IF NOT DEFINED _AECHO (SET _AECHO=REM)
IF NOT DEFINED _CECHO (SET _CECHO=REM)
IF NOT DEFINED _VECHO (SET _VECHO=REM)
@@ -93,17 +137,35 @@ IF NOT DEFINED PLATFORMS (
%_VECHO% Platforms = '%PLATFORMS%'
REM
+REM NOTE: If the list of configurations is not already set, use the default
+REM list.
+REM
+IF NOT DEFINED CONFIGURATIONS (
+ SET CONFIGURATIONS=Debug Retail
+)
+
+%_VECHO% Configurations = '%CONFIGURATIONS%'
+
+REM
REM NOTE: Setup environment variables to translate between the MSVC platform
REM names and the names to be used for the platform-specific binary
REM directories.
REM
+SET amd64_NAME=x64
+SET arm_NAME=ARM
+SET x64_NAME=x64
SET x86_NAME=x86
SET x86_amd64_NAME=x64
SET x86_arm_NAME=ARM
+SET x86_x64_NAME=x64
+%_VECHO% amd64_Name = '%amd64_NAME%'
+%_VECHO% arm_Name = '%arm_NAME%'
+%_VECHO% x64_Name = '%x64_NAME%'
%_VECHO% x86_Name = '%x86_NAME%'
%_VECHO% x86_amd64_Name = '%x86_amd64_NAME%'
%_VECHO% x86_arm_Name = '%x86_arm_NAME%'
+%_VECHO% x86_x64_Name = '%x86_x64_NAME%'
REM
REM NOTE: Check for the external tools needed during the build process ^(i.e.
@@ -115,6 +177,24 @@ FOR %%T IN (gawk.exe tclsh85.exe) DO (
)
REM
+REM NOTE: The Gawk executable "gawk.exe" is required during the SQLite build
+REM process unless a pre-existing amalgamation file is used.
+REM
+IF NOT DEFINED gawk.exe_PATH (
+ ECHO The Gawk executable "gawk.exe" is required to be in the PATH.
+ GOTO errors
+)
+
+REM
+REM NOTE: The Tcl 8.5 executable "tclsh85.exe" is required during the SQLite
+REM build process unless a pre-existing amalgamation file is used.
+REM
+IF NOT DEFINED tclsh85.exe_PATH (
+ ECHO The Tcl 8.5 executable "tclsh85.exe" is required to be in the PATH.
+ GOTO errors
+)
+
+REM
REM NOTE: Set the TOOLPATH variable to contain all the directories where the
REM external tools were found in the search above.
REM
@@ -127,12 +207,31 @@ REM NOTE: Check for MSVC 2012 because the Windows SDK directory handling is
REM slightly different for that version.
REM
IF "%VisualStudioVersion%" == "11.0" (
- SET SET_NSDKLIBPATH=1
+ REM
+ REM NOTE: If the Windows SDK library path has already been set, do not set
+ REM it to something else later on.
+ REM
+ IF NOT DEFINED NSDKLIBPATH (
+ SET SET_NSDKLIBPATH=1
+ )
) ELSE (
CALL :fn_UnsetVariable SET_NSDKLIBPATH
)
REM
+REM NOTE: Check if this is the Windows Phone SDK. If so, a different batch
+REM file is necessary to setup the build environment. Since the variable
+REM values involved here may contain parenthesis, using GOTO instead of
+REM an IF block is required.
+REM
+IF DEFINED WindowsPhoneKitDir GOTO set_vcvarsall_phone
+SET VCVARSALL=%VCINSTALLDIR%\vcvarsall.bat
+GOTO set_vcvarsall_done
+:set_vcvarsall_phone
+SET VCVARSALL=%VCINSTALLDIR%\WPSDK\WP80\vcvarsphoneall.bat
+:set_vcvarsall_done
+
+REM
REM NOTE: This is the outer loop. There should be exactly one iteration per
REM platform.
REM
@@ -142,7 +241,7 @@ FOR %%P IN (%PLATFORMS%) DO (
REM be used for the name of the platform-specific binary directory via
REM the environment variables setup earlier.
REM
- CALL :fn_SetVariable %%P_NAME PLATFORMNAME
+ CALL :fn_CopyVariable %%P_NAME PLATFORMNAME
REM
REM NOTE: This is the inner loop. There should be exactly one iteration.
@@ -172,6 +271,7 @@ FOR %%P IN (%PLATFORMS%) DO (
CALL :fn_UnsetVariable Platform
REM CALL :fn_UnsetVariable VCINSTALLDIR
CALL :fn_UnsetVariable VSINSTALLDIR
+ CALL :fn_UnsetVariable WindowsPhoneKitDir
CALL :fn_UnsetVariable WindowsSdkDir
CALL :fn_UnsetVariable WindowsSdkDir_35
CALL :fn_UnsetVariable WindowsSdkDir_old
@@ -181,129 +281,151 @@ FOR %%P IN (%PLATFORMS%) DO (
REM
SET PATH=%TOOLPATH%;%SystemRoot%\System32;%SystemRoot%
- REM
- REM NOTE: Launch a nested command shell to perform the following steps:
- REM
- REM 1. Setup the MSVC environment for this platform using the
- REM official batch file.
- REM
- REM 2. Make sure that no stale build output files are present.
- REM
- REM 3. Build the "sqlite3.dll" and "sqlite3.lib" binaries for this
- REM platform.
- REM
- REM 4. Copy the "sqlite3.dll" and "sqlite3.lib" binaries for this
- REM platform to the platform-specific directory beneath the
- REM binary directory.
- REM
- "%ComSpec%" /C (
+ FOR %%B IN (%CONFIGURATIONS%) DO (
REM
- REM NOTE: Attempt to setup the MSVC environment for this platform.
+ REM NOTE: When preparing the debug build, set the DEBUG and MEMDEBUG
+ REM environment variables to be picked up by the MSVC makefile
+ REM itself.
REM
- %__ECHO% CALL "%VCINSTALLDIR%\vcvarsall.bat" %%P
-
- IF ERRORLEVEL 1 (
- ECHO Failed to call "%VCINSTALLDIR%\vcvarsall.bat" for platform %%P.
- GOTO errors
+ IF /I "%%B" == "Debug" (
+ SET DEBUG=2
+ SET MEMDEBUG=1
+ ) ELSE (
+ CALL :fn_UnsetVariable DEBUG
+ CALL :fn_UnsetVariable MEMDEBUG
)
REM
- REM NOTE: If this batch file is not running in "what-if" mode, check to
- REM be sure we were actually able to setup the MSVC environment as
- REM current versions of their official batch file do not set the
- REM exit code upon failure.
+ REM NOTE: Launch a nested command shell to perform the following steps:
REM
- IF NOT DEFINED __ECHO (
- IF NOT DEFINED WindowsSdkDir (
- ECHO Cannot build, Windows SDK not found for platform %%P.
- GOTO errors
- )
- )
-
+ REM 1. Setup the MSVC environment for this platform using the
+ REM official batch file.
REM
- REM NOTE: When using MSVC 2012, the native SDK path cannot simply use
- REM the "lib" sub-directory beneath the location specified in the
- REM WindowsSdkDir environment variable because that location does
- REM not actually contain the necessary library files for x86.
- REM This must be done for each iteration because it relies upon
- REM the WindowsSdkDir environment variable being set by the batch
- REM file used to setup the MSVC environment.
+ REM 2. Make sure that no stale build output files are present.
REM
- IF DEFINED SET_NSDKLIBPATH (
- CALL :fn_SetVariable WindowsSdkDir NSDKLIBPATH
- CALL :fn_AppendVariable NSDKLIBPATH \lib\win8\um\x86
- )
-
+ REM 3. Build the "sqlite3.dll" and "sqlite3.lib" binaries for this
+ REM platform.
REM
- REM NOTE: Unless prevented from doing so, invoke NMAKE with the MSVC
- REM makefile to clean any stale build output from previous
- REM iterations of this loop and/or previous runs of this batch
- REM file, etc.
+ REM 4. Copy the "sqlite3.dll" and "sqlite3.lib" binaries for this
+ REM platform to the platform-specific directory beneath the
+ REM binary directory.
REM
- IF NOT DEFINED NOCLEAN (
- %__ECHO% nmake -f Makefile.msc clean
+ "%ComSpec%" /C (
+ REM
+ REM NOTE: Attempt to setup the MSVC environment for this platform.
+ REM
+ %__ECHO3% CALL "%VCVARSALL%" %%P
IF ERRORLEVEL 1 (
- ECHO Failed to clean for platform %%P.
+ ECHO Failed to call "%VCVARSALL%" for platform %%P.
GOTO errors
)
- ) ELSE (
+
REM
- REM NOTE: Even when the cleaning step has been disabled, we still need
- REM to remove the build output for the files we are specifically
- REM wanting to build for each platform.
+ REM NOTE: If this batch file is not running in "what-if" mode, check to
+ REM be sure we were actually able to setup the MSVC environment
+ REM as current versions of their official batch file do not set
+ REM the exit code upon failure.
REM
- %__ECHO% DEL /Q sqlite3.dll sqlite3.lib sqlite3.pdb
- )
+ IF NOT DEFINED __ECHO3 (
+ IF NOT DEFINED WindowsPhoneKitDir (
+ IF NOT DEFINED WindowsSdkDir (
+ ECHO Cannot build, Windows SDK not found for platform %%P.
+ GOTO errors
+ )
+ )
+ )
- REM
- REM NOTE: Invoke NMAKE with the MSVC makefile to build the "sqlite3.dll"
- REM binary. The x86 compiler will be used to compile the native
- REM command line tools needed during the build process itself.
- REM Also, disable looking for and/or linking to the native Tcl
- REM runtime library.
- REM
- %__ECHO% nmake -f Makefile.msc sqlite3.dll "NCC=""%VCINSTALLDIR%\bin\cl.exe""" USE_NATIVE_LIBPATHS=1 NO_TCL=1 %NMAKE_ARGS%
+ REM
+ REM NOTE: When using MSVC 2012, the native SDK path cannot simply use
+ REM the "lib" sub-directory beneath the location specified in the
+ REM WindowsSdkDir environment variable because that location does
+ REM not actually contain the necessary library files for x86.
+ REM This must be done for each iteration because it relies upon
+ REM the WindowsSdkDir environment variable being set by the batch
+ REM file used to setup the MSVC environment.
+ REM
+ IF DEFINED SET_NSDKLIBPATH (
+ IF DEFINED WindowsPhoneKitDir (
+ CALL :fn_CopyVariable WindowsPhoneKitDir NSDKLIBPATH
+ CALL :fn_AppendVariable NSDKLIBPATH \lib\x86
+ ) ELSE IF DEFINED WindowsSdkDir (
+ CALL :fn_CopyVariable WindowsSdkDir NSDKLIBPATH
+ CALL :fn_AppendVariable NSDKLIBPATH \lib\win8\um\x86
+ )
+ )
- IF ERRORLEVEL 1 (
- ECHO Failed to build "sqlite3.dll" for platform %%P.
- GOTO errors
- )
+ REM
+ REM NOTE: Unless prevented from doing so, invoke NMAKE with the MSVC
+ REM makefile to clean any stale build output from previous
+ REM iterations of this loop and/or previous runs of this batch
+ REM file, etc.
+ REM
+ IF NOT DEFINED NOCLEAN (
+ %__ECHO% nmake -f Makefile.msc clean
+
+ IF ERRORLEVEL 1 (
+ ECHO Failed to clean for platform %%P.
+ GOTO errors
+ )
+ ) ELSE (
+ REM
+ REM NOTE: Even when the cleaning step has been disabled, we still
+ REM need to remove the build output for the files we are
+ REM specifically wanting to build for each platform.
+ REM
+ %__ECHO% DEL /Q sqlite3.dll sqlite3.lib sqlite3.pdb
+ )
- REM
- REM NOTE: Copy the "sqlite3.dll" file to the platform-specific directory
- REM beneath the binary directory.
- REM
- %__ECHO% XCOPY sqlite3.dll "%BINARYDIRECTORY%\%%D\" %FFLAGS% %DFLAGS%
+ REM
+ REM NOTE: Call NMAKE with the MSVC makefile to build the "sqlite3.dll"
+ REM binary. The x86 compiler will be used to compile the native
+ REM command line tools needed during the build process itself.
+ REM Also, disable looking for and/or linking to the native Tcl
+ REM runtime library.
+ REM
+ %__ECHO% nmake -f Makefile.msc sqlite3.dll XCOMPILE=1 USE_NATIVE_LIBPATHS=1 NO_TCL=1 %NMAKE_ARGS%
- IF ERRORLEVEL 1 (
- ECHO Failed to copy "sqlite3.dll" to "%BINARYDIRECTORY%\%%D\".
- GOTO errors
- )
+ IF ERRORLEVEL 1 (
+ ECHO Failed to build %%B "sqlite3.dll" for platform %%P.
+ GOTO errors
+ )
- REM
- REM NOTE: Copy the "sqlite3.lib" file to the platform-specific directory
- REM beneath the binary directory.
- REM
- %__ECHO% XCOPY sqlite3.lib "%BINARYDIRECTORY%\%%D\" %FFLAGS% %DFLAGS%
+ REM
+ REM NOTE: Copy the "sqlite3.dll" file to the appropriate directory for
+ REM the build and platform beneath the binary directory.
+ REM
+ %__ECHO% XCOPY sqlite3.dll "%BINARYDIRECTORY%\%%B\%%D\" %FFLAGS% %DFLAGS%
- IF ERRORLEVEL 1 (
- ECHO Failed to copy "sqlite3.lib" to "%BINARYDIRECTORY%\%%D\".
- GOTO errors
- )
+ IF ERRORLEVEL 1 (
+ ECHO Failed to copy "sqlite3.dll" to "%BINARYDIRECTORY%\%%B\%%D\".
+ GOTO errors
+ )
- REM
- REM NOTE: Copy the "sqlite3.pdb" file to the platform-specific directory
- REM beneath the binary directory unless we are prevented from doing
- REM so.
- REM
- IF NOT DEFINED NOSYMBOLS (
- %__ECHO% XCOPY sqlite3.pdb "%BINARYDIRECTORY%\%%D\" %FFLAGS% %DFLAGS%
+ REM
+ REM NOTE: Copy the "sqlite3.lib" file to the appropriate directory for
+ REM the build and platform beneath the binary directory.
+ REM
+ %__ECHO% XCOPY sqlite3.lib "%BINARYDIRECTORY%\%%B\%%D\" %FFLAGS% %DFLAGS%
IF ERRORLEVEL 1 (
- ECHO Failed to copy "sqlite3.pdb" to "%BINARYDIRECTORY%\%%D\".
+ ECHO Failed to copy "sqlite3.lib" to "%BINARYDIRECTORY%\%%B\%%D\".
GOTO errors
)
+
+ REM
+ REM NOTE: Copy the "sqlite3.pdb" file to the appropriate directory for
+ REM the build and platform beneath the binary directory unless we
+ REM are prevented from doing so.
+ REM
+ IF NOT DEFINED NOSYMBOLS (
+ %__ECHO% XCOPY sqlite3.pdb "%BINARYDIRECTORY%\%%B\%%D\" %FFLAGS% %DFLAGS%
+
+ IF ERRORLEVEL 1 (
+ ECHO Failed to copy "sqlite3.pdb" to "%BINARYDIRECTORY%\%%B\%%D\".
+ GOTO errors
+ )
+ )
)
)
)
@@ -339,7 +461,7 @@ GOTO no_errors
VERIFY MAYBE 2> NUL
GOTO :EOF
-:fn_SetVariable
+:fn_CopyVariable
SETLOCAL
IF NOT DEFINED %1 GOTO :EOF
IF "%2" == "" GOTO :EOF
diff --git a/tool/build-shell.sh b/tool/build-shell.sh
index 8e62a71..6a48299 100644
--- a/tool/build-shell.sh
+++ b/tool/build-shell.sh
@@ -17,5 +17,6 @@ gcc -o sqlite3 -g -Os -I. \
-DSQLITE_ENABLE_RTREE \
-DHAVE_READLINE \
-DHAVE_USLEEP=1 \
- ../sqlite/src/shell.c ../sqlite/src/test_vfstrace.c \
+ ../sqlite/src/shell.c \
+ ../sqlite/src/test_vfstrace.c \
sqlite3.c -ldl -lreadline -lncurses
diff --git a/tool/mksqlite3c.tcl b/tool/mksqlite3c.tcl
index deb20a9..07c01ef 100644
--- a/tool/mksqlite3c.tcl
+++ b/tool/mksqlite3c.tcl
@@ -23,7 +23,7 @@
#
# Begin by reading the "sqlite3.h" header file. Extract the version number
-# from in this file. The versioon number is needed to generate the header
+# from in this file. The version number is needed to generate the header
# comment of the amalgamation.
#
if {[lsearch $argv --nostatic]>=0} {
@@ -92,6 +92,7 @@ if {$addstatic} {
#
foreach hdr {
crypto.h
+ sqlcipher.h
btree.h
btreeInt.h
fts3.h
@@ -221,11 +222,15 @@ proc copy_file {filename} {
# used subroutines first in order to help the compiler find
# inlining opportunities.
#
+
foreach file {
sqliteInt.h
crypto.c
crypto_impl.c
+ crypto_libtomcrypt.c
+ crypto_openssl.c
+ crypto_cc.c
global.c
ctime.c
@@ -315,6 +320,7 @@ foreach file {
fts3_porter.c
fts3_tokenizer.c
fts3_tokenizer1.c
+ fts3_tokenize_vtab.c
fts3_write.c
fts3_snippet.c
fts3_unicode.c
diff --git a/tool/mksqlite3h.tcl b/tool/mksqlite3h.tcl
index f68f61a..a89b9f9 100644
--- a/tool/mksqlite3h.tcl
+++ b/tool/mksqlite3h.tcl
@@ -68,9 +68,14 @@ set declpattern {^ *[a-zA-Z][a-zA-Z_0-9 ]+ \**sqlite3_[_a-zA-Z0-9]+\(}
# Force the output to use unix line endings, even on Windows.
fconfigure stdout -translation lf
-# Process the src/sqlite.h.in ext/rtree/sqlite3rtree.h files.
+set filelist [subst {
+ $TOP/src/sqlite.h.in
+ $TOP/ext/rtree/sqlite3rtree.h
+}]
+
+# Process the source files.
#
-foreach file [list $TOP/src/sqlite.h.in $TOP/ext/rtree/sqlite3rtree.h] {
+foreach file $filelist {
set in [open $file]
while {![eof $in]} {
diff --git a/tool/mkvsix.tcl b/tool/mkvsix.tcl
index a751778..e9f1f81 100644
--- a/tool/mkvsix.tcl
+++ b/tool/mkvsix.tcl
@@ -2,7 +2,95 @@
#
# This script is used to generate a VSIX (Visual Studio Extension) file for
# SQLite usable by Visual Studio.
-
+#
+# PREREQUISITES
+#
+# 1. Tcl 8.4 and later are supported, earlier versions have not been tested.
+#
+# 2. The "sqlite3.h" file is assumed to exist in the parent directory of the
+# directory containing this script. The [optional] second command line
+# argument to this script may be used to specify an alternate location.
+# This script also assumes that the "sqlite3.h" file corresponds with the
+# version of the binaries to be packaged. This assumption is not verified
+# by this script.
+#
+# 3. The temporary directory specified in the TEMP or TMP environment variables
+# must refer to an existing directory writable by the current user.
+#
+# 4. The "zip" and "unzip" command line tools must be located either in a
+# directory contained in the PATH environment variable or specified as the
+# exact file names to execute in the "ZipTool" and "UnZipTool" environment
+# variables, respectively.
+#
+# 5. The template VSIX file (which is basically a zip file) must be located in
+# a "win" directory inside the directory containing this script. It should
+# not contain any executable binaries. It should only contain dynamic
+# textual content files to be processed using [subst] and/or static content
+# files to be copied verbatim.
+#
+# 6. The executable and other compiled binary files to be packaged into the
+# final VSIX file (e.g. DLLs, LIBs, and PDBs) must be located in a single
+# directory tree. The top-level directory of the tree must be specified as
+# the first command line argument to this script. The second level
+# sub-directory names must match those of the build configuration (e.g.
+# "Debug" or "Retail"). The third level sub-directory names must match
+# those of the platform (e.g. "x86", "x64", and "ARM"). For example, the
+# binary files to be packaged would need to be organized as follows when
+# packaging the "Debug" and "Retail" build configurations for the "x86" and
+# "x64" platforms (in this example, "C:\temp" is the top-level directory as
+# specified in the first command line argument):
+#
+# C:\Temp\Debug\x86\sqlite3.lib
+# C:\Temp\Debug\x86\sqlite3.dll
+# C:\Temp\Debug\x86\sqlite3.pdb
+# C:\Temp\Debug\x64\sqlite3.lib
+# C:\Temp\Debug\x64\sqlite3.dll
+# C:\Temp\Debug\x64\sqlite3.pdb
+# C:\Temp\Retail\x86\sqlite3.lib
+# C:\Temp\Retail\x86\sqlite3.dll
+# C:\Temp\Retail\x86\sqlite3.pdb
+# C:\Temp\Retail\x64\sqlite3.lib
+# C:\Temp\Retail\x64\sqlite3.dll
+# C:\Temp\Retail\x64\sqlite3.pdb
+#
+# The above directory tree organization is performed automatically if the
+# "tool\build-all-msvc.bat" batch script is used to build the binary files
+# to be packaged.
+#
+# USAGE
+#
+# The first argument to this script is required and must be the name of the
+# top-level directory containing the directories and files organized into a
+# tree as described in item 6 of the PREREQUISITES section, above. The second
+# argument is optional and if present must contain the name of the directory
+# containing the root of the source tree for SQLite. The third argument is
+# optional and if present must contain the flavor the VSIX package to build.
+# Currently, the only supported package flavors are "WinRT" and "WP80". The
+# fourth argument is optional and if present must be a string containing a list
+# of platforms to include in the VSIX package. The format of the platform list
+# string is "platform1,platform2,platform3". Typically, when on Windows, this
+# script is executed using commands similar to the following from a normal
+# Windows command prompt:
+#
+# CD /D C:\dev\sqlite\core
+# tclsh85 tool\mkvsix.tcl C:\Temp
+#
+# In the example above, "C:\dev\sqlite\core" represents the root of the source
+# tree for SQLite and "C:\Temp" represents the top-level directory containing
+# the executable and other compiled binary files, organized into a directory
+# tree as described in item 6 of the PREREQUISITES section, above.
+#
+# This script should work on non-Windows platforms as well, provided that all
+# the requirements listed in the PREREQUISITES section are met.
+#
+# NOTES
+#
+# The temporary directory is used as a staging area for the final VSIX file.
+# The template VSIX file is extracted, its contents processed, and then the
+# resulting files are packaged into the final VSIX file.
+#
+package require Tcl 8.4
+
proc fail { {error ""} {usage false} } {
if {[string length $error] > 0} then {
puts stdout $error
@@ -11,7 +99,8 @@ proc fail { {error ""} {usage false} } {
puts stdout "usage:\
[file tail [info nameofexecutable]]\
-[file tail [info script]] <binaryDirectory> \[sourceDirectory\]"
+[file tail [info script]] <binaryDirectory> \[sourceDirectory\]\
+\[packageFlavor\] \[platformNames\]"
exit 1
}
@@ -84,20 +173,24 @@ proc writeFile { fileName data } {
proc substFile { fileName } {
#
# NOTE: Performs all Tcl command, variable, and backslash substitutions in
- # the specified file and then re-writes the contents of that same file
+ # the specified file and then rewrites the contents of that same file
# with the substituted data.
#
return [writeFile $fileName [uplevel 1 [list subst [readFile $fileName]]]]
}
-proc replacePlatform { fileName platformName } {
+proc replaceFileNameTokens { fileName name buildName platformName } {
#
# NOTE: Returns the specified file name containing the platform name instead
# of platform placeholder tokens.
#
- return [string map [list <platform> $platformName] $fileName]
+ return [string map [list <build> $buildName <platform> $platformName \
+ <name> $name] $fileName]
}
+#
+# NOTE: This is the entry point for this script.
+#
set script [file normalize [info script]]
if {[string length $script] == 0} then {
@@ -113,7 +206,7 @@ set rootName [file rootname [file tail $script]]
# NOTE: Process and verify all the command line arguments.
#
set argc [llength $argv]
-if {$argc != 1 && $argc != 2} then {fail}
+if {$argc < 1 || $argc > 4} then {fail}
set binaryDirectory [lindex $argv 0]
@@ -126,7 +219,7 @@ if {![file exists $binaryDirectory] || \
fail "binary directory does not exist"
}
-if {$argc == 2} then {
+if {$argc >= 2} then {
set sourceDirectory [lindex $argv 1]
} else {
#
@@ -145,6 +238,47 @@ if {![file exists $sourceDirectory] || \
fail "source directory does not exist"
}
+if {$argc >= 3} then {
+ set packageFlavor [lindex $argv 2]
+} else {
+ #
+ # NOTE: Assume the package flavor is WinRT.
+ #
+ set packageFlavor WinRT
+}
+
+if {[string length $packageFlavor] == 0} then {
+ fail "invalid package flavor"
+}
+
+if {[string equal -nocase $packageFlavor WinRT]} then {
+ set shortName SQLite.WinRT
+ set displayName "SQLite for Windows Runtime"
+ set targetPlatformIdentifier Windows
+ set extraSdkPath ""
+ set extraFileListAttributes [appendArgs \
+ "\r\n " {AppliesTo="WindowsAppContainer"} \
+ "\r\n " {DependsOn="Microsoft.VCLibs, version=11.0"}]
+} elseif {[string equal -nocase $packageFlavor WP80]} then {
+ set shortName SQLite.WP80
+ set displayName "SQLite for Windows Phone"
+ set targetPlatformIdentifier "Windows Phone"
+ set extraSdkPath "\\..\\$targetPlatformIdentifier"
+ set extraFileListAttributes ""
+} else {
+ fail "unsupported package flavor, must be \"WinRT\" or \"WP80\""
+}
+
+if {$argc >= 4} then {
+ set platformNames [list]
+
+ foreach platformName [split [lindex $argv 3] ", "] {
+ if {[string length $platformName] > 0} then {
+ lappend platformNames $platformName
+ }
+ }
+}
+
###############################################################################
#
@@ -168,7 +302,8 @@ if {![file exists $templateFile] || \
}
set currentDirectory [pwd]
-set outputFile [file join $currentDirectory sqlite-output.vsix]
+set outputFile [file join $currentDirectory [appendArgs sqlite- \
+ $packageFlavor -output.vsix]]
if {[file exists $outputFile]} then {
fail [appendArgs "output file \"" $outputFile "\" already exists"]
@@ -244,60 +379,92 @@ if {![regexp -line -- $pattern $data dummy version]} then {
###############################################################################
#
-# NOTE: Setup the master file list data, including the necessary flags.
+# NOTE: Setup all the master file list data. This includes the source file
+# names, the destination file names, and the file processing flags. The
+# possible file processing flags are:
+#
+# "buildNeutral" -- This flag indicates the file location and content do
+# not depend on the build configuration.
+#
+# "platformNeutral" -- This flag indicates the file location and content
+# do not depend on the build platform.
+#
+# "subst" -- This flag indicates that the file contains dynamic textual
+# content that needs to be processed using [subst] prior to
+# packaging the file into the final VSIX package. The primary
+# use of this flag is to insert the name of the VSIX package,
+# some package flavor-specific value, or the SQLite version
+# into a file.
+#
+# "noDebug" -- This flag indicates that the file should be skipped when
+# processing the debug build.
+#
+# "noRetail" -- This flag indicates that the file should be skipped when
+# processing the retail build.
+#
+# "move" -- This flag indicates that the file should be moved from the
+# source to the destination instead of being copied.
+#
+# This file metadata may be overridden, either in whole or in part, via
+# the user-specific customizations file.
#
if {![info exists fileNames(source)]} then {
- set fileNames(source) [list "" "" "" \
- [file join $sourceDirectory sqlite3.h] \
- [file join $binaryDirectory <platform> sqlite3.lib] \
- [file join $binaryDirectory <platform> sqlite3.dll]]
+ set fileNames(source) [list "" "" \
+ [file join $stagingDirectory DesignTime <build> <platform> sqlite3.props] \
+ [file join $sourceDirectory sqlite3.h] \
+ [file join $binaryDirectory <build> <platform> sqlite3.lib] \
+ [file join $binaryDirectory <build> <platform> sqlite3.dll]]
if {![info exists no(symbols)]} then {
lappend fileNames(source) \
- [file join $binaryDirectory <platform> sqlite3.pdb]
+ [file join $binaryDirectory <build> <platform> sqlite3.pdb]
}
}
if {![info exists fileNames(destination)]} then {
set fileNames(destination) [list \
- [file join $stagingDirectory extension.vsixmanifest] \
- [file join $stagingDirectory SDKManifest.xml] \
- [file join $stagingDirectory DesignTime CommonConfiguration \
- <platform> SQLite.WinRT.props] \
- [file join $stagingDirectory DesignTime CommonConfiguration \
- <platform> sqlite3.h] \
- [file join $stagingDirectory DesignTime CommonConfiguration \
- <platform> sqlite3.lib] \
- [file join $stagingDirectory Redist CommonConfiguration \
- <platform> sqlite3.dll]]
+ [file join $stagingDirectory extension.vsixmanifest] \
+ [file join $stagingDirectory SDKManifest.xml] \
+ [file join $stagingDirectory DesignTime <build> <platform> <name>.props] \
+ [file join $stagingDirectory DesignTime <build> <platform> sqlite3.h] \
+ [file join $stagingDirectory DesignTime <build> <platform> sqlite3.lib] \
+ [file join $stagingDirectory Redist <build> <platform> sqlite3.dll]]
if {![info exists no(symbols)]} then {
lappend fileNames(destination) \
- [file join $stagingDirectory Redist Debug \
- <platform> sqlite3.pdb]
+ [file join $stagingDirectory Redist <build> <platform> sqlite3.pdb]
}
}
-if {![info exists fileNames(neutral)]} then {
- set fileNames(neutral) [list 1 1 1 1 0 0]
+if {![info exists fileNames(flags)]} then {
+ set fileNames(flags) [list \
+ [list buildNeutral platformNeutral subst] \
+ [list buildNeutral platformNeutral subst] \
+ [list buildNeutral platformNeutral subst move] \
+ [list buildNeutral platformNeutral] \
+ [list] [list] [list noRetail]]
if {![info exists no(symbols)]} then {
- lappend fileNames(neutral) 0
+ lappend fileNames(flags) [list noRetail]
}
}
-if {![info exists fileNames(subst)]} then {
- set fileNames(subst) [list 1 1 1 0 0 0]
+###############################################################################
- if {![info exists no(symbols)]} then {
- lappend fileNames(subst) 0
- }
+#
+# NOTE: Setup the list of builds supported by this script. These may be
+# overridden via the user-specific customizations file.
+#
+if {![info exists buildNames]} then {
+ set buildNames [list Debug Retail]
}
###############################################################################
#
-# NOTE: Setup the list of platforms supported by this script.
+# NOTE: Setup the list of platforms supported by this script. These may be
+# overridden via the command line or the user-specific customizations
+# file.
#
if {![info exists platformNames]} then {
set platformNames [list x86 x64 ARM]
@@ -311,72 +478,117 @@ if {![info exists platformNames]} then {
file mkdir $stagingDirectory
#
-# NOTE: Build the Tcl command used to extract the template package to the
-# staging directory.
+# NOTE: Build the Tcl command used to extract the template VSIX package to
+# the staging directory.
#
set extractCommand [list exec -- $unzip $templateFile -d $stagingDirectory]
#
-# NOTE: Extract the template package to the staging directory.
+# NOTE: Extract the template VSIX package to the staging directory.
#
eval $extractCommand
###############################################################################
#
-# NOTE: Process each file in the master file list. There are actually four
-# parallel lists that contain the source file names, destination file
-# names, the platform-neutral flags, and the use-subst flags. When the
-# platform-neutral flag is non-zero, the file is not platform-specific.
-# When the use-subst flag is non-zero, the file is considered to be a
-# text file that may contain Tcl variable and/or command replacements,
-# to be dynamically replaced during processing. If the source file name
-# is an empty string, then the destination file name will be assumed to
-# already exist in the staging directory and will not be copied; however,
-# dynamic replacements may still be performed on the destination file
-# prior to the package being re-zipped.
-#
-foreach sourceFileName $fileNames(source) \
- destinationFileName $fileNames(destination) \
- isNeutral $fileNames(neutral) useSubst $fileNames(subst) {
+# NOTE: Process each file in the master file list. There are actually three
+# parallel lists that contain the source file names, the destination file
+# names, and the file processing flags. If the "buildNeutral" flag is
+# present, the file location and content do not depend on the build
+# configuration and "CommonConfiguration" will be used in place of the
+# build configuration name. If the "platformNeutral" flag is present,
+# the file location and content do not depend on the build platform and
+# "neutral" will be used in place of the build platform name. If the
+# "subst" flag is present, the file is assumed to be a text file that may
+# contain Tcl variable, command, and backslash replacements, to be
+# dynamically replaced during processing using the Tcl [subst] command.
+# If the "noDebug" flag is present, the file will be skipped when
+# processing for the debug build. If the "noRetail" flag is present, the
+# file will be skipped when processing for the retail build. If the
+# "move" flag is present, the source file will be deleted after it is
+# copied to the destination file. If the source file name is an empty
+# string, the destination file name will be assumed to already exist in
+# the staging directory and will not be copied; however, Tcl variable,
+# command, and backslash replacements may still be performed on the
+# destination file prior to the final VSIX package being built if the
+# "subst" flag is present.
+#
+foreach sourceFileName $fileNames(source) \
+ destinationFileName $fileNames(destination) \
+ fileFlags $fileNames(flags) {
+ #
+ # NOTE: Process the file flags into separate boolean variables that may be
+ # used within the loop.
+ #
+ set isBuildNeutral [expr {[lsearch $fileFlags buildNeutral] != -1}]
+ set isPlatformNeutral [expr {[lsearch $fileFlags platformNeutral] != -1}]
+ set isMove [expr {[lsearch $fileFlags move] != -1}]
+ set useSubst [expr {[lsearch $fileFlags subst] != -1}]
+
#
- # NOTE: If the current file is platform-neutral, then only one platform will
- # be processed for it, namely "neutral"; otherwise, each supported
- # platform will be processed for it individually.
+ # NOTE: If the current file is build-neutral, then only one build will
+ # be processed for it, namely "CommonConfiguration"; otherwise, each
+ # supported build will be processed for it individually.
#
- foreach platformName [expr {$isNeutral ? [list neutral] : $platformNames}] {
+ foreach buildName \
+ [expr {$isBuildNeutral ? [list CommonConfiguration] : $buildNames}] {
#
- # NOTE: Use the actual platform name in the destination file name.
+ # NOTE: Should the current file be skipped for this build?
#
- set newDestinationFileName [replacePlatform $destinationFileName \
- $platformName]
+ if {[lsearch $fileFlags no${buildName}] != -1} then {
+ continue
+ }
#
- # NOTE: Does the source file need to be copied to the destination file?
+ # NOTE: If the current file is platform-neutral, then only one platform
+ # will be processed for it, namely "neutral"; otherwise, each
+ # supported platform will be processed for it individually.
#
- if {[string length $sourceFileName] > 0} then {
+ foreach platformName \
+ [expr {$isPlatformNeutral ? [list neutral] : $platformNames}] {
#
- # NOTE: First, make sure the destination directory exists.
+ # NOTE: Use the actual platform name in the destination file name.
#
- file mkdir [file dirname $newDestinationFileName]
+ set newDestinationFileName [replaceFileNameTokens $destinationFileName \
+ $shortName $buildName $platformName]
#
- # NOTE: Then, copy the source file to the destination file verbatim.
+ # NOTE: Does the source file need to be copied to the destination file?
#
- file copy [replacePlatform $sourceFileName $platformName] \
- $newDestinationFileName
- }
+ if {[string length $sourceFileName] > 0} then {
+ #
+ # NOTE: First, make sure the destination directory exists.
+ #
+ file mkdir [file dirname $newDestinationFileName]
+
+ #
+ # NOTE: Then, copy the source file to the destination file verbatim.
+ #
+ set newSourceFileName [replaceFileNameTokens $sourceFileName \
+ $shortName $buildName $platformName]
+
+ file copy $newSourceFileName $newDestinationFileName
+
+ #
+ # NOTE: If this is a move instead of a copy, delete the source file
+ # now.
+ #
+ if {$isMove} then {
+ file delete $newSourceFileName
+ }
+ }
- #
- # NOTE: Does the destination file contain dynamic replacements that must
- # be processed now?
- #
- if {$useSubst} then {
#
- # NOTE: Perform any dynamic replacements contained in the destination
- # file and then re-write it in-place.
+ # NOTE: Does the destination file contain dynamic replacements that must
+ # be processed now?
#
- substFile $newDestinationFileName
+ if {$useSubst} then {
+ #
+ # NOTE: Perform any dynamic replacements contained in the destination
+ # file and then re-write it in-place.
+ #
+ substFile $newDestinationFileName
+ }
}
}
}
@@ -391,13 +603,13 @@ foreach sourceFileName $fileNames(source) \
cd $stagingDirectory
#
-# NOTE: Build the Tcl command used to archive the final package in the
+# NOTE: Build the Tcl command used to archive the final VSIX package in the
# output directory.
#
set archiveCommand [list exec -- $zip -r $outputFile *]
#
-# NOTE: Build the final package archive in the output directory.
+# NOTE: Build the final VSIX package archive in the output directory.
#
eval $archiveCommand
diff --git a/tool/showdb.c b/tool/showdb.c
index d378d05..27424e0 100644
--- a/tool/showdb.c
+++ b/tool/showdb.c
@@ -6,7 +6,11 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
+
+#if !defined(_MSC_VER)
#include <unistd.h>
+#endif
+
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
@@ -172,7 +176,7 @@ static void print_db_header(void){
print_decode_line(aData, 56, 4, "Text encoding");
print_decode_line(aData, 60, 4, "User version");
print_decode_line(aData, 64, 4, "Incremental-vacuum mode");
- print_decode_line(aData, 68, 4, "meta[7]");
+ print_decode_line(aData, 68, 4, "Application ID");
print_decode_line(aData, 72, 4, "meta[8]");
print_decode_line(aData, 76, 4, "meta[9]");
print_decode_line(aData, 80, 4, "meta[10]");
@@ -467,7 +471,7 @@ static void page_usage_msg(int pgno, const char *zFormat, ...){
zMsg = sqlite3_vmprintf(zFormat, ap);
va_end(ap);
if( pgno<=0 || pgno>mxPage ){
- printf("ERROR: page %d out of bounds. Range=1..%d. Msg: %s\n",
+ printf("ERROR: page %d out of range 1..%d: %s\n",
pgno, mxPage, zMsg);
sqlite3_free(zMsg);
return;
@@ -475,7 +479,7 @@ static void page_usage_msg(int pgno, const char *zFormat, ...){
if( zPageUse[pgno]!=0 ){
printf("ERROR: page %d used multiple times:\n", pgno);
printf("ERROR: previous: %s\n", zPageUse[pgno]);
- printf("ERROR: current: %s\n", zPageUse[pgno]);
+ printf("ERROR: current: %s\n", zMsg);
sqlite3_free(zPageUse[pgno]);
}
zPageUse[pgno] = zMsg;
@@ -612,14 +616,31 @@ static void page_usage_freelist(int pgno){
}
/*
+** Determine pages used as PTRMAP pages
+*/
+static void page_usage_ptrmap(unsigned char *a){
+ if( a[55] ){
+ int usable = pagesize - a[20];
+ int pgno = 2;
+ int perPage = usable/5;
+ while( pgno<=mxPage ){
+ page_usage_msg(pgno, "PTRMAP page covering %d..%d",
+ pgno+1, pgno+perPage);
+ pgno += perPage + 1;
+ }
+ }
+}
+
+/*
** Try to figure out how every page in the database file is being used.
*/
static void page_usage_report(const char *zDbName){
- int i;
+ int i, j;
int rc;
sqlite3 *db;
sqlite3_stmt *pStmt;
unsigned char *a;
+ char zQuery[200];
/* Avoid the pathological case */
if( mxPage<1 ){
@@ -644,20 +665,26 @@ static void page_usage_report(const char *zDbName){
/* Discover the usage of each page */
a = getContent(0, 100);
page_usage_freelist(decodeInt32(a+32));
+ page_usage_ptrmap(a);
free(a);
page_usage_btree(1, 0, 0, "sqlite_master");
- rc = sqlite3_prepare_v2(db,
- "SELECT type, name, rootpage FROM SQLITE_MASTER WHERE rootpage",
- -1, &pStmt, 0);
- if( rc==SQLITE_OK ){
- while( sqlite3_step(pStmt)==SQLITE_ROW ){
- int pgno = sqlite3_column_int(pStmt, 2);
- page_usage_btree(pgno, 0, 0, sqlite3_column_text(pStmt, 1));
+ sqlite3_exec(db, "PRAGMA writable_schema=ON", 0, 0, 0);
+ for(j=0; j<2; j++){
+ sqlite3_snprintf(sizeof(zQuery), zQuery,
+ "SELECT type, name, rootpage FROM SQLITE_MASTER WHERE rootpage"
+ " ORDER BY rowid %s", j?"DESC":"");
+ rc = sqlite3_prepare_v2(db, zQuery, -1, &pStmt, 0);
+ if( rc==SQLITE_OK ){
+ while( sqlite3_step(pStmt)==SQLITE_ROW ){
+ int pgno = sqlite3_column_int(pStmt, 2);
+ page_usage_btree(pgno, 0, 0, sqlite3_column_text(pStmt, 1));
+ }
+ }else{
+ printf("ERROR: cannot query database: %s\n", sqlite3_errmsg(db));
}
- }else{
- printf("ERROR: cannot query database: %s\n", sqlite3_errmsg(db));
+ rc = sqlite3_finalize(pStmt);
+ if( rc==SQLITE_OK ) break;
}
- sqlite3_finalize(pStmt);
sqlite3_close(db);
/* Print the report and free memory used */
@@ -670,6 +697,53 @@ static void page_usage_report(const char *zDbName){
}
/*
+** Try to figure out how every page in the database file is being used.
+*/
+static void ptrmap_coverage_report(const char *zDbName){
+ unsigned int pgno;
+ unsigned char *aHdr;
+ unsigned char *a;
+ int usable;
+ int perPage;
+ unsigned int i;
+
+ /* Avoid the pathological case */
+ if( mxPage<1 ){
+ printf("empty database\n");
+ return;
+ }
+
+ /* Make sure PTRMAPs are used in this database */
+ aHdr = getContent(0, 100);
+ if( aHdr[55]==0 ){
+ printf("database does not use PTRMAP pages\n");
+ return;
+ }
+ usable = pagesize - aHdr[20];
+ perPage = usable/5;
+ free(aHdr);
+ printf("%5d: root of sqlite_master\n", 1);
+ for(pgno=2; pgno<=mxPage; pgno += perPage+1){
+ printf("%5d: PTRMAP page covering %d..%d\n", pgno,
+ pgno+1, pgno+perPage);
+ a = getContent((pgno-1)*pagesize, usable);
+ for(i=0; i+5<=usable && pgno+1+i/5<=mxPage; i+=5){
+ const char *zType = "???";
+ unsigned int iFrom = decodeInt32(&a[i+1]);
+ switch( a[i] ){
+ case 1: zType = "b-tree root page"; break;
+ case 2: zType = "freelist page"; break;
+ case 3: zType = "first page of overflow"; break;
+ case 4: zType = "later page of overflow"; break;
+ case 5: zType = "b-tree non-root page"; break;
+ }
+ printf("%5d: %s, parent=%u\n", pgno+1+i/5, zType, iFrom);
+ }
+ free(a);
+ }
+}
+
+/*
** Print a usage comment
*/
static void usage(const char *argv0){
@@ -678,6 +752,7 @@ static void usage(const char *argv0){
"args:\n"
" dbheader Show database header\n"
" pgidx Index of how each page is used\n"
+ " ptrmap Show all PTRMAP page content\n"
" NNN..MMM Show hex of pages NNN through MMM\n"
" NNN..end Show hex of pages NNN through end of file\n"
" NNNb Decode btree page NNN\n"
@@ -727,6 +802,14 @@ int main(int argc, char **argv){
page_usage_report(argv[1]);
continue;
}
+ if( strcmp(argv[i], "ptrmap")==0 ){
+ ptrmap_coverage_report(argv[1]);
+ continue;
+ }
+ if( strcmp(argv[i], "help")==0 ){
+ usage(argv[0]);
+ continue;
+ }
if( !isdigit(argv[i][0]) ){
fprintf(stderr, "%s: unknown option: [%s]\n", argv[0], argv[i]);
continue;
diff --git a/tool/showwal.c b/tool/showwal.c
index ae25a59..2888c10 100644
--- a/tool/showwal.c
+++ b/tool/showwal.c
@@ -18,6 +18,65 @@ static int perLine = 16; /* HEX elements to print per line */
typedef long long int i64; /* Datatype for 64-bit integers */
+/* Information for computing the checksum */
+typedef struct Cksum Cksum;
+struct Cksum {
+ int bSwap; /* True to do byte swapping on 32-bit words */
+ unsigned s0, s1; /* Current checksum value */
+};
+
+/*
+** extract a 32-bit big-endian integer
+*/
+static unsigned int getInt32(const unsigned char *a){
+ unsigned int x = (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
+ return x;
+}
+
+/*
+** Swap bytes on a 32-bit unsigned integer
+*/
+static unsigned int swab32(unsigned int x){
+ return (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8)
+ + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24);
+}
+
+/* Extend the checksum. Reinitialize the checksum if bInit is true.
+*/
+static void extendCksum(
+ Cksum *pCksum,
+ unsigned char *aData,
+ unsigned int nByte,
+ int bInit
+){
+ unsigned int *a32;
+ if( bInit ){
+ int a = 0;
+ *((char*)&a) = 1;
+ if( a==1 ){
+ /* Host is little-endian */
+ pCksum->bSwap = getInt32(aData)!=0x377f0682;
+ }else{
+ /* Host is big-endian */
+ pCksum->bSwap = getInt32(aData)!=0x377f0683;
+ }
+ pCksum->s0 = 0;
+ pCksum->s1 = 0;
+ }
+ a32 = (unsigned int*)aData;
+ while( nByte>0 ){
+ unsigned int x0 = a32[0];
+ unsigned int x1 = a32[1];
+ if( pCksum->bSwap ){
+ x0 = swab32(x0);
+ x1 = swab32(x1);
+ }
+ pCksum->s0 += x0 + pCksum->s1;
+ pCksum->s1 += x1 + pCksum->s0;
+ nByte -= 8;
+ a32 += 2;
+ }
+}
/*
** Convert the var-int format into i64. Return the number of bytes
@@ -152,39 +211,46 @@ static void print_frame(int iFrame){
}
/*
-** extract a 32-bit big-endian integer
-*/
-static unsigned int getInt32(const unsigned char *a){
- unsigned int x = (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
- return x;
-}
-
-/*
-** Print an entire page of content as hex
+** Summarize a single frame on a single line.
*/
-static void print_oneline_frame(int iFrame){
+static void print_oneline_frame(int iFrame, Cksum *pCksum){
int iStart;
unsigned char *aData;
+ unsigned int s0, s1;
iStart = 32 + (iFrame-1)*(pagesize+24);
aData = getContent(iStart, 24);
- fprintf(stdout, "Frame %4d: %6d %6d 0x%08x 0x%08x 0x%08x 0x%08x\n",
+ extendCksum(pCksum, aData, 8, 0);
+ extendCksum(pCksum, getContent(iStart+24, pagesize), pagesize, 0);
+ s0 = getInt32(aData+16);
+ s1 = getInt32(aData+20);
+ fprintf(stdout, "Frame %4d: %6d %6d 0x%08x,%08x 0x%08x,%08x %s\n",
iFrame,
getInt32(aData),
getInt32(aData+4),
getInt32(aData+8),
getInt32(aData+12),
- getInt32(aData+16),
- getInt32(aData+20)
+ s0,
+ s1,
+ (s0==pCksum->s0 && s1==pCksum->s1) ? "" : "cksum-fail"
);
+
+ /* Reset the checksum so that a single frame checksum failure will not
+ ** cause all subsequent frames to also show a failure. */
+ pCksum->s0 = s0;
+ pCksum->s1 = s1;
free(aData);
}
/*
** Decode the WAL header.
*/
-static void print_wal_header(void){
+static void print_wal_header(Cksum *pCksum){
unsigned char *aData;
aData = getContent(0, 32);
+ if( pCksum ){
+ extendCksum(pCksum, aData, 24, 1);
+ printf("Checksum byte order: %s\n", pCksum->bSwap ? "swapped" : "native");
+ }
printf("WAL Header:\n");
print_decode_line(aData, 0, 4,1,"Magic. 0x377f0682 (le) or 0x377f0683 (be)");
print_decode_line(aData, 4, 4, 0, "File format");
@@ -194,60 +260,199 @@ static void print_wal_header(void){
print_decode_line(aData, 20,4, 1, "Salt-2");
print_decode_line(aData, 24,4, 1, "Checksum-1");
print_decode_line(aData, 28,4, 1, "Checksum-2");
+ if( pCksum ){
+ if( pCksum->s0!=getInt32(aData+24) ){
+ printf("**** cksum-1 mismatch: 0x%08x\n", pCksum->s0);
+ }
+ if( pCksum->s1!=getInt32(aData+28) ){
+ printf("**** cksum-2 mismatch: 0x%08x\n", pCksum->s1);
+ }
+ }
free(aData);
}
+/*
+** Describe cell content.
+*/
+static int describeContent(
+ unsigned char *a, /* Cell content */
+ int nLocal, /* Bytes in a[] */
+ char *zDesc /* Write description here */
+){
+ int nDesc = 0;
+ int n, i, j;
+ i64 x, v;
+ const unsigned char *pData;
+ const unsigned char *pLimit;
+ char sep = ' ';
+
+ pLimit = &a[nLocal];
+ n = decodeVarint(a, &x);
+ pData = &a[x];
+ a += n;
+ i = x - n;
+ while( i>0 && pData<=pLimit ){
+ n = decodeVarint(a, &x);
+ a += n;
+ i -= n;
+ nLocal -= n;
+ zDesc[0] = sep;
+ sep = ',';
+ nDesc++;
+ zDesc++;
+ if( x==0 ){
+ sprintf(zDesc, "*"); /* NULL is a "*" */
+ }else if( x>=1 && x<=6 ){
+ v = (signed char)pData[0];
+ pData++;
+ switch( x ){
+ case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
+ case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
+ case 4: v = (v<<8) + pData[0]; pData++;
+ case 3: v = (v<<8) + pData[0]; pData++;
+ case 2: v = (v<<8) + pData[0]; pData++;
+ }
+ sprintf(zDesc, "%lld", v);
+ }else if( x==7 ){
+ sprintf(zDesc, "real");
+ pData += 8;
+ }else if( x==8 ){
+ sprintf(zDesc, "0");
+ }else if( x==9 ){
+ sprintf(zDesc, "1");
+ }else if( x>=12 ){
+ int size = (x-12)/2;
+ if( (x&1)==0 ){
+ sprintf(zDesc, "blob(%d)", size);
+ }else{
+ sprintf(zDesc, "txt(%d)", size);
+ }
+ pData += size;
+ }
+ j = strlen(zDesc);
+ zDesc += j;
+ nDesc += j;
+ }
+ return nDesc;
+}
+
+/*
+** Compute the local payload size given the total payload size and
+** the page size.
+*/
+static int localPayload(i64 nPayload, char cType){
+ int maxLocal;
+ int minLocal;
+ int surplus;
+ int nLocal;
+ if( cType==13 ){
+ /* Table leaf */
+ maxLocal = pagesize-35;
+ minLocal = (pagesize-12)*32/255-23;
+ }else{
+ maxLocal = (pagesize-12)*64/255-23;
+ minLocal = (pagesize-12)*32/255-23;
+ }
+ if( nPayload>maxLocal ){
+ surplus = minLocal + (nPayload-minLocal)%(pagesize-4);
+ if( surplus<=maxLocal ){
+ nLocal = surplus;
+ }else{
+ nLocal = minLocal;
+ }
+ }else{
+ nLocal = nPayload;
+ }
+ return nLocal;
+}
/*
** Create a description for a single cell.
+**
+** The return value is the local cell size.
*/
-static int describeCell(unsigned char cType, unsigned char *a, char **pzDesc){
+static int describeCell(
+ unsigned char cType, /* Page type */
+ unsigned char *a, /* Cell content */
+ int showCellContent, /* Show cell content if true */
+ char **pzDesc /* Store description here */
+){
int i;
int nDesc = 0;
int n = 0;
int leftChild;
i64 nPayload;
i64 rowid;
- static char zDesc[100];
+ int nLocal;
+ static char zDesc[1000];
i = 0;
if( cType<=5 ){
leftChild = ((a[0]*256 + a[1])*256 + a[2])*256 + a[3];
a += 4;
n += 4;
- sprintf(zDesc, "left-child: %d ", leftChild);
+ sprintf(zDesc, "lx: %d ", leftChild);
nDesc = strlen(zDesc);
}
if( cType!=5 ){
i = decodeVarint(a, &nPayload);
a += i;
n += i;
- sprintf(&zDesc[nDesc], "sz: %lld ", nPayload);
+ sprintf(&zDesc[nDesc], "n: %lld ", nPayload);
nDesc += strlen(&zDesc[nDesc]);
+ nLocal = localPayload(nPayload, cType);
+ }else{
+ nPayload = nLocal = 0;
}
if( cType==5 || cType==13 ){
i = decodeVarint(a, &rowid);
a += i;
n += i;
- sprintf(&zDesc[nDesc], "rowid: %lld ", rowid);
+ sprintf(&zDesc[nDesc], "r: %lld ", rowid);
nDesc += strlen(&zDesc[nDesc]);
}
+ if( nLocal<nPayload ){
+ int ovfl;
+ unsigned char *b = &a[nLocal];
+ ovfl = ((b[0]*256 + b[1])*256 + b[2])*256 + b[3];
+ sprintf(&zDesc[nDesc], "ov: %d ", ovfl);
+ nDesc += strlen(&zDesc[nDesc]);
+ n += 4;
+ }
+ if( showCellContent && cType!=5 ){
+ nDesc += describeContent(a, nLocal, &zDesc[nDesc-1]);
+ }
*pzDesc = zDesc;
- return n;
+ return nLocal+n;
}
/*
** Decode a btree page
*/
-static void decode_btree_page(unsigned char *a, int pgno, int hdrSize){
+static void decode_btree_page(
+ unsigned char *a, /* Content of the btree page to be decoded */
+ int pgno, /* Page number */
+ int hdrSize, /* Size of the page1-header in bytes */
+ const char *zArgs /* Flags to control formatting */
+){
const char *zType = "unknown";
int nCell;
- int i;
+ int i, j;
int iCellPtr;
+ int showCellContent = 0;
+ int showMap = 0;
+ char *zMap = 0;
switch( a[0] ){
case 2: zType = "index interior node"; break;
case 5: zType = "table interior node"; break;
case 10: zType = "index leaf"; break;
case 13: zType = "table leaf"; break;
}
+ while( zArgs[0] ){
+ switch( zArgs[0] ){
+ case 'c': showCellContent = 1; break;
+ case 'm': showMap = 1; break;
+ }
+ zArgs++;
+ }
printf("Decode of btree page %d:\n", pgno);
print_decode_line(a, 0, 1, 0, zType);
print_decode_line(a, 1, 2, 0, "Offset to first freeblock");
@@ -261,13 +466,40 @@ static void decode_btree_page(unsigned char *a, int pgno, int hdrSize){
}else{
iCellPtr = 8;
}
+ if( nCell>0 ){
+ printf(" key: lx=left-child n=payload-size r=rowid\n");
+ }
+ if( showMap ){
+ zMap = malloc(pagesize);
+ memset(zMap, '.', pagesize);
+ memset(zMap, '1', hdrSize);
+ memset(&zMap[hdrSize], 'H', iCellPtr);
+ memset(&zMap[hdrSize+iCellPtr], 'P', 2*nCell);
+ }
for(i=0; i<nCell; i++){
int cofst = iCellPtr + i*2;
char *zDesc;
+ int n;
+
cofst = a[cofst]*256 + a[cofst+1];
- describeCell(a[0], &a[cofst-hdrSize], &zDesc);
+ n = describeCell(a[0], &a[cofst-hdrSize], showCellContent, &zDesc);
+ if( showMap ){
+ char zBuf[30];
+ memset(&zMap[cofst], '*', n);
+ zMap[cofst] = '[';
+ zMap[cofst+n-1] = ']';
+ sprintf(zBuf, "%d", i);
+ j = strlen(zBuf);
+ if( j<=n-2 ) memcpy(&zMap[cofst+1], zBuf, j);
+ }
printf(" %03x: cell[%d] %s\n", cofst, i, zDesc);
}
+ if( showMap ){
+ for(i=0; i<pagesize; i+=64){
+ printf(" %03x: %.64s\n", i, &zMap[i]);
+ }
+ free(zMap);
+ }
}
int main(int argc, char **argv){
@@ -298,15 +530,18 @@ int main(int argc, char **argv){
printf("Available pages: 1..%d\n", mxFrame);
if( argc==2 ){
int i;
- print_wal_header();
- for(i=1; i<=mxFrame; i++) print_oneline_frame(i);
+ Cksum x;
+ print_wal_header(&x);
+ for(i=1; i<=mxFrame; i++){
+ print_oneline_frame(i, &x);
+ }
}else{
int i;
for(i=2; i<argc; i++){
int iStart, iEnd;
char *zLeft;
if( strcmp(argv[i], "header")==0 ){
- print_wal_header();
+ print_wal_header(0);
continue;
}
if( !isdigit(argv[i][0]) ){
@@ -318,11 +553,11 @@ int main(int argc, char **argv){
iEnd = mxFrame;
}else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){
iEnd = strtol(&zLeft[2], 0, 0);
-#if 0
}else if( zLeft && zLeft[0]=='b' ){
int ofst, nByte, hdrSize;
unsigned char *a;
if( iStart==1 ){
+ hdrSize = 100;
ofst = hdrSize = 100;
nByte = pagesize-100;
}else{
@@ -330,11 +565,11 @@ int main(int argc, char **argv){
ofst = (iStart-1)*pagesize;
nByte = pagesize;
}
+ ofst = 32 + hdrSize + (iStart-1)*(pagesize+24) + 24;
a = getContent(ofst, nByte);
- decode_btree_page(a, iStart, hdrSize);
+ decode_btree_page(a, iStart, hdrSize, zLeft+1);
free(a);
continue;
-#endif
}else{
iEnd = iStart;
}
diff --git a/tool/spaceanal.tcl b/tool/spaceanal.tcl
index fd59670..6988f6e 100644
--- a/tool/spaceanal.tcl
+++ b/tool/spaceanal.tcl
@@ -30,32 +30,34 @@ foreach arg $argv {
}
}
if {$file_to_analyze==""} usage
-if {![file exists $file_to_analyze]} {
- puts stderr "No such file: $file_to_analyze"
+set root_filename $file_to_analyze
+regexp {^file:(//)?([^?]*)} $file_to_analyze all x1 root_filename
+if {![file exists $root_filename]} {
+ puts stderr "No such file: $root_filename"
exit 1
}
-if {![file readable $file_to_analyze]} {
- puts stderr "File is not readable: $file_to_analyze"
+if {![file readable $root_filename]} {
+ puts stderr "File is not readable: $root_filename"
exit 1
}
-set true_file_size [file size $file_to_analyze]
+set true_file_size [file size $root_filename]
if {$true_file_size<512} {
- puts stderr "Empty or malformed database: $file_to_analyze"
+ puts stderr "Empty or malformed database: $root_filename"
exit 1
}
# Compute the total file size assuming test_multiplexor is being used.
# Assume that SQLITE_ENABLE_8_3_NAMES might be enabled
#
-set extension [file extension $file_to_analyze]
-set pattern $file_to_analyze
+set extension [file extension $root_filename]
+set pattern $root_filename
append pattern {[0-3][0-9][0-9]}
foreach f [glob -nocomplain $pattern] {
incr true_file_size [file size $f]
set extension {}
}
if {[string length $extension]>=2 && [string length $extension]<=4} {
- set pattern [file rootname $file_to_analyze]
+ set pattern [file rootname $root_filename]
append pattern {.[0-3][0-9][0-9]}
foreach f [glob -nocomplain $pattern] {
incr true_file_size [file size $f]
@@ -64,7 +66,10 @@ if {[string length $extension]>=2 && [string length $extension]<=4} {
# Open the database
#
-sqlite3 db $file_to_analyze
+if {[catch {sqlite3 db $file_to_analyze -uri 1} msg]} {
+ puts stderr "error trying to open $file_to_analyze: $msg"
+ exit 1
+}
register_dbstat_vtab db
db eval {SELECT count(*) FROM sqlite_master}
@@ -484,7 +489,7 @@ set user_percent [percent $user_payload $file_bytes]
# Output the summary statistics calculated above.
#
-puts "/** Disk-Space Utilization Report For $file_to_analyze"
+puts "/** Disk-Space Utilization Report For $root_filename"
catch {
puts "*** As of [clock format [clock seconds] -format {%Y-%b-%d %H:%M:%S}]"
}
diff --git a/tool/stack_usage.tcl b/tool/stack_usage.tcl
new file mode 100644
index 0000000..b3574f0
--- /dev/null
+++ b/tool/stack_usage.tcl
@@ -0,0 +1,98 @@
+#!/usr/bin/tclsh
+#
+# Parse the output of
+#
+# objdump -d sqlite3.o
+#
+# for x64 and generate a report showing:
+#
+# (1) Stack used by each function
+# (2) Recursion paths and their aggregate stack depth
+#
+set getStack 0
+while {![eof stdin]} {
+ set line [gets stdin]
+ if {[regexp {^[0-9a-f]+ <([^>]+)>:\s*$} $line all procname]} {
+ set curfunc $procname
+ set root($curfunc) 1
+ set calls($curfunc) {}
+ set calledby($curfunc) {}
+ set recursive($curfunc) {}
+ set stkdepth($curfunc) 0
+ set getStack 1
+ continue
+ }
+ if {[regexp {callq? +[0-9a-z]+ <([^>]+)>} $line all other]} {
+ set key [list $curfunc $other]
+ set callpair($key) 1
+ unset -nocomplain root($curfunc)
+ continue
+ }
+ if {[regexp {sub +\$(0x[0-9a-z]+),%[er]sp} $line all xdepth]} {
+ if {$getStack} {
+ scan $xdepth %x depth
+ set stkdepth($curfunc) $depth
+ set getStack 0
+ }
+ continue
+ }
+}
+
+puts "****************** Stack Usage By Function ********************"
+set sdlist {}
+foreach f [array names stkdepth] {
+ lappend sdlist [list $stkdepth($f) $f]
+}
+foreach sd [lsort -integer -decr -index 0 $sdlist] {
+ foreach {depth fname} $sd break
+ puts [format {%6d %s} $depth $fname]
+}
+
+puts "****************** Stack Usage By Recursion *******************"
+foreach key [array names callpair] {
+ foreach {from to} $key break
+ lappend calls($from) $to
+ # lappend calledby($to) $from
+}
+proc all_descendents {root} {
+ global calls recursive
+ set todo($root) $root
+ set go 1
+ while {$go} {
+ set go 0
+ foreach f [array names todo] {
+ set path $todo($f)
+ unset todo($f)
+ if {![info exists calls($f)]} continue
+ foreach x $calls($f) {
+ if {$x==$root} {
+ lappend recursive($root) [concat $path $root]
+ } elseif {![info exists d($x)]} {
+ set go 1
+ set todo($x) [concat $path $x]
+ set d($x) 1
+ }
+ }
+ }
+ }
+ return [array names d]
+}
+set pathlist {}
+foreach f [array names recursive] {
+ all_descendents $f
+ foreach m $recursive($f) {
+ set depth 0
+ foreach b [lrange $m 0 end-1] {
+ set depth [expr {$depth+$stkdepth($b)}]
+ }
+ lappend pathlist [list $depth $m]
+ }
+}
+foreach path [lsort -integer -decr -index 0 $pathlist] {
+ foreach {depth m} $path break
+ set first [lindex $m 0]
+ puts [format {%6d %s %d} $depth $first $stkdepth($first)]
+ foreach b [lrange $m 1 end] {
+ puts " $b $stkdepth($b)"
+ }
+}
diff --git a/tool/vdbe-compress.tcl b/tool/vdbe-compress.tcl
index 3bcff9e..95cc1eb 100644
--- a/tool/vdbe-compress.tcl
+++ b/tool/vdbe-compress.tcl
@@ -79,6 +79,9 @@ while {![eof stdin]} {
append unionDef " $line\n"
append afterUnion $line\n
lappend vlist $vname
+ } elseif {[regexp {^#(if|endif)} $line] && [llength $vlist]>0} {
+ append unionDef "$line\n"
+ append afterUnion $line\n
} else {
break
}
diff --git a/tool/win/sqlite.vsix b/tool/win/sqlite.vsix
index 4bdfda5..93eefac 100644
--- a/tool/win/sqlite.vsix
+++ b/tool/win/sqlite.vsix
Binary files differ